|
1 | | -import * as monaco from 'monaco-editor' |
| 1 | +import { setSnippets } from 'vscode/service-override/snippets' |
2 | 2 | import snippetLoader from '../extensions/snippetLoader' |
3 | 3 |
|
4 | | -class SimpleSnippetService implements monaco.extra.ISnippetsService { |
5 | | - _serviceBrand: undefined |
6 | | - |
7 | | - isEnabled (snippet: monaco.extra.Snippet): boolean { |
8 | | - return snippet.snippetIdentifier != null || true |
9 | | - } |
10 | | - |
11 | | - updateEnablement (): void { |
12 | | - throw new Error('Method not implemented.') |
13 | | - } |
14 | | - |
15 | | - async getSnippets (languageId: string, opt?: monaco.extra.ISnippetGetOptions): Promise<monaco.extra.Snippet[]> { |
16 | | - const result: monaco.extra.Snippet[] = [] |
17 | | - const loader = snippetLoader[languageId] |
18 | | - if (loader != null) { |
19 | | - const rawSnippets = await loader() |
20 | | - const snippets = Object.entries(rawSnippets) |
21 | | - .flatMap(([name, value]) => monaco.extra.parseSnippet(name, value, [languageId], 'User Snippet', monaco.extra.SnippetSource.Extension)) |
22 | | - monaco.extra.snippetScopeSelect(snippets, languageId, result) |
23 | | - } |
24 | | - return this._filterSnippets(result, opt) |
25 | | - } |
26 | | - |
27 | | - getSnippetsSync (): monaco.extra.Snippet[] { |
28 | | - throw new Error('Method not implemented.') |
29 | | - } |
30 | | - |
31 | | - private _filterSnippets (snippets: monaco.extra.Snippet[], opts?: monaco.extra.ISnippetGetOptions): monaco.extra.Snippet[] { |
32 | | - return snippets.filter(snippet => { |
33 | | - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
34 | | - return (snippet.prefix != null || (opts?.includeNoPrefixSnippets ?? false)) && // prefix or no-prefix wanted |
35 | | - (this.isEnabled(snippet) || (opts?.includeDisabledSnippets ?? false)) // enabled or disabled wanted |
36 | | - }) |
37 | | - } |
38 | | -} |
39 | | - |
40 | | -setTimeout(() => { |
41 | | - // In a timeout so the service can be overriden |
42 | | - const languageService = monaco.extra.StandaloneServices.get(monaco.languages.ILanguageService) |
43 | | - const languageConfigurationService = monaco.extra.StandaloneServices.get(monaco.languages.ILanguageConfigurationService) |
44 | | - monaco.extra.setSnippetSuggestSupport(new monaco.extra.SnippetCompletionProvider(languageService, new SimpleSnippetService(), languageConfigurationService)) |
45 | | -}) |
| 4 | +setSnippets(Object.keys(snippetLoader).map(language => ({ |
| 5 | + language, |
| 6 | + path: `${language}-snippets.json` |
| 7 | +})), async (extensionPoint) => JSON.stringify(await snippetLoader[extensionPoint.language]!())) |
0 commit comments