Fixes #338. Restore cursor and scroll position for beautify on save

This commit is contained in:
Glavin Wiechert 2015-05-27 21:32:55 -03:00
parent 8e4df8da90
commit fd00de7067
1 changed files with 14 additions and 4 deletions

View File

@ -8,6 +8,7 @@ _ = require("lodash")
Beautifiers = require("./beautifiers") Beautifiers = require("./beautifiers")
beautifier = new Beautifiers() beautifier = new Beautifiers()
defaultLanguageOptions = beautifier.options defaultLanguageOptions = beautifier.options
logger = require('./logger')(__filename)
# Lazy loaded dependencies # Lazy loaded dependencies
fs = null fs = null
@ -355,7 +356,6 @@ debug = () ->
"Options from `.jsbeautifyrc` files starting from directory `#{path.dirname(filePath)}` and going up to root\n" + "Options from `.jsbeautifyrc` files starting from directory `#{path.dirname(filePath)}` and going up to root\n" +
"```json\n#{JSON.stringify(projectOptions, undefined, 4)}\n```") "```json\n#{JSON.stringify(projectOptions, undefined, 4)}\n```")
logs = "" logs = ""
logger = require('./logger')(__filename)
subscription = logger.onLogging((msg) -> subscription = logger.onLogging((msg) ->
# console.log('logging', msg) # console.log('logging', msg)
@ -392,18 +392,28 @@ handleSaveEvent = =>
path ?= require('path') path ?= require('path')
# Get Grammar # Get Grammar
grammar = editor.getGrammar().name grammar = editor.getGrammar().name
# Get language # Get file extension
fileExtension = path.extname(filePath) fileExtension = path.extname(filePath)
languages = beautifier.languages.getLanguages({grammar, fileExtension}) # Remove prefix "." (period) in fileExtension
fileExtension = fileExtension.substr(1)
# Get language
languages = beautifier.languages.getLanguages({grammar, extension: fileExtension})
if languages.length < 1 if languages.length < 1
return return
# TODO: select appropriate language # TODO: select appropriate language
language = languages[0] language = languages[0]
# Get language config # Get language config
beautifyOnSave = atom.config.get("atom-beautify.language_#{language.namespace}_beautify_on_save") key = "atom-beautify.language_#{language.namespace}_beautify_on_save"
beautifyOnSave = atom.config.get(key)
logger.verbose('save editor positions', key, beautifyOnSave)
if beautifyOnSave if beautifyOnSave
posArray = getCursors(editor)
origScrollTop = editor.getScrollTop()
beautifyFilePath(filePath, -> beautifyFilePath(filePath, ->
buffer.reload() buffer.reload()
logger.verbose('restore editor positions', posArray,origScrollTop)
setCursors(editor, posArray)
editor.setScrollTop(origScrollTop)
) )
) )
plugin.subscribe disposable plugin.subscribe disposable