Use Promise constructor instead of Promise.defer. Fix #110.
This commit is contained in:
parent
5773c8f47b
commit
3a43853292
1 changed files with 39 additions and 43 deletions
|
|
@ -4,33 +4,31 @@ fs = require 'fs-plus'
|
||||||
VimOption = require './vim-option'
|
VimOption = require './vim-option'
|
||||||
|
|
||||||
trySave = (func) ->
|
trySave = (func) ->
|
||||||
deferred = Promise.defer()
|
new Promise (resolve) ->
|
||||||
|
try
|
||||||
|
func()
|
||||||
|
resolve()
|
||||||
|
catch error
|
||||||
|
if error.message.endsWith('is a directory')
|
||||||
|
atom.notifications.addWarning("Unable to save file: #{error.message}")
|
||||||
|
else if error.path?
|
||||||
|
if error.code is 'EACCES'
|
||||||
|
atom.notifications
|
||||||
|
.addWarning("Unable to save file: Permission denied '#{error.path}'")
|
||||||
|
else if error.code in ['EPERM', 'EBUSY', 'UNKNOWN', 'EEXIST']
|
||||||
|
atom.notifications.addWarning("Unable to save file '#{error.path}'",
|
||||||
|
detail: error.message)
|
||||||
|
else if error.code is 'EROFS'
|
||||||
|
atom.notifications.addWarning(
|
||||||
|
"Unable to save file: Read-only file system '#{error.path}'")
|
||||||
|
else if (errorMatch =
|
||||||
|
/ENOTDIR, not a directory '([^']+)'/.exec(error.message))
|
||||||
|
fileName = errorMatch[1]
|
||||||
|
atom.notifications.addWarning("Unable to save file: A directory in the "+
|
||||||
|
"path '#{fileName}' could not be written to")
|
||||||
|
else
|
||||||
|
throw error
|
||||||
|
|
||||||
try
|
|
||||||
func()
|
|
||||||
deferred.resolve()
|
|
||||||
catch error
|
|
||||||
if error.message.endsWith('is a directory')
|
|
||||||
atom.notifications.addWarning("Unable to save file: #{error.message}")
|
|
||||||
else if error.path?
|
|
||||||
if error.code is 'EACCES'
|
|
||||||
atom.notifications
|
|
||||||
.addWarning("Unable to save file: Permission denied '#{error.path}'")
|
|
||||||
else if error.code in ['EPERM', 'EBUSY', 'UNKNOWN', 'EEXIST']
|
|
||||||
atom.notifications.addWarning("Unable to save file '#{error.path}'",
|
|
||||||
detail: error.message)
|
|
||||||
else if error.code is 'EROFS'
|
|
||||||
atom.notifications.addWarning(
|
|
||||||
"Unable to save file: Read-only file system '#{error.path}'")
|
|
||||||
else if (errorMatch =
|
|
||||||
/ENOTDIR, not a directory '([^']+)'/.exec(error.message))
|
|
||||||
fileName = errorMatch[1]
|
|
||||||
atom.notifications.addWarning("Unable to save file: A directory in the "+
|
|
||||||
"path '#{fileName}' could not be written to")
|
|
||||||
else
|
|
||||||
throw error
|
|
||||||
|
|
||||||
deferred.promise
|
|
||||||
|
|
||||||
saveAs = (filePath) ->
|
saveAs = (filePath) ->
|
||||||
editor = atom.workspace.getActiveTextEditor()
|
editor = atom.workspace.getActiveTextEditor()
|
||||||
|
|
@ -150,25 +148,23 @@ class Ex
|
||||||
if filePath.indexOf(' ') isnt -1
|
if filePath.indexOf(' ') isnt -1
|
||||||
throw new CommandError('Only one file name allowed')
|
throw new CommandError('Only one file name allowed')
|
||||||
|
|
||||||
deferred = Promise.defer()
|
new Promise (resolve) ->
|
||||||
|
|
||||||
editor = atom.workspace.getActiveTextEditor()
|
editor = atom.workspace.getActiveTextEditor()
|
||||||
saved = false
|
saved = false
|
||||||
if filePath.length isnt 0
|
if filePath.length isnt 0
|
||||||
fullPath = getFullPath(filePath)
|
fullPath = getFullPath(filePath)
|
||||||
if editor.getPath()? and (not fullPath? or editor.getPath() == fullPath)
|
if editor.getPath()? and (not fullPath? or editor.getPath() == fullPath)
|
||||||
# Use editor.save when no path is given or the path to the file is given
|
# Use editor.save when no path is given or the path to the file is given
|
||||||
trySave(-> editor.save()).then(deferred.resolve)
|
trySave(-> editor.save()).then(resolve)
|
||||||
saved = true
|
saved = true
|
||||||
else if not fullPath?
|
else if not fullPath?
|
||||||
fullPath = atom.showSaveDialogSync()
|
fullPath = atom.showSaveDialogSync()
|
||||||
|
|
||||||
if not saved and fullPath?
|
if not saved and fullPath?
|
||||||
if not force and fs.existsSync(fullPath)
|
if not force and fs.existsSync(fullPath)
|
||||||
throw new CommandError("File exists (add ! to override)")
|
throw new CommandError("File exists (add ! to override)")
|
||||||
trySave(-> saveAs(fullPath)).then(deferred.resolve)
|
trySave(-> saveAs(fullPath)).then(resolve)
|
||||||
|
|
||||||
deferred.promise
|
|
||||||
|
|
||||||
w: (args...) =>
|
w: (args...) =>
|
||||||
@write(args...)
|
@write(args...)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue