Simplify how :write works and make it work when no projects are open.

This commit is contained in:
Matthew Leeds 2015-07-27 15:23:55 -05:00
parent 13c5c84688
commit 656ed90f7e

View file

@ -36,8 +36,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('')
@ -99,7 +107,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
@ -115,32 +123,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 fullPath = getFullPath(filePath)
editorPath = editor.getPath() catch error
fullPath = getFullPath(filePath) fullPath = atom.showSaveDialogSync()
trySave(-> saveAs(fullPath)) if fullPath?
.then -> trySave(-> editor.saveAs(fullPath))
deferred.resolve() .then deferred.resolve
editor.buffer.setPath(editorPath) editor.buffer.setPath(fullPath)
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()
if fullPath?
trySave(-> editor.saveAs(fullPath))
.then deferred.resolve
deferred.promise deferred.promise