2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 // In the inspector token types have been modified to include extra mode information
27 // after the actual token type. So we can't do token === "foo". So instead we do
28 // /\bfoo\b/.test(token).
30 CodeMirror.extendMode("javascript", {
31 shouldHaveSpaceBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
34 if (content === "(") // Most keywords like "if (" but not "function(" or "typeof(".
35 return lastToken && /\bkeyword\b/.test(lastToken) && (lastContent !== "function" && lastContent !== "typeof" && lastContent !== "instanceof");
36 if (content === ":") // Ternary.
37 return (state.lexical.type === "stat" || state.lexical.type === ")");
44 if (/\boperator\b/.test(token)) {
45 if (content === "!") // Unary ! should not be confused with "!=".
47 return "+-/*&&||!===+=-=>=<=?".indexOf(content) >= 0; // Operators.
50 if (/\bkeyword\b/.test(token)) { // Most keywords require spaces before them, unless a '}' can come before it.
51 if (content === "else" || content === "catch" || content === "finally")
52 return lastContent === "}";
59 shouldHaveSpaceAfterLastToken: function(lastToken, lastContent, token, state, content, isComment)
61 if (lastToken && /\bkeyword\b/.test(lastToken)) { // Most keywords require spaces after them, unless a '{' or ';' can come after it.
62 if (lastContent === "else")
64 if (lastContent === "catch")
66 if (lastContent === "return")
67 return content !== ";";
68 if (lastContent === "throw")
70 if (lastContent === "try")
72 if (lastContent === "finally")
74 if (lastContent === "do")
79 if (lastToken && /\bcomment\b/.test(lastToken)) // Embedded /* comment */.
81 if (lastContent === ")") // "){".
82 return content === "{";
83 if (lastContent === ";") // In for loop.
84 return state.lexical.type === ")";
85 if (lastContent === "!") // Unary ! should not be confused with "!=".
88 return ",+-/*&&||:!===+=-=>=<=?".indexOf(lastContent) >= 0; // Operators.
91 newlinesAfterToken: function(lastToken, lastContent, token, state, content, isComment)
94 if (content === ",") // In object literals, like in {a:1,b:2}, but not in param lists or vardef lists.
95 return state.lexical.type === "}" ? 1 : 0;
96 if (content === ";") // Everywhere except in for loop conditions.
97 return state.lexical.type !== ")" ? 1 : 0;
98 if (content === ":" && state.lexical.type === "}" && state.lexical.prev && state.lexical.prev.type === "form") // Switch case/default.
100 return content.length === 1 && "{}".indexOf(content) >= 0 ? 1 : 0; // After braces.
109 removeLastNewline: function(lastToken, lastContent, token, state, content, isComment, firstTokenOnLine)
112 if (content === "}") // "{}".
113 return lastContent === "{";
114 if (content === ";") // "x = {};" or ";;".
115 return "};".indexOf(lastContent) >= 0;
116 if (content === ":") // Ternary.
117 return lastContent === "}" && (state.lexical.type === "stat" || state.lexical.type === ")");
118 if (",().".indexOf(content) >= 0) // "})", "}.bind", "function() { ... }()", or "}, false)".
119 return lastContent === "}";
123 if (isComment) { // Comment after semicolon.
124 if (!firstTokenOnLine && lastContent === ";")
129 if (/\bkeyword\b/.test(token)) {
130 if (content === "else" || content === "catch" || content === "finally") // "} else", "} catch", "} finally"
131 return lastContent === "}";
138 indentAfterToken: function(lastToken, lastContent, token, state, content, isComment)
140 return content === "{" || content === "case" || content === "default";
143 newlineBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
145 if (state._jsPrettyPrint.shouldIndent)
148 return content === "}" && lastContent !== "{"; // "{}"
151 indentBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
153 if (state._jsPrettyPrint.shouldIndent)
159 dedentsBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
163 if (state._jsPrettyPrint.shouldDedent)
164 dedent += state._jsPrettyPrint.dedentSize;
166 if (!token && content === "}")
168 else if (token && /\bkeyword\b/.test(token) && (content === "case" || content === "default"))
174 modifyStateForTokenPre: function(lastToken, lastContent, token, state, content, isComment)
176 if (!state._jsPrettyPrint) {
177 state._jsPrettyPrint = {
178 indentCount: 0, // How far have we indented because of single statement blocks.
179 shouldIndent: false, // Signal we should indent on entering a single statement block.
180 shouldDedent: false, // Signal we should dedent on leaving a single statement block.
181 dedentSize: 0, // How far we should dedent when leaving a single statement block.
182 lastIfIndentCount: 0, // Keep track of the indent the last time we saw an if without braces.
183 openBraceStartMarkers: [], // Keep track of non-single statement blocks.
184 openBraceTrackingCount: -1, // Keep track of "{" and "}" in non-single statement blocks.
190 // - last lexical was a "form" we haven't encountered before
191 // - last content was ")", "else", or "do"
192 // - current lexical is not ")" (in an expression or condition)
196 // - save this indent size so when we encounter the "}" we know how far to dedent
198 // - indent +0 and do not signal to add a newline and indent
199 // - mark the last if location so when we encounter an "else" we know how far to dedent
200 // - mark the lexical state so we know we are inside a single statement block
201 // 3. Token without brace.
202 // - indent +1 and signal to add a newline and indent
203 // - mark the last if location so when we encounter an "else" we know how far to dedent
204 // - mark the lexical state so we know we are inside a single statement block
205 if (!isComment && state.lexical.prev && state.lexical.prev.type === "form" && !state.lexical.prev._jsPrettyPrintMarker && (lastContent === ")" || lastContent === "else" || lastContent === "do") && (state.lexical.type !== ")")) {
206 if (content === "{") {
207 // Save the state at the opening brace so we can return to it when we see "}".
208 var savedState = {indentCount:state._jsPrettyPrint.indentCount, openBraceTrackingCount:state._jsPrettyPrint.openBraceTrackingCount};
209 state._jsPrettyPrint.openBraceStartMarkers.push(savedState);
210 state._jsPrettyPrint.openBraceTrackingCount = 1;
211 } else if (state.lexical.type !== "}") {
212 // Increase the indent count. Signal for a newline and indent if needed.
213 if (!(lastContent === "else" && content === "if")) {
214 state._jsPrettyPrint.indentCount++;
215 state._jsPrettyPrint.shouldIndent = true;
217 state.lexical.prev._jsPrettyPrintMarker = true;
218 if (state._jsPrettyPrint.enteringIf)
219 state._jsPrettyPrint.lastIfIndentCount = state._jsPrettyPrint.indentCount - 1;
225 // - we must be indented
226 // - ignore ";", wait for the next token instead.
229 // - dedent to the last "if"
230 // 2. "}" and all braces we saw are balanced
231 // - dedent to the last "{"
232 // 3. Token without a marker on the stack
233 // - dedent all the way
234 else if (state._jsPrettyPrint.indentCount) {
235 console.assert(!state._jsPrettyPrint.shouldDedent);
236 console.assert(!state._jsPrettyPrint.dedentSize);
238 // Track "{" and "}" to know when the "}" is really closing a block.
241 state._jsPrettyPrint.openBraceTrackingCount++;
242 else if (content === "}")
243 state._jsPrettyPrint.openBraceTrackingCount--;
246 if (content === ";") {
248 } else if (content === "else") {
249 // Dedent to the last "if".
250 if (lastContent !== "}") {
251 state._jsPrettyPrint.shouldDedent = true;
252 state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount - state._jsPrettyPrint.lastIfIndentCount;
253 state._jsPrettyPrint.lastIfIndentCount = 0;
255 } else if (content === "}" && !state._jsPrettyPrint.openBraceTrackingCount && state._jsPrettyPrint.openBraceStartMarkers.length) {
256 // Dedent to the last "{".
257 var savedState = state._jsPrettyPrint.openBraceStartMarkers.pop();
258 state._jsPrettyPrint.shouldDedent = true;
259 state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount - savedState.indentCount;
260 state._jsPrettyPrint.openBraceTrackingCount = savedState.openBraceTrackingCount;
262 // Dedent all the way.
263 var shouldDedent = true;
264 var lexical = state.lexical.prev;
266 if (lexical._jsPrettyPrintMarker) {
267 shouldDedent = false;
270 lexical = lexical.prev;
273 state._jsPrettyPrint.shouldDedent = true;
274 state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount;
279 // Signal for when we will be entering an if.
280 if (token && state.lexical.type === "form" && state.lexical.prev && state.lexical.prev !== "form" && /\bkeyword\b/.test(token))
281 state._jsPrettyPrint.enteringIf = (content === "if");
284 modifyStateForTokenPost: function(lastToken, lastContent, token, state, content, isComment)
286 if (state._jsPrettyPrint.shouldIndent)
287 state._jsPrettyPrint.shouldIndent = false;
289 if (state._jsPrettyPrint.shouldDedent) {
290 state._jsPrettyPrint.indentCount -= state._jsPrettyPrint.dedentSize;
291 state._jsPrettyPrint.dedentSize = 0;
292 state._jsPrettyPrint.shouldDedent = false;
297 CodeMirror.extendMode("css", {
298 shouldHaveSpaceBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
303 return ">+~-*/".indexOf(content) >= 0; // calc() expression or child/sibling selectors
309 if (/\bkeyword\b/.test(token)) {
310 if (content.charAt(0) === "!") // "!important".
318 shouldHaveSpaceAfterLastToken: function(lastToken, lastContent, token, state, content, isComment)
321 if (lastContent === ",")
323 if (lastContent === ":") // Space in "prop: value" but not in a selectors "a:link" or "div::after" or media queries "(max-device-width:480px)".
324 return state.state === "prop";
325 if (lastContent === ")" && (content !== ")" && content !== ",")) {
326 if (token === "number") // linear-gradient(rgb(...)0%,rgb(...)100%)
328 return state.state === "media" || state.state === "media_parens"; // Space in "not(foo)and" but not at the end of "not(not(foo))"
330 return ">+~-*/".indexOf(lastContent) >= 0; // calc() expression or child/sibling selectors
333 if (/\bcomment\b/.test(lastToken))
336 if (/\bkeyword\b/.test(lastToken)) // media-query keywords
337 return state.state === "media" || state.state === "media_parens";
342 newlinesAfterToken: function(lastToken, lastContent, token, state, content, isComment)
347 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.
348 if ((state.state === "top" || state.state === "media") && state._cssPrettyPrint.lineLength > 60) {
349 state._cssPrettyPrint.lineLength = 0;
356 if (content === "}") // 2 newlines between rule declarations.
367 removeLastNewline: function(lastToken, lastContent, token, state, content, isComment, firstTokenOnLine)
369 if (isComment) { // Comment after semicolon.
370 if (!firstTokenOnLine && lastContent === ";")
375 return content === "}" && (lastContent === "{" || lastContent === "}"); // "{}" and "}\n}" when closing @media.
378 indentAfterToken: function(lastToken, lastContent, token, state, content, isComment)
380 return content === "{";
383 newlineBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
385 return content === "}" && (lastContent !== "{" && lastContent !== "}"); // "{}" and "}\n}" when closing @media.
388 indentBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
393 dedentsBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
395 return content === "}" ? 1 : 0;
398 modifyStateForTokenPost: function(lastToken, lastContent, token, state, content, isComment)
400 if (!state._cssPrettyPrint)
401 state._cssPrettyPrint = {lineLength: 0};
403 // In order insert newlines in selector lists we need keep track of the length of the current line.
404 // This isn't exact line length, only the builder knows that, but it is good enough to get an idea.
405 // If we are at a top level, keep track of the current line length, otherwise we reset to 0.
406 if (!isComment && (state.state === "top" || state.state === "media" || state.state === "pseudo"))
407 state._cssPrettyPrint.lineLength += content.length;
409 state._cssPrettyPrint.lineLength = 0;