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:
commit
a187e68497
1 changed files with 20 additions and 26 deletions
|
|
@ -37,8 +37,16 @@ saveAs = (filePath) ->
|
|||
fs.writeFileSync(filePath, editor.getText())
|
||||
|
||||
getFullPath = (filePath) ->
|
||||
return filePath if path.isAbsolute(filePath)
|
||||
return path.join(atom.project.getPath(), 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)
|
||||
else
|
||||
throw new Error
|
||||
|
||||
replaceGroups = (groups, replString) ->
|
||||
arr = replString.split('')
|
||||
|
|
@ -100,7 +108,7 @@ class Ex
|
|||
tabp: => @tabprevious()
|
||||
|
||||
edit: (range, filePath) ->
|
||||
filePath = filePath.trim()
|
||||
filePath = path.normalize(filePath.trim())
|
||||
if filePath.indexOf(' ') isnt -1
|
||||
throw new CommandError('Only one file name allowed')
|
||||
buffer = atom.workspace.getActiveTextEditor().buffer
|
||||
|
|
@ -116,32 +124,18 @@ class Ex
|
|||
buffer.load()
|
||||
|
||||
write: (range, filePath) ->
|
||||
filePath = filePath.trim()
|
||||
filePath = path.normalize(filePath.trim())
|
||||
deferred = Promise.defer()
|
||||
|
||||
pane = atom.workspace.getActivePane()
|
||||
editor = atom.workspace.getActiveTextEditor()
|
||||
if atom.workspace.getActiveTextEditor().getPath() isnt undefined
|
||||
if filePath.length > 0
|
||||
editorPath = editor.getPath()
|
||||
try
|
||||
fullPath = getFullPath(filePath)
|
||||
trySave(-> saveAs(fullPath))
|
||||
.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
|
||||
catch error
|
||||
fullPath = atom.showSaveDialogSync()
|
||||
if fullPath?
|
||||
trySave(-> editor.saveAs(fullPath))
|
||||
.then deferred.resolve
|
||||
editor.buffer.setPath(fullPath)
|
||||
|
||||
deferred.promise
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue