Clean up save logic (fixes #75)
This commit is contained in:
parent
84c548a444
commit
1a117bddf9
1 changed files with 30 additions and 17 deletions
|
|
@ -37,16 +37,14 @@ saveAs = (filePath) ->
|
||||||
fs.writeFileSync(filePath, editor.getText())
|
fs.writeFileSync(filePath, editor.getText())
|
||||||
|
|
||||||
getFullPath = (filePath) ->
|
getFullPath = (filePath) ->
|
||||||
if filePath is ''
|
|
||||||
throw new Error
|
|
||||||
if path.isAbsolute(filePath)
|
if path.isAbsolute(filePath)
|
||||||
return filePath
|
fullPath = filePath
|
||||||
else if atom.workspace.getActiveTextEditor().getPath()?
|
else if atom.project.getPaths().length == 0
|
||||||
return path.join(path.dirname(atom.workspace.getActiveTextEditor().getPath()), filePath)
|
fullPath = path.join('~', filePath)
|
||||||
else if atom.project.getPaths()[0]?
|
|
||||||
return path.join(atom.project.getPaths()[0], filePath)
|
|
||||||
else
|
else
|
||||||
throw new Error
|
fullPath = path.join(atom.project.getPaths()[0], filePath)
|
||||||
|
|
||||||
|
return fs.normalize(fullPath)
|
||||||
|
|
||||||
replaceGroups = (groups, replString) ->
|
replaceGroups = (groups, replString) ->
|
||||||
arr = replString.split('')
|
arr = replString.split('')
|
||||||
|
|
@ -127,18 +125,33 @@ class Ex
|
||||||
buffer.load()
|
buffer.load()
|
||||||
|
|
||||||
write: (range, filePath) ->
|
write: (range, filePath) ->
|
||||||
filePath = fs.normalize(filePath.trim())
|
filePath = filePath.trim()
|
||||||
deferred = Promise.defer()
|
deferred = Promise.defer()
|
||||||
|
|
||||||
|
pane = atom.workspace.getActivePane()
|
||||||
editor = atom.workspace.getActiveTextEditor()
|
editor = atom.workspace.getActiveTextEditor()
|
||||||
try
|
if editor.getPath()?
|
||||||
|
if filePath.length > 0
|
||||||
|
editorPath = editor.getPath()
|
||||||
fullPath = getFullPath(filePath)
|
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()
|
fullPath = atom.showSaveDialogSync()
|
||||||
if fullPath?
|
if fullPath?
|
||||||
trySave(-> editor.saveAs(fullPath))
|
trySave(-> editor.saveAs(fullPath))
|
||||||
|
.then -> editor.buffer.setPath(fullPath)
|
||||||
.then deferred.resolve
|
.then deferred.resolve
|
||||||
editor.buffer.setPath(fullPath)
|
|
||||||
|
|
||||||
deferred.promise
|
deferred.promise
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue