]> git.r.bdr.sh - rbdr/dotfiles/blame_incremental - atom/packages/pretty-json/index.coffee
Update vim plugins
[rbdr/dotfiles] / atom / packages / pretty-json / index.coffee
... / ...
CommitLineData
1stringify = require("json-stable-stringify")
2uglify = require("jsonminify")
3formatter = {}
4
5prettify = (editor, sorted) ->
6 wholeFile = editor.getGrammar().name == 'JSON'
7
8 if wholeFile
9 text = editor.getText()
10 editor.setText(formatter.pretty(text, sorted))
11 else
12 text = editor.replaceSelectedText({}, (text) ->
13 formatter.pretty(text, sorted)
14 )
15
16minify = (editor, sorted) ->
17 wholeFile = editor.getGrammar().name == 'JSON'
18
19 if wholeFile
20 text = editor.getText()
21 editor.setText(formatter.minify(text))
22 else
23 text = editor.replaceSelectedText({}, (text) ->
24 formatter.minify(text);
25 )
26
27formatter.pretty = (text, sorted) ->
28 editorSettings = atom.config.get('editor')
29 if editorSettings.softTabs?
30 space = Array(editorSettings.tabLength + 1).join(" ")
31 else
32 space = "\t"
33
34 try
35 parsed = JSON.parse(text)
36 if sorted
37 return stringify(parsed, { space: space })
38 else
39 return JSON.stringify(parsed, null, space)
40 catch error
41 text
42
43formatter.minify = (text) ->
44 try
45 JSON.parse(text)
46 uglify(text);
47 catch error
48 text;
49
50module.exports =
51 activate: ->
52 atom.commands.add 'atom-workspace',
53 'pretty-json:prettify': ->
54 editor = atom.workspace.getActiveTextEditor()
55 prettify(editor)
56 'pretty-json:sort-and-prettify': ->
57 editor = atom.workspace.getActiveTextEditor()
58 prettify(editor, true)
59 'pretty-json:minify': ->
60 editor = atom.workspace.getActiveTextEditor()
61 minify(editor, true)