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