Skip to content

Commit

Permalink
See #36. Add right-click Beautification of single files in Tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Dec 28, 2014
1 parent 19c6046 commit e678fdf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion keymaps/atom-beautify.cson
Original file line number Diff line number Diff line change
Expand Up @@ -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'
44 changes: 43 additions & 1 deletion lib/beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -16,6 +17,8 @@ LoadingView = null
MessagePanelView = null
PlainMessageView = null
editorconfig = null
$ = null

#MessageView = require "./message-view"
findFileResults = {}

Expand Down Expand Up @@ -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()
Expand All @@ -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
6 changes: 4 additions & 2 deletions menus/atom-beautify.cson
Original file line number Diff line number Diff line change
@@ -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'
]
}
]
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"js-yaml": "^3.0.2",
"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",
"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",
"loophole": "^1.0.0",
"typescript-formatter": "~0.1.4"
},
"activationEvents": [
"beautify",
"beautify:editor",
"beautify:file",
"core:save",
"core:save-as"
]
Expand Down

0 comments on commit e678fdf

Please sign in to comment.