@@ -889,7 +889,8 @@ export class Session {
889
889
return htmlLS . getFoldingRanges ( virtualHtmlDoc ) ;
890
890
}
891
891
892
- private onDefinition ( params : lsp . TextDocumentPositionParams ) : lsp . LocationLink [ ] | null {
892
+ private onDefinition ( params : lsp . TextDocumentPositionParams ) :
893
+ lsp . Location [ ] | lsp . LocationLink [ ] | null {
893
894
const lsInfo = this . getLSAndScriptInfo ( params . textDocument ) ;
894
895
if ( lsInfo === null ) {
895
896
return null ;
@@ -900,11 +901,20 @@ export class Session {
900
901
if ( ! definition || ! definition . definitions ) {
901
902
return null ;
902
903
}
904
+
905
+ const clientSupportsLocationLinks =
906
+ this . clientCapabilities . textDocument ?. definition ?. linkSupport ?? false ;
907
+
908
+ if ( ! clientSupportsLocationLinks ) {
909
+ return this . tsDefinitionsToLspLocations ( definition . definitions ) ;
910
+ }
911
+
903
912
const originSelectionRange = tsTextSpanToLspRange ( scriptInfo , definition . textSpan ) ;
904
913
return this . tsDefinitionsToLspLocationLinks ( definition . definitions , originSelectionRange ) ;
905
914
}
906
915
907
- private onTypeDefinition ( params : lsp . TextDocumentPositionParams ) : lsp . LocationLink [ ] | null {
916
+ private onTypeDefinition ( params : lsp . TextDocumentPositionParams ) :
917
+ lsp . Location [ ] | lsp . LocationLink [ ] | null {
908
918
const lsInfo = this . getLSAndScriptInfo ( params . textDocument ) ;
909
919
if ( lsInfo === null ) {
910
920
return null ;
@@ -915,6 +925,14 @@ export class Session {
915
925
if ( ! definitions ) {
916
926
return null ;
917
927
}
928
+
929
+ const clientSupportsLocationLinks =
930
+ this . clientCapabilities . textDocument ?. typeDefinition ?. linkSupport ?? false ;
931
+
932
+ if ( ! clientSupportsLocationLinks ) {
933
+ return this . tsDefinitionsToLspLocations ( definitions ) ;
934
+ }
935
+
918
936
return this . tsDefinitionsToLspLocationLinks ( definitions ) ;
919
937
}
920
938
@@ -998,6 +1016,40 @@ export class Session {
998
1016
} ) ;
999
1017
}
1000
1018
1019
+ private tsDefinitionsToLspLocations ( definitions : readonly ts . DefinitionInfo [ ] ) : lsp . Location [ ] {
1020
+ const results : lsp . Location [ ] = [ ] ;
1021
+ for ( const d of definitions ) {
1022
+ const scriptInfo = this . projectService . getScriptInfo ( d . fileName ) ;
1023
+
1024
+ // Some definitions, like definitions of CSS files, may not be recorded files with a
1025
+ // `scriptInfo` but are still valid definitions because they are files that exist. In this
1026
+ // case, check to make sure that the text span of the definition is zero so that the file
1027
+ // doesn't have to be read; if the span is non-zero, we can't do anything with this
1028
+ // definition.
1029
+ if ( ! scriptInfo && d . textSpan . length > 0 ) {
1030
+ continue ;
1031
+ }
1032
+
1033
+ let mappedInfo = d ;
1034
+ let range = EMPTY_RANGE ;
1035
+ if ( scriptInfo ) {
1036
+ const project = this . getDefaultProjectForScriptInfo ( scriptInfo ) ;
1037
+ mappedInfo = project ? getMappedDefinitionInfo ( d , project ) : mappedInfo ;
1038
+ // After the DTS file maps to original source file, the `scriptInfo` should be updated.
1039
+ const originalScriptInfo =
1040
+ this . projectService . getScriptInfo ( mappedInfo . fileName ) ?? scriptInfo ;
1041
+ range = tsTextSpanToLspRange ( originalScriptInfo , mappedInfo . textSpan ) ;
1042
+ }
1043
+
1044
+ const uri = filePathToUri ( mappedInfo . fileName ) ;
1045
+ results . push ( {
1046
+ uri,
1047
+ range,
1048
+ } ) ;
1049
+ }
1050
+ return results ;
1051
+ }
1052
+
1001
1053
private tsDefinitionsToLspLocationLinks (
1002
1054
definitions : readonly ts . DefinitionInfo [ ] ,
1003
1055
originSelectionRange ?: lsp . Range ) : lsp . LocationLink [ ] {
0 commit comments