From ef130c4428b12785baeb399014aa79dd1cb4267b Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 22 Apr 2018 22:00:25 +0100 Subject: [PATCH] #1428 Start implementing LSP hover support --- autoload/ale/hover.vim | 121 ++++++++++++++++++++++++ autoload/ale/lsp/message.vim | 9 ++ autoload/ale/util.vim | 5 + plugin/ale.vim | 4 + test/lsp/test_lsp_client_messages.vader | 14 +++ test/test_hover.vader | 107 +++++++++++++++++++++ 6 files changed, 260 insertions(+) create mode 100644 autoload/ale/hover.vim create mode 100644 test/test_hover.vader diff --git a/autoload/ale/hover.vim b/autoload/ale/hover.vim new file mode 100644 index 0000000..fdae350 --- /dev/null +++ b/autoload/ale/hover.vim @@ -0,0 +1,121 @@ +" Author: w0rp +" 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 diff --git a/autoload/ale/lsp/message.vim b/autoload/ale/lsp/message.vim index 037e6ce..5637fa2 100644 --- a/autoload/ale/lsp/message.vim +++ b/autoload/ale/lsp/message.vim @@ -126,3 +126,12 @@ function! ale#lsp#message#References(buffer, line, column) abort \ 'context': {'includeDeclaration': v:false}, \}] 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 diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index 55d9d74..d0dbec6 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -11,6 +11,11 @@ function! ale#util#FeedKeys(...) abort return call('feedkeys', a:000) 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. function! ale#util#Execute(expr) abort execute a:expr diff --git a/plugin/ale.vim b/plugin/ale.vim index f51e175..2a97e56 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -274,6 +274,9 @@ command! -bar ALEGoToDefinitionInTab :call ale#definition#GoTo({'open_in_tab': 1 " Find references for tsserver and LSP command! -bar ALEFindReferences :call ale#references#Find() +" Get information for the cursor. +command! -bar ALEHover :call ale#hover#Show() + " mappings for commands nnoremap (ale_previous) :ALEPrevious nnoremap (ale_previous_wrap) :ALEPreviousWrap @@ -295,6 +298,7 @@ nnoremap (ale_fix) :ALEFix nnoremap (ale_go_to_definition) :ALEGoToDefinition nnoremap (ale_go_to_definition_in_tab) :ALEGoToDefinitionInTab nnoremap (ale_find_references) :ALEFindReferences +nnoremap (ale_hover) :ALEHover " Set up autocmd groups now. call ale#toggle#InitAuGroups() diff --git a/test/lsp/test_lsp_client_messages.vader b/test/lsp/test_lsp_client_messages.vader index b0f0ed2..d186f5e 100644 --- a/test/lsp/test_lsp_client_messages.vader +++ b/test/lsp/test_lsp_client_messages.vader @@ -159,6 +159,20 @@ Execute(ale#lsp#message#References() should return correct messages): \ ], \ 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): AssertEqual \ [ diff --git a/test/test_hover.vader b/test/test_hover.vader new file mode 100644 index 0000000..57734b4 --- /dev/null +++ b/test/test_hover.vader @@ -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()