Path: blob/main/src/vs/editor/contrib/inlineCompletions/browser/model/textModelValueReference.ts
5252 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { onUnexpectedError } from '../../../../../base/common/errors.js';6import { URI } from '../../../../../base/common/uri.js';7import { Position } from '../../../../common/core/position.js';8import { Range } from '../../../../common/core/range.js';9import { AbstractText } from '../../../../common/core/text/abstractText.js';10import { TextLength } from '../../../../common/core/text/textLength.js';11import { ITextModel } from '../../../../common/model.js';1213/**14* An immutable view of a text model at a specific version.15* Like TextModelText but throws if the underlying model has changed.16* This ensures data read from the reference is consistent with17* the version at construction time.18*/19export class TextModelValueReference extends AbstractText {20private readonly _version: number;2122static snapshot(textModel: ITextModel): TextModelValueReference {23return new TextModelValueReference(textModel);24}2526private constructor(private readonly _textModel: ITextModel) {27super();28this._version = _textModel.getVersionId();29}3031get uri(): URI {32return this._textModel.uri;33}3435get version(): number {36return this._version;37}3839private _assertValid(): void {40if (this._textModel.getVersionId() !== this._version) {41onUnexpectedError(new Error(`TextModel has changed: expected version ${this._version}, got ${this._textModel.getVersionId()}`));42// TODO: throw here!43}44}4546targets(textModel: ITextModel): boolean {47return this._textModel.uri.toString() === textModel.uri.toString();48}4950override getValueOfRange(range: Range): string {51this._assertValid();52return this._textModel.getValueInRange(range);53}5455override getLineLength(lineNumber: number): number {56this._assertValid();57return this._textModel.getLineLength(lineNumber);58}5960get length(): TextLength {61this._assertValid();62const lastLineNumber = this._textModel.getLineCount();63const lastLineLen = this._textModel.getLineLength(lastLineNumber);64return new TextLength(lastLineNumber - 1, lastLineLen);65}6667getEOL(): string {68this._assertValid();69return this._textModel.getEOL();70}7172getPositionAt(offset: number): Position {73this._assertValid();74return this._textModel.getPositionAt(offset);75}7677getValueInRange(range: Range): string {78this._assertValid();79return this._textModel.getValueInRange(range);80}8182getVersionId(): number {83return this._version;84}8586dangerouslyGetUnderlyingModel(): ITextModel {87return this._textModel;88}89}909192