-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9c99f91
Implement linkedEditingRange
hrsh7th 59469e5
Update vital modules
hrsh7th e5f975e
Improve implementation and add docs
hrsh7th 2c6ada3
Update vital modules
hrsh7th a513c3a
Check availability
hrsh7th 761ac6d
Check feature flags
hrsh7th 7db44b6
Always use method in TextEdit
hrsh7th 71b1c56
Fix for undo history
hrsh7th 4362c7d
Follow vital module updates (The TextMark has now using vim's positio…
hrsh7th 1fcd013
- Use switchMap instead of flatMap
hrsh7th dc67af1
Check TextChangedP
hrsh7th File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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#switchMap({ -> lsp#callbag#of(s:request_sync()) }), | ||
\ lsp#callbag#subscribe({ | ||
\ 'next': { x -> s:prepare(x) } | ||
\ }) | ||
\ ), | ||
\ 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']), | ||
\ lsp#callbag#filter({ -> g:lsp_linked_editing_range_enabled }), | ||
\ lsp#callbag#filter({ -> | ||
\ s:state.bufnr == bufnr('%') && | ||
\ s:state.changedtick != b:changedtick && | ||
\ s:state.changenr <= changenr() | ||
\ }), | ||
\ 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 s:prepare(s:request_sync()) | ||
return '' | ||
endfunction | ||
|
||
function! s:enabled(...) abort | ||
return exists('##TextChangedP') && 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 v:null | ||
endif | ||
|
||
return 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({ 'wait': 1, 'timeout': 200 })[0] | ||
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 | ||
let l:ranges = a:x['response']['result']['ranges'] | ||
|
||
let l:bufnr = bufnr('%') | ||
let s:state['bufnr'] = l:bufnr | ||
let s:state['changenr'] = changenr() | ||
let s:state['changedtick'] = b:changedtick | ||
|
||
call s:clear() | ||
call s:TextMark.set(l:bufnr, s:TEXT_MARK_NAMESPACE, map(copy(l:ranges), { _, range -> { | ||
\ 'start_pos': lsp#utils#position#lsp_to_vim(l:bufnr, range['start']), | ||
\ 'end_pos': lsp#utils#position#lsp_to_vim(l:bufnr, range['end']), | ||
\ 'highlight': 'Underlined', | ||
\ } })) | ||
|
||
" TODO: Force enable extmark's gravity option. | ||
if has('nvim') | ||
let l:new_text = lsp#utils#range#_get_text(l:bufnr, l:ranges[0]) | ||
call s:TextEdit.apply(l:bufnr, map(copy(l:ranges), { _, range -> { | ||
\ 'range': range, | ||
\ 'newText': l:new_text, | ||
\ } })) | ||
endif | ||
endfunction | ||
|
||
function! s:clear() abort | ||
call s:TextMark.clear(bufnr('%'), s:TEXT_MARK_NAMESPACE) | ||
endfunction | ||
|
||
function! s:sync() abort | ||
" get current mark and related marks. | ||
let l:bufnr = bufnr('%') | ||
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 | ||
|
||
" if new_text does not match to keyword pattern, we stop syncing and break undopoint. | ||
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 | ||
|
||
" apply new text for related marks. | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think for now it is ok. but
TextChangedP
is not available in all versions. One easy fix would be to check forTextChangedP
support ins:enabled
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.
this will also hopefully encourage folks to migrate to newer versions.