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