Skip to content

Commit

Permalink
Support changing the current hierarchy root node
Browse files Browse the repository at this point in the history
  • Loading branch information
bstaletic committed Feb 11, 2024
1 parent 09e2e32 commit e9c1e0f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
26 changes: 14 additions & 12 deletions autoload/youcompleteme/hierarchy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,21 @@ endfunction

function! s:MenuFilter( winid, key )
if a:key == "\<S-Tab>"
if s:lines_and_handles[ s:select - 1 ][ 1 ] <= 0 " TODO: switching root not impl
call popup_close( s:popup_id, [ s:select - 1, 'resolve_up' ] )
endif
let will_change_root = s:lines_and_handles[ s:select - 1 ][ 1 ] > 0
call popup_close( s:popup_id, [ s:select - 1, 'resolve_up', will_change_root ] )
return 1
endif
if a:key == "\<Tab>"
if s:lines_and_handles[ s:select - 1 ][ 1 ] >= 0 " TODO: switching root not impl
call popup_close( s:popup_id, [ s:select - 1, 'resolve_down' ] )
endif
let will_change_root = s:lines_and_handles[ s:select - 1 ][ 1 ] < 0
call popup_close( s:popup_id, [ s:select - 1, 'resolve_down', will_change_root ] )
return 1
endif
if a:key == "\<Esc>"
call popup_close( s:popup_id, [ s:select - 1, 'cancel' ] )
call popup_close( s:popup_id, [ s:select - 1, 'cancel', v:none ] )
return 1
endif
if a:key == "\<CR>"
call popup_close( s:popup_id, [ s:select - 1, 'jump' ] )
call popup_close( s:popup_id, [ s:select - 1, 'jump', v:none ] )
return 1
endif
if a:key == "\<Up>"
Expand Down Expand Up @@ -93,9 +91,9 @@ function! s:MenuCallback( winid, result )
let operation = a:result[ 1 ]
let selection = a:result[ 0 ]
if operation == 'resolve_down'
call s:ResolveItem( selection, 'down' )
call s:ResolveItem( selection, 'down', a:result[ 2 ] )
elseif operation == 'resolve_up'
call s:ResolveItem( selection, 'up' )
call s:ResolveItem( selection, 'up', a:result[ 2 ] )
else
if operation == 'jump'
let handle = s:lines_and_handles[ selection ][ 1 ]
Expand All @@ -119,16 +117,20 @@ function! s:SetupMenu()
\ 'call cursor( [' . string( s:select ) . ', 1 ] )' )
endfunction

function! s:ResolveItem( choice, direction )
function! s:ResolveItem( choice, direction, will_change_root )
let handle = s:lines_and_handles[ a:choice ][ 1 ]
if py3eval(
\ 'ycm_state.ShouldResolveItem( vimsupport.GetIntValue( "handle" ), vim.eval( "a:direction" ) )' )
let lines_and_handles_with_offset = py3eval(
\ 'ycm_state.UpdateCurrentHierarchy( ' .
\ 'vimsupport.GetIntValue( "handle" ), ' .
\ 'vim.eval( "a:direction" ) )' )
let s:select += lines_and_handles_with_offset[ 1 ]
let s:lines_and_handles = lines_and_handles_with_offset[ 0 ]
if a:will_change_root
let s:select = 1 + indexof( s:lines_and_handles, { i, v -> v[0][0] =~ "[-+]" } )
else
let s:select += lines_and_handles_with_offset[ 1 ]
endif
endif
call s:SetupMenu()
endfunction
57 changes: 36 additions & 21 deletions python/ycm/hierarchy_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,27 @@ def SetRootNode( self, items, kind : str ):

def UpdateHierarchy( self, handle : int, items, direction : str ):
current_index = abs( handle ) // 1000000
nodes = self._down_nodes if direction == 'down' else self._up_nodes
if items:
nodes.extend( [
HierarchyNode( item,
nodes[ current_index ]._distance_from_root + 1 )
for item in items ] )
nodes[ current_index ]._references = list(
range( len( nodes ) - len( items ),
len( nodes ) ) )
if ( ( handle >= 0 and direction == 'down' ) or
( handle <= 0 and direction == 'up' ) ):
nodes = self._down_nodes if direction == 'down' else self._up_nodes
if items:
nodes.extend( [
HierarchyNode( item,
nodes[ current_index ]._distance_from_root + 1 )
for item in items ] )
nodes[ current_index ]._references = list(
range( len( nodes ) - len( items ), len( nodes ) ) )
else:
nodes[ current_index ]._references = []
else:
nodes[ current_index ]._references = []
if direction == 'up':
current_node = self._down_nodes[ current_index ]
else:
current_node = self._up_nodes[ current_index ]
old_kind = self._kind
self.Reset()
self.SetRootNode( [ current_node._data ], old_kind )
self.UpdateHierarchy( 0, items, direction )


def Reset( self ):
Expand All @@ -81,29 +91,31 @@ def _HierarchyToLinesHelper( self, refs, use_down_nodes ):
next_node = nodes[ i ]
indent = 2 * next_node._distance_from_root
if i == 0:
can_expand = self._down_nodes[ 0 ]._references is None or self._up_nodes[ 0 ]._references is None
can_expand = ( self._down_nodes[ 0 ]._references is None or
self._up_nodes[ 0 ]._references is None )
else:
can_expand = next_node._references is None
symbol = '+' if can_expand else '-'
name = next_node._data[ 'name' ]
kind = next_node._data[ 'kind' ]
if use_down_nodes:
partial_result.extend( [
( ' ' * indent + symbol + kind + ': ' + name + '\t' +
( ' ' * indent + symbol + kind + ': ' + name + 3 * '\t' +
os.path.split( l[ 'filepath' ] )[ 1 ] + ':' +
str( l[ 'line_num' ] ) + '\t' + l[ 'description' ],
str( l[ 'line_num' ] ) + 3 * '\t' + l[ 'description' ],
( i * 1000000 + j ) )
for j, l in enumerate( next_node._data[ 'locations' ] ) ] )
else:
partial_result.extend( [
( ' ' * indent + symbol + kind + ': ' + name + '\t' +
( ' ' * indent + symbol + kind + ': ' + name + 3 * '\t' +
os.path.split( l[ 'filepath' ] )[ 1 ] + ':' +
str( l[ 'line_num' ] ) + '\t' + l[ 'description' ],
str( l[ 'line_num' ] ) + 3 * '\t' + l[ 'description' ],
( i * 1000000 + j ) * -1 )
for j, l in enumerate( next_node._data[ 'locations' ] ) ] )
if next_node._references:
partial_result.extend(
self._HierarchyToLinesHelper( next_node._references, use_down_nodes ) )
self._HierarchyToLinesHelper(
next_node._references, use_down_nodes ) )
return partial_result

def HierarchyToLines( self ):
Expand All @@ -126,11 +138,14 @@ def JumpToItem( self, handle : int, command ):

def ShouldResolveItem( self, handle : int, direction : str ):
node_index = abs( handle ) // 1000000
if direction == 'down':
node = self._down_nodes[ node_index ]
else:
node = self._up_nodes[ node_index ]
return node._references is None
if ( ( handle >= 0 and direction == 'down' ) or
( handle <= 0 and direction == 'up' ) ):
if direction == 'down':
node = self._down_nodes[ node_index ]
else:
node = self._up_nodes[ node_index ]
return node._references is None
return True


def ResolveArguments( self, handle : int, direction : str ):
Expand Down

0 comments on commit e9c1e0f

Please sign in to comment.