From d93bc2baf7532818e83bf2fac61fcd591beb6151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Gro=C3=9F?= <21310755+vimpostor@users.noreply.github.com> Date: Tue, 23 Aug 2022 13:22:14 +0200 Subject: [PATCH] Use native virtual-text for vim9 (#4281) 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 708e810414d124b17b0c42e872b387a7a6c2ea85). 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 got a lot of bugfixes which is why we only use this new API if vim has at least version 9.0.0214. 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. --- autoload/ale/virtualtext.vim | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/autoload/ale/virtualtext.vim b/autoload/ale/virtualtext.vim index 345deb70..f0753dfe 100644 --- a/autoload/ale/virtualtext.vim +++ b/autoload/ale/virtualtext.vim @@ -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.0214') + + 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 @@ -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 @@ -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]], @@ -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