Skip to content

Commit 91d7b22

Browse files
author
Andy
authored
Remove ILineInfo type (#17017)
1 parent bffde58 commit 91d7b22

File tree

5 files changed

+100
-130
lines changed

5 files changed

+100
-130
lines changed

src/harness/unittests/versionCache.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace ts {
77
}
88

99
function lineColToPosition(lineIndex: server.LineIndex, line: number, col: number) {
10-
const lineInfo = lineIndex.lineNumberToInfo(line);
11-
return (lineInfo.offset + col - 1);
10+
return lineIndex.absolutePositionOfStartOfLine(line) + (col - 1);
1211
}
1312

1413
function validateEdit(lineIndex: server.LineIndex, sourceText: string, position: number, deleteLength: number, insertString: string): void {
@@ -298,20 +297,17 @@ and grew 1cm per day`;
298297

299298
it("Line/offset from pos", () => {
300299
for (let i = 0; i < iterationCount; i++) {
301-
const lp = lineIndex.charOffsetToLineNumberAndPos(rsa[i]);
300+
const lp = lineIndex.positionToLineOffset(rsa[i]);
302301
const lac = ts.computeLineAndCharacterOfPosition(lineMap, rsa[i]);
303302
assert.equal(lac.line + 1, lp.line, "Line number mismatch " + (lac.line + 1) + " " + lp.line + " " + i);
304-
assert.equal(lac.character, (lp.offset), "Charachter offset mismatch " + lac.character + " " + lp.offset + " " + i);
303+
assert.equal(lac.character, lp.offset - 1, "Character offset mismatch " + lac.character + " " + (lp.offset - 1) + " " + i);
305304
}
306305
});
307306

308307
it("Start pos from line", () => {
309308
for (let i = 0; i < iterationCount; i++) {
310309
for (let j = 0; j < lines.length; j++) {
311-
const lineInfo = lineIndex.lineNumberToInfo(j + 1);
312-
const lineIndexOffset = lineInfo.offset;
313-
const lineMapOffset = lineMap[j];
314-
assert.equal(lineIndexOffset, lineMapOffset);
310+
assert.equal(lineIndex.absolutePositionOfStartOfLine(j + 1), lineMap[j]);
315311
}
316312
}
317313
});

src/server/protocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ namespace ts.server.protocol {
640640
}
641641

642642
/**
643-
* Location in source code expressed as (one-based) line and character offset.
643+
* Location in source code expressed as (one-based) line and (one-based) column offset.
644644
*/
645645
export interface Location {
646646
line: number;

src/server/scriptInfo.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace ts.server {
6161
: ScriptSnapshot.fromString(this.getOrLoadText());
6262
}
6363

64-
public getLineInfo(line: number) {
64+
public getLineInfo(line: number): AbsolutePositionAndLineText {
6565
return this.switchToScriptVersionCache().getSnapshot().index.lineNumberToInfo(line);
6666
}
6767
/**
@@ -75,16 +75,9 @@ namespace ts.server {
7575
return ts.createTextSpanFromBounds(start, end);
7676
}
7777
const index = this.svc.getSnapshot().index;
78-
const lineInfo = index.lineNumberToInfo(line + 1);
79-
let len: number;
80-
if (lineInfo.leaf) {
81-
len = lineInfo.leaf.text.length;
82-
}
83-
else {
84-
const nextLineInfo = index.lineNumberToInfo(line + 2);
85-
len = nextLineInfo.offset - lineInfo.offset;
86-
}
87-
return ts.createTextSpan(lineInfo.offset, len);
78+
const { lineText, absolutePosition } = index.lineNumberToInfo(line + 1);
79+
const len = lineText !== undefined ? lineText.length : index.absolutePositionOfStartOfLine(line + 2) - absolutePosition;
80+
return ts.createTextSpan(absolutePosition, len);
8881
}
8982

9083
/**
@@ -95,25 +88,17 @@ namespace ts.server {
9588
if (!this.svc) {
9689
return computePositionOfLineAndCharacter(this.getLineMap(), line - 1, offset - 1);
9790
}
98-
const index = this.svc.getSnapshot().index;
9991

100-
const lineInfo = index.lineNumberToInfo(line);
10192
// TODO: assert this offset is actually on the line
102-
return (lineInfo.offset + offset - 1);
93+
return this.svc.getSnapshot().index.absolutePositionOfStartOfLine(line) + (offset - 1);
10394
}
10495

105-
/**
106-
* @param line 1-based index
107-
* @param offset 1-based index
108-
*/
109-
positionToLineOffset(position: number): ILineInfo {
96+
positionToLineOffset(position: number): protocol.Location {
11097
if (!this.svc) {
11198
const { line, character } = computeLineAndCharacterOfPosition(this.getLineMap(), position);
11299
return { line: line + 1, offset: character + 1 };
113100
}
114-
const index = this.svc.getSnapshot().index;
115-
const lineOffset = index.charOffsetToLineNumberAndPos(position);
116-
return { line: lineOffset.line, offset: lineOffset.offset + 1 };
101+
return this.svc.getSnapshot().index.positionToLineOffset(position);
117102
}
118103

119104
private getFileText(tempFileName?: string) {
@@ -334,7 +319,7 @@ namespace ts.server {
334319
}
335320
}
336321

337-
getLineInfo(line: number) {
322+
getLineInfo(line: number): AbsolutePositionAndLineText {
338323
return this.textStorage.getLineInfo(line);
339324
}
340325

@@ -364,11 +349,7 @@ namespace ts.server {
364349
return this.textStorage.lineOffsetToPosition(line, offset);
365350
}
366351

367-
/**
368-
* @param line 1-based index
369-
* @param offset 1-based index
370-
*/
371-
positionToLineOffset(position: number): ILineInfo {
352+
positionToLineOffset(position: number): protocol.Location {
372353
return this.textStorage.positionToLineOffset(position);
373354
}
374355

src/server/scriptVersionCache.ts

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ namespace ts.server {
88
export interface LineCollection {
99
charCount(): number;
1010
lineCount(): number;
11-
isLeaf(): boolean;
11+
isLeaf(): this is LineLeaf;
1212
walk(rangeStart: number, rangeLength: number, walkFns: ILineIndexWalker): void;
1313
}
1414

15-
export interface ILineInfo {
16-
line: number;
17-
offset: number;
18-
text?: string;
19-
leaf?: LineLeaf;
15+
export interface AbsolutePositionAndLineText {
16+
absolutePosition: number;
17+
lineText: string | undefined;
2018
}
2119

2220
export enum CharRangeSection {
@@ -397,22 +395,27 @@ namespace ts.server {
397395
// set this to true to check each edit for accuracy
398396
checkEdits = false;
399397

400-
charOffsetToLineNumberAndPos(charOffset: number) {
401-
return this.root.charOffsetToLineNumberAndPos(1, charOffset);
398+
absolutePositionOfStartOfLine(oneBasedLine: number): number {
399+
return this.lineNumberToInfo(oneBasedLine).absolutePosition;
402400
}
403401

404-
lineNumberToInfo(lineNumber: number): ILineInfo {
402+
positionToLineOffset(position: number): protocol.Location {
403+
const { oneBasedLine, zeroBasedColumn } = this.root.charOffsetToLineInfo(1, position);
404+
return { line: oneBasedLine, offset: zeroBasedColumn + 1 };
405+
}
406+
407+
private positionToColumnAndLineText(position: number): { zeroBasedColumn: number, lineText: string } {
408+
return this.root.charOffsetToLineInfo(1, position);
409+
}
410+
411+
lineNumberToInfo(oneBasedLine: number): AbsolutePositionAndLineText {
405412
const lineCount = this.root.lineCount();
406-
if (lineNumber <= lineCount) {
407-
const lineInfo = this.root.lineNumberToInfo(lineNumber, 0);
408-
lineInfo.line = lineNumber;
409-
return lineInfo;
413+
if (oneBasedLine <= lineCount) {
414+
const { position, leaf } = this.root.lineNumberToInfo(oneBasedLine, 0);
415+
return { absolutePosition: position, lineText: leaf && leaf.text };
410416
}
411417
else {
412-
return {
413-
line: lineNumber,
414-
offset: this.root.charCount()
415-
};
418+
return { absolutePosition: this.root.charCount(), lineText: undefined };
416419
}
417420
}
418421

@@ -502,17 +505,12 @@ namespace ts.server {
502505
else if (deleteLength > 0) {
503506
// check whether last characters deleted are line break
504507
const e = pos + deleteLength;
505-
const lineInfo = this.charOffsetToLineNumberAndPos(e);
506-
if ((lineInfo && (lineInfo.offset === 0))) {
508+
const { zeroBasedColumn, lineText } = this.positionToColumnAndLineText(e);
509+
if (zeroBasedColumn === 0) {
507510
// move range end just past line that will merge with previous line
508-
deleteLength += lineInfo.text.length;
511+
deleteLength += lineText.length;
509512
// store text by appending to end of insertedText
510-
if (newText) {
511-
newText = newText + lineInfo.text;
512-
}
513-
else {
514-
newText = lineInfo.text;
515-
}
513+
newText = newText ? newText + lineText : lineText;
516514
}
517515
}
518516
if (pos < this.root.charCount()) {
@@ -676,90 +674,88 @@ namespace ts.server {
676674
}
677675
}
678676

679-
charOffsetToLineNumberAndPos(lineNumber: number, charOffset: number): ILineInfo {
680-
const childInfo = this.childFromCharOffset(lineNumber, charOffset);
677+
// Input position is relative to the start of this node.
678+
// Output line number is absolute.
679+
charOffsetToLineInfo(lineNumberAccumulator: number, relativePosition: number): { oneBasedLine: number, zeroBasedColumn: number, lineText: string | undefined } {
680+
const childInfo = this.childFromCharOffset(lineNumberAccumulator, relativePosition);
681681
if (!childInfo.child) {
682682
return {
683-
line: lineNumber,
684-
offset: charOffset,
683+
oneBasedLine: lineNumberAccumulator,
684+
zeroBasedColumn: relativePosition,
685+
lineText: undefined,
685686
};
686687
}
687688
else if (childInfo.childIndex < this.children.length) {
688689
if (childInfo.child.isLeaf()) {
689690
return {
690-
line: childInfo.lineNumber,
691-
offset: childInfo.charOffset,
692-
text: (<LineLeaf>(childInfo.child)).text,
693-
leaf: (<LineLeaf>(childInfo.child))
691+
oneBasedLine: childInfo.lineNumberAccumulator,
692+
zeroBasedColumn: childInfo.relativePosition,
693+
lineText: childInfo.child.text,
694694
};
695695
}
696696
else {
697697
const lineNode = <LineNode>(childInfo.child);
698-
return lineNode.charOffsetToLineNumberAndPos(childInfo.lineNumber, childInfo.charOffset);
698+
return lineNode.charOffsetToLineInfo(childInfo.lineNumberAccumulator, childInfo.relativePosition);
699699
}
700700
}
701701
else {
702702
const lineInfo = this.lineNumberToInfo(this.lineCount(), 0);
703-
return { line: this.lineCount(), offset: lineInfo.leaf.charCount() };
703+
return { oneBasedLine: this.lineCount(), zeroBasedColumn: lineInfo.leaf.charCount(), lineText: undefined };
704704
}
705705
}
706706

707-
lineNumberToInfo(lineNumber: number, charOffset: number): ILineInfo {
708-
const childInfo = this.childFromLineNumber(lineNumber, charOffset);
707+
lineNumberToInfo(relativeOneBasedLine: number, positionAccumulator: number): { position: number, leaf: LineLeaf | undefined } {
708+
const childInfo = this.childFromLineNumber(relativeOneBasedLine, positionAccumulator);
709709
if (!childInfo.child) {
710-
return {
711-
line: lineNumber,
712-
offset: charOffset
713-
};
710+
return { position: positionAccumulator, leaf: undefined };
714711
}
715712
else if (childInfo.child.isLeaf()) {
716-
return {
717-
line: lineNumber,
718-
offset: childInfo.charOffset,
719-
text: (<LineLeaf>(childInfo.child)).text,
720-
leaf: (<LineLeaf>(childInfo.child))
721-
};
713+
return { position: childInfo.positionAccumulator, leaf: childInfo.child };
722714
}
723715
else {
724716
const lineNode = <LineNode>(childInfo.child);
725-
return lineNode.lineNumberToInfo(childInfo.relativeLineNumber, childInfo.charOffset);
717+
return lineNode.lineNumberToInfo(childInfo.relativeOneBasedLine, childInfo.positionAccumulator);
726718
}
727719
}
728720

729-
childFromLineNumber(lineNumber: number, charOffset: number) {
721+
/**
722+
* Input line number is relative to the start of this node.
723+
* Output line number is relative to the child.
724+
* positionAccumulator will be an absolute position once relativeLineNumber reaches 0.
725+
*/
726+
private childFromLineNumber(relativeOneBasedLine: number, positionAccumulator: number): { child: LineCollection, relativeOneBasedLine: number, positionAccumulator: number } {
730727
let child: LineCollection;
731-
let relativeLineNumber = lineNumber;
732728
let i: number;
733-
let len: number;
734-
for (i = 0, len = this.children.length; i < len; i++) {
729+
for (i = 0; i < this.children.length; i++) {
735730
child = this.children[i];
736731
const childLineCount = child.lineCount();
737-
if (childLineCount >= relativeLineNumber) {
732+
if (childLineCount >= relativeOneBasedLine) {
738733
break;
739734
}
740735
else {
741-
relativeLineNumber -= childLineCount;
742-
charOffset += child.charCount();
736+
relativeOneBasedLine -= childLineCount;
737+
positionAccumulator += child.charCount();
743738
}
744739
}
745-
return { child, childIndex: i, relativeLineNumber, charOffset };
740+
return { child, relativeOneBasedLine, positionAccumulator };
746741
}
747742

748-
childFromCharOffset(lineNumber: number, charOffset: number) {
743+
private childFromCharOffset(lineNumberAccumulator: number, relativePosition: number
744+
): { child: LineCollection, childIndex: number, relativePosition: number, lineNumberAccumulator: number } {
749745
let child: LineCollection;
750746
let i: number;
751747
let len: number;
752748
for (i = 0, len = this.children.length; i < len; i++) {
753749
child = this.children[i];
754-
if (child.charCount() > charOffset) {
750+
if (child.charCount() > relativePosition) {
755751
break;
756752
}
757753
else {
758-
charOffset -= child.charCount();
759-
lineNumber += child.lineCount();
754+
relativePosition -= child.charCount();
755+
lineNumberAccumulator += child.lineCount();
760756
}
761757
}
762-
return { child, childIndex: i, charOffset, lineNumber };
758+
return { child, childIndex: i, relativePosition, lineNumberAccumulator };
763759
}
764760

765761
private splitAfter(childIndex: number) {

0 commit comments

Comments
 (0)