|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 | 6 | import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
|
7 |
| -import { DisposableStore } from 'vs/base/common/lifecycle'; |
| 7 | +import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle'; |
8 | 8 | import { assertType } from 'vs/base/common/types';
|
9 | 9 | import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
|
10 | 10 | import { EditorCommand, EditorContributionInstantiation, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
|
@@ -66,7 +66,7 @@ export class SnippetController2 implements IEditorContribution {
|
66 | 66 | private _modelVersionId: number = -1;
|
67 | 67 | private _currentChoice?: Choice;
|
68 | 68 |
|
69 |
| - private _choiceCompletionItemProvider?: CompletionItemProvider; |
| 69 | + private _choiceCompletions?: { provider: CompletionItemProvider; enable(): void; disable(): void }; |
70 | 70 |
|
71 | 71 | constructor(
|
72 | 72 | private readonly _editor: ICodeEditor,
|
@@ -156,7 +156,7 @@ export class SnippetController2 implements IEditorContribution {
|
156 | 156 |
|
157 | 157 | // regster completion item provider when there is any choice element
|
158 | 158 | if (this._session?.hasChoice) {
|
159 |
| - this._choiceCompletionItemProvider = { |
| 159 | + const provider = { |
160 | 160 | provideCompletionItems: (model: ITextModel, position: Position) => {
|
161 | 161 | if (!this._session || model !== this._editor.getModel() || !Position.equals(this._editor.getPosition(), position)) {
|
162 | 162 | return undefined;
|
@@ -185,14 +185,29 @@ export class SnippetController2 implements IEditorContribution {
|
185 | 185 | }
|
186 | 186 | };
|
187 | 187 |
|
188 |
| - const registration = this._languageFeaturesService.completionProvider.register({ |
189 |
| - language: this._editor.getModel().getLanguageId(), |
190 |
| - pattern: this._editor.getModel().uri.fsPath, |
191 |
| - scheme: this._editor.getModel().uri.scheme, |
192 |
| - exclusive: true |
193 |
| - }, this._choiceCompletionItemProvider); |
| 188 | + const model = this._editor.getModel(); |
| 189 | + |
| 190 | + let registration: IDisposable = Disposable.None; |
| 191 | + let isRegistered = false; |
| 192 | + const disable = () => { |
| 193 | + registration.dispose(); |
| 194 | + isRegistered = false; |
| 195 | + }; |
| 196 | + |
| 197 | + const enable = () => { |
| 198 | + if (!isRegistered) { |
| 199 | + registration = this._languageFeaturesService.completionProvider.register({ |
| 200 | + language: model.getLanguageId(), |
| 201 | + pattern: model.uri.fsPath, |
| 202 | + scheme: model.uri.scheme, |
| 203 | + exclusive: true |
| 204 | + }, provider); |
| 205 | + isRegistered = true; |
| 206 | + } |
| 207 | + }; |
194 | 208 |
|
195 | 209 | this._snippetListener.add(registration);
|
| 210 | + this._choiceCompletions = { provider, enable, disable }; |
196 | 211 | }
|
197 | 212 |
|
198 | 213 | this._updateState();
|
@@ -239,17 +254,20 @@ export class SnippetController2 implements IEditorContribution {
|
239 | 254 | }
|
240 | 255 |
|
241 | 256 | const { activeChoice } = this._session;
|
242 |
| - if (!activeChoice || !this._choiceCompletionItemProvider) { |
| 257 | + if (!activeChoice || !this._choiceCompletions) { |
| 258 | + this._choiceCompletions?.disable(); |
243 | 259 | this._currentChoice = undefined;
|
244 | 260 | return;
|
245 | 261 | }
|
246 | 262 |
|
247 | 263 | if (this._currentChoice !== activeChoice.choice) {
|
248 | 264 | this._currentChoice = activeChoice.choice;
|
249 | 265 |
|
| 266 | + this._choiceCompletions.enable(); |
| 267 | + |
250 | 268 | // trigger suggest with the special choice completion provider
|
251 | 269 | queueMicrotask(() => {
|
252 |
| - showSimpleSuggestions(this._editor, this._choiceCompletionItemProvider!); |
| 270 | + showSimpleSuggestions(this._editor, this._choiceCompletions!.provider); |
253 | 271 | });
|
254 | 272 | }
|
255 | 273 | }
|
|
0 commit comments