@@ -6,39 +6,35 @@ const path = require('path');
66
77const snippets = require ( './snippets' ) ;
88
9+ /** Lazily activates extension once on emitted event. */
910exports . activate = context => {
1011 const extensionId = 'javascript-snippets' ;
11- const snippetsPath = path . resolve ( __dirname , '../snippets/snippets.json' ) ;
12- const property = 'semicolons' ;
1312 let configuration = vscode . workspace . getConfiguration ( extensionId ) ;
14- const hasSemicolons = configuration . get ( property ) ;
1513
1614 try {
17- generateSnippets ( snippetsPath , snippets ( hasSemicolons ) ) ;
15+ const hasSemicolons = configuration . get ( 'semicolons' ) ;
16+
17+ generateSnippets ( snippets ( hasSemicolons ) ) ;
1818 } catch ( error ) {
1919 // TODO: Handle error
2020 }
2121
2222 const subscription = vscode . workspace . onDidChangeConfiguration ( ( ) => {
23- configuration = listener (
24- configuration ,
25- extensionId ,
26- snippetsPath ,
27- property ,
28- ) ;
23+ const previousConfiguration = configuration ;
24+ configuration = listener ( previousConfiguration , extensionId ) ;
2925 } ) ;
3026
3127 context . subscriptions . push ( subscription ) ;
3228} ;
3329
34- /** Listens for change in `semicolons` property of user's settings. */
35- function listener ( previousConfiguration , identifier , path , property ) {
36- const configuration = vscode . workspace . getConfiguration ( identifier ) ;
37- const hasSemicolons = configuration . get ( property ) ;
30+ /** Listens for changes in extension configuration and return effective user's settings. */
31+ function listener ( previousConfiguration , extensionId ) {
32+ const configuration = vscode . workspace . getConfiguration ( extensionId ) ;
3833
39- if ( hasSemicolons !== previousConfiguration . get ( property ) ) {
34+ const hasSemicolons = configuration . get ( 'semicolons' ) ;
35+ if ( hasSemicolons !== previousConfiguration . get ( 'semicolons' ) ) {
4036 try {
41- generateSnippets ( path , snippets ( hasSemicolons ) ) ;
37+ generateSnippets ( snippets ( hasSemicolons ) ) ;
4238
4339 // TODO: Ask user to reload window, in order for change to take effect
4440 } catch ( error ) {
@@ -50,6 +46,8 @@ function listener(previousConfiguration, identifier, path, property) {
5046}
5147
5248/** Synchronously generates code snippets in JSON format and writes them to file. */
53- function generateSnippets ( file , snippets ) {
54- fs . writeFileSync ( file , JSON . stringify ( snippets ) ) ;
49+ function generateSnippets ( snippets ) {
50+ const snippetsPath = path . resolve ( __dirname , '../snippets/snippets.json' ) ;
51+
52+ fs . writeFileSync ( snippetsPath , JSON . stringify ( snippets ) ) ;
5553}
0 commit comments