Skip to content

Commit

Permalink
Basic popup aesthetics
Browse files Browse the repository at this point in the history
We try to be a bit consistent with the finder poppup, but
implementation-wise this is simpler. The idea is that there are 3
columns, each having 1/3 of the popup width. We fix the width of the
popup (like we do for the finder) and set the tabstop to 1/3 of the
internal width (core_width).

Then when displaying text, we truncate "columnns" according to that
tabstop (to avoid mess). To do this, we pass structured data from the
python layer to vimscript and construct the line text there. This will
also help later when we add in the syntax highlight (text properties)
like we have for the finder popup.
  • Loading branch information
puremourning committed May 11, 2024
1 parent ae3f562 commit 40877dc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
44 changes: 37 additions & 7 deletions autoload/youcompleteme/hierarchy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ scriptencoding utf-8

let s:popup_id = -1
let s:lines_and_handles = v:none
" 1-based index of the selected item in the popup
" -1 means none set
" 0 means nothing, (Invalid)
let s:select = -1
let s:kind = ''

Expand Down Expand Up @@ -54,13 +57,17 @@ endfunction

function! s:MenuFilter( winid, key )
if a:key == "\<S-Tab>"
" ROot changes if we're showing super-tree of a sub-tree of the root
" (indeicated by the handle being positive)
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>"
" Root changes if we're showing sub-tree of a super-tree of the root
" (indeicated by the handle being negative)
let will_change_root = s:lines_and_handles[ s:select - 1 ][ 1 ] < 0
call popup_close(
\ s:popup_id,
Expand Down Expand Up @@ -119,14 +126,32 @@ function! s:MenuCallback( winid, result )
endfunction

function! s:SetUpMenu()
let menu_lines = []
for line_and_item in s:lines_and_handles
call add( menu_lines, line_and_item[ 0 ] )
endfor
let s:popup_id = popup_menu( menu_lines, #{
let s:popup_id = popup_menu( [], #{
\ filter: funcref( 's:MenuFilter' ),
\ callback: funcref( 's:MenuCallback' )
\ callback: funcref( 's:MenuCallback' ),
\ wrap: 0,
\ minwidth: &columns * 90/100,
\ maxwidth: &columns * 90/100,
\ maxheight: &lines * 75/100,
\ scrollbar: 1,
\ padding: [ 0, 0, 0, 0 ],
\ } )
let menu_lines = []
let popup_width = popup_getpos( s:popup_id ).core_width
let tabstop = popup_width / 3
for [ item, handle ] in s:lines_and_handles
let name = repeat( ' ', item.indent ) .. item.icon .. item.symbol
" TODO: Explain (understand) why it's -2 not -1 for the space (padding?)
let line = name[ : tabstop -2 ]
\ . "\t"
\ .. item.filepath[ : tabstop -2 ]
\ . "\t"
\ .. item.description[ : tabstop - 2 ]
call add( menu_lines, line )
endfor
call popup_settext( s:popup_id, menu_lines )
call win_execute( s:popup_id,
\ 'setlocal tabstop=' . tabstop )
call win_execute( s:popup_id,
\ 'call cursor( [' . string( s:select ) . ', 1 ] )' )
endfunction
Expand All @@ -141,8 +166,13 @@ function! s:ResolveItem( choice, direction, will_change_root )
\ 'vim.eval( "a:direction" ) )' )
let s:lines_and_handles = lines_and_handles_with_offset[ 0 ]
if a:will_change_root
" When re-rooting the tree, put the cursor on the new "root" item, as this
" helps with orientation. This behaviour is consistent with an expansion
" where we _don't_ re-root the tree, so feels more natural than anything
" else.
" The new root is the element with indent of 0.
let s:select = 1 + indexof( s:lines_and_handles,
\ { i, v -> v[0][0] =~ "[-+]" } )
\ { i, v -> v[0].indent == 0 } )
else
let s:select += lines_and_handles_with_offset[ 1 ]
endif
Expand Down
34 changes: 24 additions & 10 deletions python/ycm/hierarchy_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,32 @@ def _HierarchyToLinesHelper( self, refs, use_down_nodes ):
kind = next_node._data[ 'kind' ]
if use_down_nodes:
partial_result.extend( [
( ' ' * indent + symbol + kind + ': ' + name + 3 * '\t' +
os.path.split( l[ 'filepath' ] )[ 1 ] + ':' +
str( l[ 'line_num' ] ) + 3 * '\t' + l.get( 'description', '' ),
( i * 1000000 + j ) )
for j, l in enumerate( next_node._data[ 'locations' ] ) ] )
(
{
'indent': indent,
'icon': symbol,
'symbol': kind + ': ' + name,
'filepath': os.path.split( l[ 'filepath' ] )[ 1 ] + ':' + str( l[ 'line_num' ] ),
'description': l.get( 'description', '' ),
},
i * 1000000 + j
)
for j, l in enumerate( next_node._data[ 'locations' ] )
] )
else:
partial_result.extend( [
( ' ' * indent + symbol + kind + ': ' + name + 3 * '\t' +
os.path.split( l[ 'filepath' ] )[ 1 ] + ':' +
str( l[ 'line_num' ] ) + 3 * '\t' + l.get( 'description', '' ),
( i * 1000000 + j ) * -1 )
for j, l in enumerate( next_node._data[ 'locations' ] ) ] )
(
{
'indent': indent,
'icon': symbol,
'symbol': kind + ': ' + name,
'filepath': os.path.split( l[ 'filepath' ] )[ 1 ] + ':' + str( l[ 'line_num' ] ),
'description': l.get( 'description', '' ),
},
( i * 1000000 + j ) * -1
)
for j, l in enumerate( next_node._data[ 'locations' ] )
] )
if next_node._references:
partial_result.extend(
self._HierarchyToLinesHelper(
Expand Down

0 comments on commit 40877dc

Please sign in to comment.