8
8
SymbolInformation , SymbolKind , CompletionItem , Location , SignatureHelp , SignatureInformation , ParameterInformation ,
9
9
Definition , TextEdit , TextDocument , Diagnostic , DiagnosticSeverity , Range , CompletionItemKind , Hover ,
10
10
DocumentHighlight , DocumentHighlightKind , CompletionList , Position , FormattingOptions , FoldingRange , FoldingRangeKind , SelectionRange ,
11
- LanguageMode , Settings , SemanticTokenData , Workspace , DocumentContext , CompletionItemData , isCompletionItemData
11
+ LanguageMode , Settings , SemanticTokenData , Workspace , DocumentContext , CompletionItemData , isCompletionItemData , FILE_PROTOCOL , DocumentUri
12
12
} from './languageModes' ;
13
13
import { getWordAtText , isWhitespaceOnly , repeat } from '../utils/strings' ;
14
14
import { HTMLDocumentRegions } from './embeddedSupport' ;
@@ -77,18 +77,24 @@ function getLanguageServiceHost(scriptKind: ts.ScriptKind) {
77
77
78
78
}
79
79
} ;
80
- return ts . createLanguageService ( host ) ;
80
+ return {
81
+ service : ts . createLanguageService ( host ) ,
82
+ loadLibrary : libs . loadLibrary ,
83
+ } ;
81
84
} ) ;
82
85
return {
83
86
async getLanguageService ( jsDocument : TextDocument ) : Promise < ts . LanguageService > {
84
87
currentTextDocument = jsDocument ;
85
- return jsLanguageService ;
88
+ return ( await jsLanguageService ) . service ;
86
89
} ,
87
90
getCompilationSettings ( ) {
88
91
return compilerOptions ;
89
92
} ,
93
+ async loadLibrary ( fileName : string ) {
94
+ return ( await jsLanguageService ) . loadLibrary ( fileName ) ;
95
+ } ,
90
96
dispose ( ) {
91
- jsLanguageService . then ( s => s . dispose ( ) ) ;
97
+ jsLanguageService . then ( s => s . service . dispose ( ) ) ;
92
98
}
93
99
} ;
94
100
}
@@ -104,6 +110,8 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache
104
110
const host = getLanguageServiceHost ( languageId === 'javascript' ? ts . ScriptKind . JS : ts . ScriptKind . TS ) ;
105
111
const globalSettings : Settings = { } ;
106
112
113
+ const libParentUri = `${ FILE_PROTOCOL } ://${ languageId } /libs/` ;
114
+
107
115
function updateHostSettings ( settings : Settings ) {
108
116
const hostSettings = host . getCompilationSettings ( ) ;
109
117
hostSettings . experimentalDecorators = settings ?. [ 'js/ts' ] ?. implicitProjectConfig ?. experimentalDecorators ;
@@ -302,12 +310,25 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache
302
310
const jsLanguageService = await host . getLanguageService ( jsDocument ) ;
303
311
const definition = jsLanguageService . getDefinitionAtPosition ( jsDocument . uri , jsDocument . offsetAt ( position ) ) ;
304
312
if ( definition ) {
305
- return definition . filter ( d => d . fileName === jsDocument . uri ) . map ( d => {
306
- return {
307
- uri : document . uri ,
308
- range : convertRange ( jsDocument , d . textSpan )
309
- } ;
310
- } ) ;
313
+ return ( await Promise . all ( definition . map ( async d => {
314
+ if ( d . fileName === jsDocument . uri ) {
315
+ return {
316
+ uri : document . uri ,
317
+ range : convertRange ( jsDocument , d . textSpan )
318
+ } ;
319
+ } else {
320
+ const libUri = libParentUri + d . fileName ;
321
+ const content = await host . loadLibrary ( d . fileName ) ;
322
+ if ( ! content ) {
323
+ return undefined ;
324
+ }
325
+ const libDocument = TextDocument . create ( libUri , languageId , 1 , content ) ;
326
+ return {
327
+ uri : libUri ,
328
+ range : convertRange ( libDocument , d . textSpan )
329
+ } ;
330
+ }
331
+ } ) ) ) . filter ( d => ! ! d ) ;
311
332
}
312
333
return null ;
313
334
} ,
@@ -402,6 +423,12 @@ export function getJavaScriptMode(documentRegions: LanguageModelCache
402
423
getSemanticTokenLegend ( ) : { types : string [ ] ; modifiers : string [ ] } {
403
424
return getSemanticTokenLegend ( ) ;
404
425
} ,
426
+ async getTextDocumentContent ( documentUri : DocumentUri ) : Promise < string | undefined > {
427
+ if ( documentUri . startsWith ( libParentUri ) ) {
428
+ return host . loadLibrary ( documentUri . substring ( libParentUri . length ) ) ;
429
+ }
430
+ return undefined ;
431
+ } ,
405
432
dispose ( ) {
406
433
host . dispose ( ) ;
407
434
jsDocuments . dispose ( ) ;
0 commit comments