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

Commit

Permalink
Fix highlight position with unicode characters for vim and neovim
Browse files Browse the repository at this point in the history
Introduce g:lsp_cxx_hl_use_byteidx
  • Loading branch information
Kamilcuk committed Oct 18, 2021
1 parent 0925448 commit dabc787
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
18 changes: 18 additions & 0 deletions autoload/lsp_cxx_hl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ function! s:common_notify_checks(server, buffer, data) abort
return l:bufnr
endfunction

" Convert range of bytes starting from s_line character s_char
" to line e_line character e_char in buffer a:buf into a range of columns.
" These are equal when each displayed character takes exactly one column,
" but there's also UTF-8.
function! lsp_cxx_hl#bytes_to_columns(buf, s_line, s_char, e_line, e_char)
if get(g:, 'lsp_cxx_hl_use_byteidx', 0)
try
return [byteidx(getbufline(a:buf, a:s_line)[0], a:s_char),
\ byteidx(getbufline(a:buf, a:e_line)[0], a:e_char)]
" If the user fastly edits the file, this can be slower then the
" edits, and `getbufline` can return zero lines or these columns
" may just not exists anymore.
catch /^Vim\%((\a\+)\)\=:E684/
endtry
endif
return [ a:s_char, a:e_char ]
endfunction

" Section: Misc Helpers
function! s:uri2bufnr(uri) abort
" Absolute paths on windows has 3 leading /
Expand Down
3 changes: 3 additions & 0 deletions autoload/lsp_cxx_hl/textprop/symbols.vim
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ function! s:hl_symbols(bufnr, timer) abort

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

try
call prop_add(l:prop[0], l:prop[1], l:prop[2])
Expand Down
18 changes: 11 additions & 7 deletions autoload/lsp_cxx_hl/textprop_nvim.vim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
" Textprops neovim
"
"
" It should be noted that neovim uses zero based indexing like LSP
" this is unlike regular vim APIs which are 1 based.

function! lsp_cxx_hl#textprop_nvim#buf_add_hl_lsrange(buf, ns_id, hl_group,
\ range) abort
call lsp_cxx_hl#log('range = ', a:range)
return s:buf_add_hl(a:buf, a:ns_id, a:hl_group,
\ a:range['start']['line'],
\ a:range['start']['character'],
Expand All @@ -18,13 +19,16 @@ function! s:buf_add_hl(buf, ns_id, hl_group,

" single line symbol
if a:s_line == a:e_line
if a:e_char - a:s_char > 0
call nvim_buf_add_highlight(a:buf, a:ns_id, a:hl_group,
\ a:s_line, a:s_char, a:e_char)
return
else
return
let [ s_char, e_char ] = lsp_cxx_hl#bytes_to_columns(a:buf,
\ a:s_line + 1, a:s_char, a:s_line + 1, a:e_char)
if s_char < e_char
try
call nvim_buf_add_highlight(a:buf, a:ns_id, a:hl_group,
\ a:s_line, s_char, e_char)
catch /Column value outside range/
endtry
endif
return
endif

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

0 comments on commit dabc787

Please sign in to comment.