Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit d2659c2

Browse files
committed
Fix highlight position with unicode characters for vim and neovim
Introduce g:lsp_cxx_hl_use_byteidx
1 parent 0925448 commit d2659c2

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

autoload/lsp_cxx_hl.vim

+18
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,24 @@ function! s:common_notify_checks(server, buffer, data) abort
163163
return l:bufnr
164164
endfunction
165165

166+
" Convert range of bytes starting from s_line character s_char
167+
" to line e_line character e_char in buffer a:buf into a range of columns.
168+
" These are equal when each displayed character takes exactly one column,
169+
" but there's also UTF-8.
170+
function! lsp_cxx_hl#bytes_to_columns(buf, s_line, s_char, e_line, e_char)
171+
if get(g:, 'lsp_cxx_hl_use_byteidx', 0)
172+
try
173+
return [byteidx(getbufline(a:buf, a:s_line)[0], a:s_char),
174+
\ byteidx(getbufline(a:buf, a:e_line)[0], a:e_char)]
175+
" If the user fastly edits the file, this can be slower then the
176+
" edits, and `getbufline` can return zero lines or these columns
177+
" may just not exists anymore.
178+
catch /^Vim\%((\a\+)\)\=:E684/
179+
endtry
180+
endif
181+
return [ a:s_char, a:e_char ]
182+
endfunction
183+
166184
" Section: Misc Helpers
167185
function! s:uri2bufnr(uri) abort
168186
" Absolute paths on windows has 3 leading /

autoload/lsp_cxx_hl/textprop/symbols.vim

+3
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ function! s:hl_symbols(bufnr, timer) abort
152152

153153
for l:prop in l:props
154154
call extend(l:prop[2], l:prop_extra)
155+
let [ l:prop[1], l:prop[2]['end_col'] ] = lsp_cxx_hl#bytes_to_columns(
156+
\ a:bufnr, l:prop[0], l:prop[1],
157+
\ get(l:prop[2], 'end_lnum', l:prop[0]), l:prop[2]['end_col'])
155158

156159
try
157160
call prop_add(l:prop[0], l:prop[1], l:prop[2])

autoload/lsp_cxx_hl/textprop_nvim.vim

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
" Textprops neovim
2-
"
2+
"
33
" It should be noted that neovim uses zero based indexing like LSP
44
" this is unlike regular vim APIs which are 1 based.
55

@@ -18,13 +18,16 @@ function! s:buf_add_hl(buf, ns_id, hl_group,
1818

1919
" single line symbol
2020
if a:s_line == a:e_line
21-
if a:e_char - a:s_char > 0
22-
call nvim_buf_add_highlight(a:buf, a:ns_id, a:hl_group,
23-
\ a:s_line, a:s_char, a:e_char)
24-
return
25-
else
26-
return
21+
let [ s_char, e_char ] = lsp_cxx_hl#bytes_to_columns(a:buf,
22+
\ a:s_line + 1, a:s_char, a:s_line + 1, a:e_char)
23+
if s_char < e_char
24+
try
25+
call nvim_buf_add_highlight(a:buf, a:ns_id, a:hl_group,
26+
\ a:s_line, s_char, e_char)
27+
catch /Column value outside range/
28+
endtry
2729
endif
30+
return
2831
endif
2932

3033
call lsp_cxx_hl#log('Error (textprop_nvim): symbol (', a:hl_group,

0 commit comments

Comments
 (0)