Path: blob/main/extensions/copilot/src/util/common/test/shims/textEditor.ts
13405 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 type * as vscode from 'vscode';6import { ReadonlyError, illegalArgument } from '../../../vs/base/common/errors';7import { Position } from '../../../vs/workbench/api/common/extHostTypes/position';8import { Range } from '../../../vs/workbench/api/common/extHostTypes/range';9import { Selection } from '../../../vs/workbench/api/common/extHostTypes/selection';10import { SnippetString } from '../../../vs/workbench/api/common/extHostTypes/snippetString';11import { EndOfLine } from '../../../vs/workbench/api/common/extHostTypes/textEdit';1213interface ITextEditOperation {14range: vscode.Range;15text: string | null;16forceMoveMarkers: boolean;17}1819interface IEditData {20documentVersionId: number;21edits: ITextEditOperation[];22setEndOfLine: vscode.EndOfLine | undefined;23undoStopBefore: boolean;24undoStopAfter: boolean;25}2627class TextEditorEdit {2829private readonly _document: vscode.TextDocument;30private readonly _documentVersionId: number;31private readonly _undoStopBefore: boolean;32private readonly _undoStopAfter: boolean;33private _collectedEdits: ITextEditOperation[] = [];34private _setEndOfLine: vscode.EndOfLine | undefined = undefined;35private _finalized: boolean = false;3637constructor(document: vscode.TextDocument, options: { undoStopBefore: boolean; undoStopAfter: boolean }) {38this._document = document;39this._documentVersionId = document.version;40this._undoStopBefore = options.undoStopBefore;41this._undoStopAfter = options.undoStopAfter;42}4344finalize(): IEditData {45this._finalized = true;46return {47documentVersionId: this._documentVersionId,48edits: this._collectedEdits,49setEndOfLine: this._setEndOfLine,50undoStopBefore: this._undoStopBefore,51undoStopAfter: this._undoStopAfter52};53}5455private _throwIfFinalized() {56if (this._finalized) {57throw new Error('Edit is only valid while callback runs');58}59}6061replace(location: Position | Range | Selection, value: string): void {62this._throwIfFinalized();63let range: Range | null = null;6465if (location instanceof Position) {66range = new Range(location, location);67} else if (location instanceof Range) {68range = location;69} else {70throw new Error('Unrecognized location');71}7273this._pushEdit(range, value, false);74}7576insert(location: Position, value: string): void {77this._throwIfFinalized();78this._pushEdit(new Range(location, location), value, true);79}8081delete(location: Range | Selection): void {82this._throwIfFinalized();83let range: Range | null = null;8485if (location instanceof Range) {86range = location;87} else {88throw new Error('Unrecognized location');89}9091this._pushEdit(range, null, true);92}9394private _pushEdit(range: Range, text: string | null, forceMoveMarkers: boolean): void {95const validRange = this._document.validateRange(range);96this._collectedEdits.push({97range: validRange,98text: text,99forceMoveMarkers: forceMoveMarkers100});101}102103setEndOfLine(endOfLine: vscode.EndOfLine): void {104this._throwIfFinalized();105if (endOfLine !== EndOfLine.LF && endOfLine !== EndOfLine.CRLF) {106throw illegalArgument('endOfLine');107}108109this._setEndOfLine = endOfLine;110}111}112113export class ExtHostTextEditor {114115private _selections: vscode.Selection[];116private _options: vscode.TextEditorOptions;117private _visibleRanges: vscode.Range[];118private _viewColumn: vscode.ViewColumn | undefined;119120readonly value: vscode.TextEditor;121122constructor(123document: vscode.TextDocument,124selections: vscode.Selection[],125options: vscode.TextEditorOptions,126visibleRanges: vscode.Range[],127viewColumn: vscode.ViewColumn | undefined128) {129this._selections = selections;130this._options = options;131this._visibleRanges = visibleRanges;132this._viewColumn = viewColumn;133134const that = this;135136this.value = Object.freeze({137get document(): vscode.TextDocument {138return document;139},140set document(_value) {141throw new ReadonlyError('document');142},143// --- selection144get selection(): vscode.Selection {145return that._selections && that._selections[0];146},147set selection(value: Selection) {148if (!(value instanceof Selection)) {149throw illegalArgument('selection');150}151that._selections = [value];152},153get selections(): vscode.Selection[] {154return that._selections;155},156set selections(value: Selection[]) {157if (!Array.isArray(value) || value.some(a => !(a instanceof Selection))) {158throw illegalArgument('selections');159}160that._selections = value;161},162// --- visible ranges163get visibleRanges(): vscode.Range[] {164return that._visibleRanges;165},166set visibleRanges(_value: Range[]) {167throw new ReadonlyError('visibleRanges');168},169// --- options170get options(): vscode.TextEditorOptions {171return that._options;172},173set options(value: vscode.TextEditorOptions) {174throw new Error('Not implemented');175},176// --- view column177get viewColumn(): vscode.ViewColumn | undefined {178return that._viewColumn;179},180set viewColumn(_value) {181throw new ReadonlyError('viewColumn');182},183// --- edit184edit(callback: (edit: TextEditorEdit) => void, options: { undoStopBefore: boolean; undoStopAfter: boolean } = { undoStopBefore: true, undoStopAfter: true }): Promise<boolean> {185throw new Error('Not implemented');186},187// --- snippet edit188insertSnippet(snippet: SnippetString, where?: Position | readonly Position[] | Range | readonly Range[], options: { undoStopBefore: boolean; undoStopAfter: boolean } = { undoStopBefore: true, undoStopAfter: true }): Promise<boolean> {189throw new Error('Not implemented');190},191setDecorations(decorationType: vscode.TextEditorDecorationType, ranges: Range[] | vscode.DecorationOptions[]): void {192throw new Error('Not implemented');193},194revealRange(range: Range, revealType: vscode.TextEditorRevealType): void {195throw new Error('Not implemented');196},197show(column: vscode.ViewColumn) {198throw new Error('Not implemented');199},200hide() {201throw new Error('Not implemented');202}203});204}205206_acceptOptions(options: vscode.TextEditorOptions): void {207this._options = options;208}209210_acceptVisibleRanges(value: readonly vscode.Range[]): void {211this._visibleRanges = value.slice(0);212}213214_acceptViewColumn(value: vscode.ViewColumn) {215this._viewColumn = value;216}217218_acceptSelections(selections: vscode.Selection[]): void {219this._selections = selections;220}221}222223224