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: For example:
```coffeescript ```coffeescript
'.editor': # Available from Editor only '.editor':
'ctrl-alt-b': 'beautify' 'ctrl-alt-b': 'beautify:beautify-editor'
``` ```
### Package Options ### Package Options

View File

@ -13,6 +13,8 @@ fs = null
path = require("path") path = require("path")
strip = null strip = null
yaml = null yaml = null
async = null
dir = null # Node-Dir
LoadingView = null LoadingView = null
MessagePanelView = null MessagePanelView = null
PlainMessageView = null PlainMessageView = null
@ -44,7 +46,7 @@ setCursors = (editor, posArray) ->
editor.addCursorAtBufferPosition bufferPosition editor.addCursorAtBufferPosition bufferPosition
return return
beautify = ({onSave})-> beautify = ({onSave}) ->
path ?= require("path") path ?= require("path")
MessagePanelView ?= require('atom-message-panel').MessagePanelView MessagePanelView ?= require('atom-message-panel').MessagePanelView
PlainMessageView ?= require('atom-message-panel').PlainMessageView PlainMessageView ?= require('atom-message-panel').PlainMessageView
@ -133,22 +135,20 @@ beautify = ({onSave})->
showError(e) showError(e)
return return
beautifyFile = (event)-> beautifyFilePath = (filePath, callback) ->
# console.log('beautifyFile', arguments) # Show in progress indicate on file's tree-view entry
entry = event.target $ ?= require("space-pen").$
# console.log('entry', entry) $el = $(".icon-file-text[data-path=\"#{filePath}\"]")
return unless entry $el.addClass('beautifying')
$ ?= (require "space-pen").$ # Cleanup and return callback function
$entry = $(entry) cb = (err, result) ->
if $entry.prop("tagName") is "LI" $el = $(".icon-file-text[data-path=\"#{filePath}\"]")
$entry = $("span.name", $entry) $el.removeClass('beautifying')
# console.log($entry) return callback(err, result)
filePath = $entry.data('path')
# console.log('filePath', filePath)
# Get contents of file # Get contents of file
fs ?= require "fs" fs ?= require "fs"
fs.readFile(filePath, (err, data) -> fs.readFile(filePath, (err, data) ->
throw error if err return cb(err) if err
input = data?.toString() input = data?.toString()
grammar = atom.grammars.selectGrammar(filePath, input) grammar = atom.grammars.selectGrammar(filePath, input)
grammarName = grammar.name grammarName = grammar.name
@ -157,20 +157,52 @@ beautifyFile = (event)->
# Beautify File # Beautify File
completionFun = (output) -> completionFun = (output) ->
if output instanceof Error if output instanceof Error
throw output # output == Error return cb(output, null) # output == Error
else if typeof output is "string" else if typeof output is "string"
fs.writeFile(filePath, output, (err) -> fs.writeFile(filePath, output, (err) ->
throw err if err return cb(err) if err
return cb(null, output)
) )
else else
console.log(output) return cb(new Error("Unknown beautification result #{output}."), output)
try try
beautifier.beautify input, grammarName, allOptions, completionFun beautifier.beautify input, grammarName, allOptions, completionFun
catch e 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 = => handleSaveEvent = =>
atom.workspace.eachEditor (editor) => atom.workspace.eachEditor (editor) =>
buffer = editor.getBuffer() buffer = editor.getBuffer()
@ -194,6 +226,6 @@ plugin.configDefaults = _.merge(
plugin.activate = -> plugin.activate = ->
handleSaveEvent() handleSaveEvent()
plugin.subscribe atom.config.observe("atom-beautify.beautifyOnSave", handleSaveEvent) plugin.subscribe atom.config.observe("atom-beautify.beautifyOnSave", handleSaveEvent)
atom.workspaceView.command "beautify", beautify atom.commands.add "atom-workspace", "beautify:beautify-editor", beautify
atom.workspaceView.command "beautify:editor", beautify atom.commands.add ".tree-view .file .name", "beautify:beautify-file", beautifyFile
atom.workspaceView.command "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 # See https://atom.io/docs/latest/creating-a-package#menus for more details
'context-menu': 'context-menu':
'.workspace .editor:not(.mini)': '.workspace .editor:not(.mini)':
'Enable atom-beautify': 'beautify:editor' 'Enable atom-beautify': 'beautify:beautify-editor'
'.tree-view li[is="tree-view-file"].file.entry': '.tree-view .file > .name':
'Beautify File': 'beautify:file' 'Beautify File': 'beautify:beautify-file'
'.tree-view .directory > .header > .name':
'Beautify Directory': 'beautify:beautify-directory'
'menu': [ 'menu': [
{ {
'label': 'Packages' 'label': 'Packages'
'submenu': [ 'submenu': [
'label': 'Beautify' 'label': 'Beautify'
'command': 'beautify:editor' 'command': 'beautify:beautify-editor'
] ]
} }
] ]

View File

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