|
| 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 | + |
0 commit comments