Add textDocument/typeDefinition for LSP (#2226)

* Add textDocument/typeDefinition for LSP

Doc to spec https://microsoft.github.io/language-server-protocol/specification#textDocument_typeDefinition

This works like textDocument/definition but resolves a location of a
type of an expression under the cursor.

I'm not sure what to do with tsserver though.

* Fix passing column to LSP
* test_go_to_definition: wording
* Add tests for textDocument/typeDefinition
* Add docs for textDocument/typeDefinition
This commit is contained in:
Andrey Popp
2019-01-22 02:06:28 +03:00
committed by w0rp
parent a4932679b5
commit d0284f22ea
7 changed files with 201 additions and 16 deletions

View File

@@ -57,7 +57,7 @@ function! ale#definition#HandleLSPResponse(conn_id, response) abort
endif
endfunction
function! s:OnReady(linter, lsp_details, line, column, options, ...) abort
function! s:OnReady(linter, lsp_details, line, column, options, capability, ...) abort
let l:buffer = a:lsp_details.buffer
let l:id = a:lsp_details.connection_id
@@ -80,7 +80,14 @@ function! s:OnReady(linter, lsp_details, line, column, options, ...) abort
" For LSP completions, we need to clamp the column to the length of
" the line. python-language-server and perhaps others do not implement
" this correctly.
let l:message = ale#lsp#message#Definition(l:buffer, a:line, a:column)
if a:capability is# 'definition'
let l:message = ale#lsp#message#Definition(l:buffer, a:line, a:column)
elseif a:capability is# 'typeDefinition'
let l:message = ale#lsp#message#TypeDefinition(l:buffer, a:line, a:column)
else
" XXX: log here?
return
endif
endif
let l:request_id = ale#lsp#Send(l:id, l:message)
@@ -90,7 +97,7 @@ function! s:OnReady(linter, lsp_details, line, column, options, ...) abort
\}
endfunction
function! s:GoToLSPDefinition(linter, options) abort
function! s:GoToLSPDefinition(linter, options, capability) abort
let l:buffer = bufnr('')
let [l:line, l:column] = getcurpos()[1:2]
let l:lsp_details = ale#lsp_linter#StartLSP(l:buffer, a:linter)
@@ -105,15 +112,29 @@ function! s:GoToLSPDefinition(linter, options) abort
let l:id = l:lsp_details.connection_id
call ale#lsp#WaitForCapability(l:id, 'definition', function('s:OnReady', [
\ a:linter, l:lsp_details, l:line, l:column, a:options
call ale#lsp#WaitForCapability(l:id, a:capability, function('s:OnReady', [
\ a:linter, l:lsp_details, l:line, l:column, a:options, a:capability
\]))
endfunction
function! ale#definition#GoTo(options) abort
for l:linter in ale#linter#Get(&filetype)
if !empty(l:linter.lsp)
call s:GoToLSPDefinition(l:linter, a:options)
call s:GoToLSPDefinition(l:linter, a:options, 'definition')
endif
endfor
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
endfunction

View File

@@ -43,6 +43,7 @@ function! ale#lsp#Register(executable_or_address, project, init_options) abort
\ 'completion': 0,
\ 'completion_trigger_characters': [],
\ 'definition': 0,
\ 'typeDefinition': 0,
\ 'symbol_search': 0,
\ },
\}
@@ -207,6 +208,10 @@ function! s:UpdateCapabilities(conn, capabilities) abort
let a:conn.capabilities.definition = 1
endif
if get(a:capabilities, 'typeDefinitionProvider') is v:true
let a:conn.capabilities.typeDefinition = 1
endif
if get(a:capabilities, 'workspaceSymbolProvider') is v:true
let a:conn.capabilities.symbol_search = 1
endif

View File

@@ -124,6 +124,15 @@ function! ale#lsp#message#Definition(buffer, line, column) abort
\}]
endfunction
function! ale#lsp#message#TypeDefinition(buffer, line, column) abort
return [0, 'textDocument/typeDefinition', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column - 1},
\}]
endfunction
function! ale#lsp#message#References(buffer, line, column) abort
return [0, 'textDocument/references', {
\ 'textDocument': {