Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/inlineCompletions/browser/model/textModelValueReference.ts
5252 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { onUnexpectedError } from '../../../../../base/common/errors.js';
7
import { URI } from '../../../../../base/common/uri.js';
8
import { Position } from '../../../../common/core/position.js';
9
import { Range } from '../../../../common/core/range.js';
10
import { AbstractText } from '../../../../common/core/text/abstractText.js';
11
import { TextLength } from '../../../../common/core/text/textLength.js';
12
import { ITextModel } from '../../../../common/model.js';
13
14
/**
15
* An immutable view of a text model at a specific version.
16
* Like TextModelText but throws if the underlying model has changed.
17
* This ensures data read from the reference is consistent with
18
* the version at construction time.
19
*/
20
export class TextModelValueReference extends AbstractText {
21
private readonly _version: number;
22
23
static snapshot(textModel: ITextModel): TextModelValueReference {
24
return new TextModelValueReference(textModel);
25
}
26
27
private constructor(private readonly _textModel: ITextModel) {
28
super();
29
this._version = _textModel.getVersionId();
30
}
31
32
get uri(): URI {
33
return this._textModel.uri;
34
}
35
36
get version(): number {
37
return this._version;
38
}
39
40
private _assertValid(): void {
41
if (this._textModel.getVersionId() !== this._version) {
42
onUnexpectedError(new Error(`TextModel has changed: expected version ${this._version}, got ${this._textModel.getVersionId()}`));
43
// TODO: throw here!
44
}
45
}
46
47
targets(textModel: ITextModel): boolean {
48
return this._textModel.uri.toString() === textModel.uri.toString();
49
}
50
51
override getValueOfRange(range: Range): string {
52
this._assertValid();
53
return this._textModel.getValueInRange(range);
54
}
55
56
override getLineLength(lineNumber: number): number {
57
this._assertValid();
58
return this._textModel.getLineLength(lineNumber);
59
}
60
61
get length(): TextLength {
62
this._assertValid();
63
const lastLineNumber = this._textModel.getLineCount();
64
const lastLineLen = this._textModel.getLineLength(lastLineNumber);
65
return new TextLength(lastLineNumber - 1, lastLineLen);
66
}
67
68
getEOL(): string {
69
this._assertValid();
70
return this._textModel.getEOL();
71
}
72
73
getPositionAt(offset: number): Position {
74
this._assertValid();
75
return this._textModel.getPositionAt(offset);
76
}
77
78
getValueInRange(range: Range): string {
79
this._assertValid();
80
return this._textModel.getValueInRange(range);
81
}
82
83
getVersionId(): number {
84
return this._version;
85
}
86
87
dangerouslyGetUnderlyingModel(): ITextModel {
88
return this._textModel;
89
}
90
}
91
92