Skip to content

Commit cdba44d

Browse files
committed
Rewrite extension.js
- Handles snippet resolving in memory instead of through file system I/O - Bumps `vscode` engine to 1.8.0 - Chnages configuration identifier and semicolon property - Formats CHANGELOG.md - Updates prettier config of trailing commas
1 parent 4814540 commit cdba44d

File tree

9 files changed

+139
-179
lines changed

9 files changed

+139
-179
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,3 @@ FakesAssemblies/
196196
*.opt
197197

198198
*.vsix
199-
snippets/snippets.json

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"singleQuote": true,
3-
"trailingComma": "es5",
3+
"trailingComma": "none",
44
"semi": true
55
}

.vscode/launch.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"request": "launch",
88
"runtimeExecutable": "${execPath}",
99
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
10-
"stopOnEntry": false,
11-
"preLaunchTask": "clean"
10+
"stopOnEntry": false
1211
}
1312
]
1413
}

.vscode/tasks.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Change Log
22

3-
This project adheres to [Semantic Versioning](http://semver.org/).
4-
Every release, along with the migration instructions, is documented on the Github [Releases](https://github.com/xabikos/vscode-javascript/releases) page.
3+
This project adheres to [Semantic Versioning](http://semver.org/). Every
4+
release, along with the migration instructions, is documented on the Github
5+
[Releases](https://github.com/xabikos/vscode-javascript/releases) page.

package.json

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"url": "https://github.com/xabikos/vscode-javascript"
1212
},
1313
"engines": {
14-
"vscode": "^1.3.0"
14+
"vscode": "^1.8.0"
1515
},
1616
"main": "./src/extension",
1717
"activationEvents": [
@@ -29,43 +29,19 @@
2929
"configuration": {
3030
"title": "JavaScript Snippets",
3131
"properties": {
32-
"javascript-snippets.semicolons": {
32+
"snippets-javascript.semi": {
3333
"type": "boolean",
3434
"default": true,
35-
"description": "Uses semicolons. Restart is requierd."
35+
"description": "Uses semicolons"
3636
}
3737
}
38-
},
39-
"snippets": [
40-
{
41-
"language": "javascript",
42-
"path": "./snippets/snippets.json"
43-
},
44-
{
45-
"language": "typescript",
46-
"path": "./snippets/snippets.json"
47-
},
48-
{
49-
"language": "javascriptreact",
50-
"path": "./snippets/snippets.json"
51-
},
52-
{
53-
"language": "typescriptreact",
54-
"path": "./snippets/snippets.json"
55-
},
56-
{
57-
"language": "html",
58-
"path": "./snippets/snippets.json"
59-
}
60-
]
38+
}
6139
},
6240
"scripts": {
63-
"postinstall": "node ./node_modules/vscode/bin/install",
64-
"clean": "rimraf ./snippets/snippets.json"
41+
"postinstall": "node ./node_modules/vscode/bin/install"
6542
},
6643
"devDependencies": {
6744
"eslint": "^4.10.0",
68-
"rimraf": "^2.6.2",
6945
"vscode": "^1.1.6"
7046
}
7147
}

snippets/sample.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/extension.js

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,83 @@
11
'use strict';
22

3-
const fs = require('fs');
4-
const path = require('path');
53
const vscode = require('vscode');
64

7-
const snippets = require('./snippets');
5+
const resolveSnippets = require('./snippets');
86

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+
}
1038

1139
/** Activates extension on an emitted event. Invoked only once. */
1240
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');
1443

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);
2145

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+
);
2652

27-
context.subscriptions.push(subscription);
53+
context.subscriptions.push(
54+
registeredSnippetProvider,
55+
changedConfigurationListener
56+
);
2857
};
2958

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);
4765
}
4866

4967
return configuration;
5068
}
5169

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+
}
5574

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+
);
5881

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;
7383
}

0 commit comments

Comments
 (0)