Skip to content

Commit

Permalink
Allow callbacks for floating preview popups (dense-analysis#4247)
Browse files Browse the repository at this point in the history
* Add extra config options for virtualtext

* Undo virtualtext changes and move to floating preview

* revert changes to pass hightlight group to floating preview

* rename var

* Document changes

* Add updates based on feedback

* Check for string type and attempt to call the function

* Fix lint errors

Co-authored-by: Shaun Duncan <[email protected]>
  • Loading branch information
shaunduncan and shaunduncan authored Aug 23, 2022
1 parent d93bc2b commit 6996d1c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
45 changes: 35 additions & 10 deletions autoload/ale/floating_preview.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
" Author: Jan-Grimo Sobez <[email protected]>
" Author: Kevin Clark <[email protected]>
" Author: D. Ben Knoble <[email protected]>
" Author: Shaun Duncan <[email protected]>
" Description: Floating preview window for showing whatever information in.

" Precondition: exists('*nvim_open_win') || has('popupwin')
Expand Down Expand Up @@ -133,15 +134,18 @@ function! s:NvimPrepareWindowContent(lines) abort
endfunction

function! s:NvimCreate(options) abort
let l:popup_opts = extend({
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': 4,
\ 'style': 'minimal'
\ }, s:GetPopupOpts())

let l:buffer = nvim_create_buf(v:false, v:false)
let l:winid = nvim_open_win(l:buffer, v:false, {
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': 4,
\ 'style': 'minimal'
\ })
let l:winid = nvim_open_win(l:buffer, v:false, l:popup_opts)

call nvim_buf_set_option(l:buffer, 'buftype', 'acwrite')
call nvim_buf_set_option(l:buffer, 'bufhidden', 'delete')
call nvim_buf_set_option(l:buffer, 'swapfile', v:false)
Expand All @@ -151,7 +155,8 @@ function! s:NvimCreate(options) abort
endfunction

function! s:VimCreate(options) abort
let l:popup_id = popup_create([], {
" default options
let l:popup_opts = extend({
\ 'line': 'cursor+1',
\ 'col': 'cursor',
\ 'drag': v:true,
Expand All @@ -170,7 +175,9 @@ function! s:VimCreate(options) abort
\ get(g:ale_floating_window_border, 5, '+'),
\ ],
\ 'moved': 'any',
\ })
\ }, s:GetPopupOpts())

let l:popup_id = popup_create([], l:popup_opts)
call setbufvar(winbufnr(l:popup_id), '&filetype', get(a:options, 'filetype', 'ale-preview'))
let w:preview = {'id': l:popup_id}
endfunction
Expand Down Expand Up @@ -204,3 +211,21 @@ function! s:VimClose() abort
call popup_close(w:preview['id'])
unlet w:preview
endfunction

" get either the results of a function callback or dictionary for popup overrides
function! s:GetPopupOpts() abort
if exists('g:ale_floating_preview_popup_opts')
let l:ref = g:ale_floating_preview_popup_opts

if type(l:ref) is# v:t_dict
return l:ref
elseif type(l:ref) is# v:t_string
try
return function(l:ref)()
catch /E700/
endtry
endif
endif

return {}
endfunction
23 changes: 23 additions & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,29 @@ g:ale_floating_preview *g:ale_floating_preview*
|g:ale_detail_to_floating_preview| to `1`.


g:ale_floating_preview_popup_opts *g:ale_floating_preview_popup_opts*

Type: |String| or |Dictionary|
Default: `''`

Either a dictionary of options or the string name of a function that returns a
dictionary of options. This will be used as an argument to |popup_create| for
Vim users or |nvim_open_win| for NeoVim users. Note that in either case, the
resulting dictionary is merged with ALE defaults rather than expliciting overriding
them. This only takes effect if |g:ale_floating_preview| is enabled.

NOTE: for Vim users see |popup_create-arguments|, for NeoVim users see
|nvim_open_win| for argument details

For example, to enhance popups with a title: >
function! CustomOpts() abort {
let [l:info, l:loc] = ale#util#FindItemAtCursor(bufnr(''))
return {'title': ' ALE: ' . (l:loc.linter_name) . ' '}
endfunction
<


g:ale_floating_window_border *g:ale_floating_window_border*

Type: |List|
Expand Down

0 comments on commit 6996d1c

Please sign in to comment.