@@ -856,7 +856,8 @@ export class Session {
856
856
return htmlLS . getFoldingRanges ( virtualHtmlDoc ) ;
857
857
}
858
858
859
- private onDefinition ( params : lsp . TextDocumentPositionParams ) : lsp . LocationLink [ ] | null {
859
+ private onDefinition ( params : lsp . TextDocumentPositionParams ) :
860
+ lsp . Location [ ] | lsp . LocationLink [ ] | null {
860
861
const lsInfo = this . getLSAndScriptInfo ( params . textDocument ) ;
861
862
if ( lsInfo === null ) {
862
863
return null ;
@@ -867,11 +868,20 @@ export class Session {
867
868
if ( ! definition || ! definition . definitions ) {
868
869
return null ;
869
870
}
871
+
872
+ const clientSupportsLocationLinks =
873
+ this . clientCapabilities . textDocument ?. definition ?. linkSupport ?? false ;
874
+
875
+ if ( ! clientSupportsLocationLinks ) {
876
+ return this . tsDefinitionsToLspLocations ( definition . definitions ) ;
877
+ }
878
+
870
879
const originSelectionRange = tsTextSpanToLspRange ( scriptInfo , definition . textSpan ) ;
871
880
return this . tsDefinitionsToLspLocationLinks ( definition . definitions , originSelectionRange ) ;
872
881
}
873
882
874
- private onTypeDefinition ( params : lsp . TextDocumentPositionParams ) : lsp . LocationLink [ ] | null {
883
+ private onTypeDefinition ( params : lsp . TextDocumentPositionParams ) :
884
+ lsp . Location [ ] | lsp . LocationLink [ ] | null {
875
885
const lsInfo = this . getLSAndScriptInfo ( params . textDocument ) ;
876
886
if ( lsInfo === null ) {
877
887
return null ;
@@ -882,6 +892,14 @@ export class Session {
882
892
if ( ! definitions ) {
883
893
return null ;
884
894
}
895
+
896
+ const clientSupportsLocationLinks =
897
+ this . clientCapabilities . textDocument ?. typeDefinition ?. linkSupport ?? false ;
898
+
899
+ if ( ! clientSupportsLocationLinks ) {
900
+ return this . tsDefinitionsToLspLocations ( definitions ) ;
901
+ }
902
+
885
903
return this . tsDefinitionsToLspLocationLinks ( definitions ) ;
886
904
}
887
905
@@ -965,6 +983,40 @@ export class Session {
965
983
} ) ;
966
984
}
967
985
986
+ private tsDefinitionsToLspLocations ( definitions : readonly ts . DefinitionInfo [ ] ) : lsp . Location [ ] {
987
+ const results : lsp . Location [ ] = [ ] ;
988
+ for ( const d of definitions ) {
989
+ const scriptInfo = this . projectService . getScriptInfo ( d . fileName ) ;
990
+
991
+ // Some definitions, like definitions of CSS files, may not be recorded files with a
992
+ // `scriptInfo` but are still valid definitions because they are files that exist. In this
993
+ // case, check to make sure that the text span of the definition is zero so that the file
994
+ // doesn't have to be read; if the span is non-zero, we can't do anything with this
995
+ // definition.
996
+ if ( ! scriptInfo && d . textSpan . length > 0 ) {
997
+ continue ;
998
+ }
999
+
1000
+ let mappedInfo = d ;
1001
+ let range = EMPTY_RANGE ;
1002
+ if ( scriptInfo ) {
1003
+ const project = this . getDefaultProjectForScriptInfo ( scriptInfo ) ;
1004
+ mappedInfo = project ? getMappedDefinitionInfo ( d , project ) : mappedInfo ;
1005
+ // After the DTS file maps to original source file, the `scriptInfo` should be updated.
1006
+ const originalScriptInfo =
1007
+ this . projectService . getScriptInfo ( mappedInfo . fileName ) ?? scriptInfo ;
1008
+ range = tsTextSpanToLspRange ( originalScriptInfo , mappedInfo . textSpan ) ;
1009
+ }
1010
+
1011
+ const uri = filePathToUri ( mappedInfo . fileName ) ;
1012
+ results . push ( {
1013
+ uri,
1014
+ range,
1015
+ } ) ;
1016
+ }
1017
+ return results ;
1018
+ }
1019
+
968
1020
private tsDefinitionsToLspLocationLinks (
969
1021
definitions : readonly ts . DefinitionInfo [ ] ,
970
1022
originSelectionRange ?: lsp . Range ) : lsp . LocationLink [ ] {
0 commit comments