Skip to content

Commit bd68122

Browse files
author
Andy
authored
scriptVersionCache: Export less (microsoft#17202)
* scriptVersionCache: Export less * Remove LineIndexForTest, use LineIndex * Add /* @internal */ to class LineIndex * Make CharRangeSelection a const enum
1 parent c399230 commit bd68122

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/server/scriptInfo.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ts.server {
1616

1717
public getVersion() {
1818
return this.svc
19-
? `SVC-${this.svcVersion}-${this.svc.getSnapshot().version}`
19+
? `SVC-${this.svcVersion}-${this.svc.getSnapshotVersion()}`
2020
: `Text-${this.textVersion}`;
2121
}
2222

@@ -62,22 +62,19 @@ namespace ts.server {
6262
}
6363

6464
public getLineInfo(line: number): AbsolutePositionAndLineText {
65-
return this.switchToScriptVersionCache().getSnapshot().index.lineNumberToInfo(line);
65+
return this.switchToScriptVersionCache().getLineInfo(line);
6666
}
6767
/**
6868
* @param line 0 based index
6969
*/
70-
lineToTextSpan(line: number) {
70+
lineToTextSpan(line: number): TextSpan {
7171
if (!this.svc) {
7272
const lineMap = this.getLineMap();
7373
const start = lineMap[line]; // -1 since line is 1-based
7474
const end = line + 1 < lineMap.length ? lineMap[line + 1] : this.text.length;
7575
return createTextSpanFromBounds(start, end);
7676
}
77-
const index = this.svc.getSnapshot().index;
78-
const { lineText, absolutePosition } = index.lineNumberToInfo(line + 1);
79-
const len = lineText !== undefined ? lineText.length : index.absolutePositionOfStartOfLine(line + 2) - absolutePosition;
80-
return createTextSpan(absolutePosition, len);
77+
return this.svc.lineToTextSpan(line);
8178
}
8279

8380
/**
@@ -90,15 +87,15 @@ namespace ts.server {
9087
}
9188

9289
// TODO: assert this offset is actually on the line
93-
return this.svc.getSnapshot().index.absolutePositionOfStartOfLine(line) + (offset - 1);
90+
return this.svc.lineOffsetToPosition(line, offset);
9491
}
9592

9693
positionToLineOffset(position: number): protocol.Location {
9794
if (!this.svc) {
9895
const { line, character } = computeLineAndCharacterOfPosition(this.getLineMap(), position);
9996
return { line: line + 1, offset: character + 1 };
10097
}
101-
return this.svc.getSnapshot().index.positionToLineOffset(position);
98+
return this.svc.positionToLineOffset(position);
10299
}
103100

104101
private getFileText(tempFileName?: string) {

src/server/scriptVersionCache.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace ts.server {
66
const lineCollectionCapacity = 4;
77

8-
export interface LineCollection {
8+
interface LineCollection {
99
charCount(): number;
1010
lineCount(): number;
1111
isLeaf(): this is LineLeaf;
@@ -17,7 +17,7 @@ namespace ts.server {
1717
lineText: string | undefined;
1818
}
1919

20-
export enum CharRangeSection {
20+
const enum CharRangeSection {
2121
PreStart,
2222
Start,
2323
Entire,
@@ -26,7 +26,7 @@ namespace ts.server {
2626
PostEnd
2727
}
2828

29-
export interface ILineIndexWalker {
29+
interface ILineIndexWalker {
3030
goSubtree: boolean;
3131
done: boolean;
3232
leaf(relativeStart: number, relativeLength: number, lineCollection: LineLeaf): void;
@@ -243,7 +243,7 @@ namespace ts.server {
243243
}
244244

245245
// text change information
246-
export class TextChange {
246+
class TextChange {
247247
constructor(public pos: number, public deleteLen: number, public insertedText?: string) {
248248
}
249249

@@ -285,17 +285,6 @@ namespace ts.server {
285285
}
286286
}
287287

288-
latest() {
289-
return this.versions[this.currentVersionToIndex()];
290-
}
291-
292-
latestVersion() {
293-
if (this.changes.length > 0) {
294-
this.getSnapshot();
295-
}
296-
return this.currentVersion;
297-
}
298-
299288
// reload whole script, leaving no change history behind reload
300289
reload(script: string) {
301290
this.currentVersion++;
@@ -314,7 +303,9 @@ namespace ts.server {
314303
this.minVersion = this.currentVersion;
315304
}
316305

317-
getSnapshot() {
306+
getSnapshot(): IScriptSnapshot { return this._getSnapshot(); }
307+
308+
private _getSnapshot(): LineIndexSnapshot {
318309
let snap = this.versions[this.currentVersionToIndex()];
319310
if (this.changes.length > 0) {
320311
let snapIndex = snap.index;
@@ -334,6 +325,29 @@ namespace ts.server {
334325
return snap;
335326
}
336327

328+
getSnapshotVersion(): number {
329+
return this._getSnapshot().version;
330+
}
331+
332+
getLineInfo(line: number): AbsolutePositionAndLineText {
333+
return this._getSnapshot().index.lineNumberToInfo(line);
334+
}
335+
336+
lineOffsetToPosition(line: number, column: number): number {
337+
return this._getSnapshot().index.absolutePositionOfStartOfLine(line) + (column - 1);
338+
}
339+
340+
positionToLineOffset(position: number): protocol.Location {
341+
return this._getSnapshot().index.positionToLineOffset(position);
342+
}
343+
344+
lineToTextSpan(line: number): TextSpan {
345+
const index = this._getSnapshot().index;
346+
const { lineText, absolutePosition } = index.lineNumberToInfo(line + 1);
347+
const len = lineText !== undefined ? lineText.length : index.absolutePositionOfStartOfLine(line + 2) - absolutePosition;
348+
return createTextSpan(absolutePosition, len);
349+
}
350+
337351
getTextChangesBetweenVersions(oldVersion: number, newVersion: number) {
338352
if (oldVersion < newVersion) {
339353
if (oldVersion >= this.minVersion) {
@@ -365,7 +379,7 @@ namespace ts.server {
365379
}
366380
}
367381

368-
export class LineIndexSnapshot implements IScriptSnapshot {
382+
class LineIndexSnapshot implements IScriptSnapshot {
369383
constructor(readonly version: number, readonly cache: ScriptVersionCache, readonly index: LineIndex, readonly changesSincePreviousVersion: ReadonlyArray<TextChange> = emptyArray) {
370384
}
371385

@@ -389,6 +403,7 @@ namespace ts.server {
389403
}
390404
}
391405

406+
/* @internal */
392407
export class LineIndex {
393408
root: LineNode;
394409
// set this to true to check each edit for accuracy
@@ -561,7 +576,7 @@ namespace ts.server {
561576
}
562577
}
563578

564-
export class LineNode implements LineCollection {
579+
class LineNode implements LineCollection {
565580
totalChars = 0;
566581
totalLines = 0;
567582

@@ -844,7 +859,7 @@ namespace ts.server {
844859
}
845860
}
846861

847-
export class LineLeaf implements LineCollection {
862+
class LineLeaf implements LineCollection {
848863
constructor(public text: string) {
849864
}
850865

0 commit comments

Comments
 (0)