Skip to content

Commit ec6e92c

Browse files
authored
Merge #303 label-mode: use virtual text in nvim ≥ 0.5
2 parents 93395f5 + 3ff1033 commit ec6e92c

File tree

2 files changed

+75
-47
lines changed

2 files changed

+75
-47
lines changed

autoload/sneak/label.vim

+32-47
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
" NOTES:
22
" problem: cchar cannot be more than 1 character.
33
" strategy: make fg/bg the same color, then conceal the other char.
4-
"
5-
" problem: [before 7.4.792] keyword highlight takes priority over conceal.
6-
" strategy: syntax clear | [do the conceal] | let &syntax=s:o_syntax
74

85
let g:sneak#target_labels = get(g:, 'sneak#target_labels', ";sftunq/SFGHLTUNRMQZ?0")
96

10-
let s:clear_syntax = !has('patch-7.4.792')
117
let s:matchmap = {}
12-
let s:match_ids = []
138
let s:orig_conceal_matches = []
149

10+
let s:use_virt_text = has('nvim-0.5')
11+
if s:use_virt_text
12+
call luaeval('require("sneak").init()')
13+
else
14+
let s:match_ids = []
15+
endif
16+
1517
if exists('*strcharpart')
1618
func! s:strchar(s, i) abort
1719
return strcharpart(a:s, a:i, 1)
@@ -24,10 +26,10 @@ endif
2426

2527
func! s:placematch(c, pos) abort
2628
let s:matchmap[a:c] = a:pos
27-
let pat = '\%'.a:pos[0].'l\%'.a:pos[1].'c.'
28-
if s:clear_syntax
29-
exec "syntax match SneakLabel '".pat."' conceal cchar=".a:c
29+
if s:use_virt_text
30+
call luaeval('require("sneak").placematch(_A[1], _A[2], _A[3])', [a:c, a:pos[0] - 1, a:pos[1] - 1])
3031
else
32+
let pat = '\%'.a:pos[0].'l\%'.a:pos[1].'c.'
3133
let id = matchadd('Conceal', pat, 999, -1, { 'conceal': a:c })
3234
call add(s:match_ids, id)
3335
endif
@@ -127,25 +129,17 @@ endf "}}}
127129
func! s:after() abort
128130
autocmd! sneak_label_cleanup
129131
try | call matchdelete(s:sneak_cursor_hl) | catch | endtry
130-
call map(s:match_ids, 'matchdelete(v:val)')
131-
let s:match_ids = []
132-
" Remove temporary highlight links.
133-
exec 'hi! link Conceal '.s:orig_hl_conceal
134-
call s:restore_conceal_matches()
135-
exec 'hi! link Sneak '.s:orig_hl_sneak
136-
137-
if s:clear_syntax
138-
let &l:synmaxcol=s:o_synmaxcol
139-
" Always clear before restore, in case user has `:syntax off`. #200
140-
syntax clear
141-
silent! let &l:foldmethod=s:o_fdm
142-
silent! let &l:syntax=s:o_syntax
143-
" Force Vim to reapply 'spell' (must set 'spelllang'). #110
144-
let [&l:spell,&l:spelllang]=[s:o_spell,s:o_spelllang]
145-
call s:restore_conceal_in_other_windows()
132+
if s:use_virt_text
133+
call luaeval('require("sneak").after()')
134+
else
135+
call map(s:match_ids, 'matchdelete(v:val)')
136+
let s:match_ids = []
137+
" Remove temporary highlight links.
138+
exec 'hi! link Conceal '.s:orig_hl_conceal
139+
call s:restore_conceal_matches()
140+
let [&l:concealcursor,&l:conceallevel]=[s:o_cocu,s:o_cole]
146141
endif
147-
148-
let [&l:concealcursor,&l:conceallevel]=[s:o_cocu,s:o_cole]
142+
exec 'hi! link Sneak '.s:orig_hl_sneak
149143
endf
150144

151145
func! s:disable_conceal_in_other_windows() abort
@@ -168,34 +162,25 @@ endf
168162

169163
func! s:before() abort
170164
let s:matchmap = {}
171-
for o in ['spell', 'spelllang', 'cocu', 'cole', 'fdm', 'synmaxcol', 'syntax']
172-
exe 'let s:o_'.o.'=&l:'.o
173-
endfor
174-
175-
setlocal concealcursor=ncv conceallevel=2
176165

177166
" Highlight the cursor location (because cursor is hidden during getchar()).
178167
let s:sneak_cursor_hl = matchadd("SneakScope", '\%#', 11, -1)
179168

180-
if s:clear_syntax
181-
setlocal nospell
182-
" Prevent highlighting in other windows showing the same buffer.
183-
ownsyntax sneak_label
184-
" Avoid broken folds when we clear syntax below.
185-
if &l:foldmethod ==# 'syntax'
186-
setlocal foldmethod=manual
187-
endif
188-
syntax clear
189-
" This is fast because we cleared syntax. Allows Sneak to work on very long wrapped lines.
190-
setlocal synmaxcol=0
191-
call s:disable_conceal_in_other_windows()
169+
if s:use_virt_text
170+
call luaeval('require("sneak").before()')
171+
else
172+
for o in ['cocu', 'cole']
173+
exe 'let s:o_'.o.'=&l:'.o
174+
endfor
175+
setlocal concealcursor=ncv conceallevel=2
176+
177+
let s:orig_hl_conceal = sneak#util#links_to('Conceal')
178+
call s:save_conceal_matches()
179+
" Set temporary link to our custom 'conceal' highlight.
180+
hi! link Conceal SneakLabel
192181
endif
193182

194-
let s:orig_hl_conceal = sneak#util#links_to('Conceal')
195-
call s:save_conceal_matches()
196183
let s:orig_hl_sneak = sneak#util#links_to('Sneak')
197-
" Set temporary link to our custom 'conceal' highlight.
198-
hi! link Conceal SneakLabel
199184
" Set temporary link to hide the sneak search targets.
200185
hi! link Sneak SneakLabelMask
201186

lua/sneak.lua

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
local M = {}
2+
3+
local ns = vim.api.nvim_create_namespace('sneak')
4+
5+
local matches
6+
7+
function M.before()
8+
matches = {}
9+
end
10+
11+
function M.placematch(c, row, col)
12+
matches[#matches+1] = { c, row, col }
13+
end
14+
15+
function M.after()
16+
matches = nil
17+
end
18+
19+
function M.init()
20+
vim.api.nvim_set_decoration_provider(ns, {
21+
on_start = function(_, _)
22+
if not matches then
23+
return false
24+
end
25+
end,
26+
on_win = function(_, win, _, _, _)
27+
if win ~= vim.api.nvim_get_current_win() then
28+
return false
29+
end
30+
for _, m in ipairs(matches) do
31+
local c, row, col = unpack(m)
32+
vim.api.nvim_buf_set_extmark(0, ns, row, col, {
33+
priority = 1000,
34+
virt_text = { {c, 'SneakLabel'} },
35+
virt_text_pos = 'overlay',
36+
ephemeral = true,
37+
})
38+
end
39+
end
40+
})
41+
end
42+
43+
return M

0 commit comments

Comments
 (0)