From e678fdf759a338f74c9111c993474b06f6f29644 Mon Sep 17 00:00:00 2001 From: Glavin Wiechert Date: Sun, 28 Dec 2014 18:17:46 -0400 Subject: [PATCH] See #36. Add right-click Beautification of single files in Tree view --- keymaps/atom-beautify.cson | 2 +- lib/beautify.coffee | 44 +++++++++++++++++++++++++++++++++++++- menus/atom-beautify.cson | 6 ++++-- package.json | 22 ++++++++++--------- 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/keymaps/atom-beautify.cson b/keymaps/atom-beautify.cson index f8f25b6..ddf0c68 100644 --- a/keymaps/atom-beautify.cson +++ b/keymaps/atom-beautify.cson @@ -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' diff --git a/lib/beautify.coffee b/lib/beautify.coffee index 91543a1..3a86402 100644 --- a/lib/beautify.coffee +++ b/lib/beautify.coffee @@ -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 diff --git a/menus/atom-beautify.cson b/menus/atom-beautify.cson index dbc366a..03daed5 100644 --- a/menus/atom-beautify.cson +++ b/menus/atom-beautify.cson @@ -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' ] } ] diff --git a/package.json b/package.json index 9ebcf29..d16f70f 100644 --- a/package.json +++ b/package.json @@ -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" ]