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 26function Formatter(codeMirror, builder) 27{ 28 console.assert(codeMirror); 29 console.assert(builder); 30 31 this._codeMirror = codeMirror; 32 this._builder = builder; 33 34 this._lastToken = null; 35 this._lastContent = ""; 36}; 37 38Formatter.prototype = { 39 constructor: Formatter, 40 41 // Public 42 43 format: function(from, to) 44 { 45 console.assert(this._builder.originalContent === null); 46 if (this._builder.originalContent !== null) 47 return; 48 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); 53 54 var lineOffset = 0; 55 var lines = content.split("\n"); 56 for (var i = 0; i < lines.length; ++i) { 57 var line = lines[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; 70 } 71 72 if (firstTokenOnLine) 73 this._handleEmptyLine(); 74 75 lineOffset += line.length + 1; // +1 for the "\n" removed in split. 76 this._handleLineEnding(lineOffset - 1); // -1 for the index of the "\n". 77 } 78 79 this._builder.finish(); 80 }, 81 82 // Private 83 84 _handleToken: function(mode, token, state, stream, originalPosition, isWhiteSpace, startOfNewLine, firstTokenOnLine) 85 { 86 // String content of the token. 87 var content = stream.current(); 88 89 // Start of a new line. Insert a newline to be safe if code was not-ASI safe. These are collapsed. 90 if (startOfNewLine) 91 this._builder.appendNewline(); 92 93 // Whitespace. Collapse to a single space. 94 if (isWhiteSpace) { 95 this._builder.appendSpace(); 96 return; 97 } 98 99 // Avoid some hooks for content in comments. 100 var isComment = token && /\bcomment\b/.test(token); 101 102 if (mode.modifyStateForTokenPre) 103 mode.modifyStateForTokenPre(this._lastToken, this._lastContent, token, state, content, isComment); 104 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(); 108 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(); 112 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(); 116 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(); 121 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(); 125 126 // Should we indent before this token? 127 if (mode.indentBeforeToken(this._lastToken, this._lastContent, token, state, content, isComment)) 128 this._builder.indent(); 129 130 // Append token. 131 this._builder.appendToken(content, originalPosition); 132 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); 136 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(); 140 141 // Should we add newlines after this token? 142 var newlines = mode.newlinesAfterToken(this._lastToken, this._lastContent, token, state, content, isComment); 143 if (newlines) 144 this._builder.appendMultipleNewlines(newlines); 145 146 // Record this token as the last token. 147 this._lastToken = token; 148 this._lastContent = content; 149 }, 150 151 _handleEmptyLine: function() 152 { 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); 157 }, 158 159 _handleLineEnding: function(originalNewLinePosition) 160 { 161 // Record the original line ending. 162 this._builder.addOriginalLineEnding(originalNewLinePosition); 163 } 164}; 165