Merge pull request #216 from lloeki/eh-refactor-write

Refactor :write command
This commit is contained in:
Edvin Hultberg 2018-05-31 12:21:44 +02:00 committed by GitHub
commit ef4bb8a6ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -229,29 +229,30 @@ class Ex
deferred = defer() deferred = defer()
editor = atom.workspace.getActiveTextEditor() editor = atom.workspace.getActiveTextEditor()
saved = false
# Case 1; path is provided
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 saveas # Only write when it does not exist or we have a force flag set.
throw new CommandError("Argument required") if force or not fs.existsSync(fullPath)
else editor.saveAs(fullPath)
# Use editor.save when no path is given or the path to the file is given return deferred.promise
trySave(-> editor.save()).then(deferred.resolve)
saved = true
else if not fullPath?
fullPath = atom.showSaveDialogSync()
if not saved and fullPath?
if not force and fs.existsSync(fullPath)
throw new CommandError("File exists (add ! to override)") throw new CommandError("File exists (add ! to override)")
if saveas or editor.getFileName() == null
editor = atom.workspace.getActiveTextEditor()
trySave(-> editor.saveAs(fullPath, editor)).then(deferred.resolve)
else
trySave(-> saveAs(fullPath, editor)).then(deferred.resolve)
deferred.promise # Case 2; no path provided, call editor save.
editor = atom.workspace.getActiveTextEditor()
# Does the current buffer exist?
if editor.getPath()? and fs.existsSync(editor.getPath())
trySave(-> editor.save()).then(deferred.promise)
else
# Cant see what the better API is but Pane.saveActiveItemAs() is the only call
# I could find that states it will ask the user.
trySave(-> atom.workspace.getActivePane().saveActiveItemAs()).then(deferred.promise)
return deferred.promise
wall: -> wall: ->
atom.workspace.saveAll() atom.workspace.saveAll()