From edbedc7d5d7ca04021fc7f4722e09538497ad9fe Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Mon, 16 Feb 2015 17:47:32 -0800 Subject: [PATCH] Make code more clearly state that it is one based. --- src/compiler/emitter.ts | 14 ++++++-------- src/services/formatting/smartIndenter.ts | 12 ++++++------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 0f2042b0f68..2e78b9a2a46 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1733,15 +1733,13 @@ module ts { } } - function getOneBasedLineAndCharacterOfPosition(sourceFile: SourceFile, pos: number) { - var result = getZeroBasedLineAndCharacterOfPosition(sourceFile, pos); - result.line++; - result.character++; - return result; - } - function recordSourceMapSpan(pos: number) { - var sourceLinePos = getOneBasedLineAndCharacterOfPosition(currentSourceFile, pos); + var sourceLinePos = getZeroBasedLineAndCharacterOfPosition(currentSourceFile, pos); + + // Convert the location to be one-based. + sourceLinePos.line++; + sourceLinePos.character++; + var emittedLine = writer.getLine(); var emittedColumn = writer.getColumn(); diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 35be18910e3..9d81ba50348 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -43,7 +43,7 @@ module ts.formatting { while (current) { if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current.kind, previous ? previous.kind : SyntaxKind.Unknown)) { - currentStart = getStartLineAndCharacterForNode(current, sourceFile); + currentStart = getOneBasedStartLineAndCharacterForNode(current, sourceFile); if (nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile)) { indentationDelta = 0; @@ -196,14 +196,14 @@ module ts.formatting { // class A { // $} - var nextTokenStartLine = getStartLineAndCharacterForNode(nextToken, sourceFile).line; + var nextTokenStartLine = getOneBasedStartLineAndCharacterForNode(nextToken, sourceFile).line; return lineAtPosition === nextTokenStartLine; } return false; } - function getStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter { + function getOneBasedStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter { return sourceFile.getOneBasedLineAndCharacterOfPosition(n.getStart(sourceFile)); } @@ -216,7 +216,7 @@ module ts.formatting { var elseKeyword = findChildOfKind(parent, SyntaxKind.ElseKeyword, sourceFile); Debug.assert(elseKeyword !== undefined); - var elseKeywordStartLine = getStartLineAndCharacterForNode(elseKeyword, sourceFile).line; + var elseKeywordStartLine = getOneBasedStartLineAndCharacterForNode(elseKeyword, sourceFile).line; return elseKeywordStartLine === childStartLine; } @@ -286,7 +286,7 @@ module ts.formatting { // walk toward the start of the list starting from current node and check if the line is the same for all items. // if end line for item [i - 1] differs from the start line for item [i] - find column of the first non-whitespace character on the line of item [i] - var lineAndCharacter = getStartLineAndCharacterForNode(node, sourceFile); + var lineAndCharacter = getOneBasedStartLineAndCharacterForNode(node, sourceFile); for (var i = index - 1; i >= 0; --i) { if (list[i].kind === SyntaxKind.CommaToken) { continue; @@ -297,7 +297,7 @@ module ts.formatting { return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options); } - lineAndCharacter = getStartLineAndCharacterForNode(list[i], sourceFile); + lineAndCharacter = getOneBasedStartLineAndCharacterForNode(list[i], sourceFile); } return -1; }