]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/pretty-json/index.coffee
Update atoms
[rbdr/dotfiles] / atom / packages / pretty-json / index.coffee
1 stringify = require("json-stable-stringify")
2 uglify = require("jsonminify")
3 formatter = {}
4
5 prettify = (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
16 minify = (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
27 formatter.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
43 formatter.minify = (text) ->
44 try
45 JSON.parse(text)
46 uglify(text);
47 catch error
48 text;
49
50 module.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)