#1428 Start implementing LSP hover support

This commit is contained in:
w0rp 2018-04-22 22:00:25 +01:00
parent 0b3ee11546
commit ef130c4428
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
6 changed files with 260 additions and 0 deletions

121
autoload/ale/hover.vim Normal file
View File

@ -0,0 +1,121 @@
" Author: w0rp <devw0rp@gmail.com>
" Description: Hover support for LSP linters.
let s:hover_map = {}
" Used to get the hover map in tests.
function! ale#hover#GetMap() abort
return deepcopy(s:hover_map)
endfunction
" Used to set the hover map in tests.
function! ale#hover#SetMap(map) abort
let s:hover_map = a:map
endfunction
function! ale#hover#ClearLSPData() abort
let s:hover_map = {}
endfunction
function! ale#hover#HandleTSServerResponse(conn_id, response) abort
endfunction
function! ale#hover#HandleLSPResponse(conn_id, response) abort
if has_key(a:response, 'id')
\&& has_key(s:hover_map, a:response.id)
let l:options = remove(s:hover_map, a:response.id)
let l:buffer = bufnr('')
let [l:line, l:column] = getcurpos()[1:2]
let l:end = len(getline(l:line))
if l:buffer isnot l:options.buffer
\|| l:line isnot l:options.line
\|| min([l:column, l:end]) isnot min([l:options.column, l:end])
" Cancel display the message if the cursor has moved.
return
endif
" The result can be a Dictionary item, a List of the same, or null.
let l:result = get(a:response, 'result', v:null)
if l:result is v:null
return
endif
let l:result = l:result.contents
if type(l:result) is type('')
" The result can be just a string.
let l:result = [l:result]
endif
if type(l:result) is type({})
" If the result is an object, then it's markup content.
let l:result = [l:result.value]
endif
if type(l:result) is type([])
" Replace objects with text values.
call map(l:result, 'type(v:val) is type('''') ? v:val : v:val.value')
let l:str = join(l:result, "\n")
let l:str = substitute(l:str, '^\s*\(.\{-}\)\s*$', '\1', '')
if !empty(l:str)
" Compress multi-line hover messages into one line.
let l:str = substitute(l:str, "\n", ' ', 'g')
let l:str = substitute(l:str, ' \+', ' ', 'g')
let l:str = substitute(l:str, '^\s*\(.\{-}\)\s*$', '\1', '')
call ale#util#Echo(l:str)
endif
endif
endif
endfunction
function! s:ShowDetails(linter) abort
let l:buffer = bufnr('')
let [l:line, l:column] = getcurpos()[1:2]
let l:Callback = a:linter.lsp is# 'tsserver'
\ ? function('ale#hover#HandleTSServerResponse')
\ : function('ale#hover#HandleLSPResponse')
let l:lsp_details = ale#linter#StartLSP(l:buffer, a:linter, l:Callback)
if empty(l:lsp_details)
return 0
endif
let l:id = l:lsp_details.connection_id
let l:root = l:lsp_details.project_root
if a:linter.lsp is# 'tsserver'
" TODO: Implement this.
return
else
" Send a message saying the buffer has changed first, or the
" hover position probably won't make sense.
call ale#lsp#Send(l:id, ale#lsp#message#DidChange(l:buffer), l:root)
let l:column = min([l:column, len(getline(l:line))])
let l:message = ale#lsp#message#Hover(l:buffer, l:line, l:column)
endif
let l:request_id = ale#lsp#Send(l:id, l:message, l:root)
let s:hover_map[l:request_id] = {
\ 'buffer': l:buffer,
\ 'line': l:line,
\ 'column': l:column,
\}
endfunction
function! ale#hover#Show() abort
for l:linter in ale#linter#Get(&filetype)
if !empty(l:linter.lsp) && l:linter.lsp isnot# 'tsserver'
call s:ShowDetails(l:linter)
endif
endfor
endfunction

View File

@ -126,3 +126,12 @@ function! ale#lsp#message#References(buffer, line, column) abort
\ 'context': {'includeDeclaration': v:false}, \ 'context': {'includeDeclaration': v:false},
\}] \}]
endfunction endfunction
function! ale#lsp#message#Hover(buffer, line, column) abort
return [0, 'textDocument/hover', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column},
\}]
endfunction

View File

@ -11,6 +11,11 @@ function! ale#util#FeedKeys(...) abort
return call('feedkeys', a:000) return call('feedkeys', a:000)
endfunction endfunction
" A wrapper function for echo so we can test calls for it.
function! ale#util#Echo(string) abort
execute 'echo a:string'
endfunction
" A wrapper function for execute, so we can test executing some commands. " A wrapper function for execute, so we can test executing some commands.
function! ale#util#Execute(expr) abort function! ale#util#Execute(expr) abort
execute a:expr execute a:expr

View File

@ -274,6 +274,9 @@ command! -bar ALEGoToDefinitionInTab :call ale#definition#GoTo({'open_in_tab': 1
" Find references for tsserver and LSP " Find references for tsserver and LSP
command! -bar ALEFindReferences :call ale#references#Find() command! -bar ALEFindReferences :call ale#references#Find()
" Get information for the cursor.
command! -bar ALEHover :call ale#hover#Show()
" <Plug> mappings for commands " <Plug> mappings for commands
nnoremap <silent> <Plug>(ale_previous) :ALEPrevious<Return> nnoremap <silent> <Plug>(ale_previous) :ALEPrevious<Return>
nnoremap <silent> <Plug>(ale_previous_wrap) :ALEPreviousWrap<Return> nnoremap <silent> <Plug>(ale_previous_wrap) :ALEPreviousWrap<Return>
@ -295,6 +298,7 @@ nnoremap <silent> <Plug>(ale_fix) :ALEFix<Return>
nnoremap <silent> <Plug>(ale_go_to_definition) :ALEGoToDefinition<Return> nnoremap <silent> <Plug>(ale_go_to_definition) :ALEGoToDefinition<Return>
nnoremap <silent> <Plug>(ale_go_to_definition_in_tab) :ALEGoToDefinitionInTab<Return> nnoremap <silent> <Plug>(ale_go_to_definition_in_tab) :ALEGoToDefinitionInTab<Return>
nnoremap <silent> <Plug>(ale_find_references) :ALEFindReferences<Return> nnoremap <silent> <Plug>(ale_find_references) :ALEFindReferences<Return>
nnoremap <silent> <Plug>(ale_hover) :ALEHover<Return>
" Set up autocmd groups now. " Set up autocmd groups now.
call ale#toggle#InitAuGroups() call ale#toggle#InitAuGroups()

View File

@ -159,6 +159,20 @@ Execute(ale#lsp#message#References() should return correct messages):
\ ], \ ],
\ ale#lsp#message#References(bufnr(''), 12, 34) \ ale#lsp#message#References(bufnr(''), 12, 34)
Execute(ale#lsp#message#Hover() should return correct messages):
AssertEqual
\ [
\ 0,
\ 'textDocument/hover',
\ {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(g:dir . '/foo/bar.ts'),
\ },
\ 'position': {'line': 11, 'character': 34},
\ }
\ ],
\ ale#lsp#message#Hover(bufnr(''), 12, 34)
Execute(ale#lsp#tsserver_message#Open() should return correct messages): Execute(ale#lsp#tsserver_message#Open() should return correct messages):
AssertEqual AssertEqual
\ [ \ [

107
test/test_hover.vader Normal file
View File

@ -0,0 +1,107 @@
Before:
call ale#test#SetDirectory('/testplugin/test')
call ale#test#SetFilename('dummy.txt')
let g:Callback = 0
let g:message_list = []
let g:item_list = []
let g:echo_list = []
runtime autoload/ale/linter.vim
runtime autoload/ale/lsp.vim
runtime autoload/ale/util.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
call add(g:message_list, a:message)
return 42
endfunction
function! ale#util#Echo(string) abort
call add(g:echo_list, a:string)
endfunction
function! HandleValidLSPResult(result) abort
" The cursor is beyond the length of the line.
" We will clamp the cursor position with the line length.
call setpos('.', [bufnr(''), 1, 5, 0])
call ale#hover#SetMap({3: {
\ 'buffer': bufnr(''),
\ 'line': 1,
\ 'column': 5,
\}})
call ale#hover#HandleLSPResponse(
\ 1,
\ {
\ 'id': 3,
\ 'result': a:result,
\ }
\)
endfunction
After:
call ale#hover#SetMap({})
call ale#test#RestoreDirectory()
call ale#linter#Reset()
unlet! g:Callback
unlet! g:message_list
unlet! b:ale_linters
unlet! g:echo_list
delfunction HandleValidLSPResult
runtime autoload/ale/linter.vim
runtime autoload/ale/lsp.vim
runtime autoload/ale/util.vim
Given python(Some Python file):
foo
somelongerline
bazxyzxyzxyz
Execute(LSP hover responses with just a string should be handled):
call HandleValidLSPResult({'contents': 'foobar'})
AssertEqual ['foobar'], g:echo_list
AssertEqual {}, ale#hover#GetMap()
Execute(LSP hover null responses should be handled):
call HandleValidLSPResult(v:null)
AssertEqual [], g:echo_list
AssertEqual {}, ale#hover#GetMap()
Execute(LSP hover responses with markup content should be handled):
call HandleValidLSPResult({'contents': {'kind': 'something', 'value': 'markup'}})
AssertEqual ['markup'], g:echo_list
AssertEqual {}, ale#hover#GetMap()
Execute(LSP hover response with lists of strings should be handled):
call HandleValidLSPResult({'contents': [
\ "foo\n",
\ "bar\n",
\]})
AssertEqual ['foo bar'], g:echo_list
AssertEqual {}, ale#hover#GetMap()
Execute(LSP hover response with lists of strings and marked strings should be handled):
call HandleValidLSPResult({'contents': [
\ {'language': 'rust', 'value': 'foo'},
\ "bar\n",
\]})
AssertEqual ['foo bar'], g:echo_list
AssertEqual {}, ale#hover#GetMap()