|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const fs = require('fs'); |
4 |
| -const path = require('path'); |
5 | 3 | const vscode = require('vscode');
|
6 | 4 |
|
7 |
| -const snippets = require('./snippets'); |
| 5 | +const resolveSnippets = require('./snippets'); |
8 | 6 |
|
9 |
| -const EXTENSION_ID = 'xabikos.JavaScriptSnippets'; |
| 7 | +const EXTENSION_ID = 'snippets-javascript'; |
| 8 | + |
| 9 | +const languageSelector = [ |
| 10 | + 'javascript', // language identifier |
| 11 | + 'typescript', |
| 12 | + 'javascriptreact', |
| 13 | + 'typescriptreact', |
| 14 | + 'html' |
| 15 | +]; |
| 16 | +let registeredSnippetProvider = undefined; |
| 17 | + |
| 18 | +class SnippetItem extends vscode.CompletionItem { |
| 19 | + constructor(snippet) { |
| 20 | + super(snippet.prefix, vscode.CompletionItemKind.Snippet); |
| 21 | + |
| 22 | + this.insertText = new vscode.SnippetString(snippet.body); |
| 23 | + this.detail = snippet.description; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class SnippetProvider { |
| 28 | + constructor(snippets) { |
| 29 | + this.snippets = snippets; |
| 30 | + } |
| 31 | + |
| 32 | + provideCompletionItems() { |
| 33 | + return Object.keys(this.snippets).map( |
| 34 | + name => new SnippetItem(this.snippets[name]) |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
10 | 38 |
|
11 | 39 | /** Activates extension on an emitted event. Invoked only once. */
|
12 | 40 | exports.activate = context => {
|
13 |
| - let configuration = vscode.workspace.getConfiguration('javascript-snippets'); |
| 41 | + let configuration = vscode.workspace.getConfiguration(EXTENSION_ID); |
| 42 | + const semi = configuration.get('semi'); |
14 | 43 |
|
15 |
| - try { |
16 |
| - writeSnippets(snippets(configuration.get('semicolons'))); |
17 |
| - } catch (error) { |
18 |
| - console.error(`[${EXTENSION_ID}] ${error}`); |
19 |
| - throw error; // terminates activation and shows a warning message |
20 |
| - } |
| 44 | + registeredSnippetProvider = registerSnippetProvider(semi); |
21 | 45 |
|
22 |
| - const subscription = vscode.workspace.onDidChangeConfiguration(() => { |
23 |
| - const previousConfiguration = configuration; |
24 |
| - configuration = listener(previousConfiguration); |
25 |
| - }); |
| 46 | + const changedConfigurationListener = vscode.workspace.onDidChangeConfiguration( |
| 47 | + () => { |
| 48 | + const previousConfiguration = configuration; |
| 49 | + configuration = handleChangedConfiguration(previousConfiguration); |
| 50 | + } |
| 51 | + ); |
26 | 52 |
|
27 |
| - context.subscriptions.push(subscription); |
| 53 | + context.subscriptions.push( |
| 54 | + registeredSnippetProvider, |
| 55 | + changedConfigurationListener |
| 56 | + ); |
28 | 57 | };
|
29 | 58 |
|
30 |
| -/** Listens for changes in extension configuration and return effective user's settings. */ |
31 |
| -function listener(previousConfiguration) { |
32 |
| - const configuration = vscode.workspace.getConfiguration( |
33 |
| - 'javascript-snippets' |
34 |
| - ); |
35 |
| - const hasSemicolons = configuration.get('semicolons'); |
36 |
| - |
37 |
| - if (hasSemicolons !== previousConfiguration.get('semicolons')) { |
38 |
| - try { |
39 |
| - writeSnippets(snippets(hasSemicolons)); |
40 |
| - promptToReloadWindow(); |
41 |
| - } catch (error) { |
42 |
| - vscode.window.showErrorMessage( |
43 |
| - `Extension \`${EXTENSION_ID}\` failed to write snippets to the file.` |
44 |
| - ); |
45 |
| - console.error(error); |
46 |
| - } |
| 59 | +function handleChangedConfiguration(previousConfiguration) { |
| 60 | + const configuration = vscode.workspace.getConfiguration(EXTENSION_ID); |
| 61 | + const semi = configuration.get('semi'); |
| 62 | + |
| 63 | + if (previousConfiguration.get('semi') !== semi) { |
| 64 | + registeredSnippetProvider = registerSnippetProvider(semi); |
47 | 65 | }
|
48 | 66 |
|
49 | 67 | return configuration;
|
50 | 68 | }
|
51 | 69 |
|
52 |
| -/** Synchronously writes snippets in JSON format to the file. */ |
53 |
| -function writeSnippets(snippets) { |
54 |
| - const snippetsPath = path.resolve(__dirname, '../snippets/snippets.json'); |
| 70 | +function registerSnippetProvider(semi) { |
| 71 | + if (registeredSnippetProvider) { |
| 72 | + registeredSnippetProvider.dispose(); |
| 73 | + } |
55 | 74 |
|
56 |
| - fs.writeFileSync(snippetsPath, JSON.stringify(snippets)); |
57 |
| -} |
| 75 | + const snippets = resolveSnippets(semi); |
| 76 | + const provider = vscode.languages.registerCompletionItemProvider( |
| 77 | + languageSelector, |
| 78 | + new SnippetProvider(snippets), |
| 79 | + '*' |
| 80 | + ); |
58 | 81 |
|
59 |
| -/** Prompts user to reload editor window in order for configuration change to take effect. */ |
60 |
| -function promptToReloadWindow() { |
61 |
| - const action = 'Reload'; |
62 |
| - |
63 |
| - vscode.window |
64 |
| - .showInformationMessage( |
65 |
| - `Reload window in order for change in extension \`${EXTENSION_ID}\` configuration to take effect.`, |
66 |
| - action |
67 |
| - ) |
68 |
| - .then(selectedAction => { |
69 |
| - if (selectedAction === action) { |
70 |
| - vscode.commands.executeCommand('workbench.action.reloadWindow'); |
71 |
| - } |
72 |
| - }); |
| 82 | + return provider; |
73 | 83 | }
|
0 commit comments