|
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 | +// FIXME: Change scope placement |
| 17 | +let registeredSnippetProvider = undefined; |
| 18 | + |
| 19 | +class SnippetItem extends vscode.CompletionItem { |
| 20 | + constructor(snippet) { |
| 21 | + super(snippet.prefix, vscode.CompletionItemKind.Snippet); |
| 22 | + |
| 23 | + this.insertText = new vscode.SnippetString(snippet.body); |
| 24 | + this.detail = snippet.description; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class SnippetProvider { |
| 29 | + constructor(snippets) { |
| 30 | + this.snippets = snippets; |
| 31 | + } |
| 32 | + |
| 33 | + provideCompletionItems() { |
| 34 | + return Object.keys(this.snippets).map( |
| 35 | + name => new SnippetItem(this.snippets[name]) |
| 36 | + ); |
| 37 | + } |
| 38 | +} |
10 | 39 |
|
11 | 40 | /** Activates extension on an emitted event. Invoked only once. */ |
12 | 41 | exports.activate = context => { |
13 | | - let configuration = vscode.workspace.getConfiguration('javascript-snippets'); |
| 42 | + let configuration = vscode.workspace.getConfiguration(EXTENSION_ID); |
| 43 | + const semi = configuration.get('semi'); |
14 | 44 |
|
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 | | - } |
| 45 | + registeredSnippetProvider = registerSnippetProvider(semi); |
21 | 46 |
|
22 | | - const subscription = vscode.workspace.onDidChangeConfiguration(() => { |
23 | | - const previousConfiguration = configuration; |
24 | | - configuration = listener(previousConfiguration); |
25 | | - }); |
| 47 | + const changedConfigurationListener = vscode.workspace.onDidChangeConfiguration( |
| 48 | + () => { |
| 49 | + const previousConfiguration = configuration; |
| 50 | + configuration = handleChangedConfiguration(previousConfiguration); |
| 51 | + } |
| 52 | + ); |
26 | 53 |
|
27 | | - context.subscriptions.push(subscription); |
| 54 | + context.subscriptions.push( |
| 55 | + registeredSnippetProvider, |
| 56 | + changedConfigurationListener |
| 57 | + ); |
28 | 58 | }; |
29 | 59 |
|
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 | | - } |
| 60 | +function handleChangedConfiguration(previousConfiguration) { |
| 61 | + const configuration = vscode.workspace.getConfiguration(EXTENSION_ID); |
| 62 | + const semi = configuration.get('semi'); |
| 63 | + |
| 64 | + if (previousConfiguration.get('semi') !== semi) { |
| 65 | + registeredSnippetProvider = registerSnippetProvider(semi); |
| 66 | + |
| 67 | + // FIXME: Reload window |
47 | 68 | } |
48 | 69 |
|
49 | 70 | return configuration; |
50 | 71 | } |
51 | 72 |
|
52 | | -/** Synchronously writes snippets in JSON format to the file. */ |
53 | | -function writeSnippets(snippets) { |
54 | | - const snippetsPath = path.resolve(__dirname, '../snippets/snippets.json'); |
| 73 | +function registerSnippetProvider(semi) { |
| 74 | + if (registeredSnippetProvider) { |
| 75 | + registeredSnippetProvider.dispose(); |
| 76 | + } |
55 | 77 |
|
56 | | - fs.writeFileSync(snippetsPath, JSON.stringify(snippets)); |
57 | | -} |
| 78 | + const snippets = resolveSnippets(semi); |
| 79 | + const provider = vscode.languages.registerCompletionItemProvider( |
| 80 | + languageSelector, |
| 81 | + new SnippetProvider(snippets), |
| 82 | + '*' |
| 83 | + ); |
58 | 84 |
|
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 | | - }); |
| 85 | + return provider; |
73 | 86 | } |
0 commit comments