Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/snippet/browser/snippetController2.ts
5289 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 { KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';
7
import { DisposableStore, IDisposable } from '../../../../base/common/lifecycle.js';
8
import { assertType } from '../../../../base/common/types.js';
9
import { ICodeEditor } from '../../../browser/editorBrowser.js';
10
import { EditorCommand, EditorContributionInstantiation, registerEditorCommand, registerEditorContribution } from '../../../browser/editorExtensions.js';
11
import { Position } from '../../../common/core/position.js';
12
import { Range } from '../../../common/core/range.js';
13
import { IEditorContribution } from '../../../common/editorCommon.js';
14
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
15
import { CompletionItem, CompletionItemKind, CompletionItemProvider } from '../../../common/languages.js';
16
import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';
17
import { ITextModel } from '../../../common/model.js';
18
import { ILanguageFeaturesService } from '../../../common/services/languageFeatures.js';
19
import { Choice } from './snippetParser.js';
20
import { showSimpleSuggestions } from '../../suggest/browser/suggest.js';
21
import { OvertypingCapturer } from '../../suggest/browser/suggestOvertypingCapturer.js';
22
import { localize } from '../../../../nls.js';
23
import { ContextKeyExpr, IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
24
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
25
import { ILogService } from '../../../../platform/log/common/log.js';
26
import { ISnippetEdit, SnippetSession } from './snippetSession.js';
27
import { TextModelEditSource } from '../../../common/textModelEditSource.js';
28
import { IObservable, observableValue } from '../../../../base/common/observable.js';
29
30
export interface ISnippetInsertOptions {
31
overwriteBefore: number;
32
overwriteAfter: number;
33
adjustWhitespace: boolean;
34
undoStopBefore: boolean;
35
undoStopAfter: boolean;
36
clipboardText: string | undefined;
37
overtypingCapturer: OvertypingCapturer | undefined;
38
reason?: TextModelEditSource;
39
}
40
41
const _defaultOptions: ISnippetInsertOptions = {
42
overwriteBefore: 0,
43
overwriteAfter: 0,
44
undoStopBefore: true,
45
undoStopAfter: true,
46
adjustWhitespace: true,
47
clipboardText: undefined,
48
overtypingCapturer: undefined
49
};
50
51
export class SnippetController2 implements IEditorContribution {
52
53
public static readonly ID = 'snippetController2';
54
55
static get(editor: ICodeEditor): SnippetController2 | null {
56
return editor.getContribution<SnippetController2>(SnippetController2.ID);
57
}
58
59
static readonly InSnippetMode = new RawContextKey('inSnippetMode', false, localize('inSnippetMode', "Whether the editor in current in snippet mode"));
60
static readonly HasNextTabstop = new RawContextKey('hasNextTabstop', false, localize('hasNextTabstop', "Whether there is a next tab stop when in snippet mode"));
61
static readonly HasPrevTabstop = new RawContextKey('hasPrevTabstop', false, localize('hasPrevTabstop', "Whether there is a previous tab stop when in snippet mode"));
62
63
private readonly _inSnippet: IContextKey<boolean>;
64
private readonly _inSnippetObservable = observableValue(this, false);
65
private readonly _hasNextTabstop: IContextKey<boolean>;
66
private readonly _hasPrevTabstop: IContextKey<boolean>;
67
68
private _session?: SnippetSession;
69
private readonly _snippetListener = new DisposableStore();
70
private _modelVersionId: number = -1;
71
private _currentChoice?: Choice;
72
73
private _choiceCompletions?: { provider: CompletionItemProvider; enable(): void; disable(): void };
74
75
constructor(
76
private readonly _editor: ICodeEditor,
77
@ILogService private readonly _logService: ILogService,
78
@ILanguageFeaturesService private readonly _languageFeaturesService: ILanguageFeaturesService,
79
@IContextKeyService contextKeyService: IContextKeyService,
80
@ILanguageConfigurationService private readonly _languageConfigurationService: ILanguageConfigurationService,
81
) {
82
this._inSnippet = SnippetController2.InSnippetMode.bindTo(contextKeyService);
83
this._hasNextTabstop = SnippetController2.HasNextTabstop.bindTo(contextKeyService);
84
this._hasPrevTabstop = SnippetController2.HasPrevTabstop.bindTo(contextKeyService);
85
}
86
87
dispose(): void {
88
this._inSnippet.reset();
89
this._inSnippetObservable.set(false, undefined);
90
this._hasPrevTabstop.reset();
91
this._hasNextTabstop.reset();
92
this._session?.dispose();
93
this._snippetListener.dispose();
94
}
95
96
apply(edits: ISnippetEdit[], opts?: Partial<ISnippetInsertOptions>) {
97
try {
98
this._doInsert(edits, typeof opts === 'undefined' ? _defaultOptions : { ..._defaultOptions, ...opts });
99
100
} catch (e) {
101
this.cancel();
102
this._logService.error(e);
103
this._logService.error('snippet_error');
104
this._logService.error('insert_edits=', edits);
105
this._logService.error('existing_template=', this._session ? this._session._logInfo() : '<no_session>');
106
}
107
}
108
109
insert(
110
template: string,
111
opts?: Partial<ISnippetInsertOptions>
112
): void {
113
// this is here to find out more about the yet-not-understood
114
// error that sometimes happens when we fail to inserted a nested
115
// snippet
116
try {
117
this._doInsert(template, typeof opts === 'undefined' ? _defaultOptions : { ..._defaultOptions, ...opts });
118
119
} catch (e) {
120
this.cancel();
121
this._logService.error(e);
122
this._logService.error('snippet_error');
123
this._logService.error('insert_template=', template);
124
this._logService.error('existing_template=', this._session ? this._session._logInfo() : '<no_session>');
125
}
126
}
127
128
private _doInsert(
129
template: string | ISnippetEdit[],
130
opts: ISnippetInsertOptions,
131
): void {
132
if (!this._editor.hasModel()) {
133
return;
134
}
135
136
// don't listen while inserting the snippet
137
// as that is the inflight state causing cancelation
138
this._snippetListener.clear();
139
140
if (opts.undoStopBefore) {
141
this._editor.getModel().pushStackElement();
142
}
143
144
// don't merge
145
if (this._session && typeof template !== 'string') {
146
this.cancel();
147
}
148
149
if (!this._session) {
150
this._modelVersionId = this._editor.getModel().getAlternativeVersionId();
151
this._session = new SnippetSession(this._editor, template, opts, this._languageConfigurationService);
152
this._session.insert(opts.reason);
153
} else {
154
assertType(typeof template === 'string');
155
this._session.merge(template, opts);
156
}
157
158
if (opts.undoStopAfter) {
159
this._editor.getModel().pushStackElement();
160
}
161
162
// regster completion item provider when there is any choice element
163
if (this._session?.hasChoice) {
164
const provider: CompletionItemProvider = {
165
_debugDisplayName: 'snippetChoiceCompletions',
166
provideCompletionItems: (model: ITextModel, position: Position) => {
167
if (!this._session || model !== this._editor.getModel() || !Position.equals(this._editor.getPosition(), position)) {
168
return undefined;
169
}
170
const { activeChoice } = this._session;
171
if (!activeChoice || activeChoice.choice.options.length === 0) {
172
return undefined;
173
}
174
175
const word = model.getValueInRange(activeChoice.range);
176
const isAnyOfOptions = Boolean(activeChoice.choice.options.find(o => o.value === word));
177
const suggestions: CompletionItem[] = [];
178
for (let i = 0; i < activeChoice.choice.options.length; i++) {
179
const option = activeChoice.choice.options[i];
180
suggestions.push({
181
kind: CompletionItemKind.Value,
182
label: option.value,
183
insertText: option.value,
184
sortText: 'a'.repeat(i + 1),
185
range: activeChoice.range,
186
filterText: isAnyOfOptions ? `${word}_${option.value}` : undefined,
187
command: { id: 'jumpToNextSnippetPlaceholder', title: localize('next', 'Go to next placeholder...') }
188
});
189
}
190
return { suggestions };
191
}
192
};
193
194
const model = this._editor.getModel();
195
196
let registration: IDisposable | undefined;
197
let isRegistered = false;
198
const disable = () => {
199
registration?.dispose();
200
isRegistered = false;
201
};
202
203
const enable = () => {
204
if (!isRegistered) {
205
registration = this._languageFeaturesService.completionProvider.register({
206
language: model.getLanguageId(),
207
pattern: model.uri.fsPath,
208
scheme: model.uri.scheme,
209
exclusive: true
210
}, provider);
211
this._snippetListener.add(registration);
212
isRegistered = true;
213
}
214
};
215
216
this._choiceCompletions = { provider, enable, disable };
217
}
218
219
this._updateState();
220
221
this._snippetListener.add(this._editor.onDidChangeModelContent(e => e.isFlush && this.cancel()));
222
this._snippetListener.add(this._editor.onDidChangeModel(() => this.cancel()));
223
this._snippetListener.add(this._editor.onDidChangeCursorSelection(() => this._updateState()));
224
}
225
226
private _updateState(): void {
227
if (!this._session || !this._editor.hasModel()) {
228
// canceled in the meanwhile
229
return;
230
}
231
232
if (this._modelVersionId === this._editor.getModel().getAlternativeVersionId()) {
233
// undo until the 'before' state happened
234
// and makes use cancel snippet mode
235
return this.cancel();
236
}
237
238
if (!this._session.hasPlaceholder) {
239
// don't listen for selection changes and don't
240
// update context keys when the snippet is plain text
241
return this.cancel();
242
}
243
244
if (this._session.isAtLastPlaceholder || !this._session.isSelectionWithinPlaceholders()) {
245
this._editor.getModel().pushStackElement();
246
return this.cancel();
247
}
248
249
this._inSnippet.set(true);
250
this._inSnippetObservable.set(true, undefined);
251
this._hasPrevTabstop.set(!this._session.isAtFirstPlaceholder);
252
this._hasNextTabstop.set(!this._session.isAtLastPlaceholder);
253
254
this._handleChoice();
255
}
256
257
private _handleChoice(): void {
258
if (!this._session || !this._editor.hasModel()) {
259
this._currentChoice = undefined;
260
return;
261
}
262
263
const { activeChoice } = this._session;
264
if (!activeChoice || !this._choiceCompletions) {
265
this._choiceCompletions?.disable();
266
this._currentChoice = undefined;
267
return;
268
}
269
270
if (this._currentChoice !== activeChoice.choice) {
271
this._currentChoice = activeChoice.choice;
272
273
this._choiceCompletions.enable();
274
275
// trigger suggest with the special choice completion provider
276
queueMicrotask(() => {
277
showSimpleSuggestions(this._editor, this._choiceCompletions!.provider);
278
});
279
}
280
}
281
282
finish(): void {
283
while (this._inSnippet.get()) {
284
this.next();
285
}
286
}
287
288
cancel(resetSelection: boolean = false): void {
289
this._inSnippet.reset();
290
this._inSnippetObservable.set(false, undefined);
291
this._hasPrevTabstop.reset();
292
this._hasNextTabstop.reset();
293
this._snippetListener.clear();
294
295
this._currentChoice = undefined;
296
297
this._session?.dispose();
298
this._session = undefined;
299
this._modelVersionId = -1;
300
if (resetSelection) {
301
// reset selection to the primary cursor when being asked
302
// for. this happens when explicitly cancelling snippet mode,
303
// e.g. when pressing ESC
304
this._editor.setSelections([this._editor.getSelection()!]);
305
}
306
}
307
308
prev(): void {
309
this._session?.prev();
310
this._updateState();
311
}
312
313
next(): void {
314
this._session?.next();
315
this._updateState();
316
}
317
318
isInSnippet(): boolean {
319
return Boolean(this._inSnippet.get());
320
}
321
322
get isInSnippetObservable(): IObservable<boolean> {
323
return this._inSnippetObservable;
324
}
325
326
getSessionEnclosingRange(): Range | undefined {
327
if (this._session) {
328
return this._session.getEnclosingRange();
329
}
330
return undefined;
331
}
332
}
333
334
335
registerEditorContribution(SnippetController2.ID, SnippetController2, EditorContributionInstantiation.Lazy);
336
337
const CommandCtor = EditorCommand.bindToContribution<SnippetController2>(SnippetController2.get);
338
339
registerEditorCommand(new CommandCtor({
340
id: 'jumpToNextSnippetPlaceholder',
341
precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasNextTabstop),
342
handler: ctrl => ctrl.next(),
343
kbOpts: {
344
weight: KeybindingWeight.EditorContrib + 30,
345
kbExpr: EditorContextKeys.textInputFocus,
346
primary: KeyCode.Tab
347
}
348
}));
349
registerEditorCommand(new CommandCtor({
350
id: 'jumpToPrevSnippetPlaceholder',
351
precondition: ContextKeyExpr.and(SnippetController2.InSnippetMode, SnippetController2.HasPrevTabstop),
352
handler: ctrl => ctrl.prev(),
353
kbOpts: {
354
weight: KeybindingWeight.EditorContrib + 30,
355
kbExpr: EditorContextKeys.textInputFocus,
356
primary: KeyMod.Shift | KeyCode.Tab
357
}
358
}));
359
registerEditorCommand(new CommandCtor({
360
id: 'leaveSnippet',
361
precondition: SnippetController2.InSnippetMode,
362
handler: ctrl => ctrl.cancel(true),
363
kbOpts: {
364
weight: KeybindingWeight.EditorContrib + 30,
365
kbExpr: EditorContextKeys.textInputFocus,
366
primary: KeyCode.Escape,
367
secondary: [KeyMod.Shift | KeyCode.Escape]
368
}
369
}));
370
371
registerEditorCommand(new CommandCtor({
372
id: 'acceptSnippet',
373
precondition: SnippetController2.InSnippetMode,
374
handler: ctrl => ctrl.finish(),
375
// kbOpts: {
376
// weight: KeybindingWeight.EditorContrib + 30,
377
// kbExpr: EditorContextKeys.textFocus,
378
// primary: KeyCode.Enter,
379
// }
380
}));
381
382