@@ -52,43 +52,42 @@ export async function activate(context: vscode.ExtensionContext) {
5252 )
5353 ) ;
5454
55- context . subscriptions . push (
56- vscode . commands . registerCommand ( 'vetur.chooseTypeScriptRefactoring' , ( args : any ) => {
57- client . sendRequest < WorkspaceEdit | undefined > ( 'requestCodeActionEdits' , args ) . then ( edits => {
58- if ( edits ) {
59- vscode . workspace . applyEdit ( client . protocol2CodeConverter . asWorkspaceEdit ( edits ) ! ) ;
60- }
61- } ) ;
62- } )
63- ) ;
64-
65- registerLanguageConfigurations ( ) ;
66-
67- /**
68- * Vue Language Server Initialization
69- */
70-
71- const serverModule = context . asAbsolutePath ( join ( 'server' , 'dist' , 'vueServerMain.js' ) ) ;
72- const client = initializeLanguageClient ( serverModule , globalSnippetDir ) ;
73- context . subscriptions . push ( client . start ( ) ) ;
74-
75- const promise = client
76- . onReady ( )
77- . then ( ( ) => {
78- registerCustomClientNotificationHandlers ( client ) ;
79- registerCustomLSPCommands ( context , client ) ;
80- } )
81- . catch ( e => {
82- console . log ( 'Client initialization failed' ) ;
83- } ) ;
84-
85- return vscode . window . withProgress (
86- {
87- title : 'Vetur initialization' ,
88- location : vscode . ProgressLocation . Window
89- } ,
90- ( ) => promise
91- ) ;
55+ function onDidOpenTextDocument ( document : vscode . TextDocument ) {
56+ const client = initializeClientForTextDocument ( context , serverModule , globalSnippetDir , document ) ;
57+ // TODO handle commands properly
58+ if ( ! client || clients . size > 1 ) {
59+ return ;
60+ }
61+ context . subscriptions . push (
62+ vscode . commands . registerCommand ( 'vetur.applyWorkspaceEdits' , ( args : WorkspaceEdit ) => {
63+ const edit = client . protocol2CodeConverter . asWorkspaceEdit ( args ) ! ;
64+ vscode . workspace . applyEdit ( edit ) ;
65+ } )
66+ ) ;
67+
68+ context . subscriptions . push (
69+ vscode . commands . registerCommand ( 'vetur.chooseTypeScriptRefactoring' , ( args : any ) => {
70+ client . sendRequest < WorkspaceEdit | undefined > ( 'requestCodeActionEdits' , args ) . then ( edits => {
71+ if ( edits ) {
72+ vscode . workspace . applyEdit ( client . protocol2CodeConverter . asWorkspaceEdit ( edits ) ! ) ;
73+ }
74+ } ) ;
75+ } )
76+ ) ;
77+ }
78+
79+ vscode . workspace . onDidOpenTextDocument ( onDidOpenTextDocument ) ;
80+ vscode . workspace . textDocuments . forEach ( onDidOpenTextDocument ) ;
81+
82+ vscode . workspace . onDidChangeWorkspaceFolders ( event => {
83+ for ( const folder of event . removed ) {
84+ const client = clients . get ( folder . uri . toString ( ) ) ;
85+ if ( client ) {
86+ clients . delete ( folder . uri . toString ( ) ) ;
87+ client . stop ( ) ;
88+ }
89+ }
90+ } ) ;
9291}
9392
9493function registerCustomClientNotificationHandlers ( client : LanguageClient ) {
@@ -111,3 +110,35 @@ function registerCustomLSPCommands(context: vscode.ExtensionContext, client: Lan
111110 vscode . commands . registerCommand ( 'vetur.showCorrespondingVirtualFile' , generateShowVirtualFileCommand ( client ) )
112111 ) ;
113112}
113+
114+ function initializeClientForTextDocument (
115+ context : vscode . ExtensionContext ,
116+ serverModule : string ,
117+ globalSnippetDir : string ,
118+ document : vscode . TextDocument
119+ ) : LanguageClient | undefined {
120+ const folder = vscode . workspace . getWorkspaceFolder ( document . uri ) ;
121+
122+ if ( ! folder || document . languageId !== 'vue' ) {
123+ return ;
124+ }
125+
126+ if ( ! clients . has ( folder . uri . toString ( ) ) ) {
127+ const client = initializeLanguageClient ( serverModule , globalSnippetDir , folder ) ;
128+
129+ client
130+ . onReady ( )
131+ . then ( ( ) => {
132+ registerCustomClientNotificationHandlers ( client ) ;
133+ registerCustomLSPCommands ( context , client ) ;
134+ } )
135+ . catch ( e => {
136+ console . log ( `Client initialization failed for workspace: ${ folder . uri . toString ( ) } ` ) ;
137+ } ) ;
138+
139+ context . subscriptions . push ( client . start ( ) ) ;
140+ clients . set ( folder . uri . toString ( ) , client ) ;
141+
142+ return client ;
143+ }
144+ }
0 commit comments