hierarchy_tree.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 ToRootLocation( self, subindex : int ):
  26. location = self._data.get( 'root_location' )
  27. if location:
  28. file = location[ 'filepath' ]
  29. line = location[ 'line_num' ]
  30. column = location[ 'column_num' ]
  31. return file, line, column
  32. else:
  33. return self.ToLocation( subindex )
  34. def ToLocation( self, subindex : int ):
  35. location = self._data[ 'locations' ][ subindex ]
  36. line = location[ 'line_num' ]
  37. column = location[ 'column_num' ]
  38. file = location[ 'filepath' ]
  39. return file, line, column
  40. MAX_HANDLES_PER_INDEX = 1000000
  41. def handle_to_index( handle : int ):
  42. return abs( handle ) // MAX_HANDLES_PER_INDEX
  43. def handle_to_location_index( handle : int ):
  44. return abs( handle ) % MAX_HANDLES_PER_INDEX
  45. def make_handle( index : int, location_index : int ):
  46. return index * MAX_HANDLES_PER_INDEX + location_index
  47. class HierarchyTree:
  48. def __init__( self ):
  49. self._up_nodes : List[ HierarchyNode ] = []
  50. self._down_nodes : List[ HierarchyNode ] = []
  51. self._kind : str = ''
  52. def SetRootNode( self, items, kind : str ):
  53. if items:
  54. assert len( items ) == 1
  55. self._root_node_indices = [ 0 ]
  56. self._down_nodes.append( HierarchyNode( items[ 0 ], 0 ) )
  57. self._up_nodes.append( HierarchyNode( items[ 0 ], 0 ) )
  58. self._kind = kind
  59. return self.HierarchyToLines()
  60. return []
  61. def UpdateHierarchy( self, handle : int, items, direction : str ):
  62. current_index = handle_to_index( handle )
  63. nodes = self._down_nodes if direction == 'down' else self._up_nodes
  64. if items:
  65. nodes.extend( [
  66. HierarchyNode( item,
  67. nodes[ current_index ]._distance_from_root + 1 )
  68. for item in items ] )
  69. nodes[ current_index ]._references = list(
  70. range( len( nodes ) - len( items ), len( nodes ) ) )
  71. else:
  72. nodes[ current_index ]._references = []
  73. def Reset( self ):
  74. self._down_nodes = []
  75. self._up_nodes = []
  76. self._kind = ''
  77. def _HierarchyToLinesHelper( self, refs, use_down_nodes ):
  78. partial_result = []
  79. nodes = self._down_nodes if use_down_nodes else self._up_nodes
  80. for index in refs:
  81. next_node = nodes[ index ]
  82. indent = 2 * next_node._distance_from_root
  83. if index == 0:
  84. can_expand = ( self._down_nodes[ 0 ]._references is None or
  85. self._up_nodes[ 0 ]._references is None )
  86. else:
  87. can_expand = next_node._references is None
  88. symbol = '+' if can_expand else '-'
  89. name = next_node._data[ 'name' ]
  90. kind = next_node._data[ 'kind' ]
  91. if use_down_nodes:
  92. partial_result.extend( [
  93. (
  94. {
  95. 'indent': indent,
  96. 'icon': symbol,
  97. 'symbol': name,
  98. 'kind': kind,
  99. 'filepath': os.path.split( l[ 'filepath' ] )[ 1 ],
  100. 'line_num': str( l[ 'line_num' ] ),
  101. 'description': l.get( 'description', '' ).strip(),
  102. },
  103. make_handle( index, location_index )
  104. )
  105. for location_index, l in enumerate( next_node._data[ 'locations' ] )
  106. ] )
  107. else:
  108. partial_result.extend( [
  109. (
  110. {
  111. 'indent': indent,
  112. 'icon': symbol,
  113. 'symbol': name,
  114. 'kind': kind,
  115. 'filepath': os.path.split( l[ 'filepath' ] )[ 1 ],
  116. 'line_num': str( l[ 'line_num' ] ),
  117. 'description': l.get( 'description', '' ).strip(),
  118. },
  119. make_handle( index, location_index ) * -1
  120. )
  121. for location_index, l in enumerate( next_node._data[ 'locations' ] )
  122. ] )
  123. if next_node._references:
  124. partial_result.extend(
  125. self._HierarchyToLinesHelper(
  126. next_node._references, use_down_nodes ) )
  127. return partial_result
  128. def HierarchyToLines( self ):
  129. down_lines = self._HierarchyToLinesHelper( [ 0 ], True )
  130. up_lines = self._HierarchyToLinesHelper( [ 0 ], False )
  131. up_lines.reverse()
  132. return up_lines + down_lines[ 1: ]
  133. def JumpToItem( self, handle : int, command ):
  134. node_index = handle_to_index( handle )
  135. location_index = handle_to_location_index( handle )
  136. if handle >= 0:
  137. node = self._down_nodes[ node_index ]
  138. else:
  139. node = self._up_nodes[ node_index ]
  140. file, line, column = node.ToLocation( location_index )
  141. vimsupport.JumpToLocation( file, line, column, '', command )
  142. def ShouldResolveItem( self, handle : int, direction : str ):
  143. node_index = handle_to_index( handle )
  144. if ( ( handle >= 0 and direction == 'down' ) or
  145. ( handle <= 0 and direction == 'up' ) ):
  146. if direction == 'down':
  147. node = self._down_nodes[ node_index ]
  148. else:
  149. node = self._up_nodes[ node_index ]
  150. return node._references is None
  151. return True
  152. def ResolveArguments( self, handle : int, direction : str ):
  153. node_index = handle_to_index( handle )
  154. if self._kind == 'call':
  155. direction = 'outgoing' if direction == 'up' else 'incoming'
  156. else:
  157. direction = 'supertypes' if direction == 'up' else 'subtypes'
  158. if handle >= 0:
  159. node = self._down_nodes[ node_index ]
  160. else:
  161. node = self._up_nodes[ node_index ]
  162. return [
  163. f'Resolve{ self._kind.title() }HierarchyItem',
  164. node._data,
  165. direction
  166. ]
  167. def HandleToRootLocation( self, handle : int ):
  168. node_index = handle_to_index( handle )
  169. if handle >= 0:
  170. node = self._down_nodes[ node_index ]
  171. else:
  172. node = self._up_nodes[ node_index ]
  173. location_index = handle_to_location_index( handle )
  174. return node.ToRootLocation( location_index )
  175. def UpdateChangesRoot( self, handle : int, direction : str ):
  176. return ( ( handle < 0 and direction == 'down' ) or
  177. ( handle > 0 and direction == 'up' ) )