vim:au: Improve performance of cword/selection highlight
- Combine one `exists` call with an implicit `get` into a `get` with an empty default - Do not stop any timer in the `Highlight*` functions as this done already by `ClearHighlights` - Get rid of the check for existence of `w:*_timer_id` since I believe that there is no way of it being unset in the current state. The `_Highlight*` functions are only called via the timer so there should always be the variable. I could also not reproduce the description in the comment - this might have been true in a previous revision. - Make sure that the visual selection itself is not matched. This actually increased the performance a lot while also looking like normally (there were some subtle differences between the highlight by visual mode and `CursorColumn`)
This commit is contained in:
@@ -69,30 +69,22 @@ function! HighlightCurrentWord()
|
|||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if exists('w:old_cword') && w:old_cword == expand('<cword>')
|
if get(w:, 'old_cword', '') == expand('<cword>')
|
||||||
" Nothing to do if we're still on the same word
|
" Nothing to do if we're still on the same word
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
" Clear previous highlight and kill the possibly already running timer
|
||||||
call ClearHighlights(s:CLEAR_HIGHS_CWORD)
|
call ClearHighlights(s:CLEAR_HIGHS_CWORD)
|
||||||
|
|
||||||
" Delay the highlight by 100ms so that not every word is highlighted
|
" Delay the highlight by 100ms so that not every word is highlighted
|
||||||
" while moving the cursor fast. (This kind of simulates a CursorHold
|
" while moving the cursor fast. (This kind of simulates a CursorHold
|
||||||
" event with a custom time)
|
" event with a custom time)
|
||||||
if exists('w:cword_timer_id')
|
|
||||||
" Abort the already running timer and its callback
|
|
||||||
call timer_stop(w:cword_timer_id)
|
|
||||||
endif
|
|
||||||
let w:cword_timer_id = timer_start(100, "_HighlightCurrentWord")
|
let w:cword_timer_id = timer_start(100, "_HighlightCurrentWord")
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! _HighlightCurrentWord(timer_id)
|
function! _HighlightCurrentWord(timer_id)
|
||||||
" TODO: there is probably some kind of race condition here
|
|
||||||
" NOTE: w:cword_timer_id can be unset when switching from a window that is
|
|
||||||
" currently in visual mode as then the ModeChanged event is triggered.
|
|
||||||
if exists('w:cword_timer_id')
|
|
||||||
unlet w:cword_timer_id
|
unlet w:cword_timer_id
|
||||||
endif
|
|
||||||
|
|
||||||
let l:cword = expand('<cword>')
|
let l:cword = expand('<cword>')
|
||||||
if (l:cword != '')
|
if (l:cword != '')
|
||||||
@@ -110,22 +102,17 @@ function! HighlightVisualSel()
|
|||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
" Clear previous highlight and kill the possibly already running timer
|
||||||
call ClearHighlights(s:CLEAR_HIGHS_VISUAL)
|
call ClearHighlights(s:CLEAR_HIGHS_VISUAL)
|
||||||
|
|
||||||
" Delay the highlight by 100ms so that not every selection is highlighted
|
" Delay the highlight by 100ms so that not every selection is highlighted
|
||||||
" while moving the cursor fast. (This kind of simulates a CursorHold
|
" while moving the cursor fast. (This kind of simulates a CursorHold
|
||||||
" event with a custom time)
|
" event with a custom time)
|
||||||
if exists('w:selection_timer_id')
|
|
||||||
" Abort the already running timer and its callback
|
|
||||||
call timer_stop(w:selection_timer_id)
|
|
||||||
endif
|
|
||||||
let w:selection_timer_id = timer_start(100, "_HighlightVisualSel")
|
let w:selection_timer_id = timer_start(100, "_HighlightVisualSel")
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! _HighlightVisualSel(timer)
|
function! _HighlightVisualSel(timer)
|
||||||
if exists('w:selection_timer_id')
|
|
||||||
unlet w:selection_timer_id
|
unlet w:selection_timer_id
|
||||||
endif
|
|
||||||
|
|
||||||
let l:old_reg = getreg('"')
|
let l:old_reg = getreg('"')
|
||||||
let l:old_regtype = getregtype('"')
|
let l:old_regtype = getregtype('"')
|
||||||
@@ -144,11 +131,13 @@ function! _HighlightVisualSel(timer)
|
|||||||
let w:visual_match_ids = []
|
let w:visual_match_ids = []
|
||||||
|
|
||||||
" Add match to all windows containing the current buffer
|
" Add match to all windows containing the current buffer
|
||||||
|
" NOTE: \%V\@! prevents the pattern from matching the current selection. As
|
||||||
|
" it is highlighted already this would be superfluous and inefficient.
|
||||||
for l:win in win_findbuf(bufnr())
|
for l:win in win_findbuf(bufnr())
|
||||||
let w:visual_match_ids += [[
|
let w:visual_match_ids += [[
|
||||||
\ matchadd(
|
\ matchadd(
|
||||||
\ 'CursorColumn',
|
\ 'CursorColumn',
|
||||||
\ '\V' . substitute(escape(@", '\'), '\n', '\\n', 'g'),
|
\ '\V\%V\@!' . substitute(escape(@", '\'), '\n', '\\n', 'g'),
|
||||||
\ -1,
|
\ -1,
|
||||||
\ -1,
|
\ -1,
|
||||||
\ {'window': l:win}),
|
\ {'window': l:win}),
|
||||||
|
|||||||
Reference in New Issue
Block a user