Skip to content

Commit 5b89891

Browse files
johnsoncodehkantfu
andauthored
feat: update to @volar/monaco and @vue/language-service v2 (#150)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 057af11 commit 5b89891

File tree

5 files changed

+335
-303
lines changed

5 files changed

+335
-303
lines changed

Diff for: monaco/env.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,24 @@ export async function reloadLanguageTools(ctx: PlaygroundMonacoContext) {
131131
.map(file => Uri.parse(`file:///${file.filepath}`)),
132132
...extraFiles,
133133
]
134-
const { dispose: disposeMarkers } = volar.editor.activateMarkers(
134+
const { dispose: disposeMarkers } = volar.activateMarkers(
135135
worker,
136136
languageId,
137137
'vue',
138138
getSyncUris,
139-
editor,
139+
editor as typeof import('monaco-editor').editor,
140140
)
141-
const { dispose: disposeAutoInsertion } = volar.editor.activateAutoInsertion(
141+
const { dispose: disposeAutoInsertion } = volar.activateAutoInsertion(
142142
worker,
143143
languageId,
144144
getSyncUris,
145-
editor,
145+
editor as typeof import('monaco-editor').editor,
146146
)
147-
const { dispose: disposeProvides } = await volar.languages.registerProvides(
147+
const { dispose: disposeProvides } = await volar.registerProviders(
148148
worker,
149149
languageId,
150150
getSyncUris,
151-
languages,
151+
languages as unknown as typeof import('monaco-editor').languages,
152152
)
153153

154154
disposeVue = () => {

Diff for: monaco/setup.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import vueWorker from './vue.worker?worker'
1010
import { reloadLanguageTools } from './env'
1111

1212
export function initMonaco(ctx: PlaygroundStore) {
13-
// @ts-expect-error MonacoEnvironment is a global variable injected for monaco
1413
self.MonacoEnvironment = {
1514
async getWorker(_: any, label: string) {
1615
switch (label) {

Diff for: monaco/vue.worker.ts

+44-28
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
33
import type * as monaco from 'monaco-editor-core'
44
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'
128
import type { WorkerHost } from './env'
9+
import { URI } from 'vscode-uri'
1310

1411
export interface CreateData {
1512
tsconfig: {
@@ -36,51 +33,70 @@ self.onmessage = () => {
3633
ctx: monaco.worker.IWorkerContext<WorkerHost>,
3734
{ tsconfig }: CreateData,
3835
) => {
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+
}
3941
const { options: compilerOptions } = ts.convertCompilerOptionsFromJson(
4042
tsconfig.compilerOptions || {},
4143
'',
4244
)
45+
const vueCompilerOptions = resolveVueCompilerOptions(
46+
tsconfig.vueCompilerOptions || {},
47+
)
4348

4449
// eslint-disable-next-line no-console
4550
console.log('Vue Language Services: compilerOptions', compilerOptions)
4651

47-
const env = createServiceEnvironment()
48-
const host = createLanguageHost(
49-
ctx.getMirrorModels,
50-
env,
51-
'/',
52-
compilerOptions,
53-
)
54-
5552
env.fs = {
5653
async readFile(uri) {
57-
if (isInvalidPath(uri))
54+
if (isInvalidPath(uri.path))
5855
return undefined
59-
const file = await ctx.host.fsReadFile(uri)
56+
const file = await ctx.host.fsReadFile(uri.toString())
6057
return file
6158
},
6259
async stat(uri) {
63-
if (isInvalidPath(uri))
60+
if (isInvalidPath(uri.path))
6461
return undefined
65-
const result = await ctx.host.fsStat(uri)
62+
const result = await ctx.host.fsStat(uri.toString())
6663
return result
6764
},
6865
async readDirectory(uri) {
69-
const dirs = await ctx.host.fsReadDirectory(uri)
66+
const dirs = await ctx.host.fsReadDirectory(uri.toString())
7067
return dirs
7168
},
7269
}
7370

74-
return createLanguageService(
75-
{ typescript: ts },
71+
return createTypeScriptWorkerService({
72+
typescript: ts,
7673
env,
77-
resolveConfig(
74+
compilerOptions,
75+
uriConverter: {
76+
asFileName,
77+
asUri,
78+
},
79+
workerContext: ctx,
80+
languagePlugins: [createVueLanguagePlugin(
7881
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+
},
8093
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+
})
85101
})
86102
}

Diff for: package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"dependencies": {
1717
"@shikijs/core": "^1.5.2",
1818
"@shikijs/monaco": "^1.5.2",
19-
"@volar/monaco": "^1.11.1",
20-
"@vue/language-service": "^1.8.27",
19+
"@volar/monaco": "~2.4.0-alpha.12",
20+
"@vue/language-service": "^2.0.26-alpha.2",
2121
"@webcontainer/api": "^1.1.9",
2222
"@xterm/addon-fit": "^0.10.0",
2323
"@xterm/xterm": "^5.5.0",
@@ -33,6 +33,7 @@
3333
"strip-json-comments": "^5.0.1",
3434
"theme-vitesse": "^0.8.0",
3535
"unified": "^11.0.4",
36+
"vscode-uri": "^3.0.8",
3637
"vue": "^3.4.27",
3738
"vue-router": "^4.3.2"
3839
},
@@ -56,13 +57,13 @@
5657
"execa": "^9.1.0",
5758
"fast-glob": "^3.3.2",
5859
"fuse.js": "^7.0.0",
59-
"monaco-editor-core": "^0.48.0",
60+
"monaco-editor-core": "^0.50.0",
6061
"nuxt": "^3.11.2",
6162
"nuxt-icon": "^0.6.10",
6263
"pathe": "^1.1.2",
6364
"remark-external-links": "^9.0.1",
6465
"typescript": "^5.4.5",
65-
"vue-tsc": "^2.0.19"
66+
"vue-tsc": "^2.0.26-alpha.2"
6667
},
6768
"pnpm": {
6869
"patchedDependencies": {

0 commit comments

Comments
 (0)