Skip to content

Commit

Permalink
Merge pull request #398 from brandon1024/hover-popup
Browse files Browse the repository at this point in the history
Show popup when node name extends beyond drawer width
  • Loading branch information
lambdalisue authored Mar 15, 2022
2 parents 6a0763f + 87b3b68 commit 63b79ab
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions autoload/fern.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ call s:Config.config(expand('<sfile>:p'), {
\ 'disable_drawer_auto_winfixwidth': 0,
\ 'disable_drawer_auto_resize': 0,
\ 'disable_drawer_smart_quit': get(g:, 'disable_drawer_auto_quit', 0),
\ 'disable_drawer_hover_popup': 0,
\ 'disable_drawer_auto_restore_focus': 0,
\ 'default_hidden': 0,
\ 'default_include': '',
Expand Down
1 change: 1 addition & 0 deletions autoload/fern/internal/drawer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function! fern#internal#drawer#init() abort
call fern#internal#drawer#auto_winfixwidth#init()
call fern#internal#drawer#auto_restore_focus#init()
call fern#internal#drawer#smart_quit#init()
call fern#internal#drawer#hover_popup#init()
call fern#internal#drawer#resize()
setlocal winfixwidth
endfunction
Expand Down
63 changes: 63 additions & 0 deletions autoload/fern/internal/drawer/hover_popup.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function! fern#internal#drawer#hover_popup#init() abort
if g:fern#disable_drawer_hover_popup
return
endif

if !exists('*popup_create')
call fern#logger#warn('hover popup is not supported, popup_create()
\ does not exist. Disable this message
\ with g:fern#disable_drawer_hover_popup.')
return
endif

augroup fern_internal_drawer_hover_popup_init
autocmd! * <buffer>
autocmd CursorMoved <buffer> call s:cursor_moved_event()
augroup END
endfunction

function! fern#internal#drawer#hover_popup#calculate_node_char_offset(node) abort
" find line offset where label text begins
let line = getline('.')
let labelbegin = charidx(line, strridx(line, a:node.label))
let labelbegin = labelbegin < 0 ? 0 : labelbegin

let windowid = win_getid()

" get cursor position in drawer window (char- and byte-indexed)
let charpos = getcursorcharpos(windowid)
let pos = getcurpos(windowid)

" get cursor position relative to screen
let cursorpos = screenpos(windowid, pos[1], pos[2])

" calculate screen column where label text begins
return cursorpos['col'] - charpos[2] + labelbegin
endfunction

function! fern#internal#drawer#hover_popup#should_display_popup() abort
return len(getline('.')) >= winwidth(0)
endfunction

function! s:cursor_moved_event() abort
let helper = fern#helper#new()

if fern#internal#drawer#hover_popup#should_display_popup()
call s:show_popup(helper)
endif
endfunction

function! s:show_popup(helper) abort
let node = a:helper.sync.get_cursor_node()
if node is# v:null
return
endif

let label_offset = fern#internal#drawer#hover_popup#calculate_node_char_offset(node)
call popup_create(l:node.label, {
\ 'line': 'cursor',
\ 'col': label_offset + 1,
\ 'moved': 'any',
\})
endfunction

10 changes: 9 additions & 1 deletion doc/fern.txt
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,15 @@ VARIABLE *fern-variable*
of Neovim to avoid unwilling resize reported as #294
https://github.com/lambdalisue/fern.vim/issues/294

Default: 0
Default: 0

*g:fern#disable_drawer_hover_popup*
Set 1 to disable popups shown when the name of a node extends beyond
the width of the drawer.

Note that this feature is not supported in Neovim.

Default: 0

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

0 comments on commit 63b79ab

Please sign in to comment.