ext-beautify.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. ace.define("ace/ext/beautify/php_rules",["require","exports","module","ace/token_iterator"], function(require, exports, module) {
  2. "use strict";
  3. var TokenIterator = require("ace/token_iterator").TokenIterator;
  4. exports.newLines = [{
  5. type: 'support.php_tag',
  6. value: '<?php'
  7. }, {
  8. type: 'support.php_tag',
  9. value: '<?'
  10. }, {
  11. type: 'support.php_tag',
  12. value: '?>'
  13. }, {
  14. type: 'paren.lparen',
  15. value: '{',
  16. indent: true
  17. }, {
  18. type: 'paren.rparen',
  19. breakBefore: true,
  20. value: '}',
  21. indent: false
  22. }, {
  23. type: 'paren.rparen',
  24. breakBefore: true,
  25. value: '})',
  26. indent: false,
  27. dontBreak: true
  28. }, {
  29. type: 'comment'
  30. }, {
  31. type: 'text',
  32. value: ';'
  33. }, {
  34. type: 'text',
  35. value: ':',
  36. context: 'php'
  37. }, {
  38. type: 'keyword',
  39. value: 'case',
  40. indent: true,
  41. dontBreak: true
  42. }, {
  43. type: 'keyword',
  44. value: 'default',
  45. indent: true,
  46. dontBreak: true
  47. }, {
  48. type: 'keyword',
  49. value: 'break',
  50. indent: false,
  51. dontBreak: true
  52. }, {
  53. type: 'punctuation.doctype.end',
  54. value: '>'
  55. }, {
  56. type: 'meta.tag.punctuation.end',
  57. value: '>'
  58. }, {
  59. type: 'meta.tag.punctuation.begin',
  60. value: '<',
  61. blockTag: true,
  62. indent: true,
  63. dontBreak: true
  64. }, {
  65. type: 'meta.tag.punctuation.begin',
  66. value: '</',
  67. indent: false,
  68. breakBefore: true,
  69. dontBreak: true
  70. }, {
  71. type: 'punctuation.operator',
  72. value: ';'
  73. }];
  74. exports.spaces = [{
  75. type: 'xml-pe',
  76. prepend: true
  77. },{
  78. type: 'entity.other.attribute-name',
  79. prepend: true
  80. }, {
  81. type: 'storage.type',
  82. value: 'var',
  83. append: true
  84. }, {
  85. type: 'storage.type',
  86. value: 'function',
  87. append: true
  88. }, {
  89. type: 'keyword.operator',
  90. value: '='
  91. }, {
  92. type: 'keyword',
  93. value: 'as',
  94. prepend: true,
  95. append: true
  96. }, {
  97. type: 'keyword',
  98. value: 'function',
  99. append: true
  100. }, {
  101. type: 'support.function',
  102. next: /[^\(]/,
  103. append: true
  104. }, {
  105. type: 'keyword',
  106. value: 'or',
  107. append: true,
  108. prepend: true
  109. }, {
  110. type: 'keyword',
  111. value: 'and',
  112. append: true,
  113. prepend: true
  114. }, {
  115. type: 'keyword',
  116. value: 'case',
  117. append: true
  118. }, {
  119. type: 'keyword.operator',
  120. value: '||',
  121. append: true,
  122. prepend: true
  123. }, {
  124. type: 'keyword.operator',
  125. value: '&&',
  126. append: true,
  127. prepend: true
  128. }];
  129. exports.singleTags = ['!doctype','area','base','br','hr','input','img','link','meta'];
  130. exports.transform = function(iterator, maxPos, context) {
  131. var token = iterator.getCurrentToken();
  132. var newLines = exports.newLines;
  133. var spaces = exports.spaces;
  134. var singleTags = exports.singleTags;
  135. var code = '';
  136. var indentation = 0;
  137. var dontBreak = false;
  138. var tag;
  139. var lastTag;
  140. var lastToken = {};
  141. var nextTag;
  142. var nextToken = {};
  143. var breakAdded = false;
  144. var value = '';
  145. while (token!==null) {
  146. console.log(token);
  147. if( !token ){
  148. token = iterator.stepForward();
  149. continue;
  150. }
  151. if( token.type == 'support.php_tag' && token.value != '?>' ){
  152. context = 'php';
  153. }
  154. else if( token.type == 'support.php_tag' && token.value == '?>' ){
  155. context = 'html';
  156. }
  157. else if( token.type == 'meta.tag.name.style' && context != 'css' ){
  158. context = 'css';
  159. }
  160. else if( token.type == 'meta.tag.name.style' && context == 'css' ){
  161. context = 'html';
  162. }
  163. else if( token.type == 'meta.tag.name.script' && context != 'js' ){
  164. context = 'js';
  165. }
  166. else if( token.type == 'meta.tag.name.script' && context == 'js' ){
  167. context = 'html';
  168. }
  169. nextToken = iterator.stepForward();
  170. if (nextToken && nextToken.type.indexOf('meta.tag.name') == 0) {
  171. nextTag = nextToken.value;
  172. }
  173. if ( lastToken.type == 'support.php_tag' && lastToken.value == '<?=') {
  174. dontBreak = true;
  175. }
  176. if (token.type == 'meta.tag.name') {
  177. token.value = token.value.toLowerCase();
  178. }
  179. if (token.type == 'text') {
  180. token.value = token.value.trim();
  181. }
  182. if (!token.value) {
  183. token = nextToken;
  184. continue;
  185. }
  186. value = token.value;
  187. for (var i in spaces) {
  188. if (
  189. token.type == spaces[i].type &&
  190. (!spaces[i].value || token.value == spaces[i].value) &&
  191. (
  192. nextToken &&
  193. (!spaces[i].next || spaces[i].next.test(nextToken.value))
  194. )
  195. ) {
  196. if (spaces[i].prepend) {
  197. value = ' ' + token.value;
  198. }
  199. if (spaces[i].append) {
  200. value += ' ';
  201. }
  202. }
  203. }
  204. if (token.type.indexOf('meta.tag.name') == 0) {
  205. tag = token.value;
  206. }
  207. breakAdded = false;
  208. for (i in newLines) {
  209. if (
  210. token.type == newLines[i].type &&
  211. (
  212. !newLines[i].value ||
  213. token.value == newLines[i].value
  214. ) &&
  215. (
  216. !newLines[i].blockTag ||
  217. singleTags.indexOf(nextTag) === -1
  218. ) &&
  219. (
  220. !newLines[i].context ||
  221. newLines[i].context === context
  222. )
  223. ) {
  224. if (newLines[i].indent === false) {
  225. indentation--;
  226. }
  227. if (
  228. newLines[i].breakBefore &&
  229. ( !newLines[i].prev || newLines[i].prev.test(lastToken.value) )
  230. ) {
  231. code += "\n";
  232. breakAdded = true;
  233. for (i = 0; i < indentation; i++) {
  234. code += "\t";
  235. }
  236. }
  237. break;
  238. }
  239. }
  240. if (dontBreak===false) {
  241. for (i in newLines) {
  242. if (
  243. lastToken.type == newLines[i].type &&
  244. (
  245. !newLines[i].value || lastToken.value == newLines[i].value
  246. ) &&
  247. (
  248. !newLines[i].blockTag ||
  249. singleTags.indexOf(tag) === -1
  250. ) &&
  251. (
  252. !newLines[i].context ||
  253. newLines[i].context === context
  254. )
  255. ) {
  256. if (newLines[i].indent === true) {
  257. indentation++;
  258. }
  259. if (!newLines[i].dontBreak && !breakAdded) {
  260. code += "\n";
  261. for (i = 0; i < indentation; i++) {
  262. code += "\t";
  263. }
  264. }
  265. break;
  266. }
  267. }
  268. }
  269. code += value;
  270. if ( lastToken.type == 'support.php_tag' && lastToken.value == '?>' ) {
  271. dontBreak = false;
  272. }
  273. lastTag = tag;
  274. lastToken = token;
  275. token = nextToken;
  276. if (token===null) {
  277. break;
  278. }
  279. }
  280. return code;
  281. };
  282. });
  283. ace.define("ace/ext/beautify",["require","exports","module","ace/token_iterator","ace/ext/beautify/php_rules"], function(require, exports, module) {
  284. "use strict";
  285. var TokenIterator = require("ace/token_iterator").TokenIterator;
  286. var phpTransform = require("./beautify/php_rules").transform;
  287. exports.beautify = function(session) {
  288. var iterator = new TokenIterator(session, 0, 0);
  289. var token = iterator.getCurrentToken();
  290. var context = session.$modeId.split("/").pop();
  291. var code = phpTransform(iterator, context);
  292. session.doc.setValue(code);
  293. };
  294. exports.commands = [{
  295. name: "beautify",
  296. exec: function(editor) {
  297. exports.beautify(editor.session);
  298. },
  299. bindKey: "Ctrl-Shift-B"
  300. }]
  301. });
  302. (function() {
  303. ace.require(["ace/ext/beautify"], function() {});
  304. })();