Skip to content

Commit

Permalink
Closes #36. Add right-click beautification of directory (sub-files)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Jan 2, 2015
1 parent 0df6e47 commit bd27896
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 53 additions & 21 deletions lib/beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,7 +46,7 @@ setCursors = (editor, posArray) ->
editor.addCursorAtBufferPosition bufferPosition
return

beautify = ({onSave})->
beautify = ({onSave}) ->
path ?= require("path")
MessagePanelView ?= require('atom-message-panel').MessagePanelView
PlainMessageView ?= require('atom-message-panel').PlainMessageView
Expand Down Expand Up @@ -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
Expand All @@ -157,19 +157,51 @@ 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) =>
Expand All @@ -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
10 changes: 6 additions & 4 deletions menus/atom-beautify.cson
Original file line number Diff line number Diff line change
@@ -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'
]
}
]
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -102,8 +104,9 @@
},
"activationEvents": [
"beautify",
"beautify:editor",
"beautify:file",
"beautify:beautify-editor",
"beautify:beautify-file",
"beautify:beautify-directory",
"core:save",
"core:save-as"
]
Expand Down
18 changes: 18 additions & 0 deletions stylesheets/beautify.less
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit bd27896

Please sign in to comment.