-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatjson.js
More file actions
107 lines (91 loc) · 2.92 KB
/
formatjson.js
File metadata and controls
107 lines (91 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Format JSON extension for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function(require, exports, module) {
var ide = require("core/ide");
var ext = require("core/ext");
var util = require("core/util");
var menus = require("ext/menus/menus");
var editors = require("ext/editors/editors");
var Range = require("ace/range").Range;
var markup = require("text!ext/formatjson/formatjson.xml");
var commands = require("ext/commands/commands");
module.exports = ext.register("ext/formatjson/formatjson", {
name : "JSON Formatter",
dev : "Ajax.org",
alone : true,
type : ext.GENERAL,
markup : markup,
nodes : [],
format : function(indent){
var editor = editors.currentEditor;
var sel = editor.getSelection();
var doc = editor.getDocument();
var range = sel.getRange();
var value = doc.getTextRange(range);
try {
value = JSON.stringify(JSON.parse(value), null, indent);
}
catch (e) {
util.alert(
"Invalid JSON",
"The selection contains an invalid or incomplete JSON string",
"Please correct the JSON and try again");
return;
}
var end = doc.replace(range, value);
sel.setSelectionRange(Range.fromPoints(range.start, end));
},
hook : function(){
var _self = this;
commands.addCommand({
name : "formatjson",
bindKey : {mac: "Shift-Command-J", win: "Ctrl-Shift-J"},
hint: "reformat the current JSON document",
isAvailable : function(editor){
if (editor && editor.ceEditor) {
var range = editor.ceEditor.$editor.getSelectionRange();
return range.start.row == range.end.row
&& range.start.column == range.end.column
}
return false;
},
exec : function(){
ext.initExtension(_self);
_self.winFormat.show();
}
});
var mnuItem;
this.nodes.push(
mnuItem = menus.addItemByPath("Tools/Format JSON", new apf.item({
command : "formatjson"
}), 500)
);
},
init : function(amlNode){
this.winFormat = winFormat;
},
enable : function(){
this.nodes.each(function(item){
item.enable();
});
},
disable : function(){
this.nodes.each(function(item){
item.disable();
});
},
destroy : function(){
commands.removeCommandByName("formatjson");
this.nodes.each(function(item){
item.destroy(true, true);
});
this.nodes = [];
this.winFormat.destroy(true, true);
}
});
}
);