Skip to content

Commit

Permalink
feat: update to @volar/monaco and @vue/language-service v2 (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <github@antfu.me>
  • Loading branch information
johnsoncodehk and antfu authored Jul 2, 2024
1 parent 057af11 commit 5b89891
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 303 deletions.
12 changes: 6 additions & 6 deletions monaco/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,24 @@ export async function reloadLanguageTools(ctx: PlaygroundMonacoContext) {
.map(file => Uri.parse(`file:///${file.filepath}`)),
...extraFiles,
]
const { dispose: disposeMarkers } = volar.editor.activateMarkers(
const { dispose: disposeMarkers } = volar.activateMarkers(
worker,
languageId,
'vue',
getSyncUris,
editor,
editor as typeof import('monaco-editor').editor,
)
const { dispose: disposeAutoInsertion } = volar.editor.activateAutoInsertion(
const { dispose: disposeAutoInsertion } = volar.activateAutoInsertion(
worker,
languageId,
getSyncUris,
editor,
editor as typeof import('monaco-editor').editor,
)
const { dispose: disposeProvides } = await volar.languages.registerProvides(
const { dispose: disposeProvides } = await volar.registerProviders(
worker,
languageId,
getSyncUris,
languages,
languages as unknown as typeof import('monaco-editor').languages,
)

disposeVue = () => {
Expand Down
1 change: 0 additions & 1 deletion monaco/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import vueWorker from './vue.worker?worker'
import { reloadLanguageTools } from './env'

export function initMonaco(ctx: PlaygroundStore) {
// @ts-expect-error MonacoEnvironment is a global variable injected for monaco
self.MonacoEnvironment = {
async getWorker(_: any, label: string) {
switch (label) {
Expand Down
72 changes: 44 additions & 28 deletions monaco/vue.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
import type * as monaco from 'monaco-editor-core'
import * as ts from 'typescript/lib/tsserverlibrary'
import type { VueCompilerOptions } from '@vue/language-service'
import { resolveConfig } from '@vue/language-service'
import {
createLanguageHost,
createLanguageService,
createServiceEnvironment,
} from '@volar/monaco/worker'
import type { LanguageServiceEnvironment, VueCompilerOptions } from '@vue/language-service'
import { getFullLanguageServicePlugins, createVueLanguagePlugin, resolveVueCompilerOptions } from '@vue/language-service'
import { createTypeScriptWorkerService } from '@volar/monaco/worker'
import type { WorkerHost } from './env'
import { URI } from 'vscode-uri'

export interface CreateData {
tsconfig: {
Expand All @@ -36,51 +33,70 @@ self.onmessage = () => {
ctx: monaco.worker.IWorkerContext<WorkerHost>,
{ tsconfig }: CreateData,
) => {
const asFileName = (uri: URI) => uri.path
const asUri = (fileName: string): URI => URI.file(fileName)
const env: LanguageServiceEnvironment = {
workspaceFolders: [URI.file('/')],
}
const { options: compilerOptions } = ts.convertCompilerOptionsFromJson(
tsconfig.compilerOptions || {},
'',
)
const vueCompilerOptions = resolveVueCompilerOptions(
tsconfig.vueCompilerOptions || {},
)

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

const env = createServiceEnvironment()
const host = createLanguageHost(
ctx.getMirrorModels,
env,
'/',
compilerOptions,
)

env.fs = {
async readFile(uri) {
if (isInvalidPath(uri))
if (isInvalidPath(uri.path))
return undefined
const file = await ctx.host.fsReadFile(uri)
const file = await ctx.host.fsReadFile(uri.toString())
return file
},
async stat(uri) {
if (isInvalidPath(uri))
if (isInvalidPath(uri.path))
return undefined
const result = await ctx.host.fsStat(uri)
const result = await ctx.host.fsStat(uri.toString())
return result
},
async readDirectory(uri) {
const dirs = await ctx.host.fsReadDirectory(uri)
const dirs = await ctx.host.fsReadDirectory(uri.toString())
return dirs
},
}

return createLanguageService(
{ typescript: ts },
return createTypeScriptWorkerService({
typescript: ts,
env,
resolveConfig(
compilerOptions,
uriConverter: {
asFileName,
asUri,
},
workerContext: ctx,
languagePlugins: [createVueLanguagePlugin(
ts,
{},
asFileName,
() => '', // TODO getProjectVersion
(fileName) => {
const uri = asUri(fileName)
for (const model of ctx.getMirrorModels()) {
if (model.uri.toString() === uri.toString()) {
return true
}
}
return false
},
compilerOptions,
tsconfig?.vueCompilerOptions || {},
),
host,
)
vueCompilerOptions,
)],
languageServicePlugins: getFullLanguageServicePlugins(ts),
setup({ project }) {
project.vue = { compilerOptions: vueCompilerOptions }
},
})
})
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"dependencies": {
"@shikijs/core": "^1.5.2",
"@shikijs/monaco": "^1.5.2",
"@volar/monaco": "^1.11.1",
"@vue/language-service": "^1.8.27",
"@volar/monaco": "~2.4.0-alpha.12",
"@vue/language-service": "^2.0.26-alpha.2",
"@webcontainer/api": "^1.1.9",
"@xterm/addon-fit": "^0.10.0",
"@xterm/xterm": "^5.5.0",
Expand All @@ -33,6 +33,7 @@
"strip-json-comments": "^5.0.1",
"theme-vitesse": "^0.8.0",
"unified": "^11.0.4",
"vscode-uri": "^3.0.8",
"vue": "^3.4.27",
"vue-router": "^4.3.2"
},
Expand All @@ -56,13 +57,13 @@
"execa": "^9.1.0",
"fast-glob": "^3.3.2",
"fuse.js": "^7.0.0",
"monaco-editor-core": "^0.48.0",
"monaco-editor-core": "^0.50.0",
"nuxt": "^3.11.2",
"nuxt-icon": "^0.6.10",
"pathe": "^1.1.2",
"remark-external-links": "^9.0.1",
"typescript": "^5.4.5",
"vue-tsc": "^2.0.19"
"vue-tsc": "^2.0.26-alpha.2"
},
"pnpm": {
"patchedDependencies": {
Expand Down
Loading

0 comments on commit 5b89891

Please sign in to comment.