@@ -347,29 +347,26 @@ export class Session {
347
347
}
348
348
}
349
349
350
- private onDefinition ( params : lsp . TextDocumentPositionParams ) {
351
- const { position, textDocument} = params ;
352
- const filePath = uriToFilePath ( textDocument . uri ) ;
353
- const scriptInfo = this . projectService . getScriptInfo ( filePath ) ;
354
- if ( ! scriptInfo ) {
355
- this . connection . console . log ( `Script info not found for ${ filePath } ` ) ;
350
+ private onDefinition ( params : lsp . TextDocumentPositionParams ) : lsp . LocationLink [ ] | undefined {
351
+ const lsInfo = this . getLSAndScriptInfo ( params . textDocument ) ;
352
+ if ( lsInfo === undefined ) {
356
353
return ;
357
354
}
358
-
359
- const { fileName} = scriptInfo ;
360
- const langSvc = this . getDefaultLanguageService ( scriptInfo ) ;
361
- if ( ! langSvc ) {
362
- return ;
363
- }
364
-
365
- const offset = lspPositionToTsPosition ( scriptInfo , position ) ;
366
- const definition = langSvc . getDefinitionAndBoundSpan ( fileName , offset ) ;
355
+ const { languageService, scriptInfo} = lsInfo ;
356
+ const offset = lspPositionToTsPosition ( scriptInfo , params . position ) ;
357
+ const definition = languageService . getDefinitionAndBoundSpan ( scriptInfo . fileName , offset ) ;
367
358
if ( ! definition || ! definition . definitions ) {
368
359
return ;
369
360
}
370
361
const originSelectionRange = tsTextSpanToLspRange ( scriptInfo , definition . textSpan ) ;
362
+ return this . tsDefinitionsToLspLocationLinks ( definition . definitions , originSelectionRange ) ;
363
+ }
364
+
365
+ private tsDefinitionsToLspLocationLinks (
366
+ definitions : readonly ts . DefinitionInfo [ ] ,
367
+ originSelectionRange ?: lsp . Range ) : lsp . LocationLink [ ] {
371
368
const results : lsp . LocationLink [ ] = [ ] ;
372
- for ( const d of definition . definitions ) {
369
+ for ( const d of definitions ) {
373
370
const scriptInfo = this . projectService . getScriptInfo ( d . fileName ) ;
374
371
375
372
// Some definitions, like definitions of CSS files, may not be recorded files with a
@@ -393,22 +390,31 @@ export class Session {
393
390
return results ;
394
391
}
395
392
396
- private onHover ( params : lsp . TextDocumentPositionParams ) {
397
- const { position , textDocument } = params ;
393
+ private getLSAndScriptInfo ( textDocument : lsp . TextDocumentIdentifier ) :
394
+ { languageService : ts . LanguageService , scriptInfo : ts . server . ScriptInfo } | undefined {
398
395
const filePath = uriToFilePath ( textDocument . uri ) ;
399
- if ( ! filePath ) {
400
- return ;
401
- }
402
396
const scriptInfo = this . projectService . getScriptInfo ( filePath ) ;
403
397
if ( ! scriptInfo ) {
398
+ this . connection . console . log ( `Script info not found for ${ filePath } ` ) ;
404
399
return ;
405
400
}
406
- const langSvc = this . getDefaultLanguageService ( scriptInfo ) ;
407
- if ( ! langSvc ) {
401
+
402
+ const languageService = this . getDefaultLanguageService ( scriptInfo ) ;
403
+ if ( ! languageService ) {
404
+ return ;
405
+ }
406
+
407
+ return { languageService, scriptInfo} ;
408
+ }
409
+
410
+ private onHover ( params : lsp . TextDocumentPositionParams ) {
411
+ const lsInfo = this . getLSAndScriptInfo ( params . textDocument ) ;
412
+ if ( lsInfo === undefined ) {
408
413
return ;
409
414
}
410
- const offset = lspPositionToTsPosition ( scriptInfo , position ) ;
411
- const info = langSvc . getQuickInfoAtPosition ( scriptInfo . fileName , offset ) ;
415
+ const { languageService, scriptInfo} = lsInfo ;
416
+ const offset = lspPositionToTsPosition ( scriptInfo , params . position ) ;
417
+ const info = languageService . getQuickInfoAtPosition ( scriptInfo . fileName , offset ) ;
412
418
if ( ! info ) {
413
419
return ;
414
420
}
@@ -437,31 +443,22 @@ export class Session {
437
443
}
438
444
439
445
private onCompletion ( params : lsp . CompletionParams ) {
440
- const { position, textDocument} = params ;
441
- const filePath = uriToFilePath ( textDocument . uri ) ;
442
- if ( ! filePath ) {
443
- return ;
444
- }
445
- const scriptInfo = this . projectService . getScriptInfo ( filePath ) ;
446
- if ( ! scriptInfo ) {
447
- return ;
448
- }
449
- const { fileName} = scriptInfo ;
450
- const langSvc = this . getDefaultLanguageService ( scriptInfo ) ;
451
- if ( ! langSvc ) {
446
+ const lsInfo = this . getLSAndScriptInfo ( params . textDocument ) ;
447
+ if ( lsInfo === undefined ) {
452
448
return ;
453
449
}
454
- const offset = lspPositionToTsPosition ( scriptInfo , position ) ;
455
- const completions = langSvc . getCompletionsAtPosition (
456
- fileName , offset ,
450
+ const { languageService, scriptInfo} = lsInfo ;
451
+ const offset = lspPositionToTsPosition ( scriptInfo , params . position ) ;
452
+ const completions = languageService . getCompletionsAtPosition (
453
+ scriptInfo . fileName , offset ,
457
454
{
458
455
// options
459
456
} ) ;
460
457
if ( ! completions ) {
461
458
return ;
462
459
}
463
460
return completions . entries . map (
464
- ( e ) => tsCompletionEntryToLspCompletionItem ( e , position , scriptInfo ) ) ;
461
+ ( e ) => tsCompletionEntryToLspCompletionItem ( e , params . position , scriptInfo ) ) ;
465
462
}
466
463
467
464
/**
0 commit comments