@@ -21,8 +21,8 @@ import * as interpolationDecorators from './lib/interpolationDecorators';
21
21
import * as reactivityVisualization from './lib/reactivityVisualization' ;
22
22
import * as welcome from './lib/welcome' ;
23
23
24
- let client : lsp . BaseLanguageClient | undefined ;
25
- let needRestart = false ;
24
+ const serverPath = resolveServerPath ( ) ;
25
+ const neededRestart = ! patchTypeScriptExtension ( ) ;
26
26
27
27
for (
28
28
const incompatibleExtensionId of [
44
44
}
45
45
46
46
export = defineExtension ( ( ) => {
47
+ let client : lsp . BaseLanguageClient | undefined ;
48
+
47
49
const context = extensionContext . value ! ;
48
50
const volarLabs = createLabsInfo ( ) ;
49
51
const activeTextEditor = useActiveTextEditor ( ) ;
@@ -59,7 +61,7 @@ export = defineExtension(() => {
59
61
60
62
nextTick ( ( ) => stop ( ) ) ;
61
63
62
- if ( needRestart ) {
64
+ if ( neededRestart ) {
63
65
vscode . window . showInformationMessage (
64
66
'Please restart the extension host to activate Vue support.' ,
65
67
'Restart Extension Host' ,
@@ -75,9 +77,12 @@ export = defineExtension(() => {
75
77
return ;
76
78
}
77
79
78
- watch ( ( ) => config . server . includeLanguages , async ( ) => {
80
+ watch ( ( ) => [
81
+ config . server . path ,
82
+ config . server . includeLanguages ,
83
+ ] , async ( ) => {
79
84
const reload = await vscode . window . showInformationMessage (
80
- 'Please restart extension host to apply the new language settings.' ,
85
+ 'Please restart extension host to apply the new server settings.' ,
81
86
'Restart Extension Host' ,
82
87
) ;
83
88
if ( reload ) {
@@ -95,7 +100,14 @@ export = defineExtension(() => {
95
100
) ;
96
101
}
97
102
98
- volarLabs . addLanguageClient ( client = launch ( context ) ) ;
103
+ if ( config . server . path && ! serverPath ) {
104
+ vscode . window . showErrorMessage ( 'Cannot find @vue/language-server.' ) ;
105
+ return ;
106
+ }
107
+
108
+ client = launch ( serverPath ?? vscode . Uri . joinPath ( context . extensionUri , 'dist' , 'language-server.js' ) . fsPath ) ;
109
+
110
+ volarLabs . addLanguageClient ( client ) ;
99
111
100
112
const selectors = config . server . includeLanguages ;
101
113
@@ -123,19 +135,18 @@ export = defineExtension(() => {
123
135
return volarLabs . extensionExports ;
124
136
} ) ;
125
137
126
- function launch ( context : vscode . ExtensionContext ) {
127
- const serverModule = vscode . Uri . joinPath ( context . extensionUri , 'dist' , 'language-server.js' ) ;
138
+ function launch ( serverPath : string ) {
128
139
const client = new lsp . LanguageClient (
129
140
'vue' ,
130
141
'Vue' ,
131
142
{
132
143
run : {
133
- module : serverModule . fsPath ,
144
+ module : serverPath ,
134
145
transport : lsp . TransportKind . ipc ,
135
146
options : { } ,
136
147
} ,
137
148
debug : {
138
- module : serverModule . fsPath ,
149
+ module : serverPath ,
139
150
transport : lsp . TransportKind . ipc ,
140
151
options : { execArgv : [ '--nolazy' , '--inspect=' + 6009 ] } ,
141
152
} ,
@@ -179,21 +190,52 @@ function launch(context: vscode.ExtensionContext) {
179
190
return client ;
180
191
}
181
192
182
- const tsExtension = vscode . extensions . getExtension ( 'vscode.typescript-language-features' ) ! ;
183
- if ( tsExtension . isActive ) {
184
- needRestart = true ;
193
+ function resolveServerPath ( ) {
194
+ const tsPluginPackPath = path . join ( __dirname , '..' , 'node_modules' , 'vue-typescript-plugin-pack' , 'index.js' ) ;
195
+
196
+ if ( ! config . server . path ) {
197
+ fs . writeFileSync ( tsPluginPackPath , `module.exports = require("../../dist/typescript-plugin.js");` ) ;
198
+ return ;
199
+ }
200
+
201
+ if ( path . isAbsolute ( config . server . path ) ) {
202
+ const entryFile = require . resolve ( './index.js' , { paths : [ config . server . path ] } ) ;
203
+ const tsPluginPath = require . resolve ( '@vue/typescript-plugin' , { paths : [ path . dirname ( entryFile ) ] } ) ;
204
+ fs . writeFileSync ( tsPluginPackPath , `module.exports = require("${ tsPluginPath } ");` ) ;
205
+ return entryFile ;
206
+ }
207
+
208
+ for ( const { uri } of vscode . workspace . workspaceFolders ?? [ ] ) {
209
+ if ( uri . scheme !== 'file' ) {
210
+ continue ;
211
+ }
212
+ try {
213
+ const serverPath = path . join ( uri . fsPath , config . server . path ) ;
214
+ const entryFile = require . resolve ( './index.js' , { paths : [ serverPath ] } ) ;
215
+ const tsPluginPath = require . resolve ( '@vue/typescript-plugin' , { paths : [ path . dirname ( entryFile ) ] } ) ;
216
+ fs . writeFileSync ( tsPluginPackPath , `module.exports = require("${ tsPluginPath } ");` ) ;
217
+ return entryFile ;
218
+ }
219
+ catch { }
220
+ }
185
221
}
186
- else {
222
+
223
+ function patchTypeScriptExtension ( ) {
224
+ const tsExtension = vscode . extensions . getExtension ( 'vscode.typescript-language-features' ) ! ;
225
+ if ( tsExtension . isActive ) {
226
+ return false ;
227
+ }
228
+
187
229
const fs = require ( 'node:fs' ) ;
188
230
const readFileSync = fs . readFileSync ;
189
231
const extensionJsPath = require . resolve ( './dist/extension.js' , { paths : [ tsExtension . extensionPath ] } ) ;
190
232
const { publisher, name } = require ( './package.json' ) ;
191
233
const vueExtension = vscode . extensions . getExtension ( `${ publisher } .${ name } ` ) ! ;
192
- const vuePluginName = 'vue-typescript-plugin-pack' ;
234
+ const tsPluginName = 'vue-typescript-plugin-pack' ;
193
235
194
236
vueExtension . packageJSON . contributes . typescriptServerPlugins = [
195
237
{
196
- name : vuePluginName ,
238
+ name : tsPluginName ,
197
239
enableForWorkspaceTypeScriptVersions : true ,
198
240
configNamespace : 'typescript' ,
199
241
languages : config . server . includeLanguages ,
@@ -218,7 +260,7 @@ else {
218
260
// sort plugins for johnsoncodehk.tsslint, zardoy.ts-essential-plugins
219
261
text = text . replace (
220
262
'"--globalPlugins",i.plugins' ,
221
- s => s + `.sort((a,b)=>(b.name==="${ vuePluginName } "?-1:0)-(a.name==="${ vuePluginName } "?-1:0))` ,
263
+ s => s + `.sort((a,b)=>(b.name==="${ tsPluginName } "?-1:0)-(a.name==="${ tsPluginName } "?-1:0))` ,
222
264
) ;
223
265
224
266
return text ;
@@ -232,4 +274,5 @@ else {
232
274
const patchedModule = require ( extensionJsPath ) ;
233
275
Object . assign ( loadedModule . exports , patchedModule ) ;
234
276
}
277
+ return true ;
235
278
}
0 commit comments