diff --git a/autoload/lsp.vim b/autoload/lsp.vim index f5de1e1e9..3de09152d 100644 --- a/autoload/lsp.vim +++ b/autoload/lsp.vim @@ -61,6 +61,7 @@ function! lsp#enable() abort call lsp#ui#vim#completion#_setup() call lsp#internal#document_highlight#_enable() call lsp#internal#diagnostics#_enable() + call lsp#internal#linked_editing_range#_enable() call lsp#internal#show_message_request#_enable() call lsp#internal#work_done_progress#_enable() call s:register_events() @@ -518,6 +519,9 @@ function! lsp#default_get_supported_capabilities(server_info) abort \ 'dynamicRegistration': v:false, \ 'linkSupport' : v:true \ }, + \ 'linkedEditingRange': { + \ 'dynamicRegistration': v:false, + \ }, \ 'rangeFormatting': { \ 'dynamicRegistration': v:false, \ }, diff --git a/autoload/lsp/capabilities.vim b/autoload/lsp/capabilities.vim index b132e0708..d7d77f862 100644 --- a/autoload/lsp/capabilities.vim +++ b/autoload/lsp/capabilities.vim @@ -53,6 +53,10 @@ function! lsp#capabilities#has_implementation_provider(server_name) abort return s:has_provider(a:server_name, 'implementationProvider') endfunction +function! lsp#capabilities#has_linked_editing_range_provider(server_name) abort + return s:has_provider(a:server_name, 'linkedEditingRangeProvider') +endfunction + function! lsp#capabilities#has_code_action_provider(server_name) abort let l:capabilities = lsp#get_server_capabilities(a:server_name) if !empty(l:capabilities) && has_key(l:capabilities, 'codeActionProvider') diff --git a/autoload/lsp/internal/linked_editing_range.vim b/autoload/lsp/internal/linked_editing_range.vim new file mode 100644 index 000000000..5e4b8f026 --- /dev/null +++ b/autoload/lsp/internal/linked_editing_range.vim @@ -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() }) + \ ), + \ ) +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("\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 diff --git a/autoload/lsp/utils/range.vim b/autoload/lsp/utils/range.vim index c8aded4ef..aef91370e 100644 --- a/autoload/lsp/utils/range.vim +++ b/autoload/lsp/utils/range.vim @@ -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 + 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. diff --git a/autoload/lsp/utils/text_edit.vim b/autoload/lsp/utils/text_edit.vim index 717506e27..6196ab545 100644 --- a/autoload/lsp/utils/text_edit.vim +++ b/autoload/lsp/utils/text_edit.vim @@ -1,19 +1,10 @@ -function! lsp#utils#text_edit#apply_text_edits(uri, text_edits) abort - let l:current_bufname = bufname('%') - let l:target_bufname = lsp#utils#uri_to_path(a:uri) - let l:cursor_position = lsp#get_position() - - call s:_switch(l:target_bufname) - for l:text_edit in s:_normalize(a:text_edits) - call s:_apply(bufnr(l:target_bufname), l:text_edit, l:cursor_position) - endfor - call s:_switch(l:current_bufname) +let s:TextEdit = vital#lsp#import('VS.LSP.TextEdit') - if bufnr(l:current_bufname) == bufnr(l:target_bufname) - call cursor(lsp#utils#position#lsp_to_vim('%', l:cursor_position)) - endif +function! lsp#utils#text_edit#apply_text_edits(uri, text_edits) abort + return s:TextEdit.apply(lsp#utils#uri_to_path(a:uri), deepcopy(a:text_edits)) endfunction + " @summary Use this to convert textedit to vim list that is compatible with " quickfix and locllist items " @param uri = DocumentUri @@ -78,141 +69,3 @@ function! s:lsp_text_edit_item_to_vim(uri, text_edit, cache) abort \ } endfunction -" -" _apply -" -function! s:_apply(bufnr, text_edit, cursor_position) abort - " create before/after line. - let l:start_line = getline(a:text_edit['range']['start']['line'] + 1) - let l:end_line = getline(a:text_edit['range']['end']['line'] + 1) - let l:before_line = strcharpart(l:start_line, 0, a:text_edit['range']['start']['character']) - let l:after_line = strcharpart(l:end_line, a:text_edit['range']['end']['character'], strchars(l:end_line) - a:text_edit['range']['end']['character']) - - " create new lines. - let l:new_lines = lsp#utils#_split_by_eol(a:text_edit['newText']) - let l:new_lines[0] = l:before_line . l:new_lines[0] - let l:new_lines[-1] = l:new_lines[-1] . l:after_line - - " save length. - let l:new_lines_len = len(l:new_lines) - let l:range_len = (a:text_edit['range']['end']['line'] - a:text_edit['range']['start']['line']) + 1 - - " fixendofline - let l:buffer_length = len(getbufline(a:bufnr, '^', '$')) - let l:should_fixendofline = lsp#utils#buffer#_get_fixendofline(a:bufnr) - let l:should_fixendofline = l:should_fixendofline && l:new_lines[-1] ==# '' - let l:should_fixendofline = l:should_fixendofline && l:buffer_length <= a:text_edit['range']['end']['line'] - let l:should_fixendofline = l:should_fixendofline && a:text_edit['range']['end']['character'] == 0 - if l:should_fixendofline - call remove(l:new_lines, -1) - endif - - " fix cursor pos - if a:text_edit['range']['end']['line'] < a:cursor_position['line'] - " fix cursor line - let a:cursor_position['line'] += l:new_lines_len - l:range_len - elseif a:text_edit['range']['end']['line'] == a:cursor_position['line'] && a:text_edit['range']['end']['character'] <= a:cursor_position['character'] - " fix cursor line and col - let a:cursor_position['line'] += l:new_lines_len - l:range_len - let l:end_character = strchars(l:new_lines[-1]) - strchars(l:after_line) - let l:end_offset = a:cursor_position['character'] - a:text_edit['range']['end']['character'] - let a:cursor_position['character'] = l:end_character + l:end_offset - endif - - " append or delete lines. - if l:new_lines_len > l:range_len - call append(a:text_edit['range']['start']['line'], repeat([''], l:new_lines_len - l:range_len)) - elseif l:new_lines_len < l:range_len - let l:offset = l:range_len - l:new_lines_len - call s:delete(a:bufnr, a:text_edit['range']['start']['line'] + 1, a:text_edit['range']['start']['line'] + l:offset) - endif - - " set lines. - call setline(a:text_edit['range']['start']['line'] + 1, l:new_lines) -endfunction - -" -" _normalize -" -function! s:_normalize(text_edits) abort - let l:text_edits = type(a:text_edits) == type([]) ? a:text_edits : [a:text_edits] - let l:text_edits = filter(copy(l:text_edits), { _, text_edit -> type(text_edit) == type({}) }) - let l:text_edits = s:_range(l:text_edits) - let l:text_edits = sort(copy(l:text_edits), function('s:_compare', [], {})) - let l:text_edits = s:_check(l:text_edits) - return reverse(l:text_edits) -endfunction - -" -" _range -" -function! s:_range(text_edits) abort - for l:text_edit in a:text_edits - if l:text_edit.range.start.line > l:text_edit.range.end.line || ( - \ l:text_edit.range.start.line == l:text_edit.range.end.line && - \ l:text_edit.range.start.character > l:text_edit.range.end.character - \ ) - let l:text_edit.range = { 'start': l:text_edit.range.end, 'end': l:text_edit.range.start } - endif - endfor - return a:text_edits -endfunction - -" -" _check -" -" LSP Spec says `multiple text edits can not overlap those ranges`. -" This function check it. But does not throw error. -" -function! s:_check(text_edits) abort - if len(a:text_edits) > 1 - let l:range = a:text_edits[0].range - for l:text_edit in a:text_edits[1 : -1] - if l:range.end.line > l:text_edit.range.start.line || ( - \ l:range.end.line == l:text_edit.range.start.line && - \ l:range.end.character > l:text_edit.range.start.character - \ ) - call lsp#log('text_edit: range overlapped.') - endif - let l:range = l:text_edit.range - endfor - endif - return a:text_edits -endfunction - -" -" _compare -" -function! s:_compare(text_edit1, text_edit2) abort - let l:diff = a:text_edit1.range.start.line - a:text_edit2.range.start.line - if l:diff == 0 - return a:text_edit1.range.start.character - a:text_edit2.range.start.character - endif - return l:diff -endfunction - -" -" _switch -" -function! s:_switch(path) abort - if bufnr(a:path) >= 0 - execute printf('keepalt keepjumps %sbuffer!', bufnr(a:path)) - else - execute printf('keepalt keepjumps edit! %s', fnameescape(a:path)) - endif -endfunction - -" -" delete -" -function! s:delete(bufnr, start, end) abort - if exists('*deletebufline') - call deletebufline(a:bufnr, a:start, a:end) - else - let l:foldenable = &foldenable - setlocal nofoldenable - execute printf('%s,%sdelete _', a:start, a:end) - let &foldenable = l:foldenable - endif -endfunction - diff --git a/autoload/vital/_lsp.vim b/autoload/vital/_lsp.vim new file mode 100644 index 000000000..55104952e --- /dev/null +++ b/autoload/vital/_lsp.vim @@ -0,0 +1,9 @@ +let s:_plugin_name = expand(':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 diff --git a/autoload/vital/_lsp/VS/LSP/Position.vim b/autoload/vital/_lsp/VS/LSP/Position.vim new file mode 100644 index 000000000..56bc84d7c --- /dev/null +++ b/autoload/vital/_lsp/VS/LSP/Position.vim @@ -0,0 +1,69 @@ +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not modify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_lsp#VS#LSP#Position#import() abort', printf("return map({'cursor': '', 'vim_to_lsp': '', 'lsp_to_vim': ''}, \"vital#_lsp#function('%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ +" +" cursor +" +function! s:cursor() abort + return s:vim_to_lsp('%', getpos('.')[1 : 3]) +endfunction + +" +" vim_to_lsp +" +function! s:vim_to_lsp(expr, pos) abort + let l:line = s:_get_buffer_line(a:expr, a:pos[0]) + if l:line is v:null + return { + \ 'line': a:pos[0] - 1, + \ 'character': a:pos[1] - 1 + \ } + endif + + return { + \ 'line': a:pos[0] - 1, + \ 'character': strchars(strpart(l:line, 0, a:pos[1] - 1)) + \ } +endfunction + +" +" lsp_to_vim +" +function! s:lsp_to_vim(expr, position) abort + let l:line = s:_get_buffer_line(a:expr, a:position.line + 1) + if l:line is v:null + return [a:position.line + 1, a:position.character + 1] + endif + return [a:position.line + 1, byteidx(l:line, a:position.character) + 1] +endfunction + +" +" _get_buffer_line +" +if exists('*bufload') + function! s:_get_buffer_line(expr, lnum) abort + if bufloaded(bufnr(a:expr)) + return get(getbufline(a:expr, a:lnum), 0, v:null) + elseif filereadable(a:expr) + call bufload(bufnr(a:expr, v:true)) + return get(getbufline(a:expr, a:lnum), 0, v:null) + endif + return v:null + endfunction +else + function! s:_get_buffer_line(expr, lnum) abort + if bufloaded(bufnr(a:expr)) + return get(getbufline(a:expr, a:lnum), 0, v:null) + elseif filereadable(a:expr) + return get(readfile(a:expr, '', a:lnum), 0, v:null) + endif + return v:null + endfunction +endif + diff --git a/autoload/vital/_lsp/VS/LSP/Text.vim b/autoload/vital/_lsp/VS/LSP/Text.vim new file mode 100644 index 000000000..f25040b9c --- /dev/null +++ b/autoload/vital/_lsp/VS/LSP/Text.vim @@ -0,0 +1,23 @@ +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not modify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_lsp#VS#LSP#Text#import() abort', printf("return map({'normalize_eol': '', 'split_by_eol': ''}, \"vital#_lsp#function('%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ +" +" normalize_eol +" +function! s:normalize_eol(text) abort + return substitute(a:text, "\r\n\\|\r", "\n", 'g') +endfunction + +" +" split_by_eol +" +function! s:split_by_eol(text) abort + return split(a:text, "\r\n\\|\r\\|\n", v:true) +endfunction + diff --git a/autoload/vital/_lsp/VS/LSP/TextEdit.vim b/autoload/vital/_lsp/VS/LSP/TextEdit.vim new file mode 100644 index 000000000..34ee52ad3 --- /dev/null +++ b/autoload/vital/_lsp/VS/LSP/TextEdit.vim @@ -0,0 +1,185 @@ +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not modify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_lsp#VS#LSP#TextEdit#import() abort', printf("return map({'_vital_depends': '', 'apply': '', '_vital_loaded': ''}, \"vital#_lsp#function('%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ +" +" _vital_loaded +" +function! s:_vital_loaded(V) abort + let s:Text = a:V.import('VS.LSP.Text') + let s:Position = a:V.import('VS.LSP.Position') + let s:Buffer = a:V.import('VS.Vim.Buffer') + let s:Option = a:V.import('VS.Vim.Option') +endfunction + +" +" _vital_depends +" +function! s:_vital_depends() abort + return ['VS.LSP.Text', 'VS.LSP.Position', 'VS.Vim.Buffer', 'VS.Vim.Option'] +endfunction + +" +" apply +" +function! s:apply(path, text_edits) abort + let l:current_bufname = bufname('%') + let l:current_position = s:Position.cursor() + + let l:target_bufnr = s:_switch(a:path) + call s:_substitute(l:target_bufnr, a:text_edits, l:current_position) + let l:current_bufnr = s:_switch(l:current_bufname) + + if l:current_bufnr == l:target_bufnr + call cursor(s:Position.lsp_to_vim('%', l:current_position)) + endif +endfunction + +" +" _substitute +" +function! s:_substitute(bufnr, text_edits, current_position) abort + try + " Save state. + let l:Restore = s:Option.define({ + \ 'foldenable': '0', + \ }) + let l:view = winsaveview() + + " Apply substitute. + let [l:fixeol, l:text_edits] = s:_normalize(a:bufnr, a:text_edits) + for l:text_edit in l:text_edits + let l:start = s:Position.lsp_to_vim(a:bufnr, l:text_edit.range.start) + let l:end = s:Position.lsp_to_vim(a:bufnr, l:text_edit.range.end) + let l:text = s:Text.normalize_eol(l:text_edit.newText) + execute printf('noautocmd keeppatterns keepjumps silent %ssubstitute/\%%%sl\%%%sc\_.\{-}\%%%sl\%%%sc/\=l:text/%se', + \ l:start[0], + \ l:start[0], + \ l:start[1], + \ l:end[0], + \ l:end[1], + \ &gdefault ? 'g' : '' + \ ) + call s:_fix_cursor_position(a:current_position, l:text_edit, s:Text.split_by_eol(l:text)) + endfor + + " Remove last empty line if fixeol enabled. + if l:fixeol && getline('$') ==# '' + noautocmd keeppatterns keepjumps silent $delete _ + endif + catch /.*/ + echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint }) + finally + " Restore state. + call l:Restore() + call winrestview(l:view) + endtry +endfunction + +" +" _fix_cursor_position +" +function! s:_fix_cursor_position(position, text_edit, lines) abort + let l:lines_len = len(a:lines) + let l:range_len = (a:text_edit.range.end.line - a:text_edit.range.start.line) + 1 + + if a:text_edit.range.end.line < a:position.line + let a:position.line += l:lines_len - l:range_len + elseif a:text_edit.range.end.line == a:position.line && a:text_edit.range.end.character <= a:position.character + let a:position.line += l:lines_len - l:range_len + let a:position.character = strchars(a:lines[-1]) + (a:position.character - a:text_edit.range.end.character) + if l:lines_len == 1 + let a:position.character += a:text_edit.range.start.character + endif + endif +endfunction + +" +" _normalize +" +function! s:_normalize(bufnr, text_edits) abort + let l:text_edits = type(a:text_edits) == type([]) ? a:text_edits : [a:text_edits] + let l:text_edits = s:_range(l:text_edits) + let l:text_edits = sort(l:text_edits, function('s:_compare')) + let l:text_edits = reverse(l:text_edits) + return s:_fix_text_edits(a:bufnr, l:text_edits) +endfunction + +" +" _range +" +function! s:_range(text_edits) abort + let l:text_edits = [] + for l:text_edit in a:text_edits + if type(l:text_edit) != type({}) + continue + endif + if l:text_edit.range.start.line > l:text_edit.range.end.line || ( + \ l:text_edit.range.start.line == l:text_edit.range.end.line && + \ l:text_edit.range.start.character > l:text_edit.range.end.character + \ ) + let l:text_edit.range = { 'start': l:text_edit.range.end, 'end': l:text_edit.range.start } + endif + let l:text_edits += [l:text_edit] + endfor + return l:text_edits +endfunction + +" +" _compare +" +function! s:_compare(text_edit1, text_edit2) abort + let l:diff = a:text_edit1.range.start.line - a:text_edit2.range.start.line + if l:diff == 0 + return a:text_edit1.range.start.character - a:text_edit2.range.start.character + endif + return l:diff +endfunction + +" +" _fix_text_edits +" +function! s:_fix_text_edits(bufnr, text_edits) abort + let l:max = s:Buffer.get_line_count(a:bufnr) + + let l:fixeol = v:false + let l:text_edits = [] + for l:text_edit in a:text_edits + if l:max <= l:text_edit.range.start.line + let l:text_edit.range.start.line = l:max - 1 + let l:text_edit.range.start.character = strchars(get(getbufline(a:bufnr, '$'), 0, '')) + let l:text_edit.newText = "\n" . l:text_edit.newText + let l:fixeol = &fixendofline && !&binary + endif + if l:max <= l:text_edit.range.end.line + let l:text_edit.range.end.line = l:max - 1 + let l:text_edit.range.end.character = strchars(get(getbufline(a:bufnr, '$'), 0, '')) + let l:fixeol = &fixendofline && !&binary + endif + call add(l:text_edits, l:text_edit) + endfor + + return [l:fixeol, l:text_edits] +endfunction + +" +" _switch +" +function! s:_switch(path) abort + let l:curr = bufnr('%') + let l:next = bufnr(a:path) + if l:next >= 0 + if l:curr != l:next + execute printf('noautocmd keepalt keepjumps %sbuffer!', bufnr(a:path)) + endif + else + execute printf('noautocmd keepalt keepjumps edit! %s', fnameescape(a:path)) + endif + return bufnr('%') +endfunction + diff --git a/autoload/vital/_lsp/VS/Vim/Buffer.vim b/autoload/vital/_lsp/VS/Vim/Buffer.vim new file mode 100644 index 000000000..06a88a136 --- /dev/null +++ b/autoload/vital/_lsp/VS/Vim/Buffer.vim @@ -0,0 +1,29 @@ +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not modify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_lsp#VS#Vim#Buffer#import() abort', printf("return map({'get_line_count': ''}, \"vital#_lsp#function('%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ +" +" get_line_count +" +if exists('*nvim_buf_line_count') + function! s:get_line_count(bufnr) abort + return nvim_buf_line_count(a:bufnr) + endfunction +elseif has('patch-8.2.0019') + function! s:get_line_count(bufnr) abort + return getbufinfo(a:bufnr)[0].linecount + endfunction +else + function! s:get_line_count(bufnr) abort + if bufnr('%') == bufnr(a:bufnr) + return line('$') + endif + return len(getbufline(a:bufnr, '^', '$')) + endfunction +endif + diff --git a/autoload/vital/_lsp/VS/Vim/Buffer/TextMark.vim b/autoload/vital/_lsp/VS/Vim/Buffer/TextMark.vim new file mode 100644 index 000000000..ba6a5f539 --- /dev/null +++ b/autoload/vital/_lsp/VS/Vim/Buffer/TextMark.vim @@ -0,0 +1,274 @@ +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not modify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_lsp#VS#Vim#Buffer#TextMark#import() abort', printf("return map({'clear': '', 'set_base_mark_id': '', 'set': '', 'is_available': '', 'get': ''}, \"vital#_lsp#function('%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ +let s:namespace = {} +let s:mark_id = 50000 +let s:prop_types = {} +let s:prop_cache = {} " { ['ns'] => [...ids] } + +" +" is_available +" +function! s:is_available() abort + if has('nvim') + return exists('*nvim_buf_set_text') + else + return exists('*prop_type_add') && exists('*prop_add') && exists('*prop_find') && exists('*prop_list') + endif +endfunction + +" +" set_base_mark_id +" +function! s:set_base_mark_id(base_id) abort + if s:mark_id != 50000 + throw 'VS.Vim.Buffer.TextMark: can only be called at initialization.' + endif + let s:mark_id = a:base_id +endfunction + +" +" @param {number} bufnr +" @param {string} ns +" @param {array} marks +" @param {[number, number]} marks[number].start_pos +" @param {[number, number]} marks[number].end_pos +" @param {string?} marks[number].highlight +" +function! s:set(bufnr, ns, marks) abort + call s:_set(bufnr(a:bufnr), a:ns, a:marks) +endfunction + +" +" get +" +" @param {number} bufnr +" @param {string} ns +" @param {[number, number]?} pos +" @returns {array} +" +function! s:get(bufnr, ns, ...) abort + let l:pos = get(a:000, 0, []) + return s:_get(bufnr(a:bufnr), a:ns, l:pos) +endfunction + +" +" clear +" +" @param {number} bufnr +" @param {string} ns +" +function! s:clear(bufnr, ns) abort + return s:_clear(bufnr(a:bufnr), a:ns) +endfunction + +if has('nvim') + " + " set + " + function! s:_set(bufnr, ns, marks) abort + if !has_key(s:namespace, a:ns) + let s:namespace[a:ns] = nvim_create_namespace(a:ns) + endif + for l:mark in a:marks + let s:mark_id += 1 + + let l:opts = { + \ 'id': s:mark_id, + \ 'end_line': l:mark.end_pos[0] - 1, + \ 'end_col': l:mark.end_pos[1] - 1, + \ } + if has_key(l:mark, 'highlight') + let l:opts.hl_group = l:mark.highlight + endif + call nvim_buf_set_extmark( + \ a:bufnr, + \ s:namespace[a:ns], + \ l:mark.start_pos[0] - 1, + \ l:mark.start_pos[1] - 1, + \ l:opts + \ ) + endfor + endfunction + + " + " get + " + function! s:_get(bufnr, ns, pos) abort + if !has_key(s:namespace, a:ns) + return [] + endif + let l:extmarks = nvim_buf_get_extmarks(a:bufnr, s:namespace[a:ns], 0, -1, { 'details': v:true }) + if !empty(a:pos) + let l:marks = [] + for l:extmark in l:extmarks " TODO: efficiency. + let l:mark = s:_to_mark(l:extmark) + let l:contains = v:true + let l:contains = l:contains && l:mark.start_pos[0] < a:pos[0] || (l:mark.start_pos[0] == a:pos[0] && l:mark.start_pos[1] <= a:pos[1]) + let l:contains = l:contains && l:mark.end_pos[0] > a:pos[0] || (l:mark.end_pos[0] == a:pos[0] && l:mark.end_pos[1] >= a:pos[1]) + if !l:contains + continue + endif + let l:marks += [l:mark] + endfor + return l:marks + else + return map(l:extmarks, 's:_to_mark(v:val)') + endif + endfunction + + " + " clear + " + function! s:_clear(bufnr, ns) abort + if !has_key(s:namespace, a:ns) + return + endif + call nvim_buf_clear_namespace(a:bufnr, s:namespace[a:ns], 0, -1) + endfunction + + " + " to_mark + " + function! s:_to_mark(extmark) abort + let l:mark = {} + let l:mark.id = a:extmark[0] + let l:mark.start_pos = [a:extmark[1] + 1, a:extmark[2] + 1] + let l:mark.end_pos = [a:extmark[3].end_row + 1, a:extmark[3].end_col + 1] + if has_key(a:extmark[3], 'hl_group') + let l:mark.highlight = a:extmark[3].hl_group + endif + + " swap ranges if needed. + if l:mark.start_pos[0] > l:mark.end_pos[0] || (l:mark.start_pos[0] == l:mark.end_pos[0] && l:mark.start_pos[1] > l:mark.end_pos[1]) + let l:start_pos = l:mark.start_pos + let l:mark.start_pos = l:mark.end_pos + let l:mark.end_pos = l:start_pos + endif + + return l:mark + endfunction +else + " + " set + " + function! s:_set(bufnr, ns, marks) abort + " preare namespace. + let l:cache = s:_ensure_cache(a:bufnr, a:ns) + for l:mark in a:marks + let s:mark_id += 1 + call add(l:cache.ids, s:mark_id) + call prop_add(l:mark.start_pos[0], l:mark.start_pos[1], { + \ 'id': s:mark_id, + \ 'bufnr': a:bufnr, + \ 'end_lnum': l:mark.end_pos[0], + \ 'end_col': l:mark.end_pos[1], + \ 'type': s:_ensure_type(l:mark) + \ }) + endfor + endfunction + + " + " get + " + function! s:_get(bufnr, ns, pos) abort + let l:marks = [] + for l:prop_id in s:_ensure_cache(a:bufnr, a:ns).ids + let l:prop = prop_find({ 'id': l:prop_id, 'lnum': 1, 'col': 1, }) + if empty(l:prop) + continue + endif + + let l:start_lnum = l:prop.lnum + let l:start_col = l:prop.col + if l:prop.end + let l:end_lnum = l:start_lnum + let l:end_col = l:start_col + l:prop.length + else + let l:i = 1 + while 1 + let l:ends = filter(prop_list(l:start_lnum + l:i, { 'bufnr': a:bufnr, 'id': l:prop_id }), 'v:val.id == l:prop_id') " it seems vim's bug + if empty(l:ends) + let l:i += 1 + continue + endif + let l:end = l:ends[0] + if !l:end.end + let l:i += 1 + continue + endif + let l:end_lnum = l:start_lnum + l:i + let l:end_col = l:end.col + l:end.length + break + endwhile + endif + + " position check if specified. + if !empty(a:pos) + let l:contains = v:true + let l:contains = l:contains && l:start_lnum < a:pos[0] || (l:start_lnum == a:pos[0] && l:start_col <= a:pos[1]) + let l:contains = l:contains && l:end_lnum > a:pos[0] || (l:end_lnum == a:pos[0] && l:end_col >= a:pos[1]) + if !l:contains + continue + endif + endif + + let l:mark = {} + let l:mark.id = l:prop.id + let l:mark.start_pos = [l:start_lnum, l:start_col] + let l:mark.end_pos = [l:end_lnum, l:end_col] + if has_key(s:prop_types[l:prop.type], 'highlight') + let l:mark.highlight = s:prop_types[l:prop.type].highlight + endif + call add(l:marks, l:mark) + endfor + return l:marks + endfunction + + " + " clear + " + function! s:_clear(bufnr, ns) abort + let l:cache = s:_ensure_cache(a:bufnr, a:ns) + for l:prop_id in l:cache.ids + call prop_remove({ 'bufnr': a:bufnr, 'id': l:prop_id }) + endfor + let l:cache.ids = [] + endfunction + + " + " ensure_type + " + function! s:_ensure_type(mark) abort + let l:type = printf('VS.Vim.Buffer.TextMark: %s', get(a:mark, 'highlight', '')) + if !has_key(s:prop_types, l:type) + let s:prop_types[l:type] = { + \ 'start_incl': v:true, + \ 'end_incl': v:true, + \ } + if has_key(a:mark, 'highlight') + let s:prop_types[l:type].highlight = a:mark.highlight + endif + call prop_type_add(l:type, s:prop_types[l:type]) + endif + return l:type + endfunction + + " + " ensure_cache + " + function! s:_ensure_cache(bufnr, ns) abort + let l:key = printf('VS.Vim.Buffer.TextMark: %s: %s', a:bufnr, a:ns) + if !has_key(s:prop_cache, l:key) + let s:prop_cache[l:key] = { 'ids': [] } + endif + return s:prop_cache[l:key] + endfunction +endif + diff --git a/autoload/vital/_lsp/VS/Vim/Option.vim b/autoload/vital/_lsp/VS/Vim/Option.vim new file mode 100644 index 000000000..b2d89cee9 --- /dev/null +++ b/autoload/vital/_lsp/VS/Vim/Option.vim @@ -0,0 +1,21 @@ +" ___vital___ +" NOTE: lines between '" ___vital___' is generated by :Vitalize. +" Do not modify the code nor insert new lines before '" ___vital___' +function! s:_SID() abort + return matchstr(expand(''), '\zs\d\+\ze__SID$') +endfunction +execute join(['function! vital#_lsp#VS#Vim#Option#import() abort', printf("return map({'define': ''}, \"vital#_lsp#function('%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") +delfunction s:_SID +" ___vital___ +" +" define +" +function! s:define(map) abort + let l:old = {} + for [l:key, l:value] in items(a:map) + let l:old[l:key] = eval(printf('&%s', l:key)) + execute printf('let &%s = "%s"', l:key, l:value) + endfor + return { -> s:define(l:old) } +endfunction + diff --git a/autoload/vital/lsp.vim b/autoload/vital/lsp.vim new file mode 100644 index 000000000..6730f4e0a --- /dev/null +++ b/autoload/vital/lsp.vim @@ -0,0 +1,330 @@ +let s:plugin_name = expand(':t:r') +let s:vital_base_dir = expand(':h') +let s:project_root = expand(':h:h:h') +let s:is_vital_vim = s:plugin_name is# 'vital' + +let s:loaded = {} +let s:cache_sid = {} + +function! vital#{s:plugin_name}#new() abort + return s:new(s:plugin_name) +endfunction + +function! vital#{s:plugin_name}#import(...) abort + if !exists('s:V') + let s:V = s:new(s:plugin_name) + endif + return call(s:V.import, a:000, s:V) +endfunction + +let s:Vital = {} + +function! s:new(plugin_name) abort + let base = deepcopy(s:Vital) + let base._plugin_name = a:plugin_name + return base +endfunction + +function! s:vital_files() abort + if !exists('s:vital_files') + let s:vital_files = map( + \ s:is_vital_vim ? s:_global_vital_files() : s:_self_vital_files(), + \ 'fnamemodify(v:val, ":p:gs?[\\\\/]?/?")') + endif + return copy(s:vital_files) +endfunction +let s:Vital.vital_files = function('s:vital_files') + +function! s:import(name, ...) abort dict + let target = {} + let functions = [] + for a in a:000 + if type(a) == type({}) + let target = a + elseif type(a) == type([]) + let functions = a + endif + unlet a + endfor + let module = self._import(a:name) + if empty(functions) + call extend(target, module, 'keep') + else + for f in functions + if has_key(module, f) && !has_key(target, f) + let target[f] = module[f] + endif + endfor + endif + return target +endfunction +let s:Vital.import = function('s:import') + +function! s:load(...) abort dict + for arg in a:000 + let [name; as] = type(arg) == type([]) ? arg[: 1] : [arg, arg] + let target = split(join(as, ''), '\W\+') + let dict = self + let dict_type = type({}) + while !empty(target) + let ns = remove(target, 0) + if !has_key(dict, ns) + let dict[ns] = {} + endif + if type(dict[ns]) == dict_type + let dict = dict[ns] + else + unlet dict + break + endif + endwhile + if exists('dict') + call extend(dict, self._import(name)) + endif + unlet arg + endfor + return self +endfunction +let s:Vital.load = function('s:load') + +function! s:unload() abort dict + let s:loaded = {} + let s:cache_sid = {} + unlet! s:vital_files +endfunction +let s:Vital.unload = function('s:unload') + +function! s:exists(name) abort dict + if a:name !~# '\v^\u\w*%(\.\u\w*)*$' + throw 'vital: Invalid module name: ' . a:name + endif + return s:_module_path(a:name) isnot# '' +endfunction +let s:Vital.exists = function('s:exists') + +function! s:search(pattern) abort dict + let paths = s:_extract_files(a:pattern, self.vital_files()) + let modules = sort(map(paths, 's:_file2module(v:val)')) + return uniq(modules) +endfunction +let s:Vital.search = function('s:search') + +function! s:plugin_name() abort dict + return self._plugin_name +endfunction +let s:Vital.plugin_name = function('s:plugin_name') + +function! s:_self_vital_files() abort + let builtin = printf('%s/__%s__/', s:vital_base_dir, s:plugin_name) + let installed = printf('%s/_%s/', s:vital_base_dir, s:plugin_name) + let base = builtin . ',' . installed + return split(globpath(base, '**/*.vim', 1), "\n") +endfunction + +function! s:_global_vital_files() abort + let pattern = 'autoload/vital/__*__/**/*.vim' + return split(globpath(&runtimepath, pattern, 1), "\n") +endfunction + +function! s:_extract_files(pattern, files) abort + let tr = {'.': '/', '*': '[^/]*', '**': '.*'} + let target = substitute(a:pattern, '\.\|\*\*\?', '\=tr[submatch(0)]', 'g') + let regexp = printf('autoload/vital/[^/]\+/%s.vim$', target) + return filter(a:files, 'v:val =~# regexp') +endfunction + +function! s:_file2module(file) abort + let filename = fnamemodify(a:file, ':p:gs?[\\/]?/?') + let tail = matchstr(filename, 'autoload/vital/_\w\+/\zs.*\ze\.vim$') + return join(split(tail, '[\\/]\+'), '.') +endfunction + +" @param {string} name e.g. Data.List +function! s:_import(name) abort dict + if has_key(s:loaded, a:name) + return copy(s:loaded[a:name]) + endif + let module = self._get_module(a:name) + if has_key(module, '_vital_created') + call module._vital_created(module) + endif + let export_module = filter(copy(module), 'v:key =~# "^\\a"') + " Cache module before calling module._vital_loaded() to avoid cyclic + " dependences but remove the cache if module._vital_loaded() fails. + " let s:loaded[a:name] = export_module + let s:loaded[a:name] = export_module + if has_key(module, '_vital_loaded') + try + call module._vital_loaded(vital#{s:plugin_name}#new()) + catch + unlet s:loaded[a:name] + throw 'vital: fail to call ._vital_loaded(): ' . v:exception . " from:\n" . s:_format_throwpoint(v:throwpoint) + endtry + endif + return copy(s:loaded[a:name]) +endfunction +let s:Vital._import = function('s:_import') + +function! s:_format_throwpoint(throwpoint) abort + let funcs = [] + let stack = matchstr(a:throwpoint, '^function \zs.*, .\{-} \d\+$') + for line in split(stack, '\.\.') + let m = matchlist(line, '^\(.\+\)\%(\[\(\d\+\)\]\|, .\{-} \(\d\+\)\)$') + if !empty(m) + let [name, lnum, lnum2] = m[1:3] + if empty(lnum) + let lnum = lnum2 + endif + let info = s:_get_func_info(name) + if !empty(info) + let attrs = empty(info.attrs) ? '' : join([''] + info.attrs) + let flnum = info.lnum == 0 ? '' : printf(' Line:%d', info.lnum + lnum) + call add(funcs, printf('function %s(...)%s Line:%d (%s%s)', + \ info.funcname, attrs, lnum, info.filename, flnum)) + continue + endif + endif + " fallback when function information cannot be detected + call add(funcs, line) + endfor + return join(funcs, "\n") +endfunction + +function! s:_get_func_info(name) abort + let name = a:name + if a:name =~# '^\d\+$' " is anonymous-function + let name = printf('{%s}', a:name) + elseif a:name =~# '^\d\+$' " is lambda-function + let name = printf("{'%s'}", a:name) + endif + if !exists('*' . name) + return {} + endif + let body = execute(printf('verbose function %s', name)) + let lines = split(body, "\n") + let signature = matchstr(lines[0], '^\s*\zs.*') + let [_, file, lnum; __] = matchlist(lines[1], + \ '^\t\%(Last set from\|.\{-}:\)\s*\zs\(.\{-}\)\%( \S\+ \(\d\+\)\)\?$') + return { + \ 'filename': substitute(file, '[/\\]\+', '/', 'g'), + \ 'lnum': 0 + lnum, + \ 'funcname': a:name, + \ 'arguments': split(matchstr(signature, '(\zs.*\ze)'), '\s*,\s*'), + \ 'attrs': filter(['dict', 'abort', 'range', 'closure'], 'signature =~# (").*" . v:val)'), + \ } +endfunction + +" s:_get_module() returns module object wihch has all script local functions. +function! s:_get_module(name) abort dict + let funcname = s:_import_func_name(self.plugin_name(), a:name) + try + return call(funcname, []) + catch /^Vim\%((\a\+)\)\?:E117:/ + return s:_get_builtin_module(a:name) + endtry +endfunction + +function! s:_get_builtin_module(name) abort + return s:sid2sfuncs(s:_module_sid(a:name)) +endfunction + +if s:is_vital_vim + " For vital.vim, we can use s:_get_builtin_module directly + let s:Vital._get_module = function('s:_get_builtin_module') +else + let s:Vital._get_module = function('s:_get_module') +endif + +function! s:_import_func_name(plugin_name, module_name) abort + return printf('vital#_%s#%s#import', a:plugin_name, s:_dot_to_sharp(a:module_name)) +endfunction + +function! s:_module_sid(name) abort + let path = s:_module_path(a:name) + if !filereadable(path) + throw 'vital: module not found: ' . a:name + endif + let vital_dir = s:is_vital_vim ? '__\w\+__' : printf('_\{1,2}%s\%%(__\)\?', s:plugin_name) + let base = join([vital_dir, ''], '[/\\]\+') + let p = base . substitute('' . a:name, '\.', '[/\\\\]\\+', 'g') + let sid = s:_sid(path, p) + if !sid + call s:_source(path) + let sid = s:_sid(path, p) + if !sid + throw printf('vital: cannot get from path: %s', path) + endif + endif + return sid +endfunction + +function! s:_module_path(name) abort + return get(s:_extract_files(a:name, s:vital_files()), 0, '') +endfunction + +function! s:_module_sid_base_dir() abort + return s:is_vital_vim ? &rtp : s:project_root +endfunction + +function! s:_dot_to_sharp(name) abort + return substitute(a:name, '\.', '#', 'g') +endfunction + +function! s:_source(path) abort + execute 'source' fnameescape(a:path) +endfunction + +" @vimlint(EVL102, 1, l:_) +" @vimlint(EVL102, 1, l:__) +function! s:_sid(path, filter_pattern) abort + let unified_path = s:_unify_path(a:path) + if has_key(s:cache_sid, unified_path) + return s:cache_sid[unified_path] + endif + for line in filter(split(execute(':scriptnames'), "\n"), 'v:val =~# a:filter_pattern') + let [_, sid, path; __] = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$') + if s:_unify_path(path) is# unified_path + let s:cache_sid[unified_path] = sid + return s:cache_sid[unified_path] + endif + endfor + return 0 +endfunction + +if filereadable(expand(':r') . '.VIM') " is case-insensitive or not + let s:_unify_path_cache = {} + " resolve() is slow, so we cache results. + " Note: On windows, vim can't expand path names from 8.3 formats. + " So if getting full path via and $HOME was set as 8.3 format, + " vital load duplicated scripts. Below's :~ avoid this issue. + function! s:_unify_path(path) abort + if has_key(s:_unify_path_cache, a:path) + return s:_unify_path_cache[a:path] + endif + let value = tolower(fnamemodify(resolve(fnamemodify( + \ a:path, ':p')), ':~:gs?[\\/]?/?')) + let s:_unify_path_cache[a:path] = value + return value + endfunction +else + function! s:_unify_path(path) abort + return resolve(fnamemodify(a:path, ':p:gs?[\\/]?/?')) + endfunction +endif + +" copied and modified from Vim.ScriptLocal +let s:SNR = join(map(range(len("\")), '"[\\x" . printf("%0x", char2nr("\"[v:val])) . "]"'), '') +function! s:sid2sfuncs(sid) abort + let fs = split(execute(printf(':function /^%s%s_', s:SNR, a:sid)), "\n") + let r = {} + let pattern = printf('\m^function\s%d_\zs\w\{-}\ze(', a:sid) + for fname in map(fs, 'matchstr(v:val, pattern)') + let r[fname] = function(s:_sfuncname(a:sid, fname)) + endfor + return r +endfunction + +"" Return funcname of script local functions with SID +function! s:_sfuncname(sid, funcname) abort + return printf('%s_%s', a:sid, a:funcname) +endfunction diff --git a/autoload/vital/lsp.vital b/autoload/vital/lsp.vital new file mode 100644 index 000000000..c237d416b --- /dev/null +++ b/autoload/vital/lsp.vital @@ -0,0 +1,5 @@ +lsp +621cda4f832d2f43f3b15cdbfb70ca36b219efc2 + +VS.LSP.TextEdit +VS.Vim.Buffer.TextMark diff --git a/doc/vim-lsp.txt b/doc/vim-lsp.txt index 48cd986bf..aa5621c4d 100644 --- a/doc/vim-lsp.txt +++ b/doc/vim-lsp.txt @@ -50,6 +50,7 @@ CONTENTS *vim-lsp-contents* |g:lsp_diagnostics_virtual_text_delay| g:lsp_diagnostics_virtual_text_prefix |g:lsp_diagnostics_virtual_text_prefix| + g:lsp_linked_editing_range_enabled |g:lsp_linked_editing_range_enabled| g:lsp_format_sync_timeout |g:lsp_format_sync_timeout| g:lsp_use_event_queue |g:lsp_use_event_queue| g:lsp_document_highlight_enabled |g:lsp_document_highlight_enabled| @@ -680,6 +681,28 @@ g:lsp_diagnostics_virtual_text_prefix *g:lsp_diagnostics_virtual_text_prefix* let g:lsp_diagnostics_virtual_text_prefix = "> " let g:lsp_diagnostics_virtual_text_prefix = " ‣ " +g:lsp_linked_editing_enabled *g:lsp_linked_editing_enabled* + Type: |Number| + Default: `1` + + Enable textDocument/linkedEditingRange feature. + + NOTE: If you using `vim-matchup`, recommend to set `g:matchup_matchparen_deferred = 1` + + Example: > + let g:lsp_linked_editing_enabled = 1 + let g:lsp_linked_editing_enabled = 0 + + " The below mappings enable to support `d` or `c` commands. + nnoremap (vimrc-d) d + xnoremap (vimrc-d) d + nnoremap (vimrc-c) c + xnoremap (vimrc-c) c + nmap d (lsp-linked-editing-range-prepare)(vimrc-d) + xmap d (lsp-linked-editing-range-prepare)(vimrc-d) + nmap c (lsp-linked-editing-range-prepare)(vimrc-c) + xmap c (lsp-linked-editing-range-prepare)(vimrc-c) + g:lsp_use_event_queue *g:lsp_use_event_queue* Type: |Number| Default: `1` for neovim or vim with patch-8.1.0889 diff --git a/plugin/lsp.vim b/plugin/lsp.vim index c768a0ba4..5d9849bc6 100644 --- a/plugin/lsp.vim +++ b/plugin/lsp.vim @@ -37,6 +37,8 @@ let g:lsp_diagnostics_virtual_text_insert_mode_enabled = get(g:, 'lsp_diagnostic let g:lsp_diagnostics_virtual_text_delay = get(g:, 'lsp_diagnostics_virtual_text_delay', 500) let g:lsp_diagnostics_virtual_text_prefix = get(g:, 'lsp_diagnostics_virtual_text_prefix', '') +let g:lsp_linked_editing_range_enabled = get(g:, 'lsp_linked_editing_range_enabled', v:true) + 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) @@ -165,3 +167,6 @@ nnoremap (lsp-status) :echo lsp#get_server_status() nnoremap (lsp-next-reference) :call lsp#internal#document_highlight#jump(+1) nnoremap (lsp-previous-reference) :call lsp#internal#document_highlight#jump(-1) nnoremap (lsp-signature-help) :call lsp#ui#vim#signature_help#get_signature_help_under_cursor() +nnoremap (lsp-linked-editing-range-prepare) lsp#internal#linked_editing_range#prepare() +xnoremap (lsp-linked-editing-range-prepare) lsp#internal#linked_editing_range#prepare() + diff --git a/test/lsp/utils/range.vimspec b/test/lsp/utils/range.vimspec index af810247c..ca25834c8 100644 --- a/test/lsp/utils/range.vimspec +++ b/test/lsp/utils/range.vimspec @@ -57,5 +57,95 @@ Describe lsp#utils#range End + Describe lsp#utils#range#_contains + + It should return the range contains specified position + " not contains. + Assert Equals(lsp#utils#range#_contains({ + \ 'start': { + \ 'line': 1, + \ 'character': 1, + \ }, + \ 'end': { + \ 'line': 1, + \ 'character': 4, + \ }, + \ }, { + \ 'line': 1, + \ 'character': 0, + \ }), v:false) + Assert Equals(lsp#utils#range#_contains({ + \ 'start': { + \ 'line': 1, + \ 'character': 1, + \ }, + \ 'end': { + \ 'line': 1, + \ 'character': 4, + \ }, + \ }, { + \ 'line': 1, + \ 'character': 5, + \ }), v:false) + + " contains. + Assert Equals(lsp#utils#range#_contains({ + \ 'start': { + \ 'line': 1, + \ 'character': 1, + \ }, + \ 'end': { + \ 'line': 1, + \ 'character': 4, + \ }, + \ }, { + \ 'line': 1, + \ 'character': 1, + \ }), v:true) + Assert Equals(lsp#utils#range#_contains({ + \ 'start': { + \ 'line': 1, + \ 'character': 1, + \ }, + \ 'end': { + \ 'line': 1, + \ 'character': 4, + \ }, + \ }, { + \ 'line': 1, + \ 'character': 4, + \ }), v:true) + End + + End + + Describe lsp#utils#range#_get_text + + It should return range of text + call setline(1, ['あいうえお', 'かきくけこ', 'さしすせそ']) + Assert Equals(lsp#utils#range#_get_text('%', { + \ 'start': { + \ 'line': 1, + \ 'character': 1, + \ }, + \ 'end': { + \ 'line': 1, + \ 'character': 4, + \ }, + \ }), 'きくけ') + Assert Equals(lsp#utils#range#_get_text('%', { + \ 'start': { + \ 'line': 0, + \ 'character': 4, + \ }, + \ 'end': { + \ 'line': 2, + \ 'character': 1, + \ }, + \ }), "お\nかきくけこ\nさ") + End + + End + End