From 124a77cc3a28e4851dad1f60dda933d9db982054 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Mon, 16 Feb 2015 19:35:45 -0800 Subject: [PATCH] Remove 'ZeroBased' from all APIs, now that all APIs are zero based. --- src/compiler/emitter.ts | 30 ++++++++-------- src/compiler/scanner.ts | 12 +++---- src/compiler/tsc.ts | 2 +- src/compiler/utilities.ts | 4 +-- src/harness/fourslash.ts | 4 +-- src/harness/harness.ts | 2 +- src/harness/harnessLanguageService.ts | 6 ++-- src/harness/typeWriter.ts | 2 +- src/services/breakpoints.ts | 8 ++--- src/services/formatting/formatting.ts | 38 ++++++++++---------- src/services/formatting/formattingContext.ts | 12 +++---- src/services/formatting/smartIndenter.ts | 26 +++++++------- src/services/services.ts | 14 ++++---- src/services/utilities.ts | 2 +- 14 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 2e78b9a2a46..6fafe9c65fb 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -133,14 +133,14 @@ module ts { }; } - function getZeroBasedLineOfLocalPosition(currentSourceFile: SourceFile, pos: number) { - return getZeroBasedLineAndCharacterOfPosition(currentSourceFile, pos).line; + function getBasedLineOfLocalPosition(currentSourceFile: SourceFile, pos: number) { + return getLineAndCharacterOfPosition(currentSourceFile, pos).line; } function emitNewLineBeforeLeadingComments(currentSourceFile: SourceFile, writer: EmitTextWriter, node: TextRange, leadingComments: CommentRange[]) { // If the leading comments start on different line than the start of node, write new line if (leadingComments && leadingComments.length && node.pos !== leadingComments[0].pos && - getZeroBasedLineOfLocalPosition(currentSourceFile, node.pos) !== getZeroBasedLineOfLocalPosition(currentSourceFile, leadingComments[0].pos)) { + getBasedLineOfLocalPosition(currentSourceFile, node.pos) !== getBasedLineOfLocalPosition(currentSourceFile, leadingComments[0].pos)) { writer.writeLine(); } } @@ -169,18 +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 = getZeroBasedLineAndCharacterOfPosition(currentSourceFile, comment.pos); + var firstCommentLineAndCharacter = getLineAndCharacterOfPosition(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 + 1) === lineCount ? currentSourceFile.text.length + 1 - : getStartPositionOfZeroBasedLine(currentLine + 1, currentSourceFile); + : getStartPositionOfLine(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(getStartPositionOfZeroBasedLine(firstCommentLineAndCharacter.line, currentSourceFile), comment.pos); + firstCommentLineIndent = calculateIndent(getStartPositionOfLine(firstCommentLineAndCharacter.line, currentSourceFile), comment.pos); } // These are number of spaces writer is going to write at current indent @@ -1734,7 +1734,7 @@ module ts { } function recordSourceMapSpan(pos: number) { - var sourceLinePos = getZeroBasedLineAndCharacterOfPosition(currentSourceFile, pos); + var sourceLinePos = getLineAndCharacterOfPosition(currentSourceFile, pos); // Convert the location to be one-based. sourceLinePos.line++; @@ -2979,13 +2979,13 @@ module ts { } function isOnSameLine(node1: Node, node2: Node) { - return getZeroBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node1.pos)) === - getZeroBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos)); + return getBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node1.pos)) === + getBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos)); } function nodeEndIsOnSameLineAsNodeStart(node1: Node, node2: Node) { - return getZeroBasedLineOfLocalPosition(currentSourceFile, node1.end) === - getZeroBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos)); + return getBasedLineOfLocalPosition(currentSourceFile, node1.end) === + getBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos)); } function emitCaseOrDefaultClause(node: CaseOrDefaultClause) { @@ -4476,8 +4476,8 @@ module ts { forEach(leadingComments, comment => { if (lastComment) { - var lastCommentLine = getZeroBasedLineOfLocalPosition(currentSourceFile, lastComment.end); - var commentLine = getZeroBasedLineOfLocalPosition(currentSourceFile, comment.pos); + var lastCommentLine = getBasedLineOfLocalPosition(currentSourceFile, lastComment.end); + var commentLine = getBasedLineOfLocalPosition(currentSourceFile, comment.pos); if (commentLine >= lastCommentLine + 2) { // There was a blank line between the last comment and this comment. This @@ -4495,8 +4495,8 @@ module ts { // All comments look like they could have been part of the copyright header. Make // sure there is at least one blank line between it and the node. If not, it's not // a copyright header. - var lastCommentLine = getZeroBasedLineOfLocalPosition(currentSourceFile, detachedComments[detachedComments.length - 1].end); - var nodeLine = getZeroBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node.pos)); + var lastCommentLine = getBasedLineOfLocalPosition(currentSourceFile, detachedComments[detachedComments.length - 1].end); + var nodeLine = getBasedLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node.pos)); if (nodeLine >= lastCommentLine + 2) { // Valid detachedComments emitNewLineBeforeLeadingComments(currentSourceFile, writer, node, leadingComments); diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 186c2a5ee79..7eddbb962fc 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -278,11 +278,11 @@ module ts { return result; } - export function getPositionOfZeroBasedLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number { - return computePositionOfZeroBasedLineAndCharacter(getLineStarts(sourceFile), line, character); + export function getPositionOfLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number { + return computePositionOfLineAndCharacter(getLineStarts(sourceFile), line, character); } - export function computePositionOfZeroBasedLineAndCharacter(lineStarts: number[], line: number, character: number): number { + export function computePositionOfLineAndCharacter(lineStarts: number[], line: number, character: number): number { Debug.assert(line >= 0 && line < lineStarts.length); return lineStarts[line] + character; } @@ -291,7 +291,7 @@ module ts { return sourceFile.lineMap || (sourceFile.lineMap = computeLineStarts(sourceFile.text)); } - export function computeZeroBasedLineAndCharacterOfPosition(lineStarts: number[], position: number) { + export function computeLineAndCharacterOfPosition(lineStarts: number[], position: number) { var lineNumber = binarySearch(lineStarts, position); if (lineNumber < 0) { // If the actual position was not found, @@ -306,8 +306,8 @@ module ts { }; } - export function getZeroBasedLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter { - return computeZeroBasedLineAndCharacterOfPosition(getLineStarts(sourceFile), position); + export function getLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter { + return computeLineAndCharacterOfPosition(getLineStarts(sourceFile), position); } var hasOwnProperty = Object.prototype.hasOwnProperty; diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 64272b196ff..68e85d3f71d 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -86,7 +86,7 @@ module ts { var output = ""; if (diagnostic.file) { - var loc = getZeroBasedLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); + var loc = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start); output += diagnostic.file.fileName + "(" + (loc.line + 1) + "," + (loc.character + 1) + "): "; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index da00bba6a31..6b336544eba 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -105,7 +105,7 @@ module ts { return node; } - export function getStartPositionOfZeroBasedLine(line: number, sourceFile: SourceFile): number { + export function getStartPositionOfLine(line: number, sourceFile: SourceFile): number { Debug.assert(line >= 0); return getLineStarts(sourceFile)[line]; } @@ -113,7 +113,7 @@ module ts { // This is a useful function for debugging purposes. export function nodePosToString(node: Node): string { var file = getSourceFileOfNode(node); - var loc = getZeroBasedLineAndCharacterOfPosition(file, node.pos); + var loc = getLineAndCharacterOfPosition(file, node.pos); return file.fileName + "(" + (loc.line + 1) + "," + (loc.character + 1) + ")"; } diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 9f9c84bbc1f..e40a367074d 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -395,7 +395,7 @@ module FourSlash { this.currentCaretPosition = pos; var lineStarts = ts.computeLineStarts(this.getFileContent(this.activeFile.fileName)); - var lineCharPos = ts.computeZeroBasedLineAndCharacterOfPosition(lineStarts, pos); + var lineCharPos = ts.computeLineAndCharacterOfPosition(lineStarts, pos); this.scenarioActions.push(''); } @@ -2112,7 +2112,7 @@ module FourSlash { } private getLineColStringAtPosition(position: number) { - var pos = this.languageServiceAdapterHost.positionToZeroBasedLineAndCharacter(this.activeFile.fileName, position); + var pos = this.languageServiceAdapterHost.positionToLineAndCharacter(this.activeFile.fileName, position); return 'line ' + (pos.line + 1) + ', col ' + pos.character; } diff --git a/src/harness/harness.ts b/src/harness/harness.ts index b2c19f2f51d..8658aea0a63 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1184,7 +1184,7 @@ module Harness { } export function getMinimalDiagnostic(err: ts.Diagnostic): HarnessDiagnostic { - var errorLineInfo = err.file ? err.file.getZeroBasedLineAndCharacterOfPosition(err.start) : { line: -1, character: -1 }; + var errorLineInfo = err.file ? err.file.getLineAndCharacterOfPosition(err.start) : { line: -1, character: -1 }; return { fileName: err.file && err.file.fileName, start: err.start, diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index 8ef89b80c88..e8f16825468 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -169,11 +169,11 @@ module Harness.LanguageService { * @param line 0 based index * @param col 0 based index */ - public positionToZeroBasedLineAndCharacter(fileName: string, position: number): ts.LineAndCharacter { + public positionToLineAndCharacter(fileName: string, position: number): ts.LineAndCharacter { var script: ScriptInfo = this.fileNameToScript[fileName]; assert.isNotNull(script); - return ts.computeZeroBasedLineAndCharacterOfPosition(script.lineMap, position); + return ts.computeLineAndCharacterOfPosition(script.lineMap, position); } } @@ -221,7 +221,7 @@ module Harness.LanguageService { addScript(fileName: string, content: string): void { this.nativeHost.addScript(fileName, content); } updateScript(fileName: string, content: string): void { return this.nativeHost.updateScript(fileName, content); } editScript(fileName: string, minChar: number, limChar: number, newText: string): void { this.nativeHost.editScript(fileName, minChar, limChar, newText); } - positionToZeroBasedLineAndCharacter(fileName: string, position: number): ts.LineAndCharacter { return this.nativeHost.positionToZeroBasedLineAndCharacter(fileName, position); } + positionToLineAndCharacter(fileName: string, position: number): ts.LineAndCharacter { return this.nativeHost.positionToLineAndCharacter(fileName, position); } getCompilationSettings(): string { return JSON.stringify(this.nativeHost.getCompilationSettings()); } getCancellationToken(): ts.CancellationToken { return this.nativeHost.getCancellationToken(); } diff --git a/src/harness/typeWriter.ts b/src/harness/typeWriter.ts index 30ff313ca67..54cdc27e3b3 100644 --- a/src/harness/typeWriter.ts +++ b/src/harness/typeWriter.ts @@ -85,7 +85,7 @@ class TypeWriterWalker { private log(node: ts.Node, type: ts.Type): void { var actualPos = ts.skipTrivia(this.currentSourceFile.text, node.pos); - var lineAndCharacter = this.currentSourceFile.getZeroBasedLineAndCharacterOfPosition(actualPos); + var lineAndCharacter = this.currentSourceFile.getLineAndCharacterOfPosition(actualPos); var sourceText = ts.getTextOfNodeFromSourceText(this.currentSourceFile.text, node); // If we got an unknown type, we temporarily want to fall back to just pretending the name diff --git a/src/services/breakpoints.ts b/src/services/breakpoints.ts index 492cc7866e9..1367d1affcd 100644 --- a/src/services/breakpoints.ts +++ b/src/services/breakpoints.ts @@ -14,8 +14,8 @@ module ts.BreakpointResolver { } var tokenAtLocation = getTokenAtPosition(sourceFile, position); - var lineOfPosition = sourceFile.getZeroBasedLineAndCharacterOfPosition(position).line; - if (sourceFile.getZeroBasedLineAndCharacterOfPosition(tokenAtLocation.getStart()).line > lineOfPosition) { + var lineOfPosition = sourceFile.getLineAndCharacterOfPosition(position).line; + if (sourceFile.getLineAndCharacterOfPosition(tokenAtLocation.getStart()).line > lineOfPosition) { // Get previous token if the token is returned starts on new line // eg: var x =10; |--- cursor is here // var y = 10; @@ -24,7 +24,7 @@ module ts.BreakpointResolver { tokenAtLocation = findPrecedingToken(tokenAtLocation.pos, sourceFile); // Its a blank line - if (!tokenAtLocation || sourceFile.getZeroBasedLineAndCharacterOfPosition(tokenAtLocation.getEnd()).line !== lineOfPosition) { + if (!tokenAtLocation || sourceFile.getLineAndCharacterOfPosition(tokenAtLocation.getEnd()).line !== lineOfPosition) { return undefined; } } @@ -42,7 +42,7 @@ module ts.BreakpointResolver { } function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TextSpan { - if (node && lineOfPosition === sourceFile.getZeroBasedLineAndCharacterOfPosition(node.getStart()).line) { + if (node && lineOfPosition === sourceFile.getLineAndCharacterOfPosition(node.getStart()).line) { return spanInNode(node); } return spanInNode(otherwiseOnNode); diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 65d35969af5..180ff0c9e95 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -67,14 +67,14 @@ module ts.formatting { } export function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[] { - var line = sourceFile.getZeroBasedLineAndCharacterOfPosition(position).line; + var line = sourceFile.getLineAndCharacterOfPosition(position).line; if (line === 0) { return []; } // get the span for the previous\current line var span = { // get start position for the previous line - pos: getStartPositionOfZeroBasedLine(line - 1, sourceFile), + pos: getStartPositionOfLine(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.getZeroBasedLineAndCharacterOfPosition(n.getStart(sourceFile)).line; + var line = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(enclosingNode.getStart(sourceFile)).line; + var startLine = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(startPos).line; + var startLine = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(childStartPos); + var childStart = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(tokenInfo.token.pos).line; + startLine = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(currentTokenInfo.token.pos); + var tokenStart = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(triviaItem.pos).line; + var triviaStartLine = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(triviaItem.pos); + var triviaItemStart = sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(originalRange.pos); + var originalStart = sourceFile.getLineAndCharacterOfPosition(originalRange.pos); trimTrailingWhitespacesForLines(originalStart.line, rangeStart.line); } else { @@ -807,9 +807,9 @@ module ts.formatting { recordReplace(pos, 0, indentationString); } else { - var tokenStart = sourceFile.getZeroBasedLineAndCharacterOfPosition(pos); + var tokenStart = sourceFile.getLineAndCharacterOfPosition(pos); if (indentation !== tokenStart.character) { - var startLinePosition = getStartPositionOfZeroBasedLine(tokenStart.line, sourceFile); + var startLinePosition = getStartPositionOfLine(tokenStart.line, sourceFile); recordReplace(startLinePosition, tokenStart.character, indentationString); } } @@ -817,8 +817,8 @@ module ts.formatting { function indentMultilineComment(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean) { // split comment in lines - var startLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(commentRange.pos).line; - var endLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(commentRange.end).line; + var startLine = sourceFile.getLineAndCharacterOfPosition(commentRange.pos).line; + var endLine = sourceFile.getLineAndCharacterOfPosition(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 = getStartPositionOfZeroBasedLine(line + 1, sourceFile); + startPos = getStartPositionOfLine(line + 1, sourceFile); } parts.push({ pos: startPos, end: commentRange.end }); } - var startLinePos = getStartPositionOfZeroBasedLine(startLine, sourceFile); + var startLinePos = getStartPositionOfLine(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 = getStartPositionOfZeroBasedLine(startLine, sourceFile); + var startLinePos = getStartPositionOfLine(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 = getStartPositionOfZeroBasedLine(line, sourceFile); + var lineStartPosition = getStartPositionOfLine(line, sourceFile); var lineEndPosition = getEndLinePosition(line, sourceFile); // do not trim whitespaces in comments diff --git a/src/services/formatting/formattingContext.ts b/src/services/formatting/formattingContext.ts index 5297f9e6658..b7573526f78 100644 --- a/src/services/formatting/formattingContext.ts +++ b/src/services/formatting/formattingContext.ts @@ -71,8 +71,8 @@ module ts.formatting { public TokensAreOnSameLine(): boolean { if (this.tokensAreOnSameLine === undefined) { - var startLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(this.currentTokenSpan.pos).line; - var endLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(this.nextTokenSpan.pos).line; + var startLine = this.sourceFile.getLineAndCharacterOfPosition(this.currentTokenSpan.pos).line; + var endLine = this.sourceFile.getLineAndCharacterOfPosition(this.nextTokenSpan.pos).line; this.tokensAreOnSameLine = (startLine == endLine); } @@ -96,8 +96,8 @@ module ts.formatting { } private NodeIsOnOneLine(node: Node): boolean { - var startLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(node.getStart(this.sourceFile)).line; - var endLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(node.getEnd()).line; + var startLine = this.sourceFile.getLineAndCharacterOfPosition(node.getStart(this.sourceFile)).line; + var endLine = this.sourceFile.getLineAndCharacterOfPosition(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.getZeroBasedLineAndCharacterOfPosition(openBrace.getEnd()).line; - var endLine = this.sourceFile.getZeroBasedLineAndCharacterOfPosition(closeBrace.getStart(this.sourceFile)).line; + var startLine = this.sourceFile.getLineAndCharacterOfPosition(openBrace.getEnd()).line; + var endLine = this.sourceFile.getLineAndCharacterOfPosition(closeBrace.getStart(this.sourceFile)).line; return startLine === endLine; } return false; diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 1c36df49d47..30d53b00370 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -24,7 +24,7 @@ module ts.formatting { return 0; } - var lineAtPosition = sourceFile.getZeroBasedLineAndCharacterOfPosition(position).line; + var lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line; if (precedingToken.kind === SyntaxKind.CommaToken && precedingToken.parent.kind !== SyntaxKind.BinaryExpression) { // previous token is comma that separates items in list - find the previous item and try to derive indentation from it @@ -43,7 +43,7 @@ module ts.formatting { while (current) { if (positionBelongsToNode(current, position, sourceFile) && shouldIndentChildNode(current.kind, previous ? previous.kind : SyntaxKind.Unknown)) { - currentStart = getZeroBasedStartLineAndCharacterForNode(current, sourceFile); + currentStart = getStartLineAndCharacterForNode(current, sourceFile); if (nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile)) { indentationDelta = 0; @@ -74,7 +74,7 @@ module ts.formatting { } export function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number { - var start = sourceFile.getZeroBasedLineAndCharacterOfPosition(n.getStart(sourceFile)); + var start = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)); return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options); } @@ -135,10 +135,10 @@ module ts.formatting { function getParentStart(parent: Node, child: Node, sourceFile: SourceFile): LineAndCharacter { var containingList = getContainingList(child, sourceFile); if (containingList) { - return sourceFile.getZeroBasedLineAndCharacterOfPosition(containingList.pos); + return sourceFile.getLineAndCharacterOfPosition(containingList.pos); } - return sourceFile.getZeroBasedLineAndCharacterOfPosition(parent.getStart(sourceFile)); + return sourceFile.getLineAndCharacterOfPosition(parent.getStart(sourceFile)); } /* @@ -196,15 +196,15 @@ module ts.formatting { // class A { // $} - var nextTokenStartLine = getZeroBasedStartLineAndCharacterForNode(nextToken, sourceFile).line; + var nextTokenStartLine = getStartLineAndCharacterForNode(nextToken, sourceFile).line; return lineAtPosition === nextTokenStartLine; } return false; } - function getZeroBasedStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter { - return sourceFile.getZeroBasedLineAndCharacterOfPosition(n.getStart(sourceFile)); + function getStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter { + return sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)); } function positionBelongsToNode(candidate: Node, position: number, sourceFile: SourceFile): boolean { @@ -216,7 +216,7 @@ module ts.formatting { var elseKeyword = findChildOfKind(parent, SyntaxKind.ElseKeyword, sourceFile); Debug.assert(elseKeyword !== undefined); - var elseKeywordStartLine = getZeroBasedStartLineAndCharacterForNode(elseKeyword, sourceFile).line; + var elseKeywordStartLine = getStartLineAndCharacterForNode(elseKeyword, sourceFile).line; return elseKeywordStartLine === childStartLine; } @@ -285,24 +285,24 @@ 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 = getZeroBasedStartLineAndCharacterForNode(node, sourceFile); + var lineAndCharacter = getStartLineAndCharacterForNode(node, sourceFile); for (var i = index - 1; i >= 0; --i) { if (list[i].kind === SyntaxKind.CommaToken) { continue; } // skip list items that ends on the same line with the current list element - var prevEndLine = sourceFile.getZeroBasedLineAndCharacterOfPosition(list[i].end).line; + var prevEndLine = sourceFile.getLineAndCharacterOfPosition(list[i].end).line; if (prevEndLine !== lineAndCharacter.line) { return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options); } - lineAndCharacter = getZeroBasedStartLineAndCharacterForNode(list[i], sourceFile); + lineAndCharacter = getStartLineAndCharacterForNode(list[i], sourceFile); } return -1; } function findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter: LineAndCharacter, sourceFile: SourceFile, options: EditorOptions): number { - var lineStart = sourceFile.getPositionOfZeroBasedLineAndCharacter(lineAndCharacter.line, 0); + var lineStart = sourceFile.getPositionOfLineAndCharacter(lineAndCharacter.line, 0); return findFirstNonWhitespaceColumn(lineStart, lineStart + lineAndCharacter.character, sourceFile, options); } diff --git a/src/services/services.ts b/src/services/services.ts index 7f1e52e4962..b286507fe3c 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -61,9 +61,9 @@ module ts { scriptSnapshot: IScriptSnapshot; nameTable: Map; getNamedDeclarations(): Declaration[]; - getZeroBasedLineAndCharacterOfPosition(pos: number): LineAndCharacter; + getLineAndCharacterOfPosition(pos: number): LineAndCharacter; getLineStarts(): number[]; - getPositionOfZeroBasedLineAndCharacter(line: number, character: number): number; + getPositionOfLineAndCharacter(line: number, character: number): number; update(newText: string, textChangeRange: TextChangeRange): SourceFile; } @@ -612,7 +612,7 @@ module ts { } if (paramHelpStringMargin === undefined) { - paramHelpStringMargin = sourceFile.getZeroBasedLineAndCharacterOfPosition(firstLineParamHelpStringPos).character; + paramHelpStringMargin = sourceFile.getLineAndCharacterOfPosition(firstLineParamHelpStringPos).character; } // Now consume white spaces max @@ -750,16 +750,16 @@ module ts { return updateSourceFile(this, newText, textChangeRange); } - public getZeroBasedLineAndCharacterOfPosition(position: number): LineAndCharacter { - return ts.getZeroBasedLineAndCharacterOfPosition(this, position); + public getLineAndCharacterOfPosition(position: number): LineAndCharacter { + return ts.getLineAndCharacterOfPosition(this, position); } public getLineStarts(): number[] { return getLineStarts(this); } - public getPositionOfZeroBasedLineAndCharacter(line: number, character: number): number { - return ts.getPositionOfZeroBasedLineAndCharacter(this, line, character); + public getPositionOfLineAndCharacter(line: number, character: number): number { + return ts.getPositionOfLineAndCharacter(this, line, character); } public getNamedDeclarations() { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 72fee5260a4..13156523045 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -33,7 +33,7 @@ module ts { export function getLineStartPositionForPosition(position: number, sourceFile: SourceFile): number { var lineStarts = sourceFile.getLineStarts(); - var line = sourceFile.getZeroBasedLineAndCharacterOfPosition(position).line; + var line = sourceFile.getLineAndCharacterOfPosition(position).line; return lineStarts[line]; }