diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index c12d4dedaef..8c4f435affe 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -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 diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6323ffd4313..095044c4b8f 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -105,6 +105,16 @@ module ts { return 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); diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 322c36bf7ad..b9cc0b24060 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -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 diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 711fc0fde8d..41bd63560eb 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -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;