Close #1428 Implement LSP hover-like functionality for tsserver too

This commit is contained in:
w0rp 2018-04-26 21:54:11 +01:00
parent e6fe2d86b8
commit d8d09c2048
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
4 changed files with 80 additions and 3 deletions

View File

@ -18,6 +18,15 @@ function! ale#hover#ClearLSPData() abort
endfunction
function! ale#hover#HandleTSServerResponse(conn_id, response) abort
if get(a:response, 'command', '') is# 'quickinfo'
\&& has_key(s:hover_map, a:response.request_seq)
let l:options = remove(s:hover_map, a:response.request_seq)
if get(a:response, 'success', v:false) is v:true
\&& get(a:response, 'body', v:null) isnot v:null
call ale#util#ShowMessage(a:response.body.displayString)
endif
endif
endfunction
function! ale#hover#HandleLSPResponse(conn_id, response) abort
@ -86,8 +95,11 @@ function! s:ShowDetails(linter) abort
let l:root = l:lsp_details.project_root
if a:linter.lsp is# 'tsserver'
" TODO: Implement this.
return
let l:message = ale#lsp#tsserver_message#Quickinfo(
\ l:buffer,
\ l:line,
\ l:column
\)
else
" Send a message saying the buffer has changed first, or the
" hover position probably won't make sense.
@ -109,7 +121,7 @@ endfunction
function! ale#hover#Show() abort
for l:linter in ale#linter#Get(&filetype)
if !empty(l:linter.lsp) && l:linter.lsp isnot# 'tsserver'
if !empty(l:linter.lsp)
call s:ShowDetails(l:linter)
endif
endfor

View File

@ -69,3 +69,11 @@ function! ale#lsp#tsserver_message#References(buffer, line, column) abort
\ 'file': expand('#' . a:buffer . ':p'),
\}]
endfunction
function! ale#lsp#tsserver_message#Quickinfo(buffer, line, column) abort
return [0, 'ts@quickinfo', {
\ 'line': a:line,
\ 'offset': a:column,
\ 'file': expand('#' . a:buffer . ':p'),
\}]
endfunction

View File

@ -275,3 +275,16 @@ Execute(ale#lsp#tsserver_message#References() should return correct messages):
\ }
\ ],
\ ale#lsp#tsserver_message#References(bufnr(''), 347, 12)
Execute(ale#lsp#tsserver_message#Quickinfo() should return correct messages):
AssertEqual
\ [
\ 0,
\ 'ts@quickinfo',
\ {
\ 'file': ale#path#Simplify(g:dir . '/foo/bar.ts'),
\ 'line': 347,
\ 'offset': 12,
\ }
\ ],
\ ale#lsp#tsserver_message#Quickinfo(bufnr(''), 347, 12)

View File

@ -70,6 +70,50 @@ Given python(Some Python file):
somelongerline
bazxyzxyzxyz
Execute(Other messages for the tsserver handler should be ignored):
call ale#hover#HandleTSServerResponse(1, {'command': 'foo'})
Execute(Failed hover responses should be handled correctly):
call ale#hover#SetMap({3: {}})
call ale#hover#HandleTSServerResponse(
\ 1,
\ {'command': 'quickinfo', 'request_seq': 3}
\)
AssertEqual {}, ale#hover#GetMap()
Given typescript(Some typescript file):
foo
somelongerline
bazxyzxyzxyz
Execute(tsserver quickinfo responses will null missing bodies should be handled):
call ale#hover#SetMap({3: {}})
call ale#hover#HandleTSServerResponse(
\ 1,
\ {
\ 'command': 'quickinfo',
\ 'request_seq': 3,
\ 'success': v:true,
\ }
\)
AssertEqual {}, ale#hover#GetMap()
Execute(tsserver quickinfo displayString values should be displayed):
call ale#hover#SetMap({3: {}})
call ale#hover#HandleTSServerResponse(
\ 1,
\ {
\ 'command': 'quickinfo',
\ 'request_seq': 3,
\ 'success': v:true,
\ 'body': {'displayString': 'foo bar'},
\ }
\)
AssertEqual ['foo bar'], g:echo_list
AssertEqual {}, ale#hover#GetMap()
Execute(LSP hover responses with just a string should be handled):
call HandleValidLSPResult({'contents': 'foobar'})