@theia/monaco 1.53.0-next.55 → 1.53.0-next.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +100 -100
  2. package/data/monaco-nls.json +1379 -1379
  3. package/data/monaco-themes/vscode/dark_plus.json +201 -201
  4. package/data/monaco-themes/vscode/dark_theia.json +5 -5
  5. package/data/monaco-themes/vscode/dark_vs.json +393 -393
  6. package/data/monaco-themes/vscode/hc_black.json +457 -457
  7. package/data/monaco-themes/vscode/hc_light.json +590 -590
  8. package/data/monaco-themes/vscode/hc_theia.json +5 -5
  9. package/data/monaco-themes/vscode/hc_theia_light.json +5 -5
  10. package/data/monaco-themes/vscode/light_plus.json +202 -202
  11. package/data/monaco-themes/vscode/light_theia.json +10 -10
  12. package/data/monaco-themes/vscode/light_vs.json +421 -421
  13. package/lib/browser/monaco-editor-zone-widget.js +1 -1
  14. package/lib/browser/monaco-frontend-application-contribution.js +25 -25
  15. package/lib/browser/simple-monaco-editor.d.ts +3 -1
  16. package/lib/browser/simple-monaco-editor.d.ts.map +1 -1
  17. package/lib/browser/simple-monaco-editor.js +9 -2
  18. package/lib/browser/simple-monaco-editor.js.map +1 -1
  19. package/package.json +8 -8
  20. package/src/browser/index.ts +17 -17
  21. package/src/browser/markdown-renderer/monaco-markdown-renderer.ts +109 -109
  22. package/src/browser/monaco-bulk-edit-service.ts +64 -64
  23. package/src/browser/monaco-color-registry.ts +73 -73
  24. package/src/browser/monaco-command-registry.ts +85 -85
  25. package/src/browser/monaco-command-service.ts +90 -90
  26. package/src/browser/monaco-command.ts +303 -303
  27. package/src/browser/monaco-context-key-service.ts +144 -144
  28. package/src/browser/monaco-context-menu.ts +112 -112
  29. package/src/browser/monaco-diff-editor.ts +141 -141
  30. package/src/browser/monaco-diff-navigator-factory.ts +39 -39
  31. package/src/browser/monaco-editor-model.ts +693 -693
  32. package/src/browser/monaco-editor-peek-view-widget.ts +233 -233
  33. package/src/browser/monaco-editor-provider.ts +455 -455
  34. package/src/browser/monaco-editor-service.ts +152 -152
  35. package/src/browser/monaco-editor-zone-widget.ts +250 -250
  36. package/src/browser/monaco-editor.ts +733 -733
  37. package/src/browser/monaco-formatting-conflicts.ts +116 -116
  38. package/src/browser/monaco-frontend-application-contribution.ts +182 -182
  39. package/src/browser/monaco-frontend-module.ts +319 -319
  40. package/src/browser/monaco-gotoline-quick-access.ts +47 -47
  41. package/src/browser/monaco-gotosymbol-quick-access.ts +53 -53
  42. package/src/browser/monaco-icon-registry.ts +49 -49
  43. package/src/browser/monaco-indexed-db.ts +130 -130
  44. package/src/browser/monaco-init.ts +134 -134
  45. package/src/browser/monaco-keybinding.ts +111 -111
  46. package/src/browser/monaco-keycode-map.ts +171 -171
  47. package/src/browser/monaco-languages.ts +177 -177
  48. package/src/browser/monaco-marker-collection.ts +83 -83
  49. package/src/browser/monaco-menu.ts +147 -147
  50. package/src/browser/monaco-mime-service.ts +71 -71
  51. package/src/browser/monaco-outline-contribution.ts +404 -404
  52. package/src/browser/monaco-outline-decorator.ts +66 -66
  53. package/src/browser/monaco-quick-access-registry.ts +112 -112
  54. package/src/browser/monaco-quick-input-service.ts +701 -701
  55. package/src/browser/monaco-resolved-keybinding.ts +162 -162
  56. package/src/browser/monaco-snippet-suggest-provider.ts +306 -306
  57. package/src/browser/monaco-standalone-theme-service.ts +51 -51
  58. package/src/browser/monaco-status-bar-contribution.ts +110 -110
  59. package/src/browser/monaco-text-model-service.ts +157 -157
  60. package/src/browser/monaco-theming-service.ts +204 -204
  61. package/src/browser/monaco-to-protocol-converter.ts +82 -82
  62. package/src/browser/monaco-undo-redo-handler.ts +64 -64
  63. package/src/browser/monaco-workspace.ts +416 -416
  64. package/src/browser/protocol-to-monaco-converter.ts +158 -158
  65. package/src/browser/simple-monaco-editor.ts +228 -216
  66. package/src/browser/style/index.css +266 -266
  67. package/src/browser/textmate/index.ts +20 -20
  68. package/src/browser/textmate/monaco-textmate-frontend-bindings.ts +90 -90
  69. package/src/browser/textmate/monaco-textmate-service.ts +187 -187
  70. package/src/browser/textmate/monaco-theme-registry.ts +176 -176
  71. package/src/browser/textmate/monaco-theme-types.ts +37 -37
  72. package/src/browser/textmate/textmate-contribution.ts +29 -29
  73. package/src/browser/textmate/textmate-registry.ts +129 -129
  74. package/src/browser/textmate/textmate-snippet-completion-provider.ts +73 -73
  75. package/src/browser/textmate/textmate-tokenizer.ts +84 -84
  76. package/src/browser/workspace-symbol-command.ts +196 -196
  77. package/src/package.spec.ts +28 -28
@@ -1,306 +1,306 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2019 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
- /*---------------------------------------------------------------------------------------------
17
- * Copyright (c) Microsoft Corporation. All rights reserved.
18
- * Licensed under the MIT License. See License.txt in the project root for license information.
19
- *--------------------------------------------------------------------------------------------*/
20
-
21
- import * as jsoncparser from 'jsonc-parser';
22
- import { injectable, inject } from '@theia/core/shared/inversify';
23
- import URI from '@theia/core/lib/common/uri';
24
- import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
25
- import { FileService } from '@theia/filesystem/lib/browser/file-service';
26
- import { FileOperationError } from '@theia/filesystem/lib/common/files';
27
- import * as monaco from '@theia/monaco-editor-core';
28
- import { SnippetParser } from '@theia/monaco-editor-core/esm/vs/editor/contrib/snippet/browser/snippetParser';
29
- import { isObject } from '@theia/core/lib/common';
30
-
31
- @injectable()
32
- export class MonacoSnippetSuggestProvider implements monaco.languages.CompletionItemProvider {
33
-
34
- private static readonly _maxPrefix = 10000;
35
-
36
- @inject(FileService)
37
- protected readonly fileService: FileService;
38
-
39
- protected readonly snippets = new Map<string, Snippet[]>();
40
- protected readonly pendingSnippets = new Map<string, Promise<void>[]>();
41
-
42
- async provideCompletionItems(model: monaco.editor.ITextModel, position: monaco.Position,
43
- context: monaco.languages.CompletionContext): Promise<monaco.languages.CompletionList | undefined> {
44
-
45
- // copied and modified from https://github.com/microsoft/vscode/blob/master/src/vs/workbench/contrib/snippets/browser/snippetCompletionProvider.ts
46
- if (position.column >= MonacoSnippetSuggestProvider._maxPrefix) {
47
- return undefined;
48
- }
49
-
50
- if (context.triggerKind === monaco.languages.CompletionTriggerKind.TriggerCharacter && context.triggerCharacter === ' ') {
51
- // no snippets when suggestions have been triggered by space
52
- return undefined;
53
- }
54
-
55
- const languageId = model.getLanguageId(); // TODO: look up a language id at the position
56
- await this.loadSnippets(languageId);
57
- const snippetsForLanguage = this.snippets.get(languageId) || [];
58
-
59
- const pos = { lineNumber: position.lineNumber, column: 1 };
60
- const lineOffsets: number[] = [];
61
- const linePrefixLow = model.getLineContent(position.lineNumber).substring(0, position.column - 1).toLowerCase();
62
- const endsInWhitespace = linePrefixLow.match(/\s$/);
63
-
64
- while (pos.column < position.column) {
65
- const word = model.getWordAtPosition(pos);
66
- if (word) {
67
- // at a word
68
- lineOffsets.push(word.startColumn - 1);
69
- pos.column = word.endColumn + 1;
70
- if (word.endColumn - 1 < linePrefixLow.length && !/\s/.test(linePrefixLow[word.endColumn - 1])) {
71
- lineOffsets.push(word.endColumn - 1);
72
- }
73
- } else if (!/\s/.test(linePrefixLow[pos.column - 1])) {
74
- // at a none-whitespace character
75
- lineOffsets.push(pos.column - 1);
76
- pos.column += 1;
77
- } else {
78
- // always advance!
79
- pos.column += 1;
80
- }
81
- }
82
-
83
- const availableSnippets = new Set<Snippet>();
84
- snippetsForLanguage.forEach(availableSnippets.add, availableSnippets);
85
- const suggestions: MonacoSnippetSuggestion[] = [];
86
- for (const start of lineOffsets) {
87
- availableSnippets.forEach(snippet => {
88
- if (this.isPatternInWord(linePrefixLow, start, linePrefixLow.length, snippet.prefix.toLowerCase(), 0, snippet.prefix.length)) {
89
- suggestions.push(new MonacoSnippetSuggestion(snippet, monaco.Range.fromPositions(position.delta(0, -(linePrefixLow.length - start)), position)));
90
- availableSnippets.delete(snippet);
91
- }
92
- });
93
- }
94
- if (endsInWhitespace || lineOffsets.length === 0) {
95
- // add remaining snippets when the current prefix ends in whitespace or when no
96
- // interesting positions have been found
97
- availableSnippets.forEach(snippet => {
98
- suggestions.push(new MonacoSnippetSuggestion(snippet, monaco.Range.fromPositions(position)));
99
- });
100
- }
101
-
102
- // disambiguate suggestions with same labels
103
- suggestions.sort(MonacoSnippetSuggestion.compareByLabel);
104
- return { suggestions };
105
- }
106
-
107
- resolveCompletionItem?(item: monaco.languages.CompletionItem, token: monaco.CancellationToken): monaco.languages.CompletionItem {
108
- return item instanceof MonacoSnippetSuggestion ? item.resolve() : item;
109
- }
110
-
111
- protected async loadSnippets(scope: string): Promise<void> {
112
- const pending: Promise<void>[] = [];
113
- pending.push(...(this.pendingSnippets.get(scope) || []));
114
- pending.push(...(this.pendingSnippets.get('*') || []));
115
- if (pending.length) {
116
- await Promise.all(pending);
117
- }
118
- }
119
-
120
- fromURI(uri: string | URI, options: SnippetLoadOptions): Disposable {
121
- const toDispose = new DisposableCollection(Disposable.create(() => { /* mark as not disposed */ }));
122
- const pending = this.loadURI(uri, options, toDispose);
123
- const { language } = options;
124
- const scopes = Array.isArray(language) ? language : !!language ? [language] : ['*'];
125
- for (const scope of scopes) {
126
- const pendingSnippets = this.pendingSnippets.get(scope) || [];
127
- pendingSnippets.push(pending);
128
- this.pendingSnippets.set(scope, pendingSnippets);
129
- toDispose.push(Disposable.create(() => {
130
- const index = pendingSnippets.indexOf(pending);
131
- if (index !== -1) {
132
- pendingSnippets.splice(index, 1);
133
- }
134
- }));
135
- }
136
- return toDispose;
137
- }
138
-
139
- /**
140
- * should NOT throw to prevent load errors on suggest
141
- */
142
- protected async loadURI(uri: string | URI, options: SnippetLoadOptions, toDispose: DisposableCollection): Promise<void> {
143
- try {
144
- const resource = typeof uri === 'string' ? new URI(uri) : uri;
145
- const { value } = await this.fileService.read(resource);
146
- if (toDispose.disposed) {
147
- return;
148
- }
149
- const snippets = value && jsoncparser.parse(value, undefined, { disallowComments: false });
150
- toDispose.push(this.fromJSON(snippets, options));
151
- } catch (e) {
152
- if (!(e instanceof FileOperationError)) {
153
- console.error(e);
154
- }
155
- }
156
- }
157
-
158
- fromJSON(snippets: JsonSerializedSnippets | undefined, { language, source }: SnippetLoadOptions): Disposable {
159
- const toDispose = new DisposableCollection();
160
- this.parseSnippets(snippets, (name, snippet) => {
161
- const { isFileTemplate, prefix, body, description } = snippet;
162
- const parsedBody = Array.isArray(body) ? body.join('\n') : body;
163
- const parsedPrefixes = !prefix ? [''] : Array.isArray(prefix) ? prefix : [prefix];
164
-
165
- if (typeof parsedBody !== 'string') {
166
- return;
167
- }
168
- const scopes: string[] = [];
169
- if (language) {
170
- if (Array.isArray(language)) {
171
- scopes.push(...language);
172
- } else {
173
- scopes.push(language);
174
- }
175
- } else if (typeof snippet.scope === 'string') {
176
- for (const rawScope of snippet.scope.split(',')) {
177
- const scope = rawScope.trim();
178
- if (scope) {
179
- scopes.push(scope);
180
- }
181
- }
182
- }
183
- parsedPrefixes.forEach(parsedPrefix => toDispose.push(this.push({
184
- isFileTemplate: Boolean(isFileTemplate),
185
- scopes,
186
- name,
187
- prefix: parsedPrefix,
188
- description,
189
- body: parsedBody,
190
- source
191
- })));
192
- });
193
- return toDispose;
194
- }
195
- protected parseSnippets(snippets: JsonSerializedSnippets | undefined, accept: (name: string, snippet: JsonSerializedSnippet) => void): void {
196
- for (const [name, scopeOrTemplate] of Object.entries(snippets ?? {})) {
197
- if (JsonSerializedSnippet.is(scopeOrTemplate)) {
198
- accept(name, scopeOrTemplate);
199
- } else {
200
- // eslint-disable-next-line @typescript-eslint/no-shadow
201
- for (const [name, template] of Object.entries(scopeOrTemplate)) {
202
- accept(name, template);
203
- }
204
- }
205
- }
206
- }
207
-
208
- push(...snippets: Snippet[]): Disposable {
209
- const toDispose = new DisposableCollection();
210
- for (const snippet of snippets) {
211
- for (const scope of snippet.scopes) {
212
- const languageSnippets = this.snippets.get(scope) || [];
213
- languageSnippets.push(snippet);
214
- this.snippets.set(scope, languageSnippets);
215
- toDispose.push(Disposable.create(() => {
216
- const index = languageSnippets.indexOf(snippet);
217
- if (index !== -1) {
218
- languageSnippets.splice(index, 1);
219
- }
220
- }));
221
- }
222
- }
223
- return toDispose;
224
- }
225
-
226
- protected isPatternInWord(patternLow: string, patternPos: number, patternLen: number, wordLow: string, wordPos: number, wordLen: number): boolean {
227
- while (patternPos < patternLen && wordPos < wordLen) {
228
- if (patternLow[patternPos] === wordLow[wordPos]) {
229
- patternPos += 1;
230
- }
231
- wordPos += 1;
232
- }
233
- return patternPos === patternLen; // pattern must be exhausted
234
- }
235
-
236
- }
237
-
238
- export interface SnippetLoadOptions {
239
- language?: string | string[]
240
- source: string
241
- }
242
-
243
- export interface JsonSerializedSnippets {
244
- [name: string]: JsonSerializedSnippet | { [name: string]: JsonSerializedSnippet };
245
- }
246
- export interface JsonSerializedSnippet {
247
- isFileTemplate?: boolean;
248
- body: string | string[];
249
- scope?: string;
250
- prefix?: string | string[];
251
- description: string;
252
- }
253
- export namespace JsonSerializedSnippet {
254
- export function is(obj: unknown): obj is JsonSerializedSnippet {
255
- return isObject(obj) && 'body' in obj;
256
- }
257
- }
258
-
259
- export interface Snippet {
260
- readonly isFileTemplate: boolean
261
- readonly scopes: string[]
262
- readonly name: string
263
- readonly prefix: string
264
- readonly description: string
265
- readonly body: string
266
- readonly source: string
267
- }
268
-
269
- export class MonacoSnippetSuggestion implements monaco.languages.CompletionItem {
270
-
271
- readonly label: string;
272
- readonly detail: string;
273
- readonly sortText: string;
274
- readonly noAutoAccept = true;
275
- readonly kind = monaco.languages.CompletionItemKind.Snippet;
276
- readonly insertTextRules = monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet;
277
-
278
- insertText: string;
279
- documentation?: monaco.IMarkdownString;
280
-
281
- constructor(
282
- protected readonly snippet: Snippet,
283
- readonly range: monaco.Range
284
- ) {
285
- this.label = snippet.prefix;
286
- this.detail = `${snippet.description || snippet.name} (${snippet.source})`;
287
- this.insertText = snippet.body;
288
- this.sortText = `z-${snippet.prefix}`;
289
- this.range = range;
290
- }
291
-
292
- protected resolved = false;
293
- resolve(): MonacoSnippetSuggestion {
294
- if (!this.resolved) {
295
- const codeSnippet = new SnippetParser().parse(this.snippet.body).toString();
296
- this.documentation = { value: '```\n' + codeSnippet + '```' };
297
- this.resolved = true;
298
- }
299
- return this;
300
- }
301
-
302
- static compareByLabel(a: MonacoSnippetSuggestion, b: MonacoSnippetSuggestion): number {
303
- return a.label > b.label ? 1 : a.label < b.label ? -1 : 0;
304
- }
305
-
306
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2019 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+ /*---------------------------------------------------------------------------------------------
17
+ * Copyright (c) Microsoft Corporation. All rights reserved.
18
+ * Licensed under the MIT License. See License.txt in the project root for license information.
19
+ *--------------------------------------------------------------------------------------------*/
20
+
21
+ import * as jsoncparser from 'jsonc-parser';
22
+ import { injectable, inject } from '@theia/core/shared/inversify';
23
+ import URI from '@theia/core/lib/common/uri';
24
+ import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
25
+ import { FileService } from '@theia/filesystem/lib/browser/file-service';
26
+ import { FileOperationError } from '@theia/filesystem/lib/common/files';
27
+ import * as monaco from '@theia/monaco-editor-core';
28
+ import { SnippetParser } from '@theia/monaco-editor-core/esm/vs/editor/contrib/snippet/browser/snippetParser';
29
+ import { isObject } from '@theia/core/lib/common';
30
+
31
+ @injectable()
32
+ export class MonacoSnippetSuggestProvider implements monaco.languages.CompletionItemProvider {
33
+
34
+ private static readonly _maxPrefix = 10000;
35
+
36
+ @inject(FileService)
37
+ protected readonly fileService: FileService;
38
+
39
+ protected readonly snippets = new Map<string, Snippet[]>();
40
+ protected readonly pendingSnippets = new Map<string, Promise<void>[]>();
41
+
42
+ async provideCompletionItems(model: monaco.editor.ITextModel, position: monaco.Position,
43
+ context: monaco.languages.CompletionContext): Promise<monaco.languages.CompletionList | undefined> {
44
+
45
+ // copied and modified from https://github.com/microsoft/vscode/blob/master/src/vs/workbench/contrib/snippets/browser/snippetCompletionProvider.ts
46
+ if (position.column >= MonacoSnippetSuggestProvider._maxPrefix) {
47
+ return undefined;
48
+ }
49
+
50
+ if (context.triggerKind === monaco.languages.CompletionTriggerKind.TriggerCharacter && context.triggerCharacter === ' ') {
51
+ // no snippets when suggestions have been triggered by space
52
+ return undefined;
53
+ }
54
+
55
+ const languageId = model.getLanguageId(); // TODO: look up a language id at the position
56
+ await this.loadSnippets(languageId);
57
+ const snippetsForLanguage = this.snippets.get(languageId) || [];
58
+
59
+ const pos = { lineNumber: position.lineNumber, column: 1 };
60
+ const lineOffsets: number[] = [];
61
+ const linePrefixLow = model.getLineContent(position.lineNumber).substring(0, position.column - 1).toLowerCase();
62
+ const endsInWhitespace = linePrefixLow.match(/\s$/);
63
+
64
+ while (pos.column < position.column) {
65
+ const word = model.getWordAtPosition(pos);
66
+ if (word) {
67
+ // at a word
68
+ lineOffsets.push(word.startColumn - 1);
69
+ pos.column = word.endColumn + 1;
70
+ if (word.endColumn - 1 < linePrefixLow.length && !/\s/.test(linePrefixLow[word.endColumn - 1])) {
71
+ lineOffsets.push(word.endColumn - 1);
72
+ }
73
+ } else if (!/\s/.test(linePrefixLow[pos.column - 1])) {
74
+ // at a none-whitespace character
75
+ lineOffsets.push(pos.column - 1);
76
+ pos.column += 1;
77
+ } else {
78
+ // always advance!
79
+ pos.column += 1;
80
+ }
81
+ }
82
+
83
+ const availableSnippets = new Set<Snippet>();
84
+ snippetsForLanguage.forEach(availableSnippets.add, availableSnippets);
85
+ const suggestions: MonacoSnippetSuggestion[] = [];
86
+ for (const start of lineOffsets) {
87
+ availableSnippets.forEach(snippet => {
88
+ if (this.isPatternInWord(linePrefixLow, start, linePrefixLow.length, snippet.prefix.toLowerCase(), 0, snippet.prefix.length)) {
89
+ suggestions.push(new MonacoSnippetSuggestion(snippet, monaco.Range.fromPositions(position.delta(0, -(linePrefixLow.length - start)), position)));
90
+ availableSnippets.delete(snippet);
91
+ }
92
+ });
93
+ }
94
+ if (endsInWhitespace || lineOffsets.length === 0) {
95
+ // add remaining snippets when the current prefix ends in whitespace or when no
96
+ // interesting positions have been found
97
+ availableSnippets.forEach(snippet => {
98
+ suggestions.push(new MonacoSnippetSuggestion(snippet, monaco.Range.fromPositions(position)));
99
+ });
100
+ }
101
+
102
+ // disambiguate suggestions with same labels
103
+ suggestions.sort(MonacoSnippetSuggestion.compareByLabel);
104
+ return { suggestions };
105
+ }
106
+
107
+ resolveCompletionItem?(item: monaco.languages.CompletionItem, token: monaco.CancellationToken): monaco.languages.CompletionItem {
108
+ return item instanceof MonacoSnippetSuggestion ? item.resolve() : item;
109
+ }
110
+
111
+ protected async loadSnippets(scope: string): Promise<void> {
112
+ const pending: Promise<void>[] = [];
113
+ pending.push(...(this.pendingSnippets.get(scope) || []));
114
+ pending.push(...(this.pendingSnippets.get('*') || []));
115
+ if (pending.length) {
116
+ await Promise.all(pending);
117
+ }
118
+ }
119
+
120
+ fromURI(uri: string | URI, options: SnippetLoadOptions): Disposable {
121
+ const toDispose = new DisposableCollection(Disposable.create(() => { /* mark as not disposed */ }));
122
+ const pending = this.loadURI(uri, options, toDispose);
123
+ const { language } = options;
124
+ const scopes = Array.isArray(language) ? language : !!language ? [language] : ['*'];
125
+ for (const scope of scopes) {
126
+ const pendingSnippets = this.pendingSnippets.get(scope) || [];
127
+ pendingSnippets.push(pending);
128
+ this.pendingSnippets.set(scope, pendingSnippets);
129
+ toDispose.push(Disposable.create(() => {
130
+ const index = pendingSnippets.indexOf(pending);
131
+ if (index !== -1) {
132
+ pendingSnippets.splice(index, 1);
133
+ }
134
+ }));
135
+ }
136
+ return toDispose;
137
+ }
138
+
139
+ /**
140
+ * should NOT throw to prevent load errors on suggest
141
+ */
142
+ protected async loadURI(uri: string | URI, options: SnippetLoadOptions, toDispose: DisposableCollection): Promise<void> {
143
+ try {
144
+ const resource = typeof uri === 'string' ? new URI(uri) : uri;
145
+ const { value } = await this.fileService.read(resource);
146
+ if (toDispose.disposed) {
147
+ return;
148
+ }
149
+ const snippets = value && jsoncparser.parse(value, undefined, { disallowComments: false });
150
+ toDispose.push(this.fromJSON(snippets, options));
151
+ } catch (e) {
152
+ if (!(e instanceof FileOperationError)) {
153
+ console.error(e);
154
+ }
155
+ }
156
+ }
157
+
158
+ fromJSON(snippets: JsonSerializedSnippets | undefined, { language, source }: SnippetLoadOptions): Disposable {
159
+ const toDispose = new DisposableCollection();
160
+ this.parseSnippets(snippets, (name, snippet) => {
161
+ const { isFileTemplate, prefix, body, description } = snippet;
162
+ const parsedBody = Array.isArray(body) ? body.join('\n') : body;
163
+ const parsedPrefixes = !prefix ? [''] : Array.isArray(prefix) ? prefix : [prefix];
164
+
165
+ if (typeof parsedBody !== 'string') {
166
+ return;
167
+ }
168
+ const scopes: string[] = [];
169
+ if (language) {
170
+ if (Array.isArray(language)) {
171
+ scopes.push(...language);
172
+ } else {
173
+ scopes.push(language);
174
+ }
175
+ } else if (typeof snippet.scope === 'string') {
176
+ for (const rawScope of snippet.scope.split(',')) {
177
+ const scope = rawScope.trim();
178
+ if (scope) {
179
+ scopes.push(scope);
180
+ }
181
+ }
182
+ }
183
+ parsedPrefixes.forEach(parsedPrefix => toDispose.push(this.push({
184
+ isFileTemplate: Boolean(isFileTemplate),
185
+ scopes,
186
+ name,
187
+ prefix: parsedPrefix,
188
+ description,
189
+ body: parsedBody,
190
+ source
191
+ })));
192
+ });
193
+ return toDispose;
194
+ }
195
+ protected parseSnippets(snippets: JsonSerializedSnippets | undefined, accept: (name: string, snippet: JsonSerializedSnippet) => void): void {
196
+ for (const [name, scopeOrTemplate] of Object.entries(snippets ?? {})) {
197
+ if (JsonSerializedSnippet.is(scopeOrTemplate)) {
198
+ accept(name, scopeOrTemplate);
199
+ } else {
200
+ // eslint-disable-next-line @typescript-eslint/no-shadow
201
+ for (const [name, template] of Object.entries(scopeOrTemplate)) {
202
+ accept(name, template);
203
+ }
204
+ }
205
+ }
206
+ }
207
+
208
+ push(...snippets: Snippet[]): Disposable {
209
+ const toDispose = new DisposableCollection();
210
+ for (const snippet of snippets) {
211
+ for (const scope of snippet.scopes) {
212
+ const languageSnippets = this.snippets.get(scope) || [];
213
+ languageSnippets.push(snippet);
214
+ this.snippets.set(scope, languageSnippets);
215
+ toDispose.push(Disposable.create(() => {
216
+ const index = languageSnippets.indexOf(snippet);
217
+ if (index !== -1) {
218
+ languageSnippets.splice(index, 1);
219
+ }
220
+ }));
221
+ }
222
+ }
223
+ return toDispose;
224
+ }
225
+
226
+ protected isPatternInWord(patternLow: string, patternPos: number, patternLen: number, wordLow: string, wordPos: number, wordLen: number): boolean {
227
+ while (patternPos < patternLen && wordPos < wordLen) {
228
+ if (patternLow[patternPos] === wordLow[wordPos]) {
229
+ patternPos += 1;
230
+ }
231
+ wordPos += 1;
232
+ }
233
+ return patternPos === patternLen; // pattern must be exhausted
234
+ }
235
+
236
+ }
237
+
238
+ export interface SnippetLoadOptions {
239
+ language?: string | string[]
240
+ source: string
241
+ }
242
+
243
+ export interface JsonSerializedSnippets {
244
+ [name: string]: JsonSerializedSnippet | { [name: string]: JsonSerializedSnippet };
245
+ }
246
+ export interface JsonSerializedSnippet {
247
+ isFileTemplate?: boolean;
248
+ body: string | string[];
249
+ scope?: string;
250
+ prefix?: string | string[];
251
+ description: string;
252
+ }
253
+ export namespace JsonSerializedSnippet {
254
+ export function is(obj: unknown): obj is JsonSerializedSnippet {
255
+ return isObject(obj) && 'body' in obj;
256
+ }
257
+ }
258
+
259
+ export interface Snippet {
260
+ readonly isFileTemplate: boolean
261
+ readonly scopes: string[]
262
+ readonly name: string
263
+ readonly prefix: string
264
+ readonly description: string
265
+ readonly body: string
266
+ readonly source: string
267
+ }
268
+
269
+ export class MonacoSnippetSuggestion implements monaco.languages.CompletionItem {
270
+
271
+ readonly label: string;
272
+ readonly detail: string;
273
+ readonly sortText: string;
274
+ readonly noAutoAccept = true;
275
+ readonly kind = monaco.languages.CompletionItemKind.Snippet;
276
+ readonly insertTextRules = monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet;
277
+
278
+ insertText: string;
279
+ documentation?: monaco.IMarkdownString;
280
+
281
+ constructor(
282
+ protected readonly snippet: Snippet,
283
+ readonly range: monaco.Range
284
+ ) {
285
+ this.label = snippet.prefix;
286
+ this.detail = `${snippet.description || snippet.name} (${snippet.source})`;
287
+ this.insertText = snippet.body;
288
+ this.sortText = `z-${snippet.prefix}`;
289
+ this.range = range;
290
+ }
291
+
292
+ protected resolved = false;
293
+ resolve(): MonacoSnippetSuggestion {
294
+ if (!this.resolved) {
295
+ const codeSnippet = new SnippetParser().parse(this.snippet.body).toString();
296
+ this.documentation = { value: '```\n' + codeSnippet + '```' };
297
+ this.resolved = true;
298
+ }
299
+ return this;
300
+ }
301
+
302
+ static compareByLabel(a: MonacoSnippetSuggestion, b: MonacoSnippetSuggestion): number {
303
+ return a.label > b.label ? 1 : a.label < b.label ? -1 : 0;
304
+ }
305
+
306
+ }