Skip to content

Commit 4df521f

Browse files
committed
Allow choosing "which range" should be used when converting LSP Location-like objects
This fixes all of the remaining bugs when requesting outgoing calls of an incoming call hierarchy item. Specifically, we *want* `selectionRange` for the hierarchy root and for `root_location` of incoming call hierarchy items. JDT is special and needs `range` in the second case.
1 parent c23eff5 commit 4df521f

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

ycmd/completers/java/java_completer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,18 @@ def GetServerName( self ):
650650

651651
def GetCommandLine( self ):
652652
return self._command
653+
654+
655+
def Hierarchy( self, request_data, args ):
656+
# JDT.LS is a special snowflake and needs special snowflake treatement
657+
# See: https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/3184
658+
result = super().Hierarchy( request_data, args )
659+
preparation_item, direction, kind = args
660+
if kind == 'call' and direction == 'incoming':
661+
for item in result:
662+
# The base class does almost the same,
663+
# but uses `selectionRange` instead of `range`.
664+
item[ 'root_location' ] = responses.BuildGoToResponseFromLocation(
665+
*language_server_completer._LspLocationToLocationAndDescription(
666+
request_data, item[ 'from' ] ) )
667+
return result

ycmd/completers/language_server/language_server_completer.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,9 @@ def InitialHierarchy( self, request_data, args ):
26862686

26872687
preparation_item[ 0 ][ 'locations' ] = [
26882688
responses.BuildGoToResponseFromLocation(
2689-
*_LspLocationToLocationAndDescription( request_data, location ) )
2689+
*_LspLocationToLocationAndDescription( request_data,
2690+
location,
2691+
'selectionRange' ) )
26902692
for location in preparation_item ]
26912693
kind_string = lsp.SYMBOL_KIND[ preparation_item[ 0 ][ 'kind' ] ]
26922694
preparation_item[ 0 ][ 'kind' ] = kind_string
@@ -2735,12 +2737,10 @@ def Hierarchy( self, request_data, args ):
27352737
for location in lsp_locations ]
27362738

27372739
if direction == 'incomingCalls':
2738-
loc = {
2739-
'uri': hierarchy_item[ 'uri' ],
2740-
'range': hierarchy_item[ 'range' ]
2741-
}
27422740
item[ 'root_location' ] = responses.BuildGoToResponseFromLocation(
2743-
*_LspLocationToLocationAndDescription( request_data, loc ) )
2741+
*_LspLocationToLocationAndDescription( request_data,
2742+
hierarchy_item,
2743+
'selectionRange' ) )
27442744
else:
27452745
item[ 'kind' ] = lsp.SYMBOL_KIND[ item[ 'kind' ] ]
27462746
item[ 'locations' ] = [
@@ -3396,7 +3396,9 @@ def BuildGoToLocationFromSymbol( symbol ):
33963396
return locations
33973397

33983398

3399-
def _LspLocationToLocationAndDescription( request_data, location ):
3399+
def _LspLocationToLocationAndDescription( request_data,
3400+
location,
3401+
range_property = 'range' ):
34003402
"""Convert a LSP Location to a ycmd location."""
34013403
try:
34023404
filename = lsp.UriToFilePath( location[ 'uri' ] )
@@ -3413,9 +3415,10 @@ def _LspLocationToLocationAndDescription( request_data, location ):
34133415
'GoTo location' )
34143416
file_contents = []
34153417

3418+
range = location[ range_property ]
34163419
return _BuildLocationAndDescription( filename,
34173420
file_contents,
3418-
location[ 'range' ][ 'start' ] )
3421+
range[ 'start' ] )
34193422

34203423

34213424
def _LspToYcmdLocation( file_contents, location ):

ycmd/tests/rust/subcommands_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,14 +635,14 @@ def test_Subcommands_IncomingCallHierarchy( self, app ):
635635
LocationMatcher( filepath, 6, 11 ) ),
636636
# rust-analyzer always returns column 1 for root_location,
637637
# which is useless for us... unfortunately.
638-
'root_location': LocationMatcher( filepath, 5, 1 )
638+
'root_location': LocationMatcher( filepath, 5, 4 )
639639
} ),
640640
has_entries( {
641641
'locations': contains_exactly(
642642
LocationMatcher( filepath, 11, 5 ) ),
643643
# rust-analyzer always returns column 1 for root_location,
644644
# which is useless for us... unfortunately.
645-
'root_location': LocationMatcher( filepath, 9, 1 )
645+
'root_location': LocationMatcher( filepath, 9, 4 )
646646
} )
647647
),
648648
requests.codes.ok ],
@@ -653,7 +653,7 @@ def test_Subcommands_IncomingCallHierarchy( self, app ):
653653
LocationMatcher( filepath, 10, 13 ) ),
654654
# rust-analyzer always returns column 1 for root_location,
655655
# which is useless for us... unfortunately.
656-
'root_location': LocationMatcher( filepath, 9, 1 )
656+
'root_location': LocationMatcher( filepath, 9, 4 )
657657
} )
658658
),
659659
requests.codes.ok ],

0 commit comments

Comments
 (0)