Clean up save logic (fixes #75)

This commit is contained in:
Loic Nageleisen 2015-07-29 16:36:55 +02:00
parent 84c548a444
commit 1a117bddf9

View file

@ -37,16 +37,14 @@ saveAs = (filePath) ->
fs.writeFileSync(filePath, editor.getText()) fs.writeFileSync(filePath, editor.getText())
getFullPath = (filePath) -> getFullPath = (filePath) ->
if filePath is ''
throw new Error
if path.isAbsolute(filePath) if path.isAbsolute(filePath)
return filePath fullPath = filePath
else if atom.workspace.getActiveTextEditor().getPath()? else if atom.project.getPaths().length == 0
return path.join(path.dirname(atom.workspace.getActiveTextEditor().getPath()), filePath) fullPath = path.join('~', filePath)
else if atom.project.getPaths()[0]?
return path.join(atom.project.getPaths()[0], filePath)
else else
throw new Error fullPath = path.join(atom.project.getPaths()[0], filePath)
return fs.normalize(fullPath)
replaceGroups = (groups, replString) -> replaceGroups = (groups, replString) ->
arr = replString.split('') arr = replString.split('')
@ -127,18 +125,33 @@ class Ex
buffer.load() buffer.load()
write: (range, filePath) -> write: (range, filePath) ->
filePath = fs.normalize(filePath.trim()) filePath = filePath.trim()
deferred = Promise.defer() deferred = Promise.defer()
pane = atom.workspace.getActivePane()
editor = atom.workspace.getActiveTextEditor() editor = atom.workspace.getActiveTextEditor()
try if editor.getPath()?
fullPath = getFullPath(filePath) if filePath.length > 0
catch error editorPath = editor.getPath()
fullPath = atom.showSaveDialogSync() fullPath = getFullPath(filePath)
if fullPath? trySave(-> saveAs(fullPath))
trySave(-> editor.saveAs(fullPath)) .then editor.buffer.setPath(editorPath)
.then deferred.resolve .then deferred.resolve
editor.buffer.setPath(fullPath) else
trySave(-> editor.save())
.then deferred.resolve
else
if filePath.length > 0
fullPath = getFullPath(filePath)
trySave(-> saveAs(fullPath))
.then -> editor.buffer.setPath(fullPath)
.then deferred.resolve
else
fullPath = atom.showSaveDialogSync()
if fullPath?
trySave(-> editor.saveAs(fullPath))
.then -> editor.buffer.setPath(fullPath)
.then deferred.resolve
deferred.promise deferred.promise