Skip to content

Commit

Permalink
Use native virtual-text for vim9
Browse files Browse the repository at this point in the history
Our current virtual text implementation for vim emulates it by abusing
the textprop and popupwin feature from vim 8.2 (for more details see
commit 708e810).
This implementation sometimes is janky, for example the popups may leak
into other vim windows next to the current window.

Luckily, vim just got native virtual-text support as a proper subtype to
the prop_add() function. By using the 'text' option, the text property
automatically becomes virtual text that is appended to the current line
if col is zero.

Note that the prop_add() method now returns negative IDs for virtual
text properties.

This feature was added in vim 9.0.0067, but it is still getting bugfixes
which is why we only use this new API if vim has at least version
9.0.0193.
However, there are still some minor bugs with vim's native virtual text,
so we might have to bump the version check again in the future.

Also see #3906.

Now with proper virtual text support for both vim and neovim available,
we can tackle #2962 in the future by simply tracking multiple virt-texts
instead of just the last one.

In the future we might also want to disable our virtual text emulation
support for vim, as it is a total hack, but for now we should keep it
for backwards compatibility.
  • Loading branch information
vimpostor committed Aug 14, 2022
1 parent 233b681 commit 661c762
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions autoload/ale/virtualtext.vim
Expand Up @@ -8,14 +8,21 @@ let g:ale_virtualtext_delay = get(g:, 'ale_virtualtext_delay', 10)
let s:cursor_timer = -1
let s:last_pos = [0, 0, 0]
let s:has_virt_text = 0
let s:emulate_virt = 0

if has('nvim-0.3.2')
let s:ns_id = nvim_create_namespace('ale')
let s:has_virt_text = 1
elseif has('textprop') && has('popupwin')
call prop_type_add('ale', {})
let s:last_popup = -1
let s:has_virt_text = 1
let s:emulate_virt = !has('patch-9.0.0201')

if s:emulate_virt
call prop_type_add('ale', {})
let s:last_virt = -1
else
let s:last_virt = 1
endif
endif

function! ale#virtualtext#Clear() abort
Expand All @@ -28,10 +35,13 @@ function! ale#virtualtext#Clear() abort
if has('nvim')
call nvim_buf_clear_highlight(l:buffer, s:ns_id, 0, -1)
else
if s:last_popup != -1
if s:emulate_virt && s:last_virt != -1
call prop_remove({'type': 'ale'})
call popup_close(s:last_popup)
let s:last_popup = -1
call popup_close(s:last_virt)
let s:last_virt = -1
elseif s:last_virt != 1
call prop_remove({'id': s:last_virt})
let s:last_virt = 1
endif
endif
endfunction
Expand All @@ -48,12 +58,12 @@ function! ale#virtualtext#ShowMessage(message, hl_group) abort

if has('nvim')
call nvim_buf_set_virtual_text(l:buffer, s:ns_id, l:line-1, [[l:msg, a:hl_group]], {})
else
elseif s:emulate_virt
let l:left_pad = col('$')
call prop_add(l:line, l:left_pad, {
\ 'type': 'ale',
\})
let s:last_popup = popup_create(l:msg, {
let s:last_virt = popup_create(l:msg, {
\ 'line': -1,
\ 'padding': [0, 0, 0, 1],
\ 'mask': [[1, 1, 1, 1]],
Expand All @@ -63,6 +73,17 @@ function! ale#virtualtext#ShowMessage(message, hl_group) abort
\ 'wrap': 0,
\ 'zindex': 2
\})
else
let type = prop_type_get(a:hl_group)

if type == {}
call prop_type_add(a:hl_group, {'highlight': a:hl_group})
endif

let s:last_virt = prop_add(l:line, 0, {
\ 'type': a:hl_group,
\ 'text': ' ' . l:msg
\})
endif
endfunction

Expand Down

0 comments on commit 661c762

Please sign in to comment.