mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:33:53 -06:00
Move formating over to zero based math.
This commit is contained in:
parent
dd90c22406
commit
873e4e3e8a
@ -67,14 +67,14 @@ module ts.formatting {
|
||||
}
|
||||
|
||||
export function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[] {
|
||||
var line = sourceFile.getOneBasedLineAndCharacterOfPosition(position).line;
|
||||
if (line === 1) {
|
||||
var line = sourceFile.getZeroBasedLineAndCharacterOfPosition(position).line;
|
||||
if (line === 0) {
|
||||
return [];
|
||||
}
|
||||
// get the span for the previous\current line
|
||||
var span = {
|
||||
// get start position for the previous line
|
||||
pos: getStartPositionOfOneBasedLine(line - 1, sourceFile),
|
||||
pos: getStartPositionOfZeroBasedLine(line - 1, sourceFile),
|
||||
// get end position for the current line (end value is exclusive so add 1 to the result)
|
||||
end: getEndLinePosition(line, sourceFile) + 1
|
||||
}
|
||||
@ -283,7 +283,7 @@ module ts.formatting {
|
||||
var previousLine = Constants.Unknown;
|
||||
var childKind = SyntaxKind.Unknown;
|
||||
while (n) {
|
||||
var line = sourceFile.getOneBasedLineAndCharacterOfPosition(n.getStart(sourceFile)).line;
|
||||
var line = sourceFile.getZeroBasedLineAndCharacterOfPosition(n.getStart(sourceFile)).line;
|
||||
if (previousLine !== Constants.Unknown && line !== previousLine) {
|
||||
break;
|
||||
}
|
||||
@ -327,7 +327,7 @@ module ts.formatting {
|
||||
formattingScanner.advance();
|
||||
|
||||
if (formattingScanner.isOnToken()) {
|
||||
var startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(enclosingNode.getStart(sourceFile)).line;
|
||||
var startLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(enclosingNode.getStart(sourceFile)).line;
|
||||
var delta = getOwnOrInheritedDelta(enclosingNode, options, sourceFile);
|
||||
processNode(enclosingNode, enclosingNode, startLine, initialIndentation, delta);
|
||||
}
|
||||
@ -357,7 +357,7 @@ module ts.formatting {
|
||||
}
|
||||
}
|
||||
else {
|
||||
var startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(startPos).line;
|
||||
var startLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(startPos).line;
|
||||
var startLinePosition = getLineStartPositionForPosition(startPos, sourceFile);
|
||||
var column = SmartIndenter.findFirstNonWhitespaceColumn(startLinePosition, startPos, sourceFile, options);
|
||||
if (startLine !== parentStartLine || startPos === column) {
|
||||
@ -521,7 +521,7 @@ module ts.formatting {
|
||||
|
||||
var childStartPos = child.getStart(sourceFile);
|
||||
|
||||
var childStart = sourceFile.getOneBasedLineAndCharacterOfPosition(childStartPos);
|
||||
var childStart = sourceFile.getZeroBasedLineAndCharacterOfPosition(childStartPos);
|
||||
|
||||
// if child is a list item - try to get its indentation
|
||||
var childIndentationAmount = Constants.Unknown;
|
||||
@ -594,7 +594,7 @@ module ts.formatting {
|
||||
}
|
||||
else if (tokenInfo.token.kind === listStartToken) {
|
||||
// consume list start token
|
||||
startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(tokenInfo.token.pos).line;
|
||||
startLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(tokenInfo.token.pos).line;
|
||||
var indentation =
|
||||
computeIndentation(tokenInfo.token, startLine, Constants.Unknown, parent, parentDynamicIndentation, startLine);
|
||||
|
||||
@ -641,7 +641,7 @@ module ts.formatting {
|
||||
var lineAdded: boolean;
|
||||
var isTokenInRange = rangeContainsRange(originalRange, currentTokenInfo.token);
|
||||
|
||||
var tokenStart = sourceFile.getOneBasedLineAndCharacterOfPosition(currentTokenInfo.token.pos);
|
||||
var tokenStart = sourceFile.getZeroBasedLineAndCharacterOfPosition(currentTokenInfo.token.pos);
|
||||
if (isTokenInRange) {
|
||||
var rangeHasError = rangeContainsError(currentTokenInfo.token);
|
||||
// save prevStartLine since processRange will overwrite this value with current ones
|
||||
@ -674,7 +674,7 @@ module ts.formatting {
|
||||
continue;
|
||||
}
|
||||
|
||||
var triviaStartLine = sourceFile.getOneBasedLineAndCharacterOfPosition(triviaItem.pos).line;
|
||||
var triviaStartLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(triviaItem.pos).line;
|
||||
switch (triviaItem.kind) {
|
||||
case SyntaxKind.MultiLineCommentTrivia:
|
||||
var commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
|
||||
@ -712,7 +712,7 @@ module ts.formatting {
|
||||
for (var i = 0, len = trivia.length; i < len; ++i) {
|
||||
var triviaItem = trivia[i];
|
||||
if (isComment(triviaItem.kind) && rangeContainsRange(originalRange, triviaItem)) {
|
||||
var triviaItemStart = sourceFile.getOneBasedLineAndCharacterOfPosition(triviaItem.pos);
|
||||
var triviaItemStart = sourceFile.getZeroBasedLineAndCharacterOfPosition(triviaItem.pos);
|
||||
processRange(triviaItem, triviaItemStart, parent, contextNode, dynamicIndentation);
|
||||
}
|
||||
}
|
||||
@ -729,7 +729,7 @@ module ts.formatting {
|
||||
if (!rangeHasError && !previousRangeHasError) {
|
||||
if (!previousRange) {
|
||||
// trim whitespaces starting from the beginning of the span up to the current line
|
||||
var originalStart = sourceFile.getOneBasedLineAndCharacterOfPosition(originalRange.pos);
|
||||
var originalStart = sourceFile.getZeroBasedLineAndCharacterOfPosition(originalRange.pos);
|
||||
trimTrailingWhitespacesForLines(originalStart.line, rangeStart.line);
|
||||
}
|
||||
else {
|
||||
@ -807,18 +807,18 @@ module ts.formatting {
|
||||
recordReplace(pos, 0, indentationString);
|
||||
}
|
||||
else {
|
||||
var tokenStart = sourceFile.getOneBasedLineAndCharacterOfPosition(pos);
|
||||
if (indentation !== tokenStart.character - 1) {
|
||||
var startLinePosition = getStartPositionOfOneBasedLine(tokenStart.line, sourceFile);
|
||||
recordReplace(startLinePosition, tokenStart.character - 1, indentationString);
|
||||
var tokenStart = sourceFile.getZeroBasedLineAndCharacterOfPosition(pos);
|
||||
if (indentation !== tokenStart.character) {
|
||||
var startLinePosition = getStartPositionOfZeroBasedLine(tokenStart.line, sourceFile);
|
||||
recordReplace(startLinePosition, tokenStart.character, indentationString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function indentMultilineComment(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean) {
|
||||
// split comment in lines
|
||||
var startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(commentRange.pos).line;
|
||||
var endLine = sourceFile.getOneBasedLineAndCharacterOfPosition(commentRange.end).line;
|
||||
var startLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(commentRange.pos).line;
|
||||
var endLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(commentRange.end).line;
|
||||
|
||||
if (startLine === endLine) {
|
||||
if (!firstLineIsIndented) {
|
||||
@ -833,13 +833,13 @@ module ts.formatting {
|
||||
for (var line = startLine; line < endLine; ++line) {
|
||||
var endOfLine = getEndLinePosition(line, sourceFile);
|
||||
parts.push({ pos: startPos, end: endOfLine });
|
||||
startPos = getStartPositionOfOneBasedLine(line + 1, sourceFile);
|
||||
startPos = getStartPositionOfZeroBasedLine(line + 1, sourceFile);
|
||||
}
|
||||
|
||||
parts.push({ pos: startPos, end: commentRange.end });
|
||||
}
|
||||
|
||||
var startLinePos = getStartPositionOfOneBasedLine(startLine, sourceFile);
|
||||
var startLinePos = getStartPositionOfZeroBasedLine(startLine, sourceFile);
|
||||
|
||||
var nonWhitespaceColumnInFirstPart =
|
||||
SmartIndenter.findFirstNonWhitespaceColumn(startLinePos, parts[0].pos, sourceFile, options);
|
||||
@ -857,7 +857,7 @@ module ts.formatting {
|
||||
// shift all parts on the delta size
|
||||
var delta = indentation - nonWhitespaceColumnInFirstPart;
|
||||
for (var i = startIndex, len = parts.length; i < len; ++i, ++startLine) {
|
||||
var startLinePos = getStartPositionOfOneBasedLine(startLine, sourceFile);
|
||||
var startLinePos = getStartPositionOfZeroBasedLine(startLine, sourceFile);
|
||||
var nonWhitespaceColumn =
|
||||
i === 0
|
||||
? nonWhitespaceColumnInFirstPart
|
||||
@ -876,7 +876,7 @@ module ts.formatting {
|
||||
|
||||
function trimTrailingWhitespacesForLines(line1: number, line2: number, range?: TextRangeWithKind) {
|
||||
for (var line = line1; line < line2; ++line) {
|
||||
var lineStartPosition = getStartPositionOfOneBasedLine(line, sourceFile);
|
||||
var lineStartPosition = getStartPositionOfZeroBasedLine(line, sourceFile);
|
||||
var lineEndPosition = getEndLinePosition(line, sourceFile);
|
||||
|
||||
// do not trim whitespaces in comments
|
||||
|
||||
@ -71,8 +71,8 @@ module ts.formatting {
|
||||
|
||||
public TokensAreOnSameLine(): boolean {
|
||||
if (this.tokensAreOnSameLine === undefined) {
|
||||
var startLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(this.currentTokenSpan.pos).line;
|
||||
var endLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(this.nextTokenSpan.pos).line;
|
||||
var startLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(this.currentTokenSpan.pos).line;
|
||||
var endLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(this.nextTokenSpan.pos).line;
|
||||
this.tokensAreOnSameLine = (startLine == endLine);
|
||||
}
|
||||
|
||||
@ -96,8 +96,8 @@ module ts.formatting {
|
||||
}
|
||||
|
||||
private NodeIsOnOneLine(node: Node): boolean {
|
||||
var startLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(node.getStart(this.sourceFile)).line;
|
||||
var endLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(node.getEnd()).line;
|
||||
var startLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(node.getStart(this.sourceFile)).line;
|
||||
var endLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(node.getEnd()).line;
|
||||
return startLine == endLine;
|
||||
}
|
||||
|
||||
@ -105,8 +105,8 @@ module ts.formatting {
|
||||
var openBrace = findChildOfKind(node, SyntaxKind.OpenBraceToken, this.sourceFile);
|
||||
var closeBrace = findChildOfKind(node, SyntaxKind.CloseBraceToken, this.sourceFile);
|
||||
if (openBrace && closeBrace) {
|
||||
var startLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(openBrace.getEnd()).line;
|
||||
var endLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(closeBrace.getStart(this.sourceFile)).line;
|
||||
var startLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(openBrace.getEnd()).line;
|
||||
var endLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(closeBrace.getStart(this.sourceFile)).line;
|
||||
return startLine === endLine;
|
||||
}
|
||||
return false;
|
||||
|
||||
@ -6,12 +6,11 @@ module ts {
|
||||
}
|
||||
|
||||
export function getEndLinePosition(line: number, sourceFile: SourceFile): number {
|
||||
Debug.assert(line >= 1);
|
||||
Debug.assert(line >= 0);
|
||||
var lineStarts = sourceFile.getLineStarts();
|
||||
|
||||
// lines returned by SourceFile.getLineAndCharacterForPosition are 1-based
|
||||
var lineIndex = line - 1;
|
||||
if (lineIndex === lineStarts.length - 1) {
|
||||
var lineIndex = line;
|
||||
if (lineIndex + 1 === lineStarts.length) {
|
||||
// last line - return EOF
|
||||
return sourceFile.text.length - 1;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user