mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 04:43:37 -05:00
Expose zero-based helpers that code will be able to call.
This commit is contained in:
@@ -278,6 +278,10 @@ module ts {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getPositionFromZeroBasedLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
|
||||
return computePositionFromOneBasedLineAndCharacter(getLineStarts(sourceFile), line + 1, character + 1);
|
||||
}
|
||||
|
||||
export function getPositionFromOneBasedLineAndCharacter(sourceFile: SourceFile, line: number, character: number): number {
|
||||
return computePositionFromOneBasedLineAndCharacter(getLineStarts(sourceFile), line, character);
|
||||
}
|
||||
@@ -291,6 +295,11 @@ module ts {
|
||||
return sourceFile.lineMap || (sourceFile.lineMap = computeLineStarts(sourceFile.text));
|
||||
}
|
||||
|
||||
export function computeZeroBasedLineAndCharacterOfPosition(lineStarts: number[], position: number) {
|
||||
var oneBased = computeOneBasedLineAndCharacterOfPosition(lineStarts, position);
|
||||
return { line: oneBased.line - 1, character: oneBased.character -1 };
|
||||
}
|
||||
|
||||
export function computeOneBasedLineAndCharacterOfPosition(lineStarts: number[], position: number) {
|
||||
var lineNumber = binarySearch(lineStarts, position);
|
||||
if (lineNumber < 0) {
|
||||
@@ -306,6 +315,10 @@ module ts {
|
||||
};
|
||||
}
|
||||
|
||||
export function getZeroBasedLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter {
|
||||
return computeZeroBasedLineAndCharacterOfPosition(getLineStarts(sourceFile), position);
|
||||
}
|
||||
|
||||
export function getOneBasedLineAndCharacterOfPosition(sourceFile: SourceFile, position: number): LineAndCharacter {
|
||||
return computeOneBasedLineAndCharacterOfPosition(getLineStarts(sourceFile), position);
|
||||
}
|
||||
|
||||
@@ -1184,7 +1184,7 @@ module Harness {
|
||||
}
|
||||
|
||||
export function getMinimalDiagnostic(err: ts.Diagnostic): HarnessDiagnostic {
|
||||
var errorLineInfo = err.file ? err.file.getLineAndCharacterFromPosition(err.start) : { line: 0, character: 0 };
|
||||
var errorLineInfo = err.file ? err.file.getOneBasedLineAndCharacterFromPosition(err.start) : { line: 0, character: 0 };
|
||||
return {
|
||||
fileName: err.file && err.file.fileName,
|
||||
start: err.start,
|
||||
|
||||
@@ -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.getLineAndCharacterFromPosition(actualPos);
|
||||
var lineAndCharacter = this.currentSourceFile.getOneBasedLineAndCharacterFromPosition(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
|
||||
|
||||
@@ -14,8 +14,8 @@ module ts.BreakpointResolver {
|
||||
}
|
||||
|
||||
var tokenAtLocation = getTokenAtPosition(sourceFile, position);
|
||||
var lineOfPosition = sourceFile.getLineAndCharacterFromPosition(position).line;
|
||||
if (sourceFile.getLineAndCharacterFromPosition(tokenAtLocation.getStart()).line > lineOfPosition) {
|
||||
var lineOfPosition = sourceFile.getOneBasedLineAndCharacterFromPosition(position).line;
|
||||
if (sourceFile.getOneBasedLineAndCharacterFromPosition(tokenAtLocation.getStart()).line > lineOfPosition) {
|
||||
// Get previous token if the token is returned starts on new line
|
||||
// eg: var x =10; |--- curser is here
|
||||
// var y = 10;
|
||||
@@ -24,7 +24,7 @@ module ts.BreakpointResolver {
|
||||
tokenAtLocation = findPrecedingToken(tokenAtLocation.pos, sourceFile);
|
||||
|
||||
// Its a blank line
|
||||
if (!tokenAtLocation || sourceFile.getLineAndCharacterFromPosition(tokenAtLocation.getEnd()).line !== lineOfPosition) {
|
||||
if (!tokenAtLocation || sourceFile.getOneBasedLineAndCharacterFromPosition(tokenAtLocation.getEnd()).line !== lineOfPosition) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ module ts.BreakpointResolver {
|
||||
}
|
||||
|
||||
function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TextSpan {
|
||||
if (node && lineOfPosition === sourceFile.getLineAndCharacterFromPosition(node.getStart()).line) {
|
||||
if (node && lineOfPosition === sourceFile.getOneBasedLineAndCharacterFromPosition(node.getStart()).line) {
|
||||
return spanInNode(node);
|
||||
}
|
||||
return spanInNode(otherwiseOnNode);
|
||||
|
||||
@@ -67,7 +67,7 @@ module ts.formatting {
|
||||
}
|
||||
|
||||
export function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[] {
|
||||
var line = sourceFile.getLineAndCharacterFromPosition(position).line;
|
||||
var line = sourceFile.getOneBasedLineAndCharacterFromPosition(position).line;
|
||||
if (line === 1) {
|
||||
return [];
|
||||
}
|
||||
@@ -100,7 +100,7 @@ module ts.formatting {
|
||||
export function formatSelection(start: number, end: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[] {
|
||||
// format from the beginning of the line
|
||||
var span = {
|
||||
pos: getStartLinePositionForPosition(start, sourceFile),
|
||||
pos: getLineStartPositionForPosition(start, sourceFile),
|
||||
end: end
|
||||
};
|
||||
return formatSpan(span, sourceFile, options, rulesProvider, FormattingRequestKind.FormatSelection);
|
||||
@@ -112,7 +112,7 @@ module ts.formatting {
|
||||
return [];
|
||||
}
|
||||
var span = {
|
||||
pos: getStartLinePositionForPosition(parent.getStart(sourceFile), sourceFile),
|
||||
pos: getLineStartPositionForPosition(parent.getStart(sourceFile), sourceFile),
|
||||
end: parent.end
|
||||
};
|
||||
return formatSpan(span, sourceFile, options, rulesProvider, requestKind);
|
||||
@@ -283,7 +283,7 @@ module ts.formatting {
|
||||
var previousLine = Constants.Unknown;
|
||||
var childKind = SyntaxKind.Unknown;
|
||||
while (n) {
|
||||
var line = sourceFile.getLineAndCharacterFromPosition(n.getStart(sourceFile)).line;
|
||||
var line = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(enclosingNode.getStart(sourceFile)).line;
|
||||
var startLine = sourceFile.getOneBasedLineAndCharacterFromPosition(enclosingNode.getStart(sourceFile)).line;
|
||||
var delta = getOwnOrInheritedDelta(enclosingNode, options, sourceFile);
|
||||
processNode(enclosingNode, enclosingNode, startLine, initialIndentation, delta);
|
||||
}
|
||||
@@ -357,8 +357,8 @@ module ts.formatting {
|
||||
}
|
||||
}
|
||||
else {
|
||||
var startLine = sourceFile.getLineAndCharacterFromPosition(startPos).line;
|
||||
var startLinePosition = getStartLinePositionForPosition(startPos, sourceFile);
|
||||
var startLine = sourceFile.getOneBasedLineAndCharacterFromPosition(startPos).line;
|
||||
var startLinePosition = getLineStartPositionForPosition(startPos, sourceFile);
|
||||
var column = SmartIndenter.findFirstNonWhitespaceColumn(startLinePosition, startPos, sourceFile, options);
|
||||
if (startLine !== parentStartLine || startPos === column) {
|
||||
return column
|
||||
@@ -521,7 +521,7 @@ module ts.formatting {
|
||||
|
||||
var childStartPos = child.getStart(sourceFile);
|
||||
|
||||
var childStart = sourceFile.getLineAndCharacterFromPosition(childStartPos);
|
||||
var childStart = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(tokenInfo.token.pos).line;
|
||||
startLine = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(currentTokenInfo.token.pos);
|
||||
var tokenStart = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(triviaItem.pos).line;
|
||||
var triviaStartLine = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(triviaItem.pos);
|
||||
var triviaItemStart = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(originalRange.pos);
|
||||
var originalStart = sourceFile.getOneBasedLineAndCharacterFromPosition(originalRange.pos);
|
||||
trimTrailingWhitespacesForLines(originalStart.line, rangeStart.line);
|
||||
}
|
||||
else {
|
||||
@@ -807,7 +807,7 @@ module ts.formatting {
|
||||
recordReplace(pos, 0, indentationString);
|
||||
}
|
||||
else {
|
||||
var tokenStart = sourceFile.getLineAndCharacterFromPosition(pos);
|
||||
var tokenStart = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(commentRange.pos).line;
|
||||
var endLine = sourceFile.getLineAndCharacterFromPosition(commentRange.end).line;
|
||||
var startLine = sourceFile.getOneBasedLineAndCharacterFromPosition(commentRange.pos).line;
|
||||
var endLine = sourceFile.getOneBasedLineAndCharacterFromPosition(commentRange.end).line;
|
||||
|
||||
if (startLine === endLine) {
|
||||
if (!firstLineIsIndented) {
|
||||
|
||||
@@ -71,8 +71,8 @@ module ts.formatting {
|
||||
|
||||
public TokensAreOnSameLine(): boolean {
|
||||
if (this.tokensAreOnSameLine === undefined) {
|
||||
var startLine = this.sourceFile.getLineAndCharacterFromPosition(this.currentTokenSpan.pos).line;
|
||||
var endLine = this.sourceFile.getLineAndCharacterFromPosition(this.nextTokenSpan.pos).line;
|
||||
var startLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(this.currentTokenSpan.pos).line;
|
||||
var endLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(this.nextTokenSpan.pos).line;
|
||||
this.tokensAreOnSameLine = (startLine == endLine);
|
||||
}
|
||||
|
||||
@@ -96,8 +96,8 @@ module ts.formatting {
|
||||
}
|
||||
|
||||
private NodeIsOnOneLine(node: Node): boolean {
|
||||
var startLine = this.sourceFile.getLineAndCharacterFromPosition(node.getStart(this.sourceFile)).line;
|
||||
var endLine = this.sourceFile.getLineAndCharacterFromPosition(node.getEnd()).line;
|
||||
var startLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(node.getStart(this.sourceFile)).line;
|
||||
var endLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(openBrace.getEnd()).line;
|
||||
var endLine = this.sourceFile.getLineAndCharacterFromPosition(closeBrace.getStart(this.sourceFile)).line;
|
||||
var startLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(openBrace.getEnd()).line;
|
||||
var endLine = this.sourceFile.getOneBasedLineAndCharacterFromPosition(closeBrace.getStart(this.sourceFile)).line;
|
||||
return startLine === endLine;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -24,7 +24,7 @@ module ts.formatting {
|
||||
return 0;
|
||||
}
|
||||
|
||||
var lineAtPosition = sourceFile.getLineAndCharacterFromPosition(position).line;
|
||||
var lineAtPosition = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(n.getStart(sourceFile));
|
||||
var start = sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(containingList.pos);
|
||||
return sourceFile.getOneBasedLineAndCharacterFromPosition(containingList.pos);
|
||||
}
|
||||
|
||||
return sourceFile.getLineAndCharacterFromPosition(parent.getStart(sourceFile));
|
||||
return sourceFile.getOneBasedLineAndCharacterFromPosition(parent.getStart(sourceFile));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -204,7 +204,7 @@ module ts.formatting {
|
||||
}
|
||||
|
||||
function getStartLineAndCharacterForNode(n: Node, sourceFile: SourceFile): LineAndCharacter {
|
||||
return sourceFile.getLineAndCharacterFromPosition(n.getStart(sourceFile));
|
||||
return sourceFile.getOneBasedLineAndCharacterFromPosition(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.getLineAndCharacterFromPosition(list[i].end).line;
|
||||
var prevEndLine = sourceFile.getOneBasedLineAndCharacterFromPosition(list[i].end).line;
|
||||
if (prevEndLine !== lineAndCharacter.line) {
|
||||
return findColumnForFirstNonWhitespaceCharacterInLine(lineAndCharacter, sourceFile, options);
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ module ts {
|
||||
scriptSnapshot: IScriptSnapshot;
|
||||
nameTable: Map<string>;
|
||||
getNamedDeclarations(): Declaration[];
|
||||
getLineAndCharacterFromPosition(pos: number): LineAndCharacter;
|
||||
getOneBasedLineAndCharacterFromPosition(pos: number): LineAndCharacter;
|
||||
getLineStarts(): number[];
|
||||
getPositionFromOneBasedLineAndCharacter(line: number, character: number): number;
|
||||
update(newText: string, textChangeRange: TextChangeRange): SourceFile;
|
||||
@@ -612,7 +612,7 @@ module ts {
|
||||
}
|
||||
|
||||
if (paramHelpStringMargin === undefined) {
|
||||
paramHelpStringMargin = sourceFile.getLineAndCharacterFromPosition(firstLineParamHelpStringPos).character - 1;
|
||||
paramHelpStringMargin = sourceFile.getOneBasedLineAndCharacterFromPosition(firstLineParamHelpStringPos).character - 1;
|
||||
}
|
||||
|
||||
// Now consume white spaces max
|
||||
@@ -750,7 +750,7 @@ module ts {
|
||||
return updateSourceFile(this, newText, textChangeRange);
|
||||
}
|
||||
|
||||
public getLineAndCharacterFromPosition(position: number): LineAndCharacter {
|
||||
public getOneBasedLineAndCharacterFromPosition(position: number): LineAndCharacter {
|
||||
return getOneBasedLineAndCharacterOfPosition(this, position);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,9 +37,9 @@ module ts {
|
||||
return sourceFile.getLineStarts()[line - 1];
|
||||
}
|
||||
|
||||
export function getStartLinePositionForPosition(position: number, sourceFile: SourceFile): number {
|
||||
export function getLineStartPositionForPosition(position: number, sourceFile: SourceFile): number {
|
||||
var lineStarts = sourceFile.getLineStarts();
|
||||
var line = sourceFile.getLineAndCharacterFromPosition(position).line;
|
||||
var line = sourceFile.getOneBasedLineAndCharacterFromPosition(position).line;
|
||||
return lineStarts[line - 1];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user