11import { createRequire } from "node:module" ;
22import * as path from "node:path" ;
33
4- import {
5- createLanguageServiceHost ,
6- decorateLanguageService ,
7- } from "@volar/typescript" ;
4+ import { createLanguage as createTypescriptLanguage } from "@volar/typescript" ;
85import * as vue from "@vue/language-core" ;
96import { normalizePath } from "@vue.ts/common" ;
107import type ts from "typescript/lib/tsserverlibrary" ;
@@ -20,23 +17,27 @@ function createLanguage(
2017 return createLanguageWorker (
2118 ( ) => vue . createParsedCommandLine ( ts , ts . sys , tsconfigPath ) ,
2219 path . dirname ( tsconfig ) ,
20+ tsconfig ,
2321 ts ,
2422 ) ;
2523}
2624
2725function createLanguageWorker (
2826 loadParsedCommandLine : ( ) => vue . ParsedCommandLine ,
2927 rootPath : string ,
28+ configFileName : string | undefined ,
3029 ts : typeof import ( "typescript/lib/tsserverlibrary" ) ,
3130) {
3231 let parsedCommandLine = loadParsedCommandLine ( ) ;
3332 let fileNames = parsedCommandLine . fileNames . map ( normalizePath ) ;
3433 let projectVersion = 0 ;
3534
3635 const scriptSnapshots = new Map < string , ts . IScriptSnapshot > ( ) ;
37- const _host : vue . TypeScriptLanguageHost = {
38- workspacePath : rootPath ,
39- rootPath,
36+ const _host : vue . TypeScriptProjectHost = {
37+ // Commented to wait for volar 2.2.0-alpha.0
38+ // ...ts.sys,
39+ // configFileName,
40+ getCurrentDirectory : ( ) => rootPath ,
4041 getProjectVersion : ( ) => projectVersion . toString ( ) ,
4142 getCompilationSettings : ( ) => parsedCommandLine . options ,
4243 getScriptFileNames : ( ) => fileNames ,
@@ -51,13 +52,27 @@ function createLanguageWorker(
5152
5253 return scriptSnapshots . get ( fileName ) ;
5354 } ,
55+ getLanguageId : ( fileName ) => {
56+ if (
57+ parsedCommandLine . vueOptions . extensions . some ( ( ext ) =>
58+ fileName . endsWith ( ext ) ,
59+ )
60+ ) {
61+ return "vue" ;
62+ }
63+
64+ return vue . resolveCommonLanguageId ( fileName ) ;
65+ } ,
66+ // scriptIdToFileName: (id) => id,
67+ // fileNameToScriptId: (id) => id,
5468 } ;
5569
5670 return {
5771 ...baseCreateLanguageWorker (
58- _host ,
59- vue . resolveVueCompilerOptions ( parsedCommandLine . vueOptions ) ,
6072 ts ,
73+ configFileName ,
74+ _host ,
75+ parsedCommandLine . vueOptions ,
6176 ) ,
6277 updateFile ( fileName : string , text : string ) {
6378 fileName = normalizePath ( fileName ) ;
@@ -84,38 +99,64 @@ function createLanguageWorker(
8499type Language = ReturnType < typeof createLanguage > ;
85100
86101function baseCreateLanguageWorker (
87- host : vue . TypeScriptLanguageHost ,
88- vueCompilerOptions : vue . VueCompilerOptions ,
89102 ts : typeof import ( "typescript/lib/tsserverlibrary" ) ,
103+ configFileName : string | undefined ,
104+ host : vue . TypeScriptProjectHost ,
105+ vueCompilerOptions : vue . VueCompilerOptions ,
90106) {
91- const vueLanguages = ts
92- ? // eslint-disable-next-line etc/no-deprecated
93- vue . createLanguages ( host . getCompilationSettings ( ) , vueCompilerOptions , ts )
94- : [ ] ;
95- const core = vue . createLanguageContext ( host , vueLanguages ) ;
96- const tsLsHost = createLanguageServiceHost ( core , ts , ts . sys ) ;
97- const tsLs = ts . createLanguageService ( tsLsHost ) ;
107+ const vueLanguagePlugin = vue . createVueLanguagePlugin (
108+ ts ,
109+ ( id ) => id ,
110+ ( fileName ) => {
111+ if ( ts . sys . useCaseSensitiveFileNames ) {
112+ return host . getScriptFileNames ( ) . includes ( fileName ) ?? false ;
113+ } else {
114+ const lowerFileName = fileName . toLowerCase ( ) ;
115+ for ( const rootFile of host . getScriptFileNames ( ) ) {
116+ if ( rootFile . toLowerCase ( ) === lowerFileName ) {
117+ return true ;
118+ }
119+ }
98120
99- decorateLanguageService ( core . virtualFiles , tsLs , false ) ;
121+ return false ;
122+ }
123+ } ,
124+ host . getCompilationSettings ( ) ,
125+ vueCompilerOptions ,
126+ ) ;
127+ const language = createTypescriptLanguage (
128+ ts ,
129+ ts . sys ,
130+ [ vueLanguagePlugin ] ,
131+ configFileName ,
132+ host ,
133+ {
134+ fileIdToFileName : ( id ) => id ,
135+ fileNameToFileId : ( id ) => id ,
136+ } ,
137+ ) ;
138+ const { languageServiceHost } = language . typescript ! ;
139+ const tsLs = ts . createLanguageService ( languageServiceHost ) ;
100140
101- const getScriptKind = tsLsHost . getScriptKind ! ;
102- tsLsHost . getScriptKind = ( fileName ) => {
141+ const getScriptKind =
142+ languageServiceHost . getScriptKind ?. bind ( languageServiceHost ) ;
143+ languageServiceHost . getScriptKind = ( fileName ) => {
103144 if ( fileName . endsWith ( ".vue.js" ) ) {
104145 return ts . ScriptKind . TS ;
105146 }
106147 if ( fileName . endsWith ( ".vue.jsx" ) ) {
107148 return ts . ScriptKind . TSX ;
108149 }
109150
110- return getScriptKind ( fileName ) ;
151+ return getScriptKind ! ( fileName ) ;
111152 } ;
112153
113154 const program = tsLs . getProgram ( ) ! ;
114155 const typeChecker = program . getTypeChecker ( ) ;
115156
116157 function getScriptSetupBlock ( normalizedFilepath : string ) {
117- const sourceFile = core . virtualFiles . getSource ( normalizedFilepath ) ?. root ;
118- if ( ! ( sourceFile instanceof vue . VueFile ) ) {
158+ const sourceFile = language . files . get ( normalizedFilepath ) ?. generated ?. code ;
159+ if ( ! ( sourceFile instanceof vue . VueGeneratedCode ) ) {
119160 return ;
120161 }
121162 if ( ! sourceFile . sfc . scriptSetup ) {
0 commit comments