name more line/char functions more clearly. Use zero based indexing in breakpoints.

This commit is contained in:
Cyrus Najmabadi
2015-02-16 16:18:53 -08:00
parent afb083ae5a
commit b3366ec8b4
11 changed files with 54 additions and 49 deletions

View File

@@ -173,12 +173,12 @@ module ts {
var lastLine = 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) : getPositionFromOneBasedLineAndCharacter(currentSourceFile, currentLine + 1, /*character*/1);
var nextLineStart = currentLine === lastLine ? (comment.end + 1) : getPositionOfOneBasedLineAndCharacter(currentSourceFile, currentLine + 1, /*character*/1);
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(getPositionFromOneBasedLineAndCharacter(currentSourceFile, firstCommentLineAndCharacter.line, /*character*/1),
firstCommentLineIndent = calculateIndent(getPositionOfOneBasedLineAndCharacter(currentSourceFile, firstCommentLineAndCharacter.line, /*character*/1),
comment.pos);
}

View File

@@ -278,15 +278,15 @@ module ts {
return result;
}
export function getPositionFromZeroBasedLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
return computePositionFromOneBasedLineAndCharacter(getLineStarts(sourceFile), line + 1, character + 1);
export function getPositionOfZeroBasedLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
return computePositionOfOneBasedLineAndCharacter(getLineStarts(sourceFile), line + 1, character + 1);
}
export function getPositionFromOneBasedLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
return computePositionFromOneBasedLineAndCharacter(getLineStarts(sourceFile), line, character);
export function getPositionOfOneBasedLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
return computePositionOfOneBasedLineAndCharacter(getLineStarts(sourceFile), line, character);
}
export function computePositionFromOneBasedLineAndCharacter(lineStarts: number[], line: number, character: number): number {
export function computePositionOfOneBasedLineAndCharacter(lineStarts: number[], line: number, character: number): number {
Debug.assert(line > 0 && line <= lineStarts.length);
return lineStarts[line - 1] + character - 1;
}

View File

@@ -1184,7 +1184,7 @@ module Harness {
}
export function getMinimalDiagnostic(err: ts.Diagnostic): HarnessDiagnostic {
var errorLineInfo = err.file ? err.file.getOneBasedLineAndCharacterFromPosition(err.start) : { line: 0, character: 0 };
var errorLineInfo = err.file ? err.file.getOneBasedLineAndCharacterOfPosition(err.start) : { line: 0, character: 0 };
return {
fileName: err.file && err.file.fileName,
start: err.start,

View File

@@ -175,7 +175,7 @@ module Harness.LanguageService {
assert.isTrue(line >= 1);
assert.isTrue(col >= 1);
return ts.computePositionFromOneBasedLineAndCharacter(script.lineMap, line, col);
return ts.computePositionOfOneBasedLineAndCharacter(script.lineMap, line, col);
}
/**

View File

@@ -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.getOneBasedLineAndCharacterFromPosition(actualPos);
var lineAndCharacter = this.currentSourceFile.getOneBasedLineAndCharacterOfPosition(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

View File

@@ -14,17 +14,17 @@ module ts.BreakpointResolver {
}
var tokenAtLocation = getTokenAtPosition(sourceFile, position);
var lineOfPosition = sourceFile.getOneBasedLineAndCharacterFromPosition(position).line;
if (sourceFile.getOneBasedLineAndCharacterFromPosition(tokenAtLocation.getStart()).line > lineOfPosition) {
var lineOfPosition = sourceFile.getZeroBasedLineAndCharacterOfPosition(position).line;
if (sourceFile.getZeroBasedLineAndCharacterOfPosition(tokenAtLocation.getStart()).line > lineOfPosition) {
// Get previous token if the token is returned starts on new line
// eg: var x =10; |--- curser is here
// eg: var x =10; |--- cursor is here
// var y = 10;
// token at position will return var keyword on second line as the token but we would like to use
// token on same line if trailing trivia (comments or white spaces on same line) part of the last token on that line
tokenAtLocation = findPrecedingToken(tokenAtLocation.pos, sourceFile);
// Its a blank line
if (!tokenAtLocation || sourceFile.getOneBasedLineAndCharacterFromPosition(tokenAtLocation.getEnd()).line !== lineOfPosition) {
if (!tokenAtLocation || sourceFile.getZeroBasedLineAndCharacterOfPosition(tokenAtLocation.getEnd()).line !== lineOfPosition) {
return undefined;
}
}
@@ -42,7 +42,7 @@ module ts.BreakpointResolver {
}
function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TextSpan {
if (node && lineOfPosition === sourceFile.getOneBasedLineAndCharacterFromPosition(node.getStart()).line) {
if (node && lineOfPosition === sourceFile.getZeroBasedLineAndCharacterOfPosition(node.getStart()).line) {
return spanInNode(node);
}
return spanInNode(otherwiseOnNode);

View File

@@ -67,7 +67,7 @@ module ts.formatting {
}
export function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[] {
var line = sourceFile.getOneBasedLineAndCharacterFromPosition(position).line;
var line = sourceFile.getOneBasedLineAndCharacterOfPosition(position).line;
if (line === 1) {
return [];
}
@@ -283,7 +283,7 @@ module ts.formatting {
var previousLine = Constants.Unknown;
var childKind = SyntaxKind.Unknown;
while (n) {
var line = sourceFile.getOneBasedLineAndCharacterFromPosition(n.getStart(sourceFile)).line;
var line = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(enclosingNode.getStart(sourceFile)).line;
var startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(startPos).line;
var startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(childStartPos);
var childStart = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(tokenInfo.token.pos).line;
startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(currentTokenInfo.token.pos);
var tokenStart = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(triviaItem.pos).line;
var triviaStartLine = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(triviaItem.pos);
var triviaItemStart = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(originalRange.pos);
var originalStart = sourceFile.getOneBasedLineAndCharacterOfPosition(originalRange.pos);
trimTrailingWhitespacesForLines(originalStart.line, rangeStart.line);
}
else {
@@ -807,7 +807,7 @@ module ts.formatting {
recordReplace(pos, 0, indentationString);
}
else {
var tokenStart = sourceFile.getOneBasedLineAndCharacterFromPosition(pos);
var tokenStart = sourceFile.getOneBasedLineAndCharacterOfPosition(pos);
if (indentation !== tokenStart.character - 1) {
var startLinePosition = getStartPositionOfLine(tokenStart.line, sourceFile);
recordReplace(startLinePosition, tokenStart.character - 1, indentationString);
@@ -817,8 +817,8 @@ module ts.formatting {
function indentMultilineComment(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean) {
// split comment in lines
var startLine = sourceFile.getOneBasedLineAndCharacterFromPosition(commentRange.pos).line;
var endLine = sourceFile.getOneBasedLineAndCharacterFromPosition(commentRange.end).line;
var startLine = sourceFile.getOneBasedLineAndCharacterOfPosition(commentRange.pos).line;
var endLine = sourceFile.getOneBasedLineAndCharacterOfPosition(commentRange.end).line;
if (startLine === endLine) {
if (!firstLineIsIndented) {

View File

@@ -71,8 +71,8 @@ module ts.formatting {
public TokensAreOnSameLine(): boolean {
if (this.tokensAreOnSameLine === undefined) {
var startLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(this.currentTokenSpan.pos).line;
var endLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(this.nextTokenSpan.pos).line;
var startLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(this.currentTokenSpan.pos).line;
var endLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(this.nextTokenSpan.pos).line;
this.tokensAreOnSameLine = (startLine == endLine);
}
@@ -96,8 +96,8 @@ module ts.formatting {
}
private NodeIsOnOneLine(node: Node): boolean {
var startLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(node.getStart(this.sourceFile)).line;
var endLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(node.getEnd()).line;
var startLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(node.getStart(this.sourceFile)).line;
var endLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(openBrace.getEnd()).line;
var endLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(closeBrace.getStart(this.sourceFile)).line;
var startLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(openBrace.getEnd()).line;
var endLine = this.sourceFile.getOneBasedLineAndCharacterOfPosition(closeBrace.getStart(this.sourceFile)).line;
return startLine === endLine;
}
return false;

View File

@@ -24,7 +24,7 @@ module ts.formatting {
return 0;
}
var lineAtPosition = sourceFile.getOneBasedLineAndCharacterFromPosition(position).line;
var lineAtPosition = sourceFile.getOneBasedLineAndCharacterOfPosition(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
@@ -74,7 +74,7 @@ module ts.formatting {
}
export function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number {
var start = sourceFile.getOneBasedLineAndCharacterFromPosition(n.getStart(sourceFile));
var start = sourceFile.getOneBasedLineAndCharacterOfPosition(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.getOneBasedLineAndCharacterFromPosition(containingList.pos);
return sourceFile.getOneBasedLineAndCharacterOfPosition(containingList.pos);
}
return sourceFile.getOneBasedLineAndCharacterFromPosition(parent.getStart(sourceFile));
return sourceFile.getOneBasedLineAndCharacterOfPosition(parent.getStart(sourceFile));
}
/*
@@ -204,7 +204,7 @@ module ts.formatting {
}
function getStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter {
return sourceFile.getOneBasedLineAndCharacterFromPosition(n.getStart(sourceFile));
return sourceFile.getOneBasedLineAndCharacterOfPosition(n.getStart(sourceFile));
}
function positionBelongsToNode(candidate: Node, position: number, sourceFile: SourceFile): boolean {
@@ -292,7 +292,7 @@ module ts.formatting {
continue;
}
// skip list items that ends on the same line with the current list element
var prevEndLine = sourceFile.getOneBasedLineAndCharacterFromPosition(list[i].end).line;
var prevEndLine = sourceFile.getOneBasedLineAndCharacterOfPosition(list[i].end).line;
if (prevEndLine !== lineAndCharacter.line) {
return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options);
}
@@ -303,7 +303,7 @@ module ts.formatting {
}
function findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter: LineAndCharacter, sourceFile: SourceFile, options: EditorOptions): number {
var lineStart = sourceFile.getPositionFromOneBasedLineAndCharacter(lineAndCharacter.line, 1);
var lineStart = sourceFile.getPositionOfOneBasedLineAndCharacter(lineAndCharacter.line, 1);
return findFirstNonWhitespaceColumn(lineStart, lineStart + lineAndCharacter.character, sourceFile, options);
}

View File

@@ -61,9 +61,10 @@ module ts {
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getOneBasedLineAndCharacterFromPosition(pos: number): LineAndCharacter;
getZeroBasedLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getOneBasedLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
getPositionFromOneBasedLineAndCharacter(line: number, character: number): number;
getPositionOfOneBasedLineAndCharacter(line: number, character: number): number;
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
}
@@ -612,7 +613,7 @@ module ts {
}
if (paramHelpStringMargin === undefined) {
paramHelpStringMargin = sourceFile.getOneBasedLineAndCharacterFromPosition(firstLineParamHelpStringPos).character - 1;
paramHelpStringMargin = sourceFile.getOneBasedLineAndCharacterOfPosition(firstLineParamHelpStringPos).character - 1;
}
// Now consume white spaces max
@@ -750,16 +751,20 @@ module ts {
return updateSourceFile(this, newText, textChangeRange);
}
public getOneBasedLineAndCharacterFromPosition(position: number): LineAndCharacter {
return getOneBasedLineAndCharacterOfPosition(this, position);
public getZeroBasedLineAndCharacterOfPosition(position: number): LineAndCharacter {
return ts.getZeroBasedLineAndCharacterOfPosition(this, position);
}
public getOneBasedLineAndCharacterOfPosition(position: number): LineAndCharacter {
return ts.getOneBasedLineAndCharacterOfPosition(this, position);
}
public getLineStarts(): number[] {
return getLineStarts(this);
}
public getPositionFromOneBasedLineAndCharacter(line: number, character: number): number {
return getPositionFromOneBasedLineAndCharacter(this, line, character);
public getPositionOfOneBasedLineAndCharacter(line: number, character: number): number {
return ts.getPositionOfOneBasedLineAndCharacter(this, line, character);
}
public getNamedDeclarations() {

View File

@@ -39,7 +39,7 @@ module ts {
export function getLineStartPositionForPosition(position: number, sourceFile: SourceFile): number {
var lineStarts = sourceFile.getLineStarts();
var line = sourceFile.getOneBasedLineAndCharacterFromPosition(position).line;
var line = sourceFile.getOneBasedLineAndCharacterOfPosition(position).line;
return lineStarts[line - 1];
}