CodeMirrorFormatters.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. // In the inspector token types have been modified to include extra mode information
  26. // after the actual token type. So we can't do token === "foo". So instead we do
  27. // /\bfoo\b/.test(token).
  28. CodeMirror.extendMode("javascript", {
  29. shouldHaveSpaceBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  30. {
  31. if (!token) {
  32. if (content === "(") // Most keywords like "if (" but not "function(" or "typeof(".
  33. return lastToken && /\bkeyword\b/.test(lastToken) && (lastContent !== "function" && lastContent !== "typeof" && lastContent !== "instanceof");
  34. if (content === ":") // Ternary.
  35. return (state.lexical.type === "stat" || state.lexical.type === ")");
  36. if (content === "!") // Unary ! should not be confused with "!=".
  37. return false;
  38. return "+-/*&&||!===+=-=>=<=?".indexOf(content) >= 0; // Operators.
  39. }
  40. if (isComment)
  41. return true;
  42. if (/\bkeyword\b/.test(token)) { // Most keywords require spaces before them, unless a '}' can come before it.
  43. if (content === "else" || content === "catch" || content === "finally")
  44. return lastContent === "}";
  45. return false;
  46. }
  47. return false;
  48. },
  49. shouldHaveSpaceAfterLastToken: function(lastToken, lastContent, token, state, content, isComment)
  50. {
  51. if (lastToken && /\bkeyword\b/.test(lastToken)) { // Most keywords require spaces after them, unless a '{' or ';' can come after it.
  52. if (lastContent === "else")
  53. return true;
  54. if (lastContent === "catch")
  55. return true;
  56. if (lastContent === "return")
  57. return content !== ";";
  58. if (lastContent === "throw")
  59. return true;
  60. if (lastContent === "try")
  61. return true;
  62. if (lastContent === "finally")
  63. return true;
  64. if (lastContent === "do")
  65. return true;
  66. return false;
  67. }
  68. if (lastToken && /\bcomment\b/.test(lastToken)) // Embedded /* comment */.
  69. return true;
  70. if (lastContent === ")") // "){".
  71. return content === "{";
  72. if (lastContent === ";") // In for loop.
  73. return state.lexical.type === ")";
  74. if (lastContent === "!") // Unary ! should not be confused with "!=".
  75. return false;
  76. return ",+-/*&&||:!===+=-=>=<=?".indexOf(lastContent) >= 0; // Operators.
  77. },
  78. newlinesAfterToken: function(lastToken, lastContent, token, state, content, isComment)
  79. {
  80. if (!token) {
  81. if (content === ",") // In object literals, like in {a:1,b:2}, but not in param lists or vardef lists.
  82. return state.lexical.type === "}" ? 1 : 0;
  83. if (content === ";") // Everywhere except in for loop conditions.
  84. return state.lexical.type !== ")" ? 1 : 0;
  85. if (content === ":" && state.lexical.type === "}" && state.lexical.prev && state.lexical.prev.type === "form") // Switch case/default.
  86. return 1;
  87. return content.length === 1 && "{}".indexOf(content) >= 0 ? 1 : 0; // After braces.
  88. }
  89. if (isComment)
  90. return 1;
  91. return 0;
  92. },
  93. removeLastNewline: function(lastToken, lastContent, token, state, content, isComment, firstTokenOnLine)
  94. {
  95. if (!token) {
  96. if (content === "}") // "{}".
  97. return lastContent === "{";
  98. if (content === ";") // "x = {};" or ";;".
  99. return "};".indexOf(lastContent) >= 0;
  100. if (content === ":") // Ternary.
  101. return lastContent === "}" && (state.lexical.type === "stat" || state.lexical.type === ")");
  102. if (",().".indexOf(content) >= 0) // "})", "}.bind", "function() { ... }()", or "}, false)".
  103. return lastContent === "}";
  104. return false;
  105. }
  106. if (isComment) { // Comment after semicolon.
  107. if (!firstTokenOnLine && lastContent === ";")
  108. return true;
  109. return false;
  110. }
  111. if (/\bkeyword\b/.test(token)) {
  112. if (content === "else" || content === "catch" || content === "finally") // "} else", "} catch", "} finally"
  113. return lastContent === "}";
  114. return false;
  115. }
  116. return false;
  117. },
  118. indentAfterToken: function(lastToken, lastContent, token, state, content, isComment)
  119. {
  120. return content === "{" || content === "case" || content === "default";
  121. },
  122. newlineBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  123. {
  124. if (state._jsPrettyPrint.shouldIndent)
  125. return true;
  126. return content === "}" && lastContent !== "{"; // "{}"
  127. },
  128. indentBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  129. {
  130. if (state._jsPrettyPrint.shouldIndent)
  131. return true;
  132. return false;
  133. },
  134. dedentsBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  135. {
  136. var dedent = 0;
  137. if (state._jsPrettyPrint.shouldDedent)
  138. dedent += state._jsPrettyPrint.dedentSize;
  139. if (!token && content === "}")
  140. dedent += 1;
  141. else if (token && /\bkeyword\b/.test(token) && (content === "case" || content === "default"))
  142. dedent += 1;
  143. return dedent;
  144. },
  145. modifyStateForTokenPre: function(lastToken, lastContent, token, state, content, isComment)
  146. {
  147. if (!state._jsPrettyPrint) {
  148. state._jsPrettyPrint = {
  149. indentCount: 0, // How far have we indented because of single statement blocks.
  150. shouldIndent: false, // Signal we should indent on entering a single statement block.
  151. shouldDedent: false, // Signal we should dedent on leaving a single statement block.
  152. dedentSize: 0, // How far we should dedent when leaving a single statement block.
  153. lastIfIndentCount: 0, // Keep track of the indent the last time we saw an if without braces.
  154. openBraceStartMarkers: [], // Keep track of non-single statement blocks.
  155. openBraceTrackingCount: -1, // Keep track of "{" and "}" in non-single statement blocks.
  156. };
  157. }
  158. // - Entering:
  159. // - Preconditions:
  160. // - last lexical was a "form" we haven't encountered before
  161. // - last content was ")", "else", or "do"
  162. // - current lexical is not ")" (in an expression or condition)
  163. // - Cases:
  164. // 1. "{"
  165. // - indent +0
  166. // - save this indent size so when we encounter the "}" we know how far to dedent
  167. // 2. "else if"
  168. // - indent +0 and do not signal to add a newline and indent
  169. // - mark the last if location so when we encounter an "else" we know how far to dedent
  170. // - mark the lexical state so we know we are inside a single statement block
  171. // 3. Token without brace.
  172. // - indent +1 and signal to add a newline and indent
  173. // - mark the last if location so when we encounter an "else" we know how far to dedent
  174. // - mark the lexical state so we know we are inside a single statement block
  175. if (!isComment && state.lexical.prev && state.lexical.prev.type === "form" && !state.lexical.prev._jsPrettyPrintMarker && (lastContent === ")" || lastContent === "else" || lastContent === "do") && (state.lexical.type !== ")")) {
  176. if (content === "{") {
  177. // Save the state at the opening brace so we can return to it when we see "}".
  178. var savedState = {indentCount:state._jsPrettyPrint.indentCount, openBraceTrackingCount:state._jsPrettyPrint.openBraceTrackingCount};
  179. state._jsPrettyPrint.openBraceStartMarkers.push(savedState);
  180. state._jsPrettyPrint.openBraceTrackingCount = 1;
  181. } else if (state.lexical.type !== "}") {
  182. // Increase the indent count. Signal for a newline and indent if needed.
  183. if (!(lastContent === "else" && content === "if")) {
  184. state._jsPrettyPrint.indentCount++;
  185. state._jsPrettyPrint.shouldIndent = true;
  186. }
  187. state.lexical.prev._jsPrettyPrintMarker = true;
  188. if (state._jsPrettyPrint.enteringIf)
  189. state._jsPrettyPrint.lastIfIndentCount = state._jsPrettyPrint.indentCount - 1;
  190. }
  191. }
  192. // - Leaving:
  193. // - Preconditions:
  194. // - we must be indented
  195. // - ignore ";", wait for the next token instead.
  196. // - Cases:
  197. // 1. "else"
  198. // - dedent to the last "if"
  199. // 2. "}" and all braces we saw are balanced
  200. // - dedent to the last "{"
  201. // 3. Token without a marker on the stack
  202. // - dedent all the way
  203. else if (state._jsPrettyPrint.indentCount) {
  204. console.assert(!state._jsPrettyPrint.shouldDedent);
  205. console.assert(!state._jsPrettyPrint.dedentSize);
  206. // Track "{" and "}" to know when the "}" is really closing a block.
  207. if (!isComment) {
  208. if (content === "{")
  209. state._jsPrettyPrint.openBraceTrackingCount++;
  210. else if (content === "}")
  211. state._jsPrettyPrint.openBraceTrackingCount--;
  212. }
  213. if (content === ";") {
  214. // Ignore.
  215. } else if (content === "else") {
  216. // Dedent to the last "if".
  217. if (lastContent !== "}") {
  218. state._jsPrettyPrint.shouldDedent = true;
  219. state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount - state._jsPrettyPrint.lastIfIndentCount;
  220. state._jsPrettyPrint.lastIfIndentCount = 0;
  221. }
  222. } else if (content === "}" && !state._jsPrettyPrint.openBraceTrackingCount && state._jsPrettyPrint.openBraceStartMarkers.length) {
  223. // Dedent to the last "{".
  224. var savedState = state._jsPrettyPrint.openBraceStartMarkers.pop();
  225. state._jsPrettyPrint.shouldDedent = true;
  226. state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount - savedState.indentCount;
  227. state._jsPrettyPrint.openBraceTrackingCount = savedState.openBraceTrackingCount;
  228. } else {
  229. // Dedent all the way.
  230. var shouldDedent = true;
  231. var lexical = state.lexical.prev;
  232. while (lexical) {
  233. if (lexical._jsPrettyPrintMarker) {
  234. shouldDedent = false;
  235. break;
  236. }
  237. lexical = lexical.prev;
  238. }
  239. if (shouldDedent) {
  240. state._jsPrettyPrint.shouldDedent = true;
  241. state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount;
  242. }
  243. }
  244. }
  245. // Signal for when we will be entering an if.
  246. if (token && state.lexical.type === "form" && state.lexical.prev && state.lexical.prev !== "form" && /\bkeyword\b/.test(token))
  247. state._jsPrettyPrint.enteringIf = (content === "if");
  248. },
  249. modifyStateForTokenPost: function(lastToken, lastContent, token, state, content, isComment)
  250. {
  251. if (state._jsPrettyPrint.shouldIndent)
  252. state._jsPrettyPrint.shouldIndent = false;
  253. if (state._jsPrettyPrint.shouldDedent) {
  254. state._jsPrettyPrint.indentCount -= state._jsPrettyPrint.dedentSize;
  255. state._jsPrettyPrint.dedentSize = 0;
  256. state._jsPrettyPrint.shouldDedent = false;
  257. }
  258. }
  259. });
  260. CodeMirror.extendMode("css-base", {
  261. shouldHaveSpaceBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  262. {
  263. if (!token) {
  264. if (content === "{")
  265. return true;
  266. return false;
  267. }
  268. if (isComment)
  269. return true;
  270. if (/\bkeyword\b/.test(token)) {
  271. if (content.charAt(0) === "!") // "!important".
  272. return true;
  273. return false;
  274. }
  275. return false;
  276. },
  277. shouldHaveSpaceAfterLastToken: function(lastToken, lastContent, token, state, content, isComment)
  278. {
  279. if (!lastToken) {
  280. if (lastContent === ",")
  281. return true;
  282. return false;
  283. }
  284. if (/\boperator\b/.test(lastToken)) {
  285. if (lastContent === ":") // Space in "prop: value" but not in a selectors "a:link" or "div::after" or media queries "(max-device-width:480px)".
  286. return state.stack.lastValue === "propertyValue";
  287. return false;
  288. }
  289. if (/\bcomment\b/.test(lastToken))
  290. return true;
  291. return false;
  292. },
  293. newlinesAfterToken: function(lastToken, lastContent, token, state, content, isComment)
  294. {
  295. if (!token) {
  296. if (content === ";")
  297. return 1;
  298. if (content === ",") { // "a,b,c,...,z{}" rule list at top level or in @media top level and only if the line length will be large.
  299. if ((!state.stack.length || state.stack.lastValue === "@media{") && state._cssPrettyPrint.lineLength > 60) {
  300. state._cssPrettyPrint.lineLength = 0;
  301. return 1;
  302. }
  303. return 0;
  304. }
  305. if (content === "{")
  306. return 1;
  307. if (content === "}") // 2 newlines between rule declarations.
  308. return 2;
  309. return 0;
  310. }
  311. if (isComment)
  312. return 1;
  313. return 0;
  314. },
  315. removeLastNewline: function(lastToken, lastContent, token, state, content, isComment, firstTokenOnLine)
  316. {
  317. if (isComment) { // Comment after semicolon.
  318. if (!firstTokenOnLine && lastContent === ";")
  319. return true;
  320. return false;
  321. }
  322. return content === "}" && (lastContent === "{" || lastContent === "}"); // "{}" and "}\n}" when closing @media.
  323. },
  324. indentAfterToken: function(lastToken, lastContent, token, state, content, isComment)
  325. {
  326. return content === "{";
  327. },
  328. newlineBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  329. {
  330. return content === "}" && (lastContent !== "{" && lastContent !== "}"); // "{}" and "}\n}" when closing @media.
  331. },
  332. indentBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  333. {
  334. return false;
  335. },
  336. dedentsBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
  337. {
  338. return content === "}" ? 1 : 0;
  339. },
  340. modifyStateForTokenPost: function(lastToken, lastContent, token, state, content, isComment)
  341. {
  342. if (!state._cssPrettyPrint)
  343. state._cssPrettyPrint = {lineLength: 0};
  344. // In order insert newlines in selector lists we need keep track of the length of the current line.
  345. // This isn't exact line length, only the builder knows that, but it is good enough to get an idea.
  346. // If we are at a top level, keep track of the current line length, otherwise we reset to 0.
  347. if (!state.stack.length || state.stack.lastValue === "@media{")
  348. state._cssPrettyPrint.lineLength += content.length;
  349. else
  350. state._cssPrettyPrint.lineLength = 0;
  351. }
  352. });