-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement linkedEditingRange (experimental) #1022
Changes from 9 commits
9c99f91
59469e5
e5f975e
2c6ada3
a513c3a
761ac6d
7db44b6
71b1c56
4362c7d
1fcd013
dc67af1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
let s:TextEdit = vital#lsp#import('VS.LSP.TextEdit') | ||
let s:TextMark = vital#lsp#import('VS.Vim.Buffer.TextMark') | ||
|
||
let s:TEXT_MARK_NAMESPACE = 'lsp#internal#linked_editing_range' | ||
|
||
let s:state = {} | ||
let s:state['bufnr'] = -1 | ||
let s:state['changenr'] = -1 | ||
let s:state['changedtick'] = -1 | ||
|
||
function! lsp#internal#linked_editing_range#_enable() abort | ||
if !s:enabled() | ||
return | ||
endif | ||
|
||
let s:Dispose = lsp#callbag#merge( | ||
\ lsp#callbag#pipe( | ||
\ lsp#callbag#fromEvent(['InsertEnter']), | ||
\ lsp#callbag#filter({ -> g:lsp_linked_editing_range_enabled }), | ||
\ lsp#callbag#flatMap({ -> s:request_sync() }), | ||
\ lsp#callbag#subscribe({ | ||
\ 'next': { x -> call('s:prepare', x) } | ||
hrsh7th marked this conversation as resolved.
Show resolved
Hide resolved
|
||
\ }) | ||
\ ), | ||
\ lsp#callbag#pipe( | ||
\ lsp#callbag#fromEvent(['InsertLeave']), | ||
\ lsp#callbag#filter({ -> g:lsp_linked_editing_range_enabled }), | ||
\ lsp#callbag#subscribe({ -> s:clear() }) | ||
\ ), | ||
\ lsp#callbag#pipe( | ||
\ lsp#callbag#fromEvent(['TextChanged', 'TextChangedI', 'TextChangedP']), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think for now it is ok. but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will also hopefully encourage folks to migrate to newer versions. |
||
\ lsp#callbag#filter({ -> g:lsp_linked_editing_range_enabled }), | ||
\ lsp#callbag#subscribe({ -> s:sync() }) | ||
hrsh7th marked this conversation as resolved.
Show resolved
Hide resolved
|
||
\ ), | ||
\ ) | ||
endfunction | ||
|
||
function! lsp#internal#linked_editing_range#_disable() abort | ||
if exists('s:Dispose') | ||
call s:clear() | ||
call s:Dispose() | ||
unlet s:Dispose | ||
endif | ||
endfunction | ||
|
||
function! lsp#internal#linked_editing_range#prepare() abort | ||
if !s:enabled() | ||
return '' | ||
endif | ||
|
||
call lsp#callbag#pipe( | ||
\ s:request_sync(), | ||
\ lsp#callbag#subscribe({ | ||
\ 'next': { x -> call('s:prepare', x) }, | ||
\ 'error': { -> {} }, | ||
\ }) | ||
\ ) | ||
return '' | ||
endfunction | ||
|
||
function! s:enabled(...) abort | ||
return g:lsp_linked_editing_range_enabled && s:TextMark.is_available() | ||
endfunction | ||
|
||
function! s:request_sync() abort | ||
let l:server = lsp#get_allowed_servers(&filetype) | ||
let l:server = filter(l:server, 'lsp#capabilities#has_linked_editing_range_provider(v:val)') | ||
let l:server = get(l:server, 0, v:null) | ||
if empty(l:server) | ||
return lsp#callbag#of([v:null]) | ||
endif | ||
|
||
return lsp#callbag#of( | ||
\ lsp#callbag#pipe( | ||
\ lsp#request(l:server, { | ||
\ 'method': 'textDocument/linkedEditingRange', | ||
\ 'params': { | ||
\ 'textDocument': lsp#get_text_document_identifier(), | ||
\ 'position': lsp#get_position(), | ||
\ } | ||
\ }), | ||
\ lsp#callbag#toList() | ||
\ ).wait({ 'sleep': 1, 'timeout': 200 }) | ||
hrsh7th marked this conversation as resolved.
Show resolved
Hide resolved
|
||
\ ) | ||
endfunction | ||
|
||
function! s:prepare(x) abort | ||
if empty(a:x) || empty(get(a:x, 'response')) || empty(get(a:x['response'], 'result')) || empty(get(a:x['response']['result'], 'ranges')) | ||
return | ||
endif | ||
|
||
call s:clear() | ||
call s:TextMark.set(bufnr('%'), s:TEXT_MARK_NAMESPACE, map(a:x['response']['result']['ranges'], { _, range -> { | ||
\ 'start_pos': lsp#utils#position#lsp_to_vim('%', range['start']), | ||
\ 'end_pos': lsp#utils#position#lsp_to_vim('%', range['end']), | ||
\ 'highlight': 'Underlined', | ||
\ } })) | ||
let s:state['bufnr'] = bufnr('%') | ||
let s:state['changenr'] = changenr() | ||
let s:state['changedtick'] = b:changedtick | ||
endfunction | ||
|
||
function! s:clear() abort | ||
call s:TextMark.clear(bufnr('%'), s:TEXT_MARK_NAMESPACE) | ||
endfunction | ||
|
||
function! s:sync() abort | ||
let l:bufnr = bufnr('%') | ||
if s:state['bufnr'] != l:bufnr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for callbags this is usually not preferred. you can instead use Feel free to change or ignore it for now and I can refactor it later when it is merged. I usually use RxJS or other Rx libraries does to understand callbag. High level these are what it means.
But since you are using wait what you have should be ok. Some good reading here. https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. I learned a lot. |
||
return | ||
endif | ||
if s:state['changedtick'] == b:changedtick | ||
hrsh7th marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
endif | ||
if s:state['changenr'] > changenr() | ||
return | ||
endif | ||
|
||
" get current mark and related marks. | ||
let l:pos = getpos('.')[1 : 2] | ||
let l:current_mark = v:null | ||
let l:related_marks = [] | ||
for l:mark in s:TextMark.get(l:bufnr, s:TEXT_MARK_NAMESPACE) | ||
let l:start_pos = l:mark['start_pos'] | ||
let l:end_pos = l:mark['end_pos'] | ||
|
||
let l:contains = v:true | ||
let l:contains = l:contains && (l:start_pos[0] < l:pos[0] || l:start_pos[0] == l:pos[0] && l:start_pos[1] <= l:pos[1]) | ||
let l:contains = l:contains && (l:end_pos[0] > l:pos[0] || l:end_pos[0] == l:pos[0] && l:end_pos[1] >= l:pos[1]) | ||
if l:contains | ||
let l:current_mark = l:mark | ||
else | ||
let l:related_marks += [l:mark] | ||
endif | ||
endfor | ||
|
||
" ignore if current mark is not detected. | ||
if empty(l:current_mark) | ||
return | ||
endif | ||
|
||
" apply new text for related marks. | ||
let l:new_text = lsp#utils#range#_get_text(l:bufnr, { | ||
\ 'start': lsp#utils#position#vim_to_lsp('%', l:current_mark['start_pos']), | ||
\ 'end': lsp#utils#position#vim_to_lsp('%', l:current_mark['end_pos']), | ||
\ }) | ||
if l:new_text !~# '^\k*$' | ||
call s:clear() | ||
call feedkeys("\<C-G>u", 'n') | ||
return | ||
endif | ||
|
||
call lsp#utils#text_edit#apply_text_edits(l:bufnr, map(l:related_marks, { _, mark -> { | ||
\ 'range': { | ||
\ 'start': lsp#utils#position#vim_to_lsp('%', mark['start_pos']), | ||
\ 'end': lsp#utils#position#vim_to_lsp('%', mark['end_pos']), | ||
\ }, | ||
\ 'newText': l:new_text | ||
\ } })) | ||
|
||
" save state. | ||
let s:state['bufnr'] = l:bufnr | ||
let s:state['changenr'] = changenr() | ||
let s:state['changedtick'] = b:changedtick | ||
endfunction |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,38 @@ function! lsp#utils#range#_get_current_line_range() abort | |
return l:range | ||
endfunction | ||
|
||
" Returns the range contains specified position or not. | ||
function! lsp#utils#range#_contains(range, position) abort | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like this can be used to solve this issue now. #888 |
||
if !( | ||
\ a:range['start']['line'] <= a:position['line'] && ( | ||
\ a:range['start']['line'] == a:position['line'] && | ||
\ a:range['start']['character'] <= a:position['character'] | ||
\ ) | ||
\ ) | ||
return v:false | ||
endif | ||
if !( | ||
\ a:range['end']['line'] >= a:position['line'] && ( | ||
\ a:range['end']['line'] == a:position['line'] && | ||
\ a:range['end']['character'] >= a:position['character'] | ||
\ ) | ||
\ ) | ||
return v:false | ||
endif | ||
return v:true | ||
endfunction | ||
|
||
" Return the range of text for the specified expr. | ||
function! lsp#utils#range#_get_text(expr, range) abort | ||
let l:lines = [] | ||
for l:line in range(a:range['start']['line'], a:range['end']['line']) | ||
let l:lines += getbufline(a:expr, l:line + 1) | ||
endfor | ||
let l:lines[-1] = strcharpart(l:lines[-1], 0, a:range['end']['character']) | ||
let l:lines[0] = strcharpart(l:lines[0], a:range['start']['character'], strchars(l:lines[0])) | ||
return join(l:lines, "\n") | ||
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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason it needs to be sync?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I afraid to lost keypress if the user presses quickly. So I'm choosing it to be sync.
But if text-prop/extmark is working correct, we can choose debouncing it.