use callbag for Highlight references (#891)

This commit is contained in:
Prabir Shrestha
2020-08-30 11:11:51 -07:00
committed by GitHub
parent 1a01ffddbe
commit eb0e5fb885
6 changed files with 278 additions and 285 deletions

View File

@@ -65,6 +65,7 @@ function! lsp#enable() abort
endif
call lsp#ui#vim#completion#_setup()
call lsp#internal#diagnostics#_enable()
call lsp#internal#highlight_references#_enable()
call s:register_events()
endfunction
@@ -79,6 +80,7 @@ function! lsp#disable() abort
call lsp#ui#vim#signature_help#_disable()
call lsp#ui#vim#completion#_disable()
call lsp#internal#diagnostics#_disable()
call lsp#internal#highlight_references#_disable()
call s:unregister_events()
let s:enabled = 0
endfunction
@@ -209,10 +211,6 @@ function! s:register_events() abort
if exists('##TextChangedP')
autocmd TextChangedP * call s:on_text_document_did_change()
endif
if g:lsp_highlight_references_enabled
autocmd CursorMoved * call s:on_cursor_moved()
endif
autocmd BufWinEnter,BufWinLeave,InsertEnter * call lsp#ui#vim#references#clean_references()
augroup END
for l:bufnr in range(1, bufnr('$'))
@@ -272,15 +270,6 @@ function! s:on_text_document_did_change() abort
call s:add_didchange_queue(l:buf)
endfunction
function! s:on_cursor_moved() abort
let l:buf = bufnr('%')
if getbufvar(l:buf, '&buftype') ==# 'terminal' | return | endif
if g:lsp_highlight_references_enabled
call lsp#ui#vim#references#highlight(v:false)
endif
endfunction
function! s:call_did_save(buf, server_name, result, cb) abort
if lsp#client#is_error(a:result['response'])
return

View File

@@ -0,0 +1,218 @@
let s:use_vim_textprops = has('textprop') && !has('nvim')
let s:prop_id = 11
function! lsp#internal#highlight_references#_enable() abort
" don't event bother registering if the feature is disabled
if !g:lsp_highlight_references_enabled | return | endif
" Highlight group for references
if !hlexists('lspReference')
highlight link lspReference CursorColumn
endif
" Note:
" - update highlight references when CusorMoved or CursorHold
" - clear highlights when InsertEnter of BufLeave
" - debounce highlight requests
" - automatically switch to latest highlight request via switchMap()
" - cancel highlight request via takeUntil() when BufLeave
let s:Dispose = lsp#callbag#pipe(
\ lsp#callbag#merge(
\ lsp#callbag#fromEvent(['CursorMoved', 'CursorHold']),
\ lsp#callbag#pipe(
\ lsp#callbag#fromEvent(['InsertEnter', 'BufLeave']),
\ lsp#callbag#tap({_ -> s:clear_highlights() }),
\ )
\ ),
\ lsp#callbag#filter({_ -> g:lsp_highlight_references_enabled }),
\ lsp#callbag#debounceTime(g:lsp_highlight_references_delay),
\ lsp#callbag#map({_->{'bufnr': bufnr('%'), 'curpos': getcurpos()[0:2], 'changedtick': b:changedtick }}),
\ lsp#callbag#distinctUntilChanged({a,b -> a['bufnr'] == b['bufnr'] && a['curpos'] == b['curpos'] && a['changedtick'] == b['changedtick']}),
\ lsp#callbag#filter({_->mode() is# 'n' && getbufvar(bufnr('%'), '&buftype') !=# 'terminal' }),
\ lsp#callbag#switchMap({_->
\ lsp#callbag#pipe(
\ s:send_highlight_request(),
\ lsp#callbag#takeUntil(
\ lsp#callbag#fromEvent('BufLeave')
\ )
\ )
\ }),
\ lsp#callbag#filter({_->mode() is# 'n'}),
\ lsp#callbag#subscribe({x->s:set_highlights(x)}),
\)
endfunction
function! lsp#internal#highlight_references#_disable() abort
if exists('s:Dispose') | call s:Dispose() | endif
endfunction
function! s:send_highlight_request() abort
let l:capability = 'lsp#capabilities#has_document_highlight_provider(v:val)'
let l:servers = filter(lsp#get_allowed_servers(), l:capability)
if empty(l:servers)
return lsp#callbag#empty()
endif
return lsp#request(l:servers[0], {
\ 'method': 'textDocument/documentHighlight',
\ 'params': {
\ 'textDocument': lsp#get_text_document_identifier(),
\ 'position': lsp#get_position(),
\ },
\ })
endfunction
function! s:set_highlights(data) abort
let l:bufnr = bufnr('%')
call s:clear_highlights()
if mode() !=# 'n' | return | endif
if lsp#client#is_error(a:data) | return | endif
" Get references from the response
let l:reference_list = a:data['response']['result']
if empty(l:reference_list)
return
endif
" Convert references to vim positions
let l:position_list = []
for l:reference in l:reference_list
call extend(l:position_list, lsp#utils#range#lsp_to_vim(l:bufnr, l:reference['range']))
endfor
call sort(l:position_list, function('s:compare_positions'))
" Ignore response if the cursor is not over a reference anymore
if s:in_reference(l:position_list) == -1 | return | endif
" Store references
let b:lsp_reference_positions = l:position_list
let b:lsp_reference_matches = []
" Apply highlights to the buffer
call s:init_reference_highlight(l:bufnr)
if s:use_vim_textprops
for l:position in l:position_list
call prop_add(l:position[0], l:position[1],
\ {'id': s:prop_id,
\ 'bufnr': l:bufnr,
\ 'length': l:position[2],
\ 'type': 'vim-lsp-reference-highlight'})
call add(b:lsp_reference_matches, l:position[0])
endfor
else
for l:position in l:position_list
let l:match = matchaddpos('lspReference', [l:position], -5)
call add(b:lsp_reference_matches, l:match)
endfor
endif
endfunction
function! s:clear_highlights() abort
if exists('b:lsp_reference_matches')
if s:use_vim_textprops
let l:bufnr = bufnr('%')
for l:line in b:lsp_reference_matches
silent! call prop_remove(
\ {'id': s:prop_id,
\ 'bufnr': l:bufnr,
\ 'all': v:true}, l:line)
endfor
else
for l:match in b:lsp_reference_matches
silent! call matchdelete(l:match)
endfor
endif
unlet b:lsp_reference_matches
unlet b:lsp_reference_positions
endif
endfunction
" Compare two positions
function! s:compare_positions(p1, p2) abort
let l:line_1 = a:p1[0]
let l:line_2 = a:p2[0]
if l:line_1 != l:line_2
return l:line_1 > l:line_2 ? 1 : -1
endif
let l:col_1 = a:p1[1]
let l:col_2 = a:p2[1]
return l:col_1 - l:col_2
endfunction
" If the cursor is over a reference, return its index in
" the array. Otherwise, return -1.
function! s:in_reference(reference_list) abort
let l:line = line('.')
let l:column = col('.')
let l:index = 0
for l:position in a:reference_list
if l:line == l:position[0] &&
\ l:column >= l:position[1] &&
\ l:column < l:position[1] + l:position[2]
return l:index
endif
let l:index += 1
endfor
return -1
endfunction
function! s:init_reference_highlight(buf) abort
if !empty(getbufvar(a:buf, 'lsp_did_reference_setup'))
return
endif
if s:use_vim_textprops
call prop_type_add('vim-lsp-reference-highlight', {
\ 'bufnr': a:buf,
\ 'highlight': 'lspReference',
\ 'combine': v:true
\ })
endif
call setbufvar(a:buf, 'lsp_did_reference_setup', 1)
endfunction
" Cyclically move between references by `offset` occurrences.
function! lsp#internal#highlight_references#jump(offset) abort
if !exists('b:lsp_reference_positions')
echohl WarningMsg
echom 'References not available'
echohl None
return
endif
" Get index of reference under cursor
let l:index = s:in_reference(b:lsp_reference_positions)
if l:index < 0
return
endif
let l:n = len(b:lsp_reference_positions)
let l:index += a:offset
" Show a message when reaching TOP/BOTTOM of the file
if l:index < 0
echohl WarningMsg
echom 'search hit TOP, continuing at BOTTOM'
echohl None
elseif l:index >= len(b:lsp_reference_positions)
echohl WarningMsgand send
echom 'search hit BOTTOM, continuing at TOP'
echohl None
endif
" Wrap index
if l:index < 0 || l:index >= len(b:lsp_reference_positions)
let l:index = (l:index % l:n + l:n) % l:n
endif
" Jump
let l:target = b:lsp_reference_positions[l:index][0:1]
normal! m`
call cursor(l:target[0], l:target[1])
endfunction

View File

@@ -1,268 +0,0 @@
" Global state
let s:last_req_id = 0
let s:pending = {}
let s:use_vim_textprops = has('textprop') && !has('nvim')
let s:prop_id = 11
" Highlight group for references
if !hlexists('lspReference')
highlight link lspReference CursorColumn
endif
" Convert a LSP range to one or more vim match positions.
" If the range spans over multiple lines, break it down to multiple
" positions, one for each line.
" Return a list of positions.
function! s:range_to_position(bufnr, range) abort
let l:position = []
let [l:start_line, l:start_col] = lsp#utils#position#lsp_to_vim(a:bufnr, a:range['start'])
let [l:end_line, l:end_col] = lsp#utils#position#lsp_to_vim(a:bufnr, a:range['end'])
if l:end_line == l:start_line
let l:position = [[
\ l:start_line,
\ l:start_col,
\ l:end_col - l:start_col
\ ]]
else
" First line
let l:position = [[
\ l:start_line,
\ l:start_col,
\ 999
\ ]]
" Last line
call add(l:position, [
\ l:end_line,
\ 1,
\ l:end_col
\ ])
" Lines in the middle
let l:middle_lines = map(
\ range(l:start_line + 1, l:end_line - 1),
\ {_, l -> [l, 0, 999]}
\ )
call extend(l:position, l:middle_lines)
endif
return l:position
endfunction
" Compare two positions
function! s:compare_positions(p1, p2) abort
let l:line_1 = a:p1[0]
let l:line_2 = a:p2[0]
if l:line_1 != l:line_2
return l:line_1 > l:line_2 ? 1 : -1
endif
let l:col_1 = a:p1[1]
let l:col_2 = a:p2[1]
return l:col_1 - l:col_2
endfunction
" If the cursor is over a reference, return its index in
" the array. Otherwise, return -1.
function! s:in_reference(reference_list) abort
let l:line = line('.')
let l:column = col('.')
let l:index = 0
for l:position in a:reference_list
if l:line == l:position[0] &&
\ l:column >= l:position[1] &&
\ l:column < l:position[1] + l:position[2]
return l:index
endif
let l:index += 1
endfor
return -1
endfunction
" Handle response from server.
function! s:handle_references(ctx, data) abort
" Sanity checks
if !has_key(s:pending, a:ctx['filetype']) ||
\ !s:pending[a:ctx['filetype']]
return
endif
let s:pending[a:ctx['filetype']] = v:false
if lsp#client#is_error(a:data['response'])
return
end
" More sanity checks
if a:ctx['bufnr'] != bufnr('%') || a:ctx['last_req_id'] != s:last_req_id
return
endif
" Remove existing highlights from the buffer
call lsp#ui#vim#references#clean_references()
" Get references from the response
let l:reference_list = a:data['response']['result']
if empty(l:reference_list)
return
endif
" Convert references to vim positions
let l:position_list = []
for l:reference in l:reference_list
call extend(l:position_list, s:range_to_position(a:ctx['bufnr'], l:reference['range']))
endfor
call sort(l:position_list, function('s:compare_positions'))
" Ignore response if the cursor is not over a reference anymore
if s:in_reference(l:position_list) == -1
" If the cursor has moved: send another request
if a:ctx['curpos'] != getcurpos()
call lsp#ui#vim#references#highlight(v:true)
endif
return
endif
" Store references
let w:lsp_reference_positions = l:position_list
let w:lsp_reference_matches = []
" Apply highlights to the buffer
if g:lsp_highlight_references_enabled
let l:bufnr = bufnr('%')
call s:init_reference_highlight(l:bufnr)
if s:use_vim_textprops
for l:position in l:position_list
call prop_add(l:position[0], l:position[1],
\ {'id': s:prop_id,
\ 'bufnr': l:bufnr,
\ 'length': l:position[2],
\ 'type': 'vim-lsp-reference-highlight'})
call add(w:lsp_reference_matches, l:position[0])
endfor
else
for l:position in l:position_list
let l:match = matchaddpos('lspReference', [l:position], -5)
call add(w:lsp_reference_matches, l:match)
endfor
endif
endif
endfunction
function! s:init_reference_highlight(buf) abort
if !empty(getbufvar(a:buf, 'lsp_did_reference_setup'))
return
endif
if s:use_vim_textprops
call prop_type_add('vim-lsp-reference-highlight',
\ {'bufnr': bufnr('%'),
\ 'highlight': 'lspReference',
\ 'combine': v:true})
endif
call setbufvar(a:buf, 'lsp_did_reference_setup', 1)
endfunction
" Highlight references to the symbol under the cursor
function! lsp#ui#vim#references#highlight(force_refresh) abort
" No need to change the highlights if the cursor has not left
" the currently highlighted symbol.
if !a:force_refresh &&
\ exists('w:lsp_reference_positions') &&
\ s:in_reference(w:lsp_reference_positions) != -1
return
endif
" A request for this symbol has already been sent
if has_key(s:pending, &filetype) && s:pending[&filetype]
return
endif
" Check if any server provides document highlight
let l:capability = 'lsp#capabilities#has_document_highlight_provider(v:val)'
let l:servers = filter(lsp#get_allowed_servers(), l:capability)
if len(l:servers) == 0
return
endif
" Send a request
let s:pending[&filetype] = v:true
let s:last_req_id += 1
let l:ctx = {
\ 'last_req_id': s:last_req_id,
\ 'curpos': getcurpos(),
\ 'bufnr': bufnr('%'),
\ 'filetype': &filetype,
\ }
call lsp#send_request(l:servers[0], {
\ 'method': 'textDocument/documentHighlight',
\ 'params': {
\ 'textDocument': lsp#get_text_document_identifier(),
\ 'position': lsp#get_position(),
\ },
\ 'on_notification': function('s:handle_references', [l:ctx]),
\ })
endfunction
" Remove all reference highlights from the buffer
function! lsp#ui#vim#references#clean_references() abort
let s:pending[&filetype] = v:false
if exists('w:lsp_reference_matches')
if s:use_vim_textprops
let l:bufnr = bufnr('%')
for l:line in w:lsp_reference_matches
silent! call prop_remove(
\ {'id': s:prop_id,
\ 'bufnr': l:bufnr,
\ 'all': v:true}, l:line)
endfor
else
for l:match in w:lsp_reference_matches
silent! call matchdelete(l:match)
endfor
endif
unlet w:lsp_reference_matches
unlet w:lsp_reference_positions
endif
endfunction
" Cyclically move between references by `offset` occurrences.
function! lsp#ui#vim#references#jump(offset) abort
if !exists('w:lsp_reference_positions')
echohl WarningMsg
echom 'References not available'
echohl None
return
endif
" Get index of reference under cursor
let l:index = s:in_reference(w:lsp_reference_positions)
if l:index < 0
return
endif
let l:n = len(w:lsp_reference_positions)
let l:index += a:offset
" Show a message when reaching TOP/BOTTOM of the file
if l:index < 0
echohl WarningMsg
echom 'search hit TOP, continuing at BOTTOM'
echohl None
elseif l:index >= len(w:lsp_reference_positions)
echohl WarningMsg
echom 'search hit BOTTOM, continuing at TOP'
echohl None
endif
" Wrap index
if l:index < 0 || l:index >= len(w:lsp_reference_positions)
let l:index = (l:index % l:n + l:n) % l:n
endif
" Jump
let l:target = w:lsp_reference_positions[l:index][0:1]
normal! m`
call cursor(l:target[0], l:target[1])
endfunction

View File

@@ -29,3 +29,44 @@ function! lsp#utils#range#_get_current_line_range() abort
return l:range
endfunction
" Convert a LSP range to one or more vim match positions.
" If the range spans over multiple lines, break it down to multiple
" positions, one for each line.
" Return a list of positions.
function! lsp#utils#range#lsp_to_vim(bufnr, range) abort
let l:position = []
let [l:start_line, l:start_col] = lsp#utils#position#lsp_to_vim(a:bufnr, a:range['start'])
let [l:end_line, l:end_col] = lsp#utils#position#lsp_to_vim(a:bufnr, a:range['end'])
if l:end_line == l:start_line
let l:position = [[
\ l:start_line,
\ l:start_col,
\ l:end_col - l:start_col
\ ]]
else
" First line
let l:position = [[
\ l:start_line,
\ l:start_col,
\ 999
\ ]]
" Last line
call add(l:position, [
\ l:end_line,
\ 1,
\ l:end_col
\ ])
" Lines in the middle
let l:middle_lines = map(
\ range(l:start_line + 1, l:end_line - 1),
\ {_, l -> [l, 0, 999]}
\ )
call extend(l:position, l:middle_lines)
endif
return l:position
endfunction

View File

@@ -33,6 +33,7 @@ CONTENTS *vim-lsp-contents*
g:lsp_textprop_enabled |g:lsp_textprop_enabled|
g:lsp_use_event_queue |g:lsp_use_event_queue|
g:lsp_highlight_references_enabled |g:lsp_highlight_references_enabled|
g:lsp_highlight_references_delay |g:lsp_highlight_references_delay|
g:lsp_get_supported_capabilities |g:lsp_get_supported_capabilities|
g:lsp_peek_alignment |g:lsp_peek_alignment|
g:lsp_preview_max_width |g:lsp_preview_max_width|
@@ -542,7 +543,7 @@ g:lsp_use_event_queue *g:lsp_use_event_queue*
g:lsp_highlight_references_enabled *g:lsp_highlight_references_enabled*
Type: |Number|
Default: `0`
Default: `1`
Enable highlighting of the references to the symbol under the cursor.
@@ -557,6 +558,17 @@ g:lsp_highlight_references_enabled *g:lsp_highlight_references_enabled*
Example: >
highlight lspReference ctermfg=red guifg=red ctermbg=green guibg=green
g:lsp_highlight_references_delay *g:lsp_highlight_references_delay*
Type: |Number|
Default: `350`
Delay milliseconds to highlight references. Requires
|g:lsp_highlight_references_enabled| set to 1.
Example: >
let g:lsp_highlight_references_delay = 200
let g:lsp_highlight_references_delay = 1000
g:lsp_get_supported_capabilities *g:lsp_get_supported_capabilities*
Type: |List|
Default: `[function('lsp#default_get_supported_capabilities')]`

View File

@@ -32,7 +32,8 @@ let g:lsp_preview_keep_focus = get(g:, 'lsp_preview_keep_focus', 1)
let g:lsp_use_event_queue = get(g:, 'lsp_use_event_queue', has('nvim') || has('patch-8.1.0889'))
let g:lsp_insert_text_enabled= get(g:, 'lsp_insert_text_enabled', 1)
let g:lsp_text_edit_enabled = get(g:, 'lsp_text_edit_enabled', has('patch-8.0.1493'))
let g:lsp_highlight_references_enabled = get(g:, 'lsp_highlight_references_enabled', 0)
let g:lsp_highlight_references_enabled = get(g:, 'lsp_highlight_references_enabled', 1)
let g:lsp_highlight_references_delay = get(g:, 'lsp_highlight_references_delay', 350)
let g:lsp_preview_float = get(g:, 'lsp_preview_float', 1)
let g:lsp_preview_autoclose = get(g:, 'lsp_preview_autoclose', 1)
let g:lsp_preview_doubletap = get(g:, 'lsp_preview_doubletap', [function('lsp#ui#vim#output#focuspreview')])
@@ -99,8 +100,8 @@ command! -range LspDocumentRangeFormatSync call lsp#ui#vim#document_range_format
command! LspImplementation call lsp#ui#vim#implementation(0, <q-mods>)
command! LspPeekImplementation call lsp#ui#vim#implementation(1)
command! -nargs=0 LspStatus call lsp#print_server_status()
command! LspNextReference call lsp#ui#vim#references#jump(+1)
command! LspPreviousReference call lsp#ui#vim#references#jump(-1)
command! LspNextReference call lsp#internal#highlight_references#jump(+1)
command! LspPreviousReference call lsp#internal#highlight_references#jump(-1)
command! -nargs=? -complete=customlist,lsp#server_complete LspStopServer call lsp#ui#vim#stop_server(<f-args>)
command! -nargs=? -complete=customlist,lsp#utils#empty_complete LspSignatureHelp call lsp#ui#vim#signature_help#get_signature_help_under_cursor()
command! LspDocumentFold call lsp#ui#vim#folding#fold(0)