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 function Formatter(codeMirror, builder)
28 console.assert(codeMirror);
29 console.assert(builder);
31 this._codeMirror = codeMirror;
32 this._builder = builder;
34 this._lastToken = null;
35 this._lastContent = "";
38 Formatter.prototype = {
39 constructor: Formatter,
43 format: function(from, to)
45 console.assert(this._builder.originalContent === null);
46 if (this._builder.originalContent !== null)
49 var outerMode = this._codeMirror.getMode();
50 var content = this._codeMirror.getRange(from, to);
51 var state = CodeMirror.copyState(outerMode, this._codeMirror.getTokenAt(from).state);
52 this._builder.setOriginalContent(content);
55 var lines = content.split("\n");
56 for (var i = 0; i < lines.length; ++i) {
58 var startOfNewLine = true;
59 var firstTokenOnLine = true;
60 var stream = new CodeMirror.StringStream(line);
61 while (!stream.eol()) {
62 var innerMode = CodeMirror.innerMode(outerMode, state);
63 var token = outerMode.token(stream, state);
64 var isWhiteSpace = token === null && /^\s*$/.test(stream.current());
65 this._handleToken(innerMode.mode, token, state, stream, lineOffset + stream.start, isWhiteSpace, startOfNewLine, firstTokenOnLine);
66 stream.start = stream.pos;
67 startOfNewLine = false;
68 if (firstTokenOnLine && !isWhiteSpace)
69 firstTokenOnLine = false;
73 this._handleEmptyLine();
75 lineOffset += line.length + 1; // +1 for the "\n" removed in split.
76 this._handleLineEnding(lineOffset - 1); // -1 for the index of the "\n".
79 this._builder.finish();
84 _handleToken: function(mode, token, state, stream, originalPosition, isWhiteSpace, startOfNewLine, firstTokenOnLine)
86 // String content of the token.
87 var content = stream.current();
89 // Start of a new line. Insert a newline to be safe if code was not-ASI safe. These are collapsed.
91 this._builder.appendNewline();
93 // Whitespace. Collapse to a single space.
95 this._builder.appendSpace();
99 // Avoid some hooks for content in comments.
100 var isComment = token && /\bcomment\b/.test(token);
102 if (mode.modifyStateForTokenPre)
103 mode.modifyStateForTokenPre(this._lastToken, this._lastContent, token, state, content, isComment);
105 // Should we remove the last newline?
106 if (this._builder.lastTokenWasNewline && mode.removeLastNewline(this._lastToken, this._lastContent, token, state, content, isComment, firstTokenOnLine))
107 this._builder.removeLastNewline();
109 // Add whitespace after the last token?
110 if (!this._builder.lastTokenWasWhitespace && mode.shouldHaveSpaceAfterLastToken(this._lastToken, this._lastContent, token, state, content, isComment))
111 this._builder.appendSpace();
113 // Add whitespace before this token?
114 if (!this._builder.lastTokenWasWhitespace && mode.shouldHaveSpaceBeforeToken(this._lastToken, this._lastContent, token, state, content, isComment))
115 this._builder.appendSpace();
117 // Should we dedent before this token?
118 var dedents = mode.dedentsBeforeToken(this._lastToken, this._lastContent, token, state, content, isComment);
119 while (dedents-- > 0)
120 this._builder.dedent();
122 // Should we add a newline before this token?
123 if (mode.newlineBeforeToken(this._lastToken, this._lastContent, token, state, content, isComment))
124 this._builder.appendNewline();
126 // Should we indent before this token?
127 if (mode.indentBeforeToken(this._lastToken, this._lastContent, token, state, content, isComment))
128 this._builder.indent();
131 this._builder.appendToken(content, originalPosition);
133 // Let the pretty printer update any state it keeps track of.
134 if (mode.modifyStateForTokenPost)
135 mode.modifyStateForTokenPost(this._lastToken, this._lastContent, token, state, content, isComment);
137 // Should we indent or dedent after this token?
138 if (!isComment && mode.indentAfterToken(this._lastToken, this._lastContent, token, state, content, isComment))
139 this._builder.indent();
141 // Should we add newlines after this token?
142 var newlines = mode.newlinesAfterToken(this._lastToken, this._lastContent, token, state, content, isComment);
144 this._builder.appendMultipleNewlines(newlines);
146 // Record this token as the last token.
147 this._lastToken = token;
148 this._lastContent = content;
151 _handleEmptyLine: function()
153 // Preserve original whitespace only lines by adding a newline.
154 // However, don't do this if the builder just added multiple newlines.
155 if (!(this._builder.lastTokenWasNewline && this._builder.lastNewlineAppendWasMultiple))
156 this._builder.appendNewline(true);
159 _handleLineEnding: function(originalNewLinePosition)
161 // Record the original line ending.
162 this._builder.addOriginalLineEnding(originalNewLinePosition);