From d340710fcfbd4db7bd2a8e4cd126306d4d7ef4e2 Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Sat, 20 Feb 2021 11:09:28 -0500 Subject: [PATCH] Support going to type definition with tsserver (#3545) --- autoload/ale/definition.vim | 26 +++++++++++++------------ autoload/ale/lsp.vim | 1 + autoload/ale/lsp/tsserver_message.vim | 8 ++++++++ test/lsp/test_lsp_client_messages.vader | 13 +++++++++++++ test/test_go_to_definition.vader | 24 +++++++++++++++++++++++ 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/autoload/ale/definition.vim b/autoload/ale/definition.vim index 0c1fb7cf..a5a9271e 100644 --- a/autoload/ale/definition.vim +++ b/autoload/ale/definition.vim @@ -36,7 +36,7 @@ function! ale#definition#UpdateTagStack() abort endfunction function! ale#definition#HandleTSServerResponse(conn_id, response) abort - if get(a:response, 'command', '') is# 'definition' + if has_key(a:response, 'request_seq') \&& has_key(s:go_to_definition_map, a:response.request_seq) let l:options = remove(s:go_to_definition_map, a:response.request_seq) @@ -92,11 +92,19 @@ function! s:OnReady(line, column, options, capability, linter, lsp_details) abor call ale#lsp#RegisterCallback(l:id, l:Callback) if a:linter.lsp is# 'tsserver' - let l:message = ale#lsp#tsserver_message#Definition( - \ l:buffer, - \ a:line, - \ a:column - \) + if a:capability is# 'definition' + let l:message = ale#lsp#tsserver_message#Definition( + \ l:buffer, + \ a:line, + \ a:column + \) + elseif a:capability is# 'typeDefinition' + let l:message = ale#lsp#tsserver_message#TypeDefinition( + \ l:buffer, + \ a:line, + \ a:column + \) + endif else " Send a message saying the buffer has changed first, or the " definition position probably won't make sense. @@ -145,12 +153,6 @@ endfunction function! ale#definition#GoToType(options) abort for l:linter in ale#linter#Get(&filetype) if !empty(l:linter.lsp) - " TODO: handle typeDefinition for tsserver if supported by the - " protocol - if l:linter.lsp is# 'tsserver' - continue - endif - call s:GoToLSPDefinition(l:linter, a:options, 'typeDefinition') endif endfor diff --git a/autoload/ale/lsp.vim b/autoload/ale/lsp.vim index cb0573aa..1f854bc5 100644 --- a/autoload/ale/lsp.vim +++ b/autoload/ale/lsp.vim @@ -357,6 +357,7 @@ function! ale#lsp#MarkConnectionAsTsserver(conn_id) abort let l:conn.capabilities.completion = 1 let l:conn.capabilities.completion_trigger_characters = ['.'] let l:conn.capabilities.definition = 1 + let l:conn.capabilities.typeDefinition = 1 let l:conn.capabilities.symbol_search = 1 let l:conn.capabilities.rename = 1 let l:conn.capabilities.code_actions = 1 diff --git a/autoload/ale/lsp/tsserver_message.vim b/autoload/ale/lsp/tsserver_message.vim index 3c1b47ed..00213a75 100644 --- a/autoload/ale/lsp/tsserver_message.vim +++ b/autoload/ale/lsp/tsserver_message.vim @@ -64,6 +64,14 @@ function! ale#lsp#tsserver_message#Definition(buffer, line, column) abort \}] endfunction +function! ale#lsp#tsserver_message#TypeDefinition(buffer, line, column) abort + return [0, 'ts@typeDefinition', { + \ 'line': a:line, + \ 'offset': a:column, + \ 'file': expand('#' . a:buffer . ':p'), + \}] +endfunction + function! ale#lsp#tsserver_message#References(buffer, line, column) abort return [0, 'ts@references', { \ 'line': a:line, diff --git a/test/lsp/test_lsp_client_messages.vader b/test/lsp/test_lsp_client_messages.vader index bc91bf68..754073ec 100644 --- a/test/lsp/test_lsp_client_messages.vader +++ b/test/lsp/test_lsp_client_messages.vader @@ -307,6 +307,19 @@ Execute(ale#lsp#tsserver_message#Definition() should return correct messages): \ ], \ ale#lsp#tsserver_message#Definition(bufnr(''), 347, 12) +Execute(ale#lsp#tsserver_message#TypeDefinition() should return correct messages): + AssertEqual + \ [ + \ 0, + \ 'ts@typeDefinition', + \ { + \ 'file': ale#path#Simplify(g:dir . '/foo/bar.ts'), + \ 'line': 347, + \ 'offset': 12, + \ } + \ ], + \ ale#lsp#tsserver_message#TypeDefinition(bufnr(''), 347, 12) + Execute(ale#lsp#tsserver_message#References() should return correct messages): AssertEqual \ [ diff --git a/test/test_go_to_definition.vader b/test/test_go_to_definition.vader index f2f34280..c7805932 100644 --- a/test/test_go_to_definition.vader +++ b/test/test_go_to_definition.vader @@ -242,6 +242,30 @@ Execute(tsserver definition requests should be sent): \ g:message_list AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap() +Execute(tsserver type definition requests should be sent): + runtime ale_linters/typescript/tsserver.vim + call setpos('.', [bufnr(''), 2, 5, 0]) + + ALEGoToTypeDefinition + + " We shouldn't register the callback yet. + AssertEqual '''''', string(g:Callback) + + AssertEqual type(function('type')), type(g:InitCallback) + call g:InitCallback() + + AssertEqual 'typeDefinition', g:capability_checked + AssertEqual + \ 'function(''ale#definition#HandleTSServerResponse'')', + \ string(g:Callback) + AssertEqual + \ [ + \ ale#lsp#tsserver_message#Change(bufnr('')), + \ [0, 'ts@typeDefinition', {'file': expand('%:p'), 'line': 2, 'offset': 5}] + \ ], + \ g:message_list + AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap() + Execute(tsserver tab definition requests should be sent): runtime ale_linters/typescript/tsserver.vim call setpos('.', [bufnr(''), 2, 5, 0])