ext-beautify.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. ace.define("ace/ext/beautify",[], function(require, exports, module) {
  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.beautify = function(session) {
  10. var iterator = new TokenIterator(session, 0, 0);
  11. var token = iterator.getCurrentToken();
  12. var tabString = session.getTabString();
  13. var singletonTags = exports.singletonTags;
  14. var blockTags = exports.blockTags;
  15. var nextToken;
  16. var breakBefore = false;
  17. var spaceBefore = false;
  18. var spaceAfter = false;
  19. var code = "";
  20. var value = "";
  21. var tagName = "";
  22. var depth = 0;
  23. var lastDepth = 0;
  24. var lastIndent = 0;
  25. var indent = 0;
  26. var unindent = 0;
  27. var roundDepth = 0;
  28. var onCaseLine = false;
  29. var row;
  30. var curRow = 0;
  31. var rowsToAdd = 0;
  32. var rowTokens = [];
  33. var abort = false;
  34. var i;
  35. var indentNextLine = false;
  36. var inTag = false;
  37. var inCSS = false;
  38. var inBlock = false;
  39. var levels = {0: 0};
  40. var parents = {};
  41. var trimNext = function() {
  42. if (nextToken && nextToken.value && nextToken.type !== 'string.regexp')
  43. nextToken.value = nextToken.value.trim();
  44. };
  45. var trimLine = function() {
  46. code = code.replace(/ +$/, "");
  47. };
  48. var trimCode = function() {
  49. code = code.trimRight();
  50. breakBefore = false;
  51. };
  52. while (token !== null) {
  53. curRow = iterator.getCurrentTokenRow();
  54. rowTokens = iterator.$rowTokens;
  55. nextToken = iterator.stepForward();
  56. if (typeof token !== "undefined") {
  57. value = token.value;
  58. unindent = 0;
  59. inCSS = (tagName === "style" || session.$modeId === "ace/mode/css");
  60. if (is(token, "tag-open")) {
  61. inTag = true;
  62. if (nextToken)
  63. inBlock = (blockTags.indexOf(nextToken.value) !== -1);
  64. if (value === "</") {
  65. if (inBlock && !breakBefore && rowsToAdd < 1)
  66. rowsToAdd++;
  67. if (inCSS)
  68. rowsToAdd = 1;
  69. unindent = 1;
  70. inBlock = false;
  71. }
  72. } else if (is(token, "tag-close")) {
  73. inTag = false;
  74. } else if (is(token, "comment.start")) {
  75. inBlock = true;
  76. } else if (is(token, "comment.end")) {
  77. inBlock = false;
  78. }
  79. if (!inTag && !rowsToAdd && token.type === "paren.rparen" && token.value.substr(0, 1) === "}") {
  80. rowsToAdd++;
  81. }
  82. if (curRow !== row) {
  83. rowsToAdd = curRow;
  84. if (row)
  85. rowsToAdd -= row;
  86. }
  87. if (rowsToAdd) {
  88. trimCode();
  89. for (; rowsToAdd > 0; rowsToAdd--)
  90. code += "\n";
  91. breakBefore = true;
  92. if (!is(token, "comment") && !token.type.match(/^(comment|string)$/))
  93. value = value.trimLeft();
  94. }
  95. if (value) {
  96. if (token.type === "keyword" && value.match(/^(if|else|elseif|for|foreach|while|switch)$/)) {
  97. parents[depth] = value;
  98. trimNext();
  99. spaceAfter = true;
  100. if (value.match(/^(else|elseif)$/)) {
  101. if (code.match(/\}[\s]*$/)) {
  102. trimCode();
  103. spaceBefore = true;
  104. }
  105. }
  106. } else if (token.type === "paren.lparen") {
  107. trimNext();
  108. if (value.substr(-1) === "{") {
  109. spaceAfter = true;
  110. indentNextLine = false;
  111. if(!inTag)
  112. rowsToAdd = 1;
  113. }
  114. if (value.substr(0, 1) === "{") {
  115. spaceBefore = true;
  116. if (code.substr(-1) !== '[' && code.trimRight().substr(-1) === '[') {
  117. trimCode();
  118. spaceBefore = false;
  119. } else if (code.trimRight().substr(-1) === ')') {
  120. trimCode();
  121. } else {
  122. trimLine();
  123. }
  124. }
  125. } else if (token.type === "paren.rparen") {
  126. unindent = 1;
  127. if (value.substr(0, 1) === "}") {
  128. if (parents[depth-1] === 'case')
  129. unindent++;
  130. if (code.trimRight().substr(-1) === '{') {
  131. trimCode();
  132. } else {
  133. spaceBefore = true;
  134. if (inCSS)
  135. rowsToAdd+=2;
  136. }
  137. }
  138. if (value.substr(0, 1) === "]") {
  139. if (code.substr(-1) !== '}' && code.trimRight().substr(-1) === '}') {
  140. spaceBefore = false;
  141. indent++;
  142. trimCode();
  143. }
  144. }
  145. if (value.substr(0, 1) === ")") {
  146. if (code.substr(-1) !== '(' && code.trimRight().substr(-1) === '(') {
  147. spaceBefore = false;
  148. indent++;
  149. trimCode();
  150. }
  151. }
  152. trimLine();
  153. } else if ((token.type === "keyword.operator" || token.type === "keyword") && value.match(/^(=|==|===|!=|!==|&&|\|\||and|or|xor|\+=|.=|>|>=|<|<=|=>)$/)) {
  154. trimCode();
  155. trimNext();
  156. spaceBefore = true;
  157. spaceAfter = true;
  158. } else if (token.type === "punctuation.operator" && value === ';') {
  159. trimCode();
  160. trimNext();
  161. spaceAfter = true;
  162. if (inCSS)
  163. rowsToAdd++;
  164. } else if (token.type === "punctuation.operator" && value.match(/^(:|,)$/)) {
  165. trimCode();
  166. trimNext();
  167. spaceAfter = true;
  168. breakBefore = false;
  169. } else if (token.type === "support.php_tag" && value === "?>" && !breakBefore) {
  170. trimCode();
  171. spaceBefore = true;
  172. } else if (is(token, "attribute-name") && code.substr(-1).match(/^\s$/)) {
  173. spaceBefore = true;
  174. } else if (is(token, "attribute-equals")) {
  175. trimLine();
  176. trimNext();
  177. } else if (is(token, "tag-close")) {
  178. trimLine();
  179. if(value === "/>")
  180. spaceBefore = true;
  181. }
  182. if (breakBefore && !(token.type.match(/^(comment)$/) && !value.substr(0, 1).match(/^[/#]$/)) && !(token.type.match(/^(string)$/) && !value.substr(0, 1).match(/^['"]$/))) {
  183. indent = lastIndent;
  184. if(depth > lastDepth) {
  185. indent++;
  186. for (i=depth; i > lastDepth; i--)
  187. levels[i] = indent;
  188. } else if(depth < lastDepth)
  189. indent = levels[depth];
  190. lastDepth = depth;
  191. lastIndent = indent;
  192. if(unindent)
  193. indent -= unindent;
  194. if (indentNextLine && !roundDepth) {
  195. indent++;
  196. indentNextLine = false;
  197. }
  198. for (i = 0; i < indent; i++)
  199. code += tabString;
  200. }
  201. if (token.type === "keyword" && value.match(/^(case|default)$/)) {
  202. parents[depth] = value;
  203. depth++;
  204. }
  205. if (token.type === "keyword" && value.match(/^(break)$/)) {
  206. if(parents[depth-1] && parents[depth-1].match(/^(case|default)$/)) {
  207. depth--;
  208. }
  209. }
  210. if (token.type === "paren.lparen") {
  211. roundDepth += (value.match(/\(/g) || []).length;
  212. depth += value.length;
  213. }
  214. if (token.type === "keyword" && value.match(/^(if|else|elseif|for|while)$/)) {
  215. indentNextLine = true;
  216. roundDepth = 0;
  217. } else if (!roundDepth && value.trim() && token.type !== "comment")
  218. indentNextLine = false;
  219. if (token.type === "paren.rparen") {
  220. roundDepth -= (value.match(/\)/g) || []).length;
  221. for (i = 0; i < value.length; i++) {
  222. depth--;
  223. if(value.substr(i, 1)==='}' && parents[depth]==='case') {
  224. depth--;
  225. }
  226. }
  227. }
  228. if (spaceBefore && !breakBefore) {
  229. trimLine();
  230. if (code.substr(-1) !== "\n")
  231. code += " ";
  232. }
  233. code += value;
  234. if (spaceAfter)
  235. code += " ";
  236. breakBefore = false;
  237. spaceBefore = false;
  238. spaceAfter = false;
  239. if ((is(token, "tag-close") && (inBlock || blockTags.indexOf(tagName) !== -1)) || (is(token, "doctype") && value === ">")) {
  240. if (inBlock && nextToken && nextToken.value === "</")
  241. rowsToAdd = -1;
  242. else
  243. rowsToAdd = 1;
  244. }
  245. if (is(token, "tag-open") && value === "</") {
  246. depth--;
  247. } else if (is(token, "tag-open") && value === "<" && singletonTags.indexOf(nextToken.value) === -1) {
  248. depth++;
  249. } else if (is(token, "tag-name")) {
  250. tagName = value;
  251. } else if (is(token, "tag-close") && value === "/>" && singletonTags.indexOf(tagName) === -1){
  252. depth--;
  253. }
  254. row = curRow;
  255. }
  256. }
  257. token = nextToken;
  258. }
  259. code = code.trim();
  260. session.doc.setValue(code);
  261. };
  262. exports.commands = [{
  263. name: "beautify",
  264. exec: function(editor) {
  265. exports.beautify(editor.session);
  266. },
  267. bindKey: "Ctrl-Shift-B"
  268. }];
  269. });
  270. (function() {
  271. ace.require(["ace/ext/beautify"], function(m) {
  272. if (typeof module == "object" && typeof exports == "object" && module) {
  273. module.exports = m;
  274. }
  275. });
  276. })();