- 
                Notifications
    
You must be signed in to change notification settings  - Fork 309
 
Implement linkedEditingRange (experimental) #1022
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
Changes from 2 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,136 @@ | ||
| 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:TextEdit.is_text_mark_preserved() | ||
| return | ||
| endif | ||
| 
     | 
||
| if !g:lsp_linked_editing_range_enabled | return | endif | ||
| let s:Dispose = lsp#callbag#merge( | ||
| \ lsp#callbag#pipe( | ||
| \ lsp#callbag#fromEvent(['InsertEnter']), | ||
| \ lsp#callbag#flatMap({ -> s:request(v:false) }), | ||
| \ lsp#callbag#subscribe({ | ||
| \ 'next': { x -> s:prepare(x) } | ||
| \ }) | ||
| \ ), | ||
| \ lsp#callbag#pipe( | ||
| \ lsp#callbag#fromEvent(['InsertLeave']), | ||
| \ 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#delay(0), | ||
                
      
                  hrsh7th marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| \ 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! 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#empty() | ||
| endif | ||
| 
     | 
||
| let l:X = lsp#callbag#pipe( | ||
| \ lsp#request(l:server, { | ||
| \ 'method': 'textDocument/linkedEditingRange', | ||
| \ 'params': { | ||
| \ 'textDocument': lsp#get_text_document_identifier(), | ||
| \ 'position': lsp#get_position(), | ||
| \ } | ||
| \ }), | ||
| \ ) | ||
| if a:sync | ||
| return lsp#callbag#of( | ||
| \ get( | ||
| \ lsp#callbag#pipe( | ||
| \ l:X, | ||
| \ lsp#callbag#toList() | ||
| \ ).wait({ 'sleep': 1, 'timeout': 200 }), | ||
| \ 0, | ||
| \ v:null | ||
| \ ) | ||
| \ ) | ||
| endif | ||
| return l:X | ||
| endfunction | ||
| 
     | 
||
| function! s:prepare(x) abort | ||
| if empty(get(a:x['response']['result'], 'ranges', {})) | ||
| return | ||
| endif | ||
| 
     | 
||
| call s:TextMark.set(bufnr('%'), s:TEXT_MARK_NAMESPACE, map(a:x['response']['result']['ranges'], { _, range -> { | ||
| \ 'range': range, | ||
| \ '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 | ||
                
       | 
||
| return | ||
| endif | ||
| if s:state['changedtick'] == b:changedtick | ||
                
      
                  hrsh7th marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| return | ||
| endif | ||
| if s:state['changenr'] > changenr() | ||
                
      
                  hrsh7th marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| return | ||
| endif | ||
| 
     | 
||
| " get current mark and related marks. | ||
| let l:position = lsp#utils#position#vim_to_lsp('%', 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) | ||
| if lsp#utils#range#_contains(l:mark['range'], l:position) | ||
| 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, l:current_mark['range']) | ||
| call lsp#utils#text_edit#apply_text_edits(l:bufnr, map(l:related_marks, { _, mark -> { | ||
| \ 'range': mark['range'], | ||
| \ '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. | ||
| 
          
            
          
           | 
    ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| let s:_plugin_name = expand('<sfile>:t:r') | ||
| 
     | 
||
| function! vital#{s:_plugin_name}#new() abort | ||
| return vital#{s:_plugin_name[1:]}#new() | ||
| endfunction | ||
| 
     | 
||
| function! vital#{s:_plugin_name}#function(funcname) abort | ||
| silent! return function(a:funcname) | ||
| endfunction | 
Uh oh!
There was an error while loading. Please reload this page.