🐛 Fix behavior when no filename is given to :write.

This commit is contained in:
Matthew Leeds 2015-07-28 11:53:31 -05:00
parent da8405b387
commit f28a155234

View file

@ -37,16 +37,19 @@ saveAs = (filePath) ->
fs.writeFileSync(filePath, editor.getText()) fs.writeFileSync(filePath, editor.getText())
getFullPath = (filePath) -> getFullPath = (filePath) ->
if filePath is '' editor = atom.workspace.getActiveTextEditor()
throw new Error
if path.isAbsolute(filePath) if path.isAbsolute(filePath)
return filePath return filePath
else if atom.workspace.getActiveTextEditor().getPath()? else if editor.getPath()?
return path.join(path.dirname(atom.workspace.getActiveTextEditor().getPath()), filePath) if filePath is ''
else if atom.project.getPaths()[0]? return editor.getPath()
else
return path.join(path.dirname(editor.getPath()), filePath)
else if atom.project.getPaths()[0]? and filePath isnt ''
return path.join(atom.project.getPaths()[0], filePath) return path.join(atom.project.getPaths()[0], filePath)
else else
throw new Error throw new CommandError
return
replaceGroups = (groups, replString) -> replaceGroups = (groups, replString) ->
arr = replString.split('') arr = replString.split('')
@ -108,7 +111,9 @@ class Ex
tabp: => @tabprevious() tabp: => @tabprevious()
edit: (range, filePath) -> edit: (range, filePath) ->
filePath = fs.normalize(filePath.trim()) filePath = filePath.trim()
if filePath isnt ''
filePath = fs.normalize(filePath)
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')
buffer = atom.workspace.getActiveTextEditor().buffer buffer = atom.workspace.getActiveTextEditor().buffer
@ -124,17 +129,23 @@ class Ex
buffer.load() buffer.load()
write: (range, filePath) -> write: (range, filePath) ->
filePath = fs.normalize(filePath.trim()) filePath = filePath.trim()
if filePath isnt ''
filePath = fs.normalize(filePath)
deferred = Promise.defer() deferred = Promise.defer()
editor = atom.workspace.getActiveTextEditor() editor = atom.workspace.getActiveTextEditor()
try try
fullPath = getFullPath(filePath) fullPath = getFullPath(filePath)
catch error catch CommandError
fullPath = atom.showSaveDialogSync() fullPath = atom.showSaveDialogSync()
if fullPath? if fullPath?
trySave(-> editor.saveAs(fullPath)) if filePath is ''
.then deferred.resolve trySave(-> editor.save())
.then deferred.resolve
else
trySave(-> saveAs(fullPath))
.then deferred.resolve
editor.buffer.setPath(fullPath) editor.buffer.setPath(fullPath)
deferred.promise deferred.promise