hierarchy_tree.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Copyright (C) 2024 YouCompleteMe contributors
  2. #
  3. # This file is part of YouCompleteMe.
  4. #
  5. # YouCompleteMe is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # YouCompleteMe is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. from typing import Optional, List
  18. from ycm import vimsupport
  19. import os
  20. class HierarchyNode:
  21. def __init__( self, data, distance : int ):
  22. self._references : Optional[ List[ int ] ] = None
  23. self._data = data
  24. self._distance_from_root = distance
  25. def ToLocation( self, subindex : int ):
  26. location = self._data[ 'locations' ][ subindex ]
  27. line = location[ 'line_num' ]
  28. column = location[ 'column_num' ]
  29. file = location[ 'filepath' ]
  30. return file, line, column
  31. MAX_HANDLES_PER_INDEX = 1000000
  32. def handle_to_index( handle : int ):
  33. return abs( handle ) // MAX_HANDLES_PER_INDEX
  34. def handle_to_location_index( handle : int ):
  35. return abs( handle ) % MAX_HANDLES_PER_INDEX
  36. def make_handle( index : int, location_index : int ):
  37. return index * MAX_HANDLES_PER_INDEX + location_index
  38. class HierarchyTree:
  39. def __init__( self ):
  40. self._up_nodes : List[ HierarchyNode ] = []
  41. self._down_nodes : List[ HierarchyNode ] = []
  42. self._kind : str = ''
  43. def SetRootNode( self, items, kind : str ):
  44. if items:
  45. assert len( items ) == 1
  46. self._root_node_indices = [ 0 ]
  47. self._down_nodes.append( HierarchyNode( items[ 0 ], 0 ) )
  48. self._up_nodes.append( HierarchyNode( items[ 0 ], 0 ) )
  49. self._kind = kind
  50. return self.HierarchyToLines()
  51. return []
  52. def UpdateHierarchy( self, handle : int, items, direction : str ):
  53. current_index = handle_to_index( handle )
  54. nodes = self._down_nodes if direction == 'down' else self._up_nodes
  55. if items:
  56. nodes.extend( [
  57. HierarchyNode( item,
  58. nodes[ current_index ]._distance_from_root + 1 )
  59. for item in items ] )
  60. nodes[ current_index ]._references = list(
  61. range( len( nodes ) - len( items ), len( nodes ) ) )
  62. else:
  63. nodes[ current_index ]._references = []
  64. def Reset( self ):
  65. self._down_nodes = []
  66. self._up_nodes = []
  67. self._kind = ''
  68. def _HierarchyToLinesHelper( self, refs, use_down_nodes ):
  69. partial_result = []
  70. nodes = self._down_nodes if use_down_nodes else self._up_nodes
  71. for index in refs:
  72. next_node = nodes[ index ]
  73. indent = 2 * next_node._distance_from_root
  74. if index == 0:
  75. can_expand = ( self._down_nodes[ 0 ]._references is None or
  76. self._up_nodes[ 0 ]._references is None )
  77. else:
  78. can_expand = next_node._references is None
  79. symbol = '+' if can_expand else '-'
  80. name = next_node._data[ 'name' ]
  81. kind = next_node._data[ 'kind' ]
  82. if use_down_nodes:
  83. partial_result.extend( [
  84. (
  85. {
  86. 'indent': indent,
  87. 'icon': symbol,
  88. 'symbol': name,
  89. 'kind': kind,
  90. 'filepath': os.path.split( l[ 'filepath' ] )[ 1 ],
  91. 'line_num': str( l[ 'line_num' ] ),
  92. 'description': l.get( 'description', '' ).strip(),
  93. },
  94. make_handle( index, location_index )
  95. )
  96. for location_index, l in enumerate( next_node._data[ 'locations' ] )
  97. ] )
  98. else:
  99. partial_result.extend( [
  100. (
  101. {
  102. 'indent': indent,
  103. 'icon': symbol,
  104. 'symbol': name,
  105. 'kind': kind,
  106. 'filepath': os.path.split( l[ 'filepath' ] )[ 1 ],
  107. 'line_num': str( l[ 'line_num' ] ),
  108. 'description': l.get( 'description', '' ).strip(),
  109. },
  110. make_handle( index, location_index ) * -1
  111. )
  112. for location_index, l in enumerate( next_node._data[ 'locations' ] )
  113. ] )
  114. if next_node._references:
  115. partial_result.extend(
  116. self._HierarchyToLinesHelper(
  117. next_node._references, use_down_nodes ) )
  118. return partial_result
  119. def HierarchyToLines( self ):
  120. down_lines = self._HierarchyToLinesHelper( [ 0 ], True )
  121. up_lines = self._HierarchyToLinesHelper( [ 0 ], False )
  122. up_lines.reverse()
  123. return up_lines + down_lines[ 1: ]
  124. def JumpToItem( self, handle : int, command ):
  125. node_index = handle_to_index( handle )
  126. location_index = handle_to_location_index( handle )
  127. if handle >= 0:
  128. node = self._down_nodes[ node_index ]
  129. else:
  130. node = self._up_nodes[ node_index ]
  131. file, line, column = node.ToLocation( location_index )
  132. vimsupport.JumpToLocation( file, line, column, '', command )
  133. def ShouldResolveItem( self, handle : int, direction : str ):
  134. node_index = handle_to_index( handle )
  135. if ( ( handle >= 0 and direction == 'down' ) or
  136. ( handle <= 0 and direction == 'up' ) ):
  137. if direction == 'down':
  138. node = self._down_nodes[ node_index ]
  139. else:
  140. node = self._up_nodes[ node_index ]
  141. return node._references is None
  142. return True
  143. def ResolveArguments( self, handle : int, direction : str ):
  144. node_index = handle_to_index( handle )
  145. if self._kind == 'call':
  146. direction = 'outgoing' if direction == 'up' else 'incoming'
  147. else:
  148. direction = 'supertypes' if direction == 'up' else 'subtypes'
  149. if handle >= 0:
  150. node = self._down_nodes[ node_index ]
  151. else:
  152. node = self._up_nodes[ node_index ]
  153. return [
  154. f'Resolve{ self._kind.title() }HierarchyItem',
  155. node._data,
  156. direction
  157. ]
  158. def HandleToLocation( self, handle : int ):
  159. node_index = handle_to_index( handle )
  160. if handle >= 0:
  161. node = self._down_nodes[ node_index ]
  162. else:
  163. node = self._up_nodes[ node_index ]
  164. location_index = handle_to_location_index( handle )
  165. return node.ToLocation( location_index )
  166. def UpdateChangesRoot( self, handle : int, direction : str ):
  167. return ( ( handle < 0 and direction == 'down' ) or
  168. ( handle > 0 and direction == 'up' ) )