Quietly add go to definition support for tsserver
This commit is contained in:
parent
5a3a695586
commit
7ea3aba5e5
82
autoload/ale/definition.vim
Normal file
82
autoload/ale/definition.vim
Normal file
@ -0,0 +1,82 @@
|
||||
" Author: w0rp <devw0rp@gmail.com>
|
||||
" Description: Go to definition support for LSP linters.
|
||||
|
||||
let s:go_to_definition_map = {}
|
||||
|
||||
" Used to get the definition map in tests.
|
||||
function! ale#definition#GetMap() abort
|
||||
return deepcopy(s:go_to_definition_map)
|
||||
endfunction
|
||||
|
||||
" Used to set the definition map in tests.
|
||||
function! ale#definition#SetMap(map) abort
|
||||
let s:go_to_definition_map = a:map
|
||||
endfunction
|
||||
|
||||
" This function is used so we can check the execution of commands without
|
||||
" running them.
|
||||
function! ale#definition#Execute(expr) abort
|
||||
execute a:expr
|
||||
endfunction
|
||||
|
||||
function! ale#definition#Open(options, filename, line, column) abort
|
||||
if a:options.open_in_tab
|
||||
call ale#definition#Execute('tabedit ' . fnameescape(a:filename))
|
||||
else
|
||||
call ale#definition#Execute('edit ' . fnameescape(a:filename))
|
||||
endif
|
||||
|
||||
call cursor(a:line, a:column)
|
||||
endfunction
|
||||
|
||||
function! ale#definition#HandleTSServerResponse(conn_id, response) abort
|
||||
if get(a:response, 'command', '') is# 'definition'
|
||||
\&& has_key(s:go_to_definition_map, a:response.request_seq)
|
||||
let l:options = remove(s:go_to_definition_map, a:response.request_seq)
|
||||
|
||||
if get(a:response, 'success', v:false) is v:true
|
||||
let l:filename = a:response.body[0].file
|
||||
let l:line = a:response.body[0].start.line
|
||||
let l:column = a:response.body[0].start.offset
|
||||
|
||||
call ale#definition#Open(l:options, l:filename, l:line, l:column)
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:GoToLSPDefinition(linter, options) abort
|
||||
let l:buffer = bufnr('')
|
||||
let [l:line, l:column] = getcurpos()[1:2]
|
||||
|
||||
let l:lsp_details = ale#linter#StartLSP(
|
||||
\ l:buffer,
|
||||
\ a:linter,
|
||||
\ function('ale#definition#HandleTSServerResponse'),
|
||||
\)
|
||||
|
||||
if empty(l:lsp_details)
|
||||
return 0
|
||||
endif
|
||||
|
||||
let l:id = l:lsp_details.connection_id
|
||||
let l:root = l:lsp_details.project_root
|
||||
|
||||
let l:message = ale#lsp#tsserver_message#Definition(
|
||||
\ l:buffer,
|
||||
\ l:line,
|
||||
\ l:column
|
||||
\)
|
||||
let l:request_id = ale#lsp#Send(l:id, l:message, l:root)
|
||||
|
||||
let s:go_to_definition_map[l:request_id] = {
|
||||
\ 'open_in_tab': get(a:options, 'open_in_tab', 0),
|
||||
\}
|
||||
endfunction
|
||||
|
||||
function! ale#definition#GoTo(options) abort
|
||||
for l:linter in ale#linter#Get(&filetype)
|
||||
if l:linter.lsp is# 'tsserver'
|
||||
call s:GoToLSPDefinition(l:linter, a:options)
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
@ -53,3 +53,11 @@ function! ale#lsp#tsserver_message#CompletionEntryDetails(buffer, line, column,
|
||||
\ 'entryNames': a:entry_names,
|
||||
\}]
|
||||
endfunction
|
||||
|
||||
function! ale#lsp#tsserver_message#Definition(buffer, line, column) abort
|
||||
return [0, 'ts@definition', {
|
||||
\ 'line': a:line,
|
||||
\ 'offset': a:column,
|
||||
\ 'file': expand('#' . a:buffer . ':p'),
|
||||
\}]
|
||||
endfunction
|
||||
|
@ -247,6 +247,10 @@ command! -bar ALEFix :call ale#fix#Fix()
|
||||
" Suggest registered functions to use for fixing problems.
|
||||
command! -bar ALEFixSuggest :call ale#fix#registry#Suggest(&filetype)
|
||||
|
||||
" Go to definition for tsserver and LSP
|
||||
command! -bar ALEGoToDefinition :call ale#definition#GoTo({})
|
||||
command! -bar ALEGoToDefinitionInTab :call ale#definition#GoTo({'open_in_tab': 1})
|
||||
|
||||
" <Plug> mappings for commands
|
||||
nnoremap <silent> <Plug>(ale_previous) :ALEPrevious<Return>
|
||||
nnoremap <silent> <Plug>(ale_previous_wrap) :ALEPreviousWrap<Return>
|
||||
@ -265,6 +269,8 @@ nnoremap <silent> <Plug>(ale_reset_buffer) :ALEResetBuffer<Return>
|
||||
nnoremap <silent> <Plug>(ale_lint) :ALELint<Return>
|
||||
nnoremap <silent> <Plug>(ale_detail) :ALEDetail<Return>
|
||||
nnoremap <silent> <Plug>(ale_fix) :ALEFix<Return>
|
||||
nnoremap <silent> <Plug>(ale_go_to_definition) :ALEGoToDefinition<Return>
|
||||
nnoremap <silent> <Plug>(ale_go_to_definition_in_tab) :ALEGoToDefinitionInTab<Return>
|
||||
|
||||
" Set up autocmd groups now.
|
||||
call ale#toggle#InitAuGroups()
|
||||
|
@ -177,3 +177,16 @@ Execute(ale#lsp#tsserver_message#CompletionEntryDetails() should return correct
|
||||
\ }
|
||||
\ ],
|
||||
\ ale#lsp#tsserver_message#CompletionEntryDetails(bufnr(''), 347, 12, ['foo', 'bar'])
|
||||
|
||||
Execute(ale#lsp#tsserver_message#Definition() should return correct messages):
|
||||
AssertEqual
|
||||
\ [
|
||||
\ 0,
|
||||
\ 'ts@definition',
|
||||
\ {
|
||||
\ 'file': ale#path#Winify(g:dir . '/foo/bar.ts'),
|
||||
\ 'line': 347,
|
||||
\ 'offset': 12,
|
||||
\ }
|
||||
\ ],
|
||||
\ ale#lsp#tsserver_message#Definition(bufnr(''), 347, 12)
|
||||
|
138
test/test_go_to_definition.vader
Normal file
138
test/test_go_to_definition.vader
Normal file
@ -0,0 +1,138 @@
|
||||
Before:
|
||||
call ale#test#SetDirectory('/testplugin/test')
|
||||
call ale#test#SetFilename('dummy.txt')
|
||||
|
||||
let g:old_filename = expand('%:p')
|
||||
let g:Callback = 0
|
||||
let g:message = []
|
||||
let g:expr_list = []
|
||||
|
||||
runtime autoload/ale/definition.vim
|
||||
runtime autoload/ale/linter.vim
|
||||
runtime autoload/ale/lsp.vim
|
||||
|
||||
function! ale#linter#StartLSP(buffer, linter, callback) abort
|
||||
let g:Callback = a:callback
|
||||
|
||||
return {
|
||||
\ 'connection_id': 347,
|
||||
\ 'project_root': '/foo/bar',
|
||||
\}
|
||||
endfunction
|
||||
|
||||
function! ale#lsp#Send(conn_id, message, root) abort
|
||||
let g:message = a:message
|
||||
|
||||
return 42
|
||||
endfunction
|
||||
|
||||
function! ale#definition#Execute(expr) abort
|
||||
call add(g:expr_list, a:expr)
|
||||
endfunction
|
||||
|
||||
After:
|
||||
call ale#test#RestoreDirectory()
|
||||
call ale#linter#Reset()
|
||||
|
||||
unlet! g:old_filename
|
||||
unlet! g:Callback
|
||||
unlet! g:message
|
||||
unlet! g:expr_list
|
||||
|
||||
runtime autoload/ale/definition.vim
|
||||
runtime autoload/ale/linter.vim
|
||||
runtime autoload/ale/lsp.vim
|
||||
|
||||
Execute(Other messages for the tsserver handler should be ignored):
|
||||
call ale#definition#HandleTSServerResponse(1, {'command': 'foo'})
|
||||
|
||||
Execute(Failed definition responses should be handled correctly):
|
||||
call ale#definition#SetMap({3: {'open_in_tab': 0}})
|
||||
call ale#definition#HandleTSServerResponse(
|
||||
\ 1,
|
||||
\ {'command': 'definition', 'request_seq': 3}
|
||||
\)
|
||||
AssertEqual {}, ale#definition#GetMap()
|
||||
|
||||
Given typescript(Some typescript file):
|
||||
foo
|
||||
somelongerline
|
||||
bazxyzxyzxyz
|
||||
|
||||
Execute(Other files should be jumped to for definition responses):
|
||||
call ale#definition#SetMap({3: {'open_in_tab': 0}})
|
||||
call ale#definition#HandleTSServerResponse(
|
||||
\ 1,
|
||||
\ {
|
||||
\ 'command': 'definition',
|
||||
\ 'request_seq': 3,
|
||||
\ 'success': v:true,
|
||||
\ 'body': [
|
||||
\ {
|
||||
\ 'file': g:dir . '/completion_dummy_file',
|
||||
\ 'start': {'line': 3, 'offset': 7},
|
||||
\ },
|
||||
\ ],
|
||||
\ }
|
||||
\)
|
||||
|
||||
AssertEqual
|
||||
\ [
|
||||
\ 'edit ' . fnameescape(g:dir . '/completion_dummy_file'),
|
||||
\ ],
|
||||
\ g:expr_list
|
||||
AssertEqual [3, 7], getpos('.')[1:2]
|
||||
AssertEqual {}, ale#definition#GetMap()
|
||||
|
||||
Execute(Other files should be jumped to for definition responses in tabs too):
|
||||
call ale#definition#SetMap({3: {'open_in_tab': 1}})
|
||||
call ale#definition#HandleTSServerResponse(
|
||||
\ 1,
|
||||
\ {
|
||||
\ 'command': 'definition',
|
||||
\ 'request_seq': 3,
|
||||
\ 'success': v:true,
|
||||
\ 'body': [
|
||||
\ {
|
||||
\ 'file': g:dir . '/completion_dummy_file',
|
||||
\ 'start': {'line': 3, 'offset': 7},
|
||||
\ },
|
||||
\ ],
|
||||
\ }
|
||||
\)
|
||||
|
||||
AssertEqual
|
||||
\ [
|
||||
\ 'tabedit ' . fnameescape(g:dir . '/completion_dummy_file'),
|
||||
\ ],
|
||||
\ g:expr_list
|
||||
AssertEqual [3, 7], getpos('.')[1:2]
|
||||
AssertEqual {}, ale#definition#GetMap()
|
||||
|
||||
Execute(tsserver completion requests should be sent):
|
||||
runtime ale_linters/typescript/tsserver.vim
|
||||
call setpos('.', [bufnr(''), 2, 5, 0])
|
||||
|
||||
ALEGoToDefinition
|
||||
|
||||
AssertEqual
|
||||
\ 'function(''ale#definition#HandleTSServerResponse'')',
|
||||
\ string(g:Callback)
|
||||
AssertEqual
|
||||
\ [0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}],
|
||||
\ g:message
|
||||
AssertEqual {'42': {'open_in_tab': 0}}, ale#definition#GetMap()
|
||||
|
||||
Execute(tsserver tab completion requests should be sent):
|
||||
runtime ale_linters/typescript/tsserver.vim
|
||||
call setpos('.', [bufnr(''), 2, 5, 0])
|
||||
|
||||
ALEGoToDefinitionInTab
|
||||
|
||||
AssertEqual
|
||||
\ 'function(''ale#definition#HandleTSServerResponse'')',
|
||||
\ string(g:Callback)
|
||||
AssertEqual
|
||||
\ [0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}],
|
||||
\ g:message
|
||||
AssertEqual {'42': {'open_in_tab': 1}}, ale#definition#GetMap()
|
Loading…
x
Reference in New Issue
Block a user