@@ -3,8 +3,8 @@ import { startCommonLanguageServer } from '../common/server';
33import { LanguageServerPlugin } from '../types' ;
44import httpSchemaRequestHandler from '../common/schemaRequestHandlers/http' ;
55import { URI } from 'vscode-uri' ;
6- import { FsReadFileRequest , FsReadDirectoryRequest } from '../protocol' ;
7- import { FileSystem , FileType } from '@volar/language-service' ;
6+ import { FsReadFileRequest , FsReadDirectoryRequest , FsStatRequest } from '../protocol' ;
7+ import { FileType } from '@volar/language-service' ;
88
99export * from '../index' ;
1010
@@ -18,6 +18,7 @@ export function createConnection() {
1818}
1919
2020export function startLanguageServer ( connection : vscode . Connection , ...plugins : LanguageServerPlugin [ ] ) {
21+
2122 startCommonLanguageServer ( connection , plugins , ( ) => ( {
2223 uriToFileName,
2324 fileNameToUri,
@@ -29,109 +30,76 @@ export function startLanguageServer(connection: vscode.Connection, ...plugins: L
2930 } ,
3031 } ,
3132 async loadTypeScript ( options ) {
32- const tsdkUri = options . typescript && 'tsdkUrl' in options . typescript
33+ const tsdkUrl = options . typescript && 'tsdkUrl' in options . typescript
3334 ? options . typescript . tsdkUrl
3435 : undefined ;
35- if ( ! tsdkUri ) {
36+ if ( ! tsdkUrl ) {
3637 return ;
3738 }
3839 const _module = globalThis . module ;
3940 globalThis . module = { exports : { } } as typeof _module ;
40- await import ( `${ tsdkUri } /typescript.js` ) ;
41+ await import ( `${ tsdkUrl } /typescript.js` ) ;
4142 const ts = globalThis . module . exports ;
4243 globalThis . module = _module ;
4344 return ts as typeof import ( 'typescript/lib/tsserverlibrary' ) ;
4445 } ,
4546 async loadTypeScriptLocalized ( options , locale ) {
46- const tsdkUri = options . typescript && 'tsdkUrl' in options . typescript
47+ const tsdkUrl = options . typescript && 'tsdkUrl' in options . typescript
4748 ? options . typescript . tsdkUrl
4849 : undefined ;
49- if ( ! tsdkUri ) {
50+ if ( ! tsdkUrl ) {
5051 return ;
5152 }
5253 try {
53- const json = await httpSchemaRequestHandler ( `${ tsdkUri } /${ locale } /diagnosticMessages.generated.json` ) ;
54+ const json = await httpSchemaRequestHandler ( `${ tsdkUrl } /${ locale } /diagnosticMessages.generated.json` ) ;
5455 if ( json ) {
5556 return JSON . parse ( json ) ;
5657 }
5758 }
5859 catch { }
5960 } ,
60- fs : createFs ( connection ) ,
61- getCancellationToken ( original ) {
62- return original ?? vscode . CancellationToken . None ;
63- } ,
64- } ) ) ;
65- }
66-
67- /**
68- * To avoid hitting the API hourly limit, we keep requests as low as possible.
69- */
70- function createFs ( connection : vscode . Connection ) : FileSystem {
71-
72- const readDirectoryResults = new Map < string , Promise < [ string , FileType ] [ ] > > ( ) ;
73-
74- return {
75- async stat ( uri ) {
76- if ( uri . startsWith ( '__invalid__:' ) ) {
77- return ;
78- }
79- if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) {
80- const text = await this . readFile ( uri ) ; // TODO: perf
81- if ( text !== undefined ) {
82- return {
83- type : FileType . File ,
84- size : text . length ,
85- ctime : - 1 ,
86- mtime : - 1 ,
87- } ;
61+ fs : {
62+ async stat ( uri ) {
63+ if ( uri . startsWith ( '__invalid__:' ) ) {
64+ return ;
8865 }
89- return undefined ;
90- }
91- const dirUri = uri . substring ( 0 , uri . lastIndexOf ( '/' ) ) ;
92- const baseName = uri . substring ( uri . lastIndexOf ( '/' ) + 1 ) ;
93- const entries = await this . readDirectory ( dirUri ) ;
94- const matches = entries . filter ( entry => entry [ 0 ] === baseName ) ;
95- if ( matches . length ) {
96- return {
97- type : matches . some ( entry => entry [ 1 ] === FileType . File ) ? FileType . File : matches [ 0 ] [ 1 ] ,
98- size : - 1 ,
99- ctime : - 1 ,
100- mtime : - 1 ,
101- } ;
102- }
103- } ,
104- async readFile ( uri ) {
105- if ( uri . startsWith ( '__invalid__:' ) ) {
106- return ;
107- }
108- if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) {
109- return await httpSchemaRequestHandler ( uri ) ;
110- }
111- const dirUri = uri . substring ( 0 , uri . lastIndexOf ( '/' ) ) ;
112- const baseName = uri . substring ( uri . lastIndexOf ( '/' ) + 1 ) ;
113- const entries = await this . readDirectory ( dirUri ) ;
114- const file = entries . filter ( entry => entry [ 0 ] === baseName && entry [ 1 ] === FileType . File ) ;
115- if ( file ) {
116- const text = await connection . sendRequest ( FsReadFileRequest . type , uri ) ;
117- if ( text !== undefined && text !== null ) {
118- return text ;
66+ if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) { // perf
67+ const text = await this . readFile ( uri ) ;
68+ if ( text !== undefined ) {
69+ return {
70+ type : FileType . File ,
71+ size : text . length ,
72+ ctime : - 1 ,
73+ mtime : - 1 ,
74+ } ;
75+ }
76+ return undefined ;
11977 }
120- }
78+ return await connection . sendRequest ( FsStatRequest . type , uri ) ;
79+ } ,
80+ async readFile ( uri ) {
81+ if ( uri . startsWith ( '__invalid__:' ) ) {
82+ return ;
83+ }
84+ if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) { // perf
85+ return await httpSchemaRequestHandler ( uri ) ;
86+ }
87+ return await connection . sendRequest ( FsReadFileRequest . type , uri ) ?? undefined ;
88+ } ,
89+ async readDirectory ( uri ) {
90+ if ( uri . startsWith ( '__invalid__:' ) ) {
91+ return [ ] ;
92+ }
93+ if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) { // perf
94+ return [ ] ;
95+ }
96+ return await connection . sendRequest ( FsReadDirectoryRequest . type , uri ) ;
97+ } ,
12198 } ,
122- async readDirectory ( uri ) {
123- if ( uri . startsWith ( '__invalid__:' ) ) {
124- return [ ] ;
125- }
126- if ( uri . startsWith ( 'http://' ) || uri . startsWith ( 'https://' ) ) {
127- return [ ] ;
128- }
129- if ( ! readDirectoryResults . has ( uri ) ) {
130- readDirectoryResults . set ( uri , connection . sendRequest ( FsReadDirectoryRequest . type , uri ) ) ;
131- }
132- return await readDirectoryResults . get ( uri ) ! ;
99+ getCancellationToken ( original ) {
100+ return original ?? vscode . CancellationToken . None ;
133101 } ,
134- } ;
102+ } ) ) ;
135103}
136104
137105function uriToFileName ( uri : string ) {
0 commit comments