From 1a117bddf903ee934cb3388c61414039a5d618f5 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Wed, 29 Jul 2015 16:36:55 +0200 Subject: [PATCH] Clean up save logic (fixes #75) --- lib/ex.coffee | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/ex.coffee b/lib/ex.coffee index 6476bad..7b7c349 100644 --- a/lib/ex.coffee +++ b/lib/ex.coffee @@ -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 - fullPath = getFullPath(filePath) - catch error - fullPath = atom.showSaveDialogSync() - if fullPath? - trySave(-> editor.saveAs(fullPath)) - .then deferred.resolve - editor.buffer.setPath(fullPath) + if editor.getPath()? + if filePath.length > 0 + editorPath = editor.getPath() + fullPath = getFullPath(filePath) + 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 deferred.promise