Skip to content

Commit

Permalink
DevTools: [Formatter] unify JavaScriptFormattedContentBuilder and CSS…
Browse files Browse the repository at this point in the history
…FormattedContentBuilder

There is no need for multiple formattedContentBuilders.

BUG=none
R=dgozman, pfeldman

Review URL: https://codereview.chromium.org/1782003009

Cr-Commit-Position: refs/heads/master@{#380868}
  • Loading branch information
aslushnikov authored and Commit bot committed Mar 12, 2016
1 parent 364df9a commit ac71c29
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 207 deletions.
175 changes: 9 additions & 166 deletions front_end/script_formatter_worker/CSSFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* @constructor
* @param {string} content
* @param {!FormatterWorker.CSSFormattedContentBuilder} builder
* @param {!FormatterWorker.FormattedContentBuilder} builder
*/
FormatterWorker.CSSFormatter = function(content, builder)
{
Expand All @@ -52,7 +52,6 @@ FormatterWorker.CSSFormatter.prototype = {
var line = lines[i];
tokenize(line, this._tokenCallback.bind(this, i));
}
this._builder.flushNewLines(true);
},

/**
Expand All @@ -71,7 +70,7 @@ FormatterWorker.CSSFormatter.prototype = {
var isWhitespace = /^\s+$/.test(token);
if (isWhitespace) {
if (!this._state.eatWhitespace)
this._builder.addSpace();
this._builder.addSoftSpace();
return;
}
this._state.eatWhitespace = false;
Expand All @@ -80,32 +79,33 @@ FormatterWorker.CSSFormatter.prototype = {

if (token !== "}") {
if (this._state.afterClosingBrace)
this._builder.addNewLine();
this._builder.addNewLine(true);
this._state.afterClosingBrace = false;
}
var startPosition = (startLine ? this._lineEndings[startLine - 1] : 0) + startColumn;
var endLine = startLine + token.lineCount() - 1;
if (token === "}") {
if (this._state.inPropertyValue)
this._builder.addNewLine();
this._builder.decreaseNestingLevel();
this._state.afterClosingBrace = true;
this._state.inPropertyValue = false;
} else if (token === ":" && !this._state.inPropertyValue && this._state.seenProperty) {
this._builder.addToken(token, startPosition, startLine, startColumn);
this._builder.addSpace();
this._builder.addToken(token, startPosition, startLine, endLine);
this._builder.addSoftSpace();
this._state.eatWhitespace = true;
this._state.inPropertyValue = true;
this._state.seenProperty = false;
return;
} else if (token === "{") {
this._builder.addSpace();
this._builder.addToken(token, startPosition, startLine, startColumn);
this._builder.addSoftSpace();
this._builder.addToken(token, startPosition, startLine, endLine);
this._builder.addNewLine();
this._builder.increaseNestingLevel();
return;
}

this._builder.addToken(token, startPosition, startLine, startColumn);
this._builder.addToken(token, startPosition, startLine, endLine);

if (type === "comment" && !this._state.inPropertyValue && !this._state.seenProperty)
this._builder.addNewLine();
Expand All @@ -117,160 +117,3 @@ FormatterWorker.CSSFormatter.prototype = {
}
}
}

/**
* @constructor
* @param {string} content
* @param {!{original: !Array.<number>, formatted: !Array.<number>}} mapping
* @param {number} originalOffset
* @param {number} formattedOffset
* @param {string} indentString
*/
FormatterWorker.CSSFormattedContentBuilder = function(content, mapping, originalOffset, formattedOffset, indentString)
{
this._originalContent = content;
this._originalOffset = originalOffset;
this._lastOriginalPosition = 0;

this._formattedContent = [];
this._formattedContentLength = 0;
this._formattedOffset = formattedOffset;
this._lastFormattedPosition = 0;

this._mapping = mapping;

this._lineNumber = 0;
this._nestingLevel = 0;
this._needNewLines = 0;
this._atLineStart = true;
this._indentString = indentString;
this._cachedIndents = {};
}

FormatterWorker.CSSFormattedContentBuilder.prototype = {
/**
* @param {string} token
* @param {number} startPosition
* @param {number} startLine
* @param {number} startColumn
*/
addToken: function(token, startPosition, startLine, startColumn)
{
if ((this._isWhitespaceRun || this._atLineStart) && /^\s+$/.test(token))
return;

if (this._isWhitespaceRun && this._lineNumber === startLine && !this._needNewLines)
this._addText(" ");

this._isWhitespaceRun = false;
this._atLineStart = false;

while (this._lineNumber < startLine) {
this._addText("\n");
this._addIndent();
this._needNewLines = 0;
this._lineNumber += 1;
this._atLineStart = true;
}

if (this._needNewLines) {
this.flushNewLines();
this._addIndent();
this._atLineStart = true;
}

this._addMappingIfNeeded(startPosition);
this._addText(token);
this._lineNumber = startLine;
},

addSpace: function()
{
if (this._isWhitespaceRun)
return;
this._isWhitespaceRun = true;
},

addNewLine: function()
{
++this._needNewLines;
},

/**
* @param {boolean=} atLeastOne
*/
flushNewLines: function(atLeastOne)
{
var newLineCount = atLeastOne && !this._needNewLines ? 1 : this._needNewLines;
if (newLineCount)
this._isWhitespaceRun = false;
for (var i = 0; i < newLineCount; ++i)
this._addText("\n");
this._needNewLines = 0;
},

increaseNestingLevel: function()
{
this._nestingLevel += 1;
},

/**
* @param {boolean=} addNewline
*/
decreaseNestingLevel: function(addNewline)
{
if (this._nestingLevel)
this._nestingLevel -= 1;
if (addNewline)
this.addNewLine();
},

/**
* @return {string}
*/
content: function()
{
return this._formattedContent.join("");
},

_addIndent: function()
{
if (this._cachedIndents[this._nestingLevel]) {
this._addText(this._cachedIndents[this._nestingLevel]);
return;
}

var fullIndent = "";
for (var i = 0; i < this._nestingLevel; ++i)
fullIndent += this._indentString;
this._addText(fullIndent);

// Cache a maximum of 20 nesting level indents.
if (this._nestingLevel <= 20)
this._cachedIndents[this._nestingLevel] = fullIndent;
},

/**
* @param {string} text
*/
_addText: function(text)
{
if (!text)
return;
this._formattedContent.push(text);
this._formattedContentLength += text.length;
},

/**
* @param {number} originalPosition
*/
_addMappingIfNeeded: function(originalPosition)
{
if (originalPosition - this._lastOriginalPosition === this._formattedContentLength - this._lastFormattedPosition)
return;
this._mapping.original.push(this._originalOffset + originalPosition);
this._lastOriginalPosition = originalPosition;
this._mapping.formatted.push(this._formattedOffset + this._formattedContentLength);
this._lastFormattedPosition = this._formattedContentLength;
}
}
Loading

0 comments on commit ac71c29

Please sign in to comment.