1- 'use strict ';
1+ import * as vscode from 'vscode ';
22
3- var vscode = require ( 'vscode' )
3+ const typeScriptExtensionId = 'vscode.typescript-language-features' ;
4+ const pluginId = 'typescript-lit-html-plugin' ;
5+ const configurationSection = 'lit-html' ;
46
5- function activate ( context ) {
6- context . subscriptions . push (
7- vscode . commands . registerCommand ( "es6stringhtml.insertComment" , ( ) => insertString ( ) ) ,
8- vscode . commands . registerCommand ( "es6stringhtml.insertTemplate" , ( ) => insertString ( true ) ) ,
9- )
10- }
7+ export async function activate ( context ) {
8+ const extension = vscode . extensions . getExtension ( typeScriptExtensionId ) ;
9+
10+ if ( ! extension ) {
11+ return ;
12+ }
13+
14+ await extension . activate ( ) ;
15+
16+ if ( ! extension . exports || ! extension . exports . getAPI ) {
17+ return ;
18+ }
19+
20+ const api = extension . exports . getAPI ( 0 ) ;
21+
22+ if ( ! api ) {
23+ return ;
24+ }
25+
26+ vscode . workspace . onDidChangeConfiguration ( e => {
27+ if ( e . affectsConfiguration ( configurationSection ) ) {
28+ synchronizeConfiguration ( api ) ;
29+ }
30+ } , undefined , context . subscriptions ) ;
1131
12- function deactivate ( ) {
13- // nothing to dispose
32+ synchronizeConfiguration ( api ) ;
1433}
1534
16- /**
17- * Insert comment or comment with string in editor
18- * @param {boolean } full
19- */
20- function insertString ( full ) {
21- const string = full ? '/*html*/ ``' : '/*html*/'
22- const editor = vscode . window . activeTextEditor
23- const selections = editor . selections
24-
25- editor . edit ( ( editBuilder ) => {
26- selections . forEach ( ( selection ) => {
27- editBuilder . replace ( selection , '' )
28- editBuilder . insert ( selection . active , string )
29- } )
30- } )
35+ function synchronizeConfiguration ( api ) {
36+ api . configurePlugin ( pluginId , getConfiguration ( ) ) ;
3137}
3238
33- module . exports = {
34- activate : activate ,
35- deactivate : deactivate
39+ function getConfiguration ( ) {
40+ const config = vscode . workspace . getConfiguration ( configurationSection ) ;
41+ const outConfig = {
42+ format : { }
43+ } ;
44+
45+ withConfigValue ( config , 'tags' , tags => { outConfig . tags = tags ; } ) ;
46+ withConfigValue ( config , 'format.enabled' , enabled => { outConfig . format . enabled = enabled ; } ) ;
47+
48+ return outConfig ;
3649}
50+
51+ function withConfigValue ( config , key , withValue ) {
52+ const configSetting = config . inspect ( key ) ;
53+
54+ if ( ! configSetting ) {
55+ return ;
56+ }
57+
58+ // Make sure the user has actually set the value.
59+ // VS Code will return the default values instead of `undefined`, even if user has not don't set anything.
60+ if ( typeof configSetting . globalValue === 'undefined' && typeof configSetting . workspaceFolderValue === 'undefined' && typeof configSetting . workspaceValue === 'undefined' ) {
61+ return ;
62+ }
63+
64+ const value = config . get ( key , undefined ) ;
65+
66+ if ( typeof value !== 'undefined' ) {
67+ withValue ( value ) ;
68+ }
69+ }
0 commit comments