Merge pull request #74 from mleeds95/fix-write

Simplify how :write works and make it work when no projects are open.
This commit is contained in:
Loic Nageleisen 2015-07-28 11:30:51 +02:00
commit a187e68497

View file

@ -37,8 +37,16 @@ saveAs = (filePath) ->
fs.writeFileSync(filePath, editor.getText()) fs.writeFileSync(filePath, editor.getText())
getFullPath = (filePath) -> getFullPath = (filePath) ->
return filePath if path.isAbsolute(filePath) if filePath is ''
return path.join(atom.project.getPath(), filePath) 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)
else
throw new Error
replaceGroups = (groups, replString) -> replaceGroups = (groups, replString) ->
arr = replString.split('') arr = replString.split('')
@ -100,7 +108,7 @@ class Ex
tabp: => @tabprevious() tabp: => @tabprevious()
edit: (range, filePath) -> edit: (range, filePath) ->
filePath = filePath.trim() filePath = path.normalize(filePath.trim())
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
@ -116,32 +124,18 @@ class Ex
buffer.load() buffer.load()
write: (range, filePath) -> write: (range, filePath) ->
filePath = filePath.trim() filePath = path.normalize(filePath.trim())
deferred = Promise.defer() deferred = Promise.defer()
pane = atom.workspace.getActivePane()
editor = atom.workspace.getActiveTextEditor() editor = atom.workspace.getActiveTextEditor()
if atom.workspace.getActiveTextEditor().getPath() isnt undefined try
if filePath.length > 0
editorPath = editor.getPath()
fullPath = getFullPath(filePath) fullPath = getFullPath(filePath)
trySave(-> saveAs(fullPath)) catch error
.then ->
deferred.resolve()
editor.buffer.setPath(editorPath)
else
trySave(-> editor.save())
.then deferred.resolve
else
if filePath.length > 0
fullPath = getFullPath(filePath)
trySave(-> saveAs(fullPath))
.then deferred.resolve
else
fullPath = atom.showSaveDialogSync() fullPath = atom.showSaveDialogSync()
if fullPath? if fullPath?
trySave(-> editor.saveAs(fullPath)) trySave(-> editor.saveAs(fullPath))
.then deferred.resolve .then deferred.resolve
editor.buffer.setPath(fullPath)
deferred.promise deferred.promise