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 === ")");
38 if (content === "!") // Unary ! should not be confused with "!=".
40 return "+-/*&&||!===+=-=>=<=?".indexOf(content) >= 0; // Operators.
46 if (/\bkeyword\b/.test(token)) { // Most keywords require spaces before them, unless a '}' can come before it.
47 if (content === "else" || content === "catch" || content === "finally")
48 return lastContent === "}";
55 shouldHaveSpaceAfterLastToken: function(lastToken, lastContent, token, state, content, isComment)
57 if (lastToken && /\bkeyword\b/.test(lastToken)) { // Most keywords require spaces after them, unless a '{' or ';' can come after it.
58 if (lastContent === "else")
60 if (lastContent === "catch")
62 if (lastContent === "return")
63 return content !== ";";
64 if (lastContent === "throw")
66 if (lastContent === "try")
68 if (lastContent === "finally")
70 if (lastContent === "do")
75 if (lastToken && /\bcomment\b/.test(lastToken)) // Embedded /* comment */.
77 if (lastContent === ")") // "){".
78 return content === "{";
79 if (lastContent === ";") // In for loop.
80 return state.lexical.type === ")";
81 if (lastContent === "!") // Unary ! should not be confused with "!=".
84 return ",+-/*&&||:!===+=-=>=<=?".indexOf(lastContent) >= 0; // Operators.
87 newlinesAfterToken: function(lastToken, lastContent, token, state, content, isComment)
90 if (content === ",") // In object literals, like in {a:1,b:2}, but not in param lists or vardef lists.
91 return state.lexical.type === "}" ? 1 : 0;
92 if (content === ";") // Everywhere except in for loop conditions.
93 return state.lexical.type !== ")" ? 1 : 0;
94 if (content === ":" && state.lexical.type === "}" && state.lexical.prev && state.lexical.prev.type === "form") // Switch case/default.
96 return content.length === 1 && "{}".indexOf(content) >= 0 ? 1 : 0; // After braces.
105 removeLastNewline: function(lastToken, lastContent, token, state, content, isComment, firstTokenOnLine)
108 if (content === "}") // "{}".
109 return lastContent === "{";
110 if (content === ";") // "x = {};" or ";;".
111 return "};".indexOf(lastContent) >= 0;
112 if (content === ":") // Ternary.
113 return lastContent === "}" && (state.lexical.type === "stat" || state.lexical.type === ")");
114 if (",().".indexOf(content) >= 0) // "})", "}.bind", "function() { ... }()", or "}, false)".
115 return lastContent === "}";
119 if (isComment) { // Comment after semicolon.
120 if (!firstTokenOnLine && lastContent === ";")
125 if (/\bkeyword\b/.test(token)) {
126 if (content === "else" || content === "catch" || content === "finally") // "} else", "} catch", "} finally"
127 return lastContent === "}";
134 indentAfterToken: function(lastToken, lastContent, token, state, content, isComment)
136 return content === "{" || content === "case" || content === "default";
139 newlineBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
141 if (state._jsPrettyPrint.shouldIndent)
144 return content === "}" && lastContent !== "{"; // "{}"
147 indentBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
149 if (state._jsPrettyPrint.shouldIndent)
155 dedentsBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
159 if (state._jsPrettyPrint.shouldDedent)
160 dedent += state._jsPrettyPrint.dedentSize;
162 if (!token && content === "}")
164 else if (token && /\bkeyword\b/.test(token) && (content === "case" || content === "default"))
170 modifyStateForTokenPre: function(lastToken, lastContent, token, state, content, isComment)
172 if (!state._jsPrettyPrint) {
173 state._jsPrettyPrint = {
174 indentCount: 0, // How far have we indented because of single statement blocks.
175 shouldIndent: false, // Signal we should indent on entering a single statement block.
176 shouldDedent: false, // Signal we should dedent on leaving a single statement block.
177 dedentSize: 0, // How far we should dedent when leaving a single statement block.
178 lastIfIndentCount: 0, // Keep track of the indent the last time we saw an if without braces.
179 openBraceStartMarkers: [], // Keep track of non-single statement blocks.
180 openBraceTrackingCount: -1, // Keep track of "{" and "}" in non-single statement blocks.
186 // - last lexical was a "form" we haven't encountered before
187 // - last content was ")", "else", or "do"
188 // - current lexical is not ")" (in an expression or condition)
192 // - save this indent size so when we encounter the "}" we know how far to dedent
194 // - indent +0 and do not signal to add a newline and indent
195 // - mark the last if location so when we encounter an "else" we know how far to dedent
196 // - mark the lexical state so we know we are inside a single statement block
197 // 3. Token without brace.
198 // - indent +1 and 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 if (!isComment && state.lexical.prev && state.lexical.prev.type === "form" && !state.lexical.prev._jsPrettyPrintMarker && (lastContent === ")" || lastContent === "else" || lastContent === "do") && (state.lexical.type !== ")")) {
202 if (content === "{") {
203 // Save the state at the opening brace so we can return to it when we see "}".
204 var savedState = {indentCount:state._jsPrettyPrint.indentCount, openBraceTrackingCount:state._jsPrettyPrint.openBraceTrackingCount};
205 state._jsPrettyPrint.openBraceStartMarkers.push(savedState);
206 state._jsPrettyPrint.openBraceTrackingCount = 1;
207 } else if (state.lexical.type !== "}") {
208 // Increase the indent count. Signal for a newline and indent if needed.
209 if (!(lastContent === "else" && content === "if")) {
210 state._jsPrettyPrint.indentCount++;
211 state._jsPrettyPrint.shouldIndent = true;
213 state.lexical.prev._jsPrettyPrintMarker = true;
214 if (state._jsPrettyPrint.enteringIf)
215 state._jsPrettyPrint.lastIfIndentCount = state._jsPrettyPrint.indentCount - 1;
221 // - we must be indented
222 // - ignore ";", wait for the next token instead.
225 // - dedent to the last "if"
226 // 2. "}" and all braces we saw are balanced
227 // - dedent to the last "{"
228 // 3. Token without a marker on the stack
229 // - dedent all the way
230 else if (state._jsPrettyPrint.indentCount) {
231 console.assert(!state._jsPrettyPrint.shouldDedent);
232 console.assert(!state._jsPrettyPrint.dedentSize);
234 // Track "{" and "}" to know when the "}" is really closing a block.
237 state._jsPrettyPrint.openBraceTrackingCount++;
238 else if (content === "}")
239 state._jsPrettyPrint.openBraceTrackingCount--;
242 if (content === ";") {
244 } else if (content === "else") {
245 // Dedent to the last "if".
246 if (lastContent !== "}") {
247 state._jsPrettyPrint.shouldDedent = true;
248 state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount - state._jsPrettyPrint.lastIfIndentCount;
249 state._jsPrettyPrint.lastIfIndentCount = 0;
251 } else if (content === "}" && !state._jsPrettyPrint.openBraceTrackingCount && state._jsPrettyPrint.openBraceStartMarkers.length) {
252 // Dedent to the last "{".
253 var savedState = state._jsPrettyPrint.openBraceStartMarkers.pop();
254 state._jsPrettyPrint.shouldDedent = true;
255 state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount - savedState.indentCount;
256 state._jsPrettyPrint.openBraceTrackingCount = savedState.openBraceTrackingCount;
258 // Dedent all the way.
259 var shouldDedent = true;
260 var lexical = state.lexical.prev;
262 if (lexical._jsPrettyPrintMarker) {
263 shouldDedent = false;
266 lexical = lexical.prev;
269 state._jsPrettyPrint.shouldDedent = true;
270 state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount;
275 // Signal for when we will be entering an if.
276 if (token && state.lexical.type === "form" && state.lexical.prev && state.lexical.prev !== "form" && /\bkeyword\b/.test(token))
277 state._jsPrettyPrint.enteringIf = (content === "if");
280 modifyStateForTokenPost: function(lastToken, lastContent, token, state, content, isComment)
282 if (state._jsPrettyPrint.shouldIndent)
283 state._jsPrettyPrint.shouldIndent = false;
285 if (state._jsPrettyPrint.shouldDedent) {
286 state._jsPrettyPrint.indentCount -= state._jsPrettyPrint.dedentSize;
287 state._jsPrettyPrint.dedentSize = 0;
288 state._jsPrettyPrint.shouldDedent = false;
293 CodeMirror.extendMode("css", {
294 shouldHaveSpaceBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
305 if (/\bkeyword\b/.test(token)) {
306 if (content.charAt(0) === "!") // "!important".
314 shouldHaveSpaceAfterLastToken: function(lastToken, lastContent, token, state, content, isComment)
317 if (lastContent === ",")
322 if (/\boperator\b/.test(lastToken)) {
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.stack.lastValue === "propertyValue";
328 if (/\bcomment\b/.test(lastToken))
334 newlinesAfterToken: function(lastToken, lastContent, token, state, content, isComment)
339 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.
340 if ((!state.stack || !state.stack.length || state.stack.lastValue === "@media{") && state._cssPrettyPrint.lineLength > 60) {
341 state._cssPrettyPrint.lineLength = 0;
348 if (content === "}") // 2 newlines between rule declarations.
359 removeLastNewline: function(lastToken, lastContent, token, state, content, isComment, firstTokenOnLine)
361 if (isComment) { // Comment after semicolon.
362 if (!firstTokenOnLine && lastContent === ";")
367 return content === "}" && (lastContent === "{" || lastContent === "}"); // "{}" and "}\n}" when closing @media.
370 indentAfterToken: function(lastToken, lastContent, token, state, content, isComment)
372 return content === "{";
375 newlineBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
377 return content === "}" && (lastContent !== "{" && lastContent !== "}"); // "{}" and "}\n}" when closing @media.
380 indentBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
385 dedentsBeforeToken: function(lastToken, lastContent, token, state, content, isComment)
387 return content === "}" ? 1 : 0;
390 modifyStateForTokenPost: function(lastToken, lastContent, token, state, content, isComment)
392 if (!state._cssPrettyPrint)
393 state._cssPrettyPrint = {lineLength: 0};
395 // In order insert newlines in selector lists we need keep track of the length of the current line.
396 // This isn't exact line length, only the builder knows that, but it is good enough to get an idea.
397 // If we are at a top level, keep track of the current line length, otherwise we reset to 0.
398 if (!state.stack || !state.stack.length || state.stack.lastValue === "@media{")
399 state._cssPrettyPrint.lineLength += content.length;
401 state._cssPrettyPrint.lineLength = 0;