Refactor handleSaveEvent to use onWillSave async (#1924)

* Update handleSaveEvent to use onWillSave and refactor

* Remove old handleSaveEvent function

* Specify minimum version of Atom of 1.21.0
This commit is contained in:
Steven Zeck 2017-11-21 21:08:06 -06:00 committed by Glavin Wiechert
parent 3dd3a2b9e9
commit a5c5d339a5
2 changed files with 8 additions and 23 deletions

View File

@ -153,7 +153,7 @@
} }
], ],
"engines": { "engines": {
"atom": ">=1.6.0 <2.0.0" "atom": ">=1.21.0 <2.0.0"
}, },
"dependencies": { "dependencies": {
"align-yaml": "^0.1.8", "align-yaml": "^0.1.8",

View File

@ -506,20 +506,17 @@ debug = () ->
handleSaveEvent = -> handleSaveEvent = ->
atom.workspace.observeTextEditors (editor) -> atom.workspace.observeTextEditors (editor) ->
pendingPaths = {}
beautifyOnSaveHandler = ({path: filePath}) -> beautifyOnSaveHandler = ({path: filePath}) ->
logger.verbose('Should beautify on this save?')
if pendingPaths[filePath]
logger.verbose("Editor with file path #{filePath} already beautified!")
return
buffer = editor.getBuffer()
path ?= require('path') path ?= require('path')
# Get Grammar
grammar = editor.getGrammar().name
# Get file extension # Get file extension
fileExtension = path.extname(filePath) fileExtension = path.extname(filePath)
# Remove prefix "." (period) in fileExtension # Remove prefix "." (period) in fileExtension
fileExtension = fileExtension.substr(1) fileExtension = fileExtension.substr(1)
# Set path of buffer for unsaved files
if editor.getPath() is undefined
editor.getBuffer().setPath(filePath)
# Get Grammar from the editor
grammar = editor.getGrammar().name
# Get language # Get language
languages = beautifier.languages.getLanguages({grammar, extension: fileExtension}) languages = beautifier.languages.getLanguages({grammar, extension: fileExtension})
if languages.length < 1 if languages.length < 1
@ -535,23 +532,11 @@ handleSaveEvent = ->
beautify({editor, onSave: true}) beautify({editor, onSave: true})
.then(() -> .then(() ->
logger.verbose('Done beautifying file', filePath) logger.verbose('Done beautifying file', filePath)
if editor.isAlive() is true
logger.verbose('Saving TextEditor...')
# Store the filePath to prevent infinite looping
# When Whitespace package has option "Ensure Single Trailing Newline" enabled
# It will add a newline and keep the file from converging on a beautified form
# and saving without emitting onDidSave event, because there were no changes.
pendingPaths[filePath] = true
Promise.resolve(editor.save()).then(() ->
delete pendingPaths[filePath]
logger.verbose('Saved TextEditor.')
)
) )
.catch((error) -> .catch((error) ->
return showError(error) return showError(error)
) )
disposable = editor.onDidSave(({path : filePath}) -> disposable = editor.getBuffer().onWillSave(({path: filePath}) ->
# TODO: Implement debouncing
beautifyOnSaveHandler({path: filePath}) beautifyOnSaveHandler({path: filePath})
) )
plugin.subscriptions.add disposable plugin.subscriptions.add disposable
@ -632,4 +617,4 @@ plugin.activate = ->
@addLanguageCommands() @addLanguageCommands()
plugin.deactivate = -> plugin.deactivate = ->
@subscriptions.dispose() @subscriptions.dispose()