Skip to content

Commit 64948a6

Browse files
committed
Improve implementation and add docs
1 parent 59469e5 commit 64948a6

File tree

3 files changed

+63
-29
lines changed

3 files changed

+63
-29
lines changed

autoload/lsp/internal/linked_editing_range.vim

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ function! lsp#internal#linked_editing_range#_enable() abort
1212
if !s:TextEdit.is_text_mark_preserved()
1313
return
1414
endif
15-
1615
if !g:lsp_linked_editing_range_enabled | return | endif
16+
1717
let s:Dispose = lsp#callbag#merge(
1818
\ lsp#callbag#pipe(
1919
\ lsp#callbag#fromEvent(['InsertEnter']),
20-
\ lsp#callbag#flatMap({ -> s:request(v:false) }),
20+
\ lsp#callbag#flatMap({ -> s:request_sync() }),
2121
\ lsp#callbag#subscribe({
22-
\ 'next': { x -> s:prepare(x) }
22+
\ 'next': { x -> call('s:prepare', x) }
2323
\ })
2424
\ ),
2525
\ lsp#callbag#pipe(
@@ -42,43 +42,52 @@ function! lsp#internal#linked_editing_range#_disable() abort
4242
endif
4343
endfunction
4444

45-
function! s:request(sync) abort
45+
function! lsp#internal#linked_editing_range#prepare() abort
46+
if !s:TextEdit.is_text_mark_preserved()
47+
return ''
48+
endif
49+
if !g:lsp_linked_editing_range_enabled
50+
return ''
51+
endif
52+
53+
call lsp#callbag#pipe(
54+
\ s:request_sync(),
55+
\ lsp#callbag#subscribe({
56+
\ 'next': { x -> call('s:prepare', x) },
57+
\ 'error': { -> {} },
58+
\ })
59+
\ )
60+
return ''
61+
endfunction
62+
63+
function! s:request_sync() abort
4664
let l:server = lsp#get_allowed_servers(&filetype)
4765
let l:server = filter(l:server, 'lsp#capabilities#has_linked_editing_range_provider(v:val)')
4866
let l:server = get(l:server, 0, v:null)
4967
if empty(l:server)
50-
return lsp#callbag#empty()
68+
return lsp#callbag#of([v:null])
5169
endif
5270

53-
let l:X = lsp#callbag#pipe(
54-
\ lsp#request(l:server, {
55-
\ 'method': 'textDocument/linkedEditingRange',
56-
\ 'params': {
57-
\ 'textDocument': lsp#get_text_document_identifier(),
58-
\ 'position': lsp#get_position(),
59-
\ }
60-
\ }),
71+
return lsp#callbag#of(
72+
\ lsp#callbag#pipe(
73+
\ lsp#request(l:server, {
74+
\ 'method': 'textDocument/linkedEditingRange',
75+
\ 'params': {
76+
\ 'textDocument': lsp#get_text_document_identifier(),
77+
\ 'position': lsp#get_position(),
78+
\ }
79+
\ }),
80+
\ lsp#callbag#toList()
81+
\ ).wait({ 'sleep': 1, 'timeout': 200 })
6182
\ )
62-
if a:sync
63-
return lsp#callbag#of(
64-
\ get(
65-
\ lsp#callbag#pipe(
66-
\ l:X,
67-
\ lsp#callbag#toList()
68-
\ ).wait({ 'sleep': 1, 'timeout': 200 }),
69-
\ 0,
70-
\ v:null
71-
\ )
72-
\ )
73-
endif
74-
return l:X
7583
endfunction
7684

7785
function! s:prepare(x) abort
78-
if empty(get(a:x['response']['result'], 'ranges', {}))
79-
return
86+
if empty(a:x) || empty(get(a:x, 'response')) || empty(get(a:x['response'], 'result')) || empty(get(a:x['response']['result'], 'ranges'))
87+
return
8088
endif
8189

90+
call s:clear()
8291
call s:TextMark.set(bufnr('%'), s:TEXT_MARK_NAMESPACE, map(a:x['response']['result']['ranges'], { _, range -> {
8392
\ 'range': range,
8493
\ 'highlight': 'Underlined',
@@ -100,7 +109,7 @@ function! s:sync() abort
100109
if s:state['changedtick'] == b:changedtick
101110
return
102111
endif
103-
if s:state['changenr'] > changenr()
112+
if s:state['changenr'] > changenr()
104113
return
105114
endif
106115

doc/vim-lsp.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CONTENTS *vim-lsp-contents*
5050
|g:lsp_diagnostics_virtual_text_delay|
5151
g:lsp_diagnostics_virtual_text_prefix
5252
|g:lsp_diagnostics_virtual_text_prefix|
53+
g:lsp_linked_editing_range_enabled |g:lsp_linked_editing_range_enabled|
5354
g:lsp_format_sync_timeout |g:lsp_format_sync_timeout|
5455
g:lsp_use_event_queue |g:lsp_use_event_queue|
5556
g:lsp_document_highlight_enabled |g:lsp_document_highlight_enabled|
@@ -680,6 +681,27 @@ g:lsp_diagnostics_virtual_text_prefix *g:lsp_diagnostics_virtual_text_prefix*
680681
let g:lsp_diagnostics_virtual_text_prefix = "> "
681682
let g:lsp_diagnostics_virtual_text_prefix = " ‣ "
682683
684+
g:lsp_linked_editing_enabled *g:lsp_linked_editing_enabled*
685+
Type: |Number|
686+
Default: `1` for neovim or vim with patch-8.1.0889
687+
688+
Enable textDocument/linkedEditingRange feature.
689+
NOTE: This only works on the environment has `nvim_buf_set_text` for now.
690+
691+
Example: >
692+
let g:lsp_linked_editing_enabled = 1
693+
let g:lsp_linked_editing_enabled = 0
694+
695+
" The below mappings enable to support `d` or `c` commands.
696+
nnoremap <buffer> <Plug>(vimrc-d) d
697+
xnoremap <buffer> <Plug>(vimrc-d) d
698+
nnoremap <buffer> <Plug>(vimrc-c) c
699+
xnoremap <buffer> <Plug>(vimrc-c) c
700+
nmap <buffer> d <Plug>(lsp-linked-editing-range-prepare)<Plug>(vimrc-d)
701+
xmap <buffer> d <Plug>(lsp-linked-editing-range-prepare)<Plug>(vimrc-d)
702+
nmap <buffer> c <Plug>(lsp-linked-editing-range-prepare)<Plug>(vimrc-c)
703+
xmap <buffer> c <Plug>(lsp-linked-editing-range-prepare)<Plug>(vimrc-c)
704+
683705
g:lsp_use_event_queue *g:lsp_use_event_queue*
684706
Type: |Number|
685707
Default: `1` for neovim or vim with patch-8.1.0889

plugin/lsp.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,6 @@ nnoremap <plug>(lsp-status) :<c-u>echo lsp#get_server_status()<cr>
167167
nnoremap <plug>(lsp-next-reference) :<c-u>call lsp#internal#document_highlight#jump(+1)<cr>
168168
nnoremap <plug>(lsp-previous-reference) :<c-u>call lsp#internal#document_highlight#jump(-1)<cr>
169169
nnoremap <plug>(lsp-signature-help) :<c-u>call lsp#ui#vim#signature_help#get_signature_help_under_cursor()<cr>
170+
nnoremap <expr> <plug>(lsp-linked-editing-range-prepare) lsp#internal#linked_editing_range#prepare()
171+
xnoremap <expr> <plug>(lsp-linked-editing-range-prepare) lsp#internal#linked_editing_range#prepare()
172+

0 commit comments

Comments
 (0)