See #36. Add right-click Beautification of single files in Tree view

This commit is contained in:
Glavin Wiechert 2014-12-28 18:17:46 -04:00
parent 19c6046cd8
commit e678fdf759
4 changed files with 60 additions and 14 deletions

View File

@ -8,4 +8,4 @@
# For more detailed documentation see
# https://atom.io/docs/latest/advanced/keymaps
'.workspace .editor:not(.mini)':
'ctrl-alt-b': 'beautify'
'ctrl-alt-b': 'beautify:editor'

View File

@ -7,6 +7,7 @@ _ = require("lodash")
beautifier = require("./language-options")
languages = beautifier.languages
defaultLanguageOptions = beautifier.defaultLanguageOptions
options = require "./options"
# Lazy loaded dependencies
fs = null
path = require("path")
@ -16,6 +17,8 @@ LoadingView = null
MessagePanelView = null
PlainMessageView = null
editorconfig = null
$ = null
#MessageView = require "./message-view"
findFileResults = {}
@ -300,6 +303,44 @@ beautify = ({onSave})->
showError(e)
return
beautifyFile = (event)->
# console.log('beautifyFile', arguments)
entry = event.target
# console.log('entry', entry)
return unless entry
$ ?= (require "space-pen").$
$entry = $(entry)
if $entry.prop("tagName") is "LI"
$entry = $("span.name", $entry)
# console.log($entry)
filePath = $entry.data('path')
# console.log('filePath', filePath)
# Get contents of file
fs ?= require "fs"
fs.readFile(filePath, (err, data) ->
throw error if err
input = data?.toString()
grammar = atom.grammars.selectGrammar(filePath, input)
grammarName = grammar.name
# Get the options
allOptions = options.getOptionsForPath(filePath)
# Beautify File
completionFun = (output) ->
if output instanceof Error
throw output # output == Error
else if typeof output is "string"
fs.writeFile(filePath, output, (err) ->
throw err if err
)
else
console.log(output)
try
beautifier.beautify input, grammarName, allOptions, completionFun
catch e
console.error(e)
)
handleSaveEvent = =>
atom.workspace.eachEditor (editor) =>
buffer = editor.getBuffer()
@ -323,4 +364,5 @@ plugin.configDefaults = _.merge(
plugin.activate = ->
handleSaveEvent()
plugin.subscribe atom.config.observe("atom-beautify.beautifyOnSave", handleSaveEvent)
atom.workspaceView.command "beautify", beautify
atom.workspaceView.command "beautify:editor", beautify
atom.workspaceView.command "beautify:file", beautifyFile

View File

@ -1,14 +1,16 @@
# See https://atom.io/docs/latest/creating-a-package#menus for more details
'context-menu':
'.workspace .editor:not(.mini)':
'Enable atom-beautify': 'beautify'
'Enable atom-beautify': 'beautify:editor'
'.tree-view li[is="tree-view-file"].file.entry':
'Beautify File': 'beautify:file'
'menu': [
{
'label': 'Packages'
'submenu': [
'label': 'Beautify'
'command': 'beautify'
'command': 'beautify:editor'
]
}
]

View File

@ -83,24 +83,26 @@
"atom": ">0.50.0"
},
"dependencies": {
"analytics-node": "^1.0.2",
"atom-message-panel": "^1.1.1",
"coffee-formatter": "^0.1.0",
"editorconfig": "^0.11.4",
"emissary": "^1.0.0",
"extend": "^1.2.1",
"js-beautify": "^1.5.4",
"lodash": "2.4.1",
"strip-json-comments": "^0.1.3",
"js-yaml": "^3.0.2",
"temp": "^0.8.0",
"prettydiff": "^1.6.11",
"node-uuid": "^1.4.1",
"analytics-node": "^1.0.2",
"coffee-formatter": "^0.1.0",
"atom-message-panel": "^1.1.1",
"editorconfig": "^0.11.4",
"lodash": "2.4.1",
"loophole": "^1.0.0",
"node-uuid": "^1.4.1",
"prettydiff": "^1.6.11",
"space-pen": "^4.3.0",
"strip-json-comments": "^0.1.3",
"temp": "^0.8.0",
"typescript-formatter": "~0.1.4"
},
"activationEvents": [
"beautify",
"beautify:editor",
"beautify:file",
"core:save",
"core:save-as"
]