Closes #36. Add right-click beautification of directory (sub-files)

This commit is contained in:
Glavin Wiechert 2015-01-01 23:34:04 -04:00
parent 0df6e4734b
commit bd278968b4
5 changed files with 85 additions and 30 deletions

View File

@ -79,8 +79,8 @@ See [Keymaps In-Depth](https://atom.io/docs/latest/advanced/keymaps) for more de
For example:
```coffeescript
'.editor': # Available from Editor only
'ctrl-alt-b': 'beautify'
'.editor':
'ctrl-alt-b': 'beautify:beautify-editor'
```
### Package Options

View File

@ -13,6 +13,8 @@ fs = null
path = require("path")
strip = null
yaml = null
async = null
dir = null # Node-Dir
LoadingView = null
MessagePanelView = null
PlainMessageView = null
@ -133,22 +135,20 @@ 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)
beautifyFilePath = (filePath, callback) ->
# Show in progress indicate on file's tree-view entry
$ ?= require("space-pen").$
$el = $(".icon-file-text[data-path=\"#{filePath}\"]")
$el.addClass('beautifying')
# Cleanup and return callback function
cb = (err, result) ->
$el = $(".icon-file-text[data-path=\"#{filePath}\"]")
$el.removeClass('beautifying')
return callback(err, result)
# Get contents of file
fs ?= require "fs"
fs.readFile(filePath, (err, data) ->
throw error if err
return cb(err) if err
input = data?.toString()
grammar = atom.grammars.selectGrammar(filePath, input)
grammarName = grammar.name
@ -157,20 +157,52 @@ beautifyFile = (event)->
# Beautify File
completionFun = (output) ->
if output instanceof Error
throw output # output == Error
return cb(output, null) # output == Error
else if typeof output is "string"
fs.writeFile(filePath, output, (err) ->
throw err if err
return cb(err) if err
return cb(null, output)
)
else
console.log(output)
return cb(new Error("Unknown beautification result #{output}."), output)
try
beautifier.beautify input, grammarName, allOptions, completionFun
catch e
console.error(e)
return cb(e)
)
beautifyFile = ({target}) ->
filePath = target.dataset.path
return unless filePath
beautifyFilePath(filePath, (err, result) ->
return console.error('beautifyFile error', err, result) if err
# console.log("Beautify File #{filePath} complete with result: ", result)
)
return
beautifyDirectory = ({target}) ->
dirPath = target.dataset.path
return unless dirPath
# Show in progress indicate on directory's tree-view entry
$ ?= require("space-pen").$
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
$el.addClass('beautifying')
# Process Directory
dir ?= require "node-dir"
async ?= require "async"
dir.files(dirPath, (err, files) ->
return console.error('beautifyDirectory error', err) if err
async.each(files, (filePath, callback) ->
# Ignore errors
beautifyFilePath(filePath, -> callback())
, (err) ->
$el = $(".icon-file-directory[data-path=\"#{dirPath}\"]")
$el.removeClass('beautifying')
# console.log('Completed beautifying directory!', dirPath)
)
)
return
handleSaveEvent = =>
atom.workspace.eachEditor (editor) =>
buffer = editor.getBuffer()
@ -194,6 +226,6 @@ 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
atom.commands.add "atom-workspace", "beautify:beautify-editor", beautify
atom.commands.add ".tree-view .file .name", "beautify:beautify-file", beautifyFile
atom.commands.add ".tree-view .directory .name", "beautify:beautify-directory", beautifyDirectory

View File

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

View File

@ -84,6 +84,7 @@
},
"dependencies": {
"analytics-node": "^1.0.2",
"async": "^0.9.0",
"atom-message-panel": "^1.1.1",
"coffee-formatter": "^0.1.1",
"editorconfig": "^0.11.4",
@ -93,6 +94,7 @@
"js-yaml": "^3.0.2",
"lodash": "2.4.1",
"loophole": "^1.0.0",
"node-dir": "^0.1.6",
"node-uuid": "^1.4.1",
"prettydiff": "^1.6.13",
"space-pen": "^4.3.0",
@ -102,8 +104,9 @@
},
"activationEvents": [
"beautify",
"beautify:editor",
"beautify:file",
"beautify:beautify-editor",
"beautify:beautify-file",
"beautify:beautify-directory",
"core:save",
"core:save-as"
]

18
stylesheets/beautify.less Normal file
View File

@ -0,0 +1,18 @@
.icon-file-text,.icon-file-directory {
// While beautifying / in progress
&.beautifying:before {
// font-family: "Octicons Regular";
// font-size: medium;
// text-align: center;
// content: "\f09e"; // Octicon-hourglass
background-image: url("images/octocat-spinner-128.gif");
// Animated spinner
background-repeat: no-repeat;
background-size: contain;
// Image should fit to size of container, font-size
content: "";
// Clear original icon content
}
}