Debian Codegraph @

extensions/CodeEditor/modules/lib/ace/ext-beautify.js

raw symbols SCIP
1 ace.define("ace/ext/beautify",["require","exports","module","ace/token_iterator"], function(require, exports, module){// [WIP]
2 "use strict";
3 var TokenIterator = require("../token_iterator").TokenIterator;
4 function is(token, type) {
5 return token.type.lastIndexOf(type + ".xml") > -1;
6 }
7 exports.singletonTags = ["area", "base", "br", "col", "command", "embed", "hr", "html", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"];
8 exports.blockTags = ["article", "aside", "blockquote", "body", "div", "dl", "fieldset", "footer", "form", "head", "header", "html", "nav", "ol", "p", "script", "section", "style", "table", "tbody", "tfoot", "thead", "ul"];
9 exports.formatOptions = {
10 lineBreaksAfterCommasInCurlyBlock: true
11 };
12 exports.beautify = function (session) {
13 var iterator = new TokenIterator(session, 0, 0);
14 var token = iterator.getCurrentToken();
15 var tabString = session.getTabString();
16 var singletonTags = exports.singletonTags;
17 var blockTags = exports.blockTags;
18 var formatOptions = exports.formatOptions || {};
19 var nextToken;
20 var breakBefore = false;
21 var spaceBefore = false;
22 var spaceAfter = false;
23 var code = "";
24 var value = "";
25 var tagName = "";
26 var depth = 0;
27 var lastDepth = 0;
28 var lastIndent = 0;
29 var indent = 0;
30 var unindent = 0;
31 var roundDepth = 0;
32 var curlyDepth = 0;
33 var row;
34 var curRow = 0;
35 var rowsToAdd = 0;
36 var rowTokens = [];
37 var abort = false;
38 var i;
39 var indentNextLine = false;
40 var inTag = false;
41 var inCSS = false;
42 var inBlock = false;
43 var levels = { 0: 0 };
44 var parents = [];
45 var caseBody = false;
46 var trimNext = function () {
47 if (nextToken && nextToken.value && nextToken.type !== 'string.regexp')
48 nextToken.value = nextToken.value.replace(/^\s*/, "");
49 };
50 var trimLine = function () {
51 var end = code.length - 1;
52 while (true) {
53 if (end == 0)
54 break;
55 if (code[end] !== " ")
56 break;
57 end = end - 1;
58 }
59 code = code.slice(0, end + 1);
60 };
61 var trimCode = function () {
62 code = code.trimRight();
63 breakBefore = false;
64 };
65 while (token !== null) {
66 curRow = iterator.getCurrentTokenRow();
67 rowTokens = iterator.$rowTokens;
68 nextToken = iterator.stepForward();
69 if (typeof token !== "undefined") {
70 value = token.value;
71 unindent = 0;
72 inCSS = (tagName === "style" || session.$modeId === "ace/mode/css");
73 if (is(token, "tag-open")) {
74 inTag = true;
75 if (nextToken)
76 inBlock = (blockTags.indexOf(nextToken.value) !== -1);
77 if (value === "</") {
78 if (inBlock && !breakBefore && rowsToAdd < 1)
79 rowsToAdd++;
80 if (inCSS)
81 rowsToAdd = 1;
82 unindent = 1;
83 inBlock = false;
84 }
85 }
86 else if (is(token, "tag-close")) {
87 inTag = false;
88 }
89 else if (is(token, "comment.start")) {
90 inBlock = true;
91 }
92 else if (is(token, "comment.end")) {
93 inBlock = false;
94 }
95 if (!inTag && !rowsToAdd && token.type === "paren.rparen" && token.value.substr(0, 1) === "}") {
96 rowsToAdd++;
97 }
98 if (curRow !== row) {
99 rowsToAdd = curRow;
100 if (row)
101 rowsToAdd -= row;
102 }
103 if (rowsToAdd) {
104 trimCode();
105 for (; rowsToAdd > 0; rowsToAdd--)
106 code += "\n";
107 breakBefore = true;
108 if (!is(token, "comment") && !token.type.match(/^(comment|string)$/))
109 value = value.trimLeft();
110 }
111 if (value) {
112 if (token.type === "keyword" && value.match(/^(if|else|elseif|for|foreach|while|switch)$/)) {
113 parents[depth] = value;
114 trimNext();
115 spaceAfter = true;
116 if (value.match(/^(else|elseif)$/)) {
117 if (code.match(/\}[\s]*$/)) {
118 trimCode();
119 spaceBefore = true;
120 }
121 }
122 }
123 else if (token.type === "paren.lparen") {
124 trimNext();
125 if (value.substr(-1) === "{") {
126 spaceAfter = true;
127 indentNextLine = false;
128 if (!inTag)
129 rowsToAdd = 1;
130 }
131 if (value.substr(0, 1) === "{") {
132 spaceBefore = true;
133 if (code.substr(-1) !== '[' && code.trimRight().substr(-1) === '[') {
134 trimCode();
135 spaceBefore = false;
136 }
137 else if (code.trimRight().substr(-1) === ')') {
138 trimCode();
139 }
140 else {
141 trimLine();
142 }
143 }
144 }
145 else if (token.type === "paren.rparen") {
146 unindent = 1;
147 if (value.substr(0, 1) === "}") {
148 if (parents[depth - 1] === 'case')
149 unindent++;
150 if (code.trimRight().substr(-1) === '{') {
151 trimCode();
152 }
153 else {
154 spaceBefore = true;
155 if (inCSS)
156 rowsToAdd += 2;
157 }
158 }
159 if (value.substr(0, 1) === "]") {
160 if (code.substr(-1) !== '}' && code.trimRight().substr(-1) === '}') {
161 spaceBefore = false;
162 indent++;
163 trimCode();
164 }
165 }
166 if (value.substr(0, 1) === ")") {
167 if (code.substr(-1) !== '(' && code.trimRight().substr(-1) === '(') {
168 spaceBefore = false;
169 indent++;
170 trimCode();
171 }
172 }
173 trimLine();
174 }
175 else if ((token.type === "keyword.operator" || token.type === "keyword") && value.match(/^(=|==|===|!=|!==|&&|\|\||and|or|xor|\+=|.=|>|>=|<|<=|=>)$/)) {
176 trimCode();
177 trimNext();
178 spaceBefore = true;
179 spaceAfter = true;
180 }
181 else if (token.type === "punctuation.operator" && value === ';') {
182 trimCode();
183 trimNext();
184 spaceAfter = true;
185 if (inCSS)
186 rowsToAdd++;
187 }
188 else if (token.type === "punctuation.operator" && value.match(/^(:|,)$/)) {
189 trimCode();
190 trimNext();
191 if (value.match(/^(,)$/) && curlyDepth > 0 && roundDepth === 0 && formatOptions.lineBreaksAfterCommasInCurlyBlock) {
192 rowsToAdd++;
193 }
194 else {
195 spaceAfter = true;
196 breakBefore = false;
197 }
198 }
199 else if (token.type === "support.php_tag" && value === "?>" && !breakBefore) {
200 trimCode();
201 spaceBefore = true;
202 }
203 else if (is(token, "attribute-name") && code.substr(-1).match(/^\s$/)) {
204 spaceBefore = true;
205 }
206 else if (is(token, "attribute-equals")) {
207 trimLine();
208 trimNext();
209 }
210 else if (is(token, "tag-close")) {
211 trimLine();
212 if (value === "/>")
213 spaceBefore = true;
214 }
215 else if (token.type === "keyword" && value.match(/^(case|default)$/)) {
216 if (caseBody)
217 unindent = 1;
218 }
219 if (breakBefore && !(token.type.match(/^(comment)$/) && !value.substr(0, 1).match(/^[/#]$/)) && !(token.type.match(/^(string)$/) && !value.substr(0, 1).match(/^['"@]$/))) {
220 indent = lastIndent;
221 if (depth > lastDepth) {
222 indent++;
223 for (i = depth; i > lastDepth; i--)
224 levels[i] = indent;
225 }
226 else if (depth < lastDepth)
227 indent = levels[depth];
228 lastDepth = depth;
229 lastIndent = indent;
230 if (unindent)
231 indent -= unindent;
232 if (indentNextLine && !roundDepth) {
233 indent++;
234 indentNextLine = false;
235 }
236 for (i = 0; i < indent; i++)
237 code += tabString;
238 }
239 if (token.type === "keyword" && value.match(/^(case|default)$/)) {
240 if (caseBody === false) {
241 parents[depth] = value;
242 depth++;
243 caseBody = true;
244 }
245 }
246 else if (token.type === "keyword" && value.match(/^(break)$/)) {
247 if (parents[depth - 1] && parents[depth - 1].match(/^(case|default)$/)) {
248 depth--;
249 caseBody = false;
250 }
251 }
252 if (token.type === "paren.lparen") {
253 roundDepth += (value.match(/\(/g) || []).length;
254 curlyDepth += (value.match(/\{/g) || []).length;
255 depth += value.length;
256 }
257 if (token.type === "keyword" && value.match(/^(if|else|elseif|for|while)$/)) {
258 indentNextLine = true;
259 roundDepth = 0;
260 }
261 else if (!roundDepth && value.trim() && token.type !== "comment")
262 indentNextLine = false;
263 if (token.type === "paren.rparen") {
264 roundDepth -= (value.match(/\)/g) || []).length;
265 curlyDepth -= (value.match(/\}/g) || []).length;
266 for (i = 0; i < value.length; i++) {
267 depth--;
268 if (value.substr(i, 1) === '}' && parents[depth] === 'case') {
269 depth--;
270 }
271 }
272 }
273 if (token.type == "text")
274 value = value.replace(/\s+$/, " ");
275 if (spaceBefore && !breakBefore) {
276 trimLine();
277 if (code.substr(-1) !== "\n")
278 code += " ";
279 }
280 code += value;
281 if (spaceAfter)
282 code += " ";
283 breakBefore = false;
284 spaceBefore = false;
285 spaceAfter = false;
286 if ((is(token, "tag-close") && (inBlock || blockTags.indexOf(tagName) !== -1)) || (is(token, "doctype") && value === ">")) {
287 if (inBlock && nextToken && nextToken.value === "</")
288 rowsToAdd = -1;
289 else
290 rowsToAdd = 1;
291 }
292 if (nextToken && singletonTags.indexOf(nextToken.value) === -1) {
293 if (is(token, "tag-open") && value === "</") {
294 depth--;
295 }
296 else if (is(token, "tag-open") && value === "<") {
297 depth++;
298 }
299 else if (is(token, "tag-close") && value === "/>") {
300 depth--;
301 }
302 }
303 if (is(token, "tag-name")) {
304 tagName = value;
305 }
306 row = curRow;
307 }
308 }
309 token = nextToken;
310 }
311 code = code.trim();
312 session.doc.setValue(code);
313 };
314 exports.commands = [{
315 name: "beautify",
316 description: "Format selection (Beautify)",
317 exec: function (editor) {
318 exports.beautify(editor.session);
319 },
320 bindKey: "Ctrl-Shift-B"
321 }];
322
323 }); (function() {
324 ace.require(["ace/ext/beautify"], function(m) {
325 if (typeof module == "object" && typeof exports == "object" && module) {
326 module.exports = m;
327 }
328 });
329 })();
330