Move the emitter over to using zero based indexing.

This commit is contained in:
Cyrus Najmabadi
2015-02-16 17:34:11 -08:00
parent ab7d36d526
commit 8ef4df8acb
4 changed files with 22 additions and 16 deletions

View File

@@ -169,17 +169,18 @@ module ts {
function writeCommentRange(currentSourceFile: SourceFile, writer: EmitTextWriter, comment: CommentRange, newLine: string){
if (currentSourceFile.text.charCodeAt(comment.pos + 1) === CharacterCodes.asterisk) {
var firstCommentLineAndCharacter = getOneBasedLineAndCharacterOfPosition(currentSourceFile, comment.pos);
var lastLine = getLineStarts(currentSourceFile).length;
var firstCommentLineAndCharacter = getZeroBasedLineAndCharacterOfPosition(currentSourceFile, comment.pos);
var lineCount = getLineStarts(currentSourceFile).length;
var firstCommentLineIndent: number;
for (var pos = comment.pos, currentLine = firstCommentLineAndCharacter.line; pos < comment.end; currentLine++) {
var nextLineStart = currentLine === lastLine ? (comment.end + 1) : getPositionOfOneBasedLineAndCharacter(currentSourceFile, currentLine + 1, /*character*/1);
var nextLineStart = (currentLine + 1) === lineCount
? currentSourceFile.text.length + 1
: getStartPositionOfZeroBasedLine(currentLine + 1, currentSourceFile);
if (pos !== comment.pos) {
// If we are not emitting first line, we need to write the spaces to adjust the alignment
if (firstCommentLineIndent === undefined) {
firstCommentLineIndent = calculateIndent(getPositionOfOneBasedLineAndCharacter(currentSourceFile, firstCommentLineAndCharacter.line, /*character*/1),
comment.pos);
firstCommentLineIndent = calculateIndent(getStartPositionOfZeroBasedLine(firstCommentLineAndCharacter.line, currentSourceFile), comment.pos);
}
// These are number of spaces writer is going to write at current indent

View File

@@ -105,6 +105,16 @@ module ts {
return <SourceFile>node;
}
export function getStartPositionOfZeroBasedLine(line: number, sourceFile: SourceFile): number {
Debug.assert(line >= 0);
return getLineStarts(sourceFile)[line];
}
export function getStartPositionOfOneBasedLine(line: number, sourceFile: SourceFile): number {
Debug.assert(line >= 1);
return getLineStarts(sourceFile)[line - 1];
}
// This is a useful function for debugging purposes.
export function nodePosToString(node: Node): string {
var file = getSourceFileOfNode(node);

View File

@@ -74,7 +74,7 @@ module ts.formatting {
// get the span for the previous\current line
var span = {
// get start position for the previous line
pos: getStartPositionOfLine(line - 1, sourceFile),
pos: getStartPositionOfOneBasedLine(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
}
@@ -809,7 +809,7 @@ module ts.formatting {
else {
var tokenStart = sourceFile.getOneBasedLineAndCharacterOfPosition(pos);
if (indentation !== tokenStart.character - 1) {
var startLinePosition = getStartPositionOfLine(tokenStart.line, sourceFile);
var startLinePosition = getStartPositionOfOneBasedLine(tokenStart.line, sourceFile);
recordReplace(startLinePosition, tokenStart.character - 1, indentationString);
}
}
@@ -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 = getStartPositionOfLine(line + 1, sourceFile);
startPos = getStartPositionOfOneBasedLine(line + 1, sourceFile);
}
parts.push({ pos: startPos, end: commentRange.end });
}
var startLinePos = getStartPositionOfLine(startLine, sourceFile);
var startLinePos = getStartPositionOfOneBasedLine(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 = getStartPositionOfLine(startLine, sourceFile);
var startLinePos = getStartPositionOfOneBasedLine(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 = getStartPositionOfLine(line, sourceFile);
var lineStartPosition = getStartPositionOfOneBasedLine(line, sourceFile);
var lineEndPosition = getEndLinePosition(line, sourceFile);
// do not trim whitespaces in comments

View File

@@ -32,11 +32,6 @@ module ts {
}
}
export function getStartPositionOfLine(line: number, sourceFile: SourceFile): number {
Debug.assert(line >= 1);
return sourceFile.getLineStarts()[line - 1];
}
export function getLineStartPositionForPosition(position: number, sourceFile: SourceFile): number {
var lineStarts = sourceFile.getLineStarts();
var line = sourceFile.getOneBasedLineAndCharacterOfPosition(position).line;