2
2
import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
3
3
import type * as monaco from 'monaco-editor-core'
4
4
import * as ts from 'typescript/lib/tsserverlibrary'
5
- import type { VueCompilerOptions } from '@vue/language-service'
6
- import { resolveConfig } from '@vue/language-service'
7
- import {
8
- createLanguageHost ,
9
- createLanguageService ,
10
- createServiceEnvironment ,
11
- } from '@volar/monaco/worker'
5
+ import type { LanguageServiceEnvironment , VueCompilerOptions } from '@vue/language-service'
6
+ import { getFullLanguageServicePlugins , createVueLanguagePlugin , resolveVueCompilerOptions } from '@vue/language-service'
7
+ import { createTypeScriptWorkerService } from '@volar/monaco/worker'
12
8
import type { WorkerHost } from './env'
9
+ import { URI } from 'vscode-uri'
13
10
14
11
export interface CreateData {
15
12
tsconfig : {
@@ -36,51 +33,70 @@ self.onmessage = () => {
36
33
ctx : monaco . worker . IWorkerContext < WorkerHost > ,
37
34
{ tsconfig } : CreateData ,
38
35
) => {
36
+ const asFileName = ( uri : URI ) => uri . path
37
+ const asUri = ( fileName : string ) : URI => URI . file ( fileName )
38
+ const env : LanguageServiceEnvironment = {
39
+ workspaceFolders : [ URI . file ( '/' ) ] ,
40
+ }
39
41
const { options : compilerOptions } = ts . convertCompilerOptionsFromJson (
40
42
tsconfig . compilerOptions || { } ,
41
43
'' ,
42
44
)
45
+ const vueCompilerOptions = resolveVueCompilerOptions (
46
+ tsconfig . vueCompilerOptions || { } ,
47
+ )
43
48
44
49
// eslint-disable-next-line no-console
45
50
console . log ( 'Vue Language Services: compilerOptions' , compilerOptions )
46
51
47
- const env = createServiceEnvironment ( )
48
- const host = createLanguageHost (
49
- ctx . getMirrorModels ,
50
- env ,
51
- '/' ,
52
- compilerOptions ,
53
- )
54
-
55
52
env . fs = {
56
53
async readFile ( uri ) {
57
- if ( isInvalidPath ( uri ) )
54
+ if ( isInvalidPath ( uri . path ) )
58
55
return undefined
59
- const file = await ctx . host . fsReadFile ( uri )
56
+ const file = await ctx . host . fsReadFile ( uri . toString ( ) )
60
57
return file
61
58
} ,
62
59
async stat ( uri ) {
63
- if ( isInvalidPath ( uri ) )
60
+ if ( isInvalidPath ( uri . path ) )
64
61
return undefined
65
- const result = await ctx . host . fsStat ( uri )
62
+ const result = await ctx . host . fsStat ( uri . toString ( ) )
66
63
return result
67
64
} ,
68
65
async readDirectory ( uri ) {
69
- const dirs = await ctx . host . fsReadDirectory ( uri )
66
+ const dirs = await ctx . host . fsReadDirectory ( uri . toString ( ) )
70
67
return dirs
71
68
} ,
72
69
}
73
70
74
- return createLanguageService (
75
- { typescript : ts } ,
71
+ return createTypeScriptWorkerService ( {
72
+ typescript : ts ,
76
73
env,
77
- resolveConfig (
74
+ compilerOptions,
75
+ uriConverter : {
76
+ asFileName,
77
+ asUri,
78
+ } ,
79
+ workerContext : ctx ,
80
+ languagePlugins : [ createVueLanguagePlugin (
78
81
ts ,
79
- { } ,
82
+ asFileName ,
83
+ ( ) => '' , // TODO getProjectVersion
84
+ ( fileName ) => {
85
+ const uri = asUri ( fileName )
86
+ for ( const model of ctx . getMirrorModels ( ) ) {
87
+ if ( model . uri . toString ( ) === uri . toString ( ) ) {
88
+ return true
89
+ }
90
+ }
91
+ return false
92
+ } ,
80
93
compilerOptions ,
81
- tsconfig ?. vueCompilerOptions || { } ,
82
- ) ,
83
- host ,
84
- )
94
+ vueCompilerOptions ,
95
+ ) ] ,
96
+ languageServicePlugins : getFullLanguageServicePlugins ( ts ) ,
97
+ setup ( { project } ) {
98
+ project . vue = { compilerOptions : vueCompilerOptions }
99
+ } ,
100
+ } )
85
101
} )
86
102
}
0 commit comments