Improve performance placing signs and virtual text (#1455)

Since the functions don't change the linecount, call
getbufinfo(...).linecount once rather than many times.
This commit is contained in:
Gabriel Barta
2023-04-06 14:35:58 +10:00
committed by GitHub
parent 7d59166b18
commit 6703edafe6
2 changed files with 12 additions and 7 deletions

View File

@@ -25,6 +25,9 @@ if !hlexists('LspHintText')
highlight link LspHintText Normal
endif
" imports
let s:Buffer = vital#lsp#import('VS.Vim.Buffer')
function! lsp#internal#diagnostics#signs#_enable() abort
" don't even bother registering if the feature is disabled
if !lsp#utils#_has_signs() | return | endif
@@ -127,15 +130,13 @@ function! s:set_signs(params) abort
endfunction
function! s:place_signs(server, diagnostics_response, bufnr) abort
let l:linecount = s:Buffer.get_line_count(a:bufnr)
for l:item in lsp#utils#iteratable(a:diagnostics_response['params']['diagnostics'])
let l:line = lsp#utils#position#lsp_line_to_vim(a:bufnr, l:item['range']['start'])
" Some language servers report an unexpected EOF one line past the end
" key 'linecount' may be missing.
if has_key(getbufinfo(a:bufnr)[0], 'linecount')
if l:line == getbufinfo(a:bufnr)[0].linecount + 1
let l:line = l:line - 1
endif
if l:line == l:linecount + 1
let l:line = l:line - 1
endif
if has_key(l:item, 'severity') && !empty(l:item['severity'])

View File

@@ -40,6 +40,9 @@ if !hlexists('LspHintVirtualText')
endif
endif
" imports
let s:Buffer = vital#lsp#import('VS.Vim.Buffer')
function! lsp#internal#diagnostics#virtual_text#_enable() abort
" don't even bother registering if the feature is disabled
if !lsp#utils#_has_nvim_virtual_text() && !lsp#utils#_has_vim_virtual_text() | return | endif
@@ -156,13 +159,14 @@ function! s:set_virtual_text(params) abort
endfunction
function! s:place_virtual_text(server, diagnostics_response, bufnr) abort
let l:linecount = s:Buffer.get_line_count(a:bufnr)
for l:item in lsp#utils#iteratable(a:diagnostics_response['params']['diagnostics'])
let l:line = lsp#utils#position#lsp_line_to_vim(a:bufnr, l:item['range']['start'])
let l:name = get(s:severity_sign_names_mapping, get(l:item, 'severity', 3), 'LspError')
let l:text = g:lsp_diagnostics_virtual_text_prefix . l:item['message']
" Some language servers report an unexpected EOF one line past the end
if l:line == getbufinfo(a:bufnr)[0].linecount + 1
if l:line == l:linecount + 1
let l:line = l:line - 1
endif
@@ -174,7 +178,7 @@ function! s:place_virtual_text(server, diagnostics_response, bufnr) abort
else
" it's an error to add virtual text on lines that don't exist
" anymore due to async processing, just skip such diagnostics
if l:line <= getbufinfo(a:bufnr)[0].linecount
if l:line <= l:linecount
let l:type = 'vim_lsp_' . l:name . '_virtual_text'
call prop_remove({'all': v:true, 'type': l:type, 'bufnr': a:bufnr}, l:line)
call prop_add(