Skip to content

Commit 87b3b68

Browse files
committed
Show popup when node name extends beyond drawer width
In cases where the node name extends beyond the width of the drawer window (often the case with long file names or a deeply nested tree), show a popup to help the user view the full node name.
1 parent 6a0763f commit 87b3b68

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

autoload/fern.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ call s:Config.config(expand('<sfile>:p'), {
3535
\ 'disable_drawer_auto_winfixwidth': 0,
3636
\ 'disable_drawer_auto_resize': 0,
3737
\ 'disable_drawer_smart_quit': get(g:, 'disable_drawer_auto_quit', 0),
38+
\ 'disable_drawer_hover_popup': 0,
3839
\ 'disable_drawer_auto_restore_focus': 0,
3940
\ 'default_hidden': 0,
4041
\ 'default_include': '',

autoload/fern/internal/drawer.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function! fern#internal#drawer#init() abort
5151
call fern#internal#drawer#auto_winfixwidth#init()
5252
call fern#internal#drawer#auto_restore_focus#init()
5353
call fern#internal#drawer#smart_quit#init()
54+
call fern#internal#drawer#hover_popup#init()
5455
call fern#internal#drawer#resize()
5556
setlocal winfixwidth
5657
endfunction
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function! fern#internal#drawer#hover_popup#init() abort
2+
if g:fern#disable_drawer_hover_popup
3+
return
4+
endif
5+
6+
if !exists('*popup_create')
7+
call fern#logger#warn('hover popup is not supported, popup_create()
8+
\ does not exist. Disable this message
9+
\ with g:fern#disable_drawer_hover_popup.')
10+
return
11+
endif
12+
13+
augroup fern_internal_drawer_hover_popup_init
14+
autocmd! * <buffer>
15+
autocmd CursorMoved <buffer> call s:cursor_moved_event()
16+
augroup END
17+
endfunction
18+
19+
function! fern#internal#drawer#hover_popup#calculate_node_char_offset(node) abort
20+
" find line offset where label text begins
21+
let line = getline('.')
22+
let labelbegin = charidx(line, strridx(line, a:node.label))
23+
let labelbegin = labelbegin < 0 ? 0 : labelbegin
24+
25+
let windowid = win_getid()
26+
27+
" get cursor position in drawer window (char- and byte-indexed)
28+
let charpos = getcursorcharpos(windowid)
29+
let pos = getcurpos(windowid)
30+
31+
" get cursor position relative to screen
32+
let cursorpos = screenpos(windowid, pos[1], pos[2])
33+
34+
" calculate screen column where label text begins
35+
return cursorpos['col'] - charpos[2] + labelbegin
36+
endfunction
37+
38+
function! fern#internal#drawer#hover_popup#should_display_popup() abort
39+
return len(getline('.')) >= winwidth(0)
40+
endfunction
41+
42+
function! s:cursor_moved_event() abort
43+
let helper = fern#helper#new()
44+
45+
if fern#internal#drawer#hover_popup#should_display_popup()
46+
call s:show_popup(helper)
47+
endif
48+
endfunction
49+
50+
function! s:show_popup(helper) abort
51+
let node = a:helper.sync.get_cursor_node()
52+
if node is# v:null
53+
return
54+
endif
55+
56+
let label_offset = fern#internal#drawer#hover_popup#calculate_node_char_offset(node)
57+
call popup_create(l:node.label, {
58+
\ 'line': 'cursor',
59+
\ 'col': label_offset + 1,
60+
\ 'moved': 'any',
61+
\})
62+
endfunction
63+

doc/fern.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,15 @@ VARIABLE *fern-variable*
411411
of Neovim to avoid unwilling resize reported as #294
412412
https://github.com/lambdalisue/fern.vim/issues/294
413413

414-
Default: 0
414+
Default: 0
415+
416+
*g:fern#disable_drawer_hover_popup*
417+
Set 1 to disable popups shown when the name of a node extends beyond
418+
the width of the drawer.
419+
420+
Note that this feature is not supported in Neovim.
421+
422+
Default: 0
415423

416424
*g:fern#disable_drawer_smart_quit*
417425
Set 1 to disable smart quit behavior when there are only two buffer

0 commit comments

Comments
 (0)