Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/test/shims/textEditor.ts
13405 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 type * as vscode from 'vscode';
7
import { ReadonlyError, illegalArgument } from '../../../vs/base/common/errors';
8
import { Position } from '../../../vs/workbench/api/common/extHostTypes/position';
9
import { Range } from '../../../vs/workbench/api/common/extHostTypes/range';
10
import { Selection } from '../../../vs/workbench/api/common/extHostTypes/selection';
11
import { SnippetString } from '../../../vs/workbench/api/common/extHostTypes/snippetString';
12
import { EndOfLine } from '../../../vs/workbench/api/common/extHostTypes/textEdit';
13
14
interface ITextEditOperation {
15
range: vscode.Range;
16
text: string | null;
17
forceMoveMarkers: boolean;
18
}
19
20
interface IEditData {
21
documentVersionId: number;
22
edits: ITextEditOperation[];
23
setEndOfLine: vscode.EndOfLine | undefined;
24
undoStopBefore: boolean;
25
undoStopAfter: boolean;
26
}
27
28
class TextEditorEdit {
29
30
private readonly _document: vscode.TextDocument;
31
private readonly _documentVersionId: number;
32
private readonly _undoStopBefore: boolean;
33
private readonly _undoStopAfter: boolean;
34
private _collectedEdits: ITextEditOperation[] = [];
35
private _setEndOfLine: vscode.EndOfLine | undefined = undefined;
36
private _finalized: boolean = false;
37
38
constructor(document: vscode.TextDocument, options: { undoStopBefore: boolean; undoStopAfter: boolean }) {
39
this._document = document;
40
this._documentVersionId = document.version;
41
this._undoStopBefore = options.undoStopBefore;
42
this._undoStopAfter = options.undoStopAfter;
43
}
44
45
finalize(): IEditData {
46
this._finalized = true;
47
return {
48
documentVersionId: this._documentVersionId,
49
edits: this._collectedEdits,
50
setEndOfLine: this._setEndOfLine,
51
undoStopBefore: this._undoStopBefore,
52
undoStopAfter: this._undoStopAfter
53
};
54
}
55
56
private _throwIfFinalized() {
57
if (this._finalized) {
58
throw new Error('Edit is only valid while callback runs');
59
}
60
}
61
62
replace(location: Position | Range | Selection, value: string): void {
63
this._throwIfFinalized();
64
let range: Range | null = null;
65
66
if (location instanceof Position) {
67
range = new Range(location, location);
68
} else if (location instanceof Range) {
69
range = location;
70
} else {
71
throw new Error('Unrecognized location');
72
}
73
74
this._pushEdit(range, value, false);
75
}
76
77
insert(location: Position, value: string): void {
78
this._throwIfFinalized();
79
this._pushEdit(new Range(location, location), value, true);
80
}
81
82
delete(location: Range | Selection): void {
83
this._throwIfFinalized();
84
let range: Range | null = null;
85
86
if (location instanceof Range) {
87
range = location;
88
} else {
89
throw new Error('Unrecognized location');
90
}
91
92
this._pushEdit(range, null, true);
93
}
94
95
private _pushEdit(range: Range, text: string | null, forceMoveMarkers: boolean): void {
96
const validRange = this._document.validateRange(range);
97
this._collectedEdits.push({
98
range: validRange,
99
text: text,
100
forceMoveMarkers: forceMoveMarkers
101
});
102
}
103
104
setEndOfLine(endOfLine: vscode.EndOfLine): void {
105
this._throwIfFinalized();
106
if (endOfLine !== EndOfLine.LF && endOfLine !== EndOfLine.CRLF) {
107
throw illegalArgument('endOfLine');
108
}
109
110
this._setEndOfLine = endOfLine;
111
}
112
}
113
114
export class ExtHostTextEditor {
115
116
private _selections: vscode.Selection[];
117
private _options: vscode.TextEditorOptions;
118
private _visibleRanges: vscode.Range[];
119
private _viewColumn: vscode.ViewColumn | undefined;
120
121
readonly value: vscode.TextEditor;
122
123
constructor(
124
document: vscode.TextDocument,
125
selections: vscode.Selection[],
126
options: vscode.TextEditorOptions,
127
visibleRanges: vscode.Range[],
128
viewColumn: vscode.ViewColumn | undefined
129
) {
130
this._selections = selections;
131
this._options = options;
132
this._visibleRanges = visibleRanges;
133
this._viewColumn = viewColumn;
134
135
const that = this;
136
137
this.value = Object.freeze({
138
get document(): vscode.TextDocument {
139
return document;
140
},
141
set document(_value) {
142
throw new ReadonlyError('document');
143
},
144
// --- selection
145
get selection(): vscode.Selection {
146
return that._selections && that._selections[0];
147
},
148
set selection(value: Selection) {
149
if (!(value instanceof Selection)) {
150
throw illegalArgument('selection');
151
}
152
that._selections = [value];
153
},
154
get selections(): vscode.Selection[] {
155
return that._selections;
156
},
157
set selections(value: Selection[]) {
158
if (!Array.isArray(value) || value.some(a => !(a instanceof Selection))) {
159
throw illegalArgument('selections');
160
}
161
that._selections = value;
162
},
163
// --- visible ranges
164
get visibleRanges(): vscode.Range[] {
165
return that._visibleRanges;
166
},
167
set visibleRanges(_value: Range[]) {
168
throw new ReadonlyError('visibleRanges');
169
},
170
// --- options
171
get options(): vscode.TextEditorOptions {
172
return that._options;
173
},
174
set options(value: vscode.TextEditorOptions) {
175
throw new Error('Not implemented');
176
},
177
// --- view column
178
get viewColumn(): vscode.ViewColumn | undefined {
179
return that._viewColumn;
180
},
181
set viewColumn(_value) {
182
throw new ReadonlyError('viewColumn');
183
},
184
// --- edit
185
edit(callback: (edit: TextEditorEdit) => void, options: { undoStopBefore: boolean; undoStopAfter: boolean } = { undoStopBefore: true, undoStopAfter: true }): Promise<boolean> {
186
throw new Error('Not implemented');
187
},
188
// --- snippet edit
189
insertSnippet(snippet: SnippetString, where?: Position | readonly Position[] | Range | readonly Range[], options: { undoStopBefore: boolean; undoStopAfter: boolean } = { undoStopBefore: true, undoStopAfter: true }): Promise<boolean> {
190
throw new Error('Not implemented');
191
},
192
setDecorations(decorationType: vscode.TextEditorDecorationType, ranges: Range[] | vscode.DecorationOptions[]): void {
193
throw new Error('Not implemented');
194
},
195
revealRange(range: Range, revealType: vscode.TextEditorRevealType): void {
196
throw new Error('Not implemented');
197
},
198
show(column: vscode.ViewColumn) {
199
throw new Error('Not implemented');
200
},
201
hide() {
202
throw new Error('Not implemented');
203
}
204
});
205
}
206
207
_acceptOptions(options: vscode.TextEditorOptions): void {
208
this._options = options;
209
}
210
211
_acceptVisibleRanges(value: readonly vscode.Range[]): void {
212
this._visibleRanges = value.slice(0);
213
}
214
215
_acceptViewColumn(value: vscode.ViewColumn) {
216
this._viewColumn = value;
217
}
218
219
_acceptSelections(selections: vscode.Selection[]): void {
220
this._selections = selections;
221
}
222
}
223
224