diff options
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale/hover.vim | 121 | ||||
-rw-r--r-- | autoload/ale/lsp/message.vim | 9 | ||||
-rw-r--r-- | autoload/ale/util.vim | 5 |
3 files changed, 135 insertions, 0 deletions
diff --git a/autoload/ale/hover.vim b/autoload/ale/hover.vim new file mode 100644 index 00000000..fdae350e --- /dev/null +++ b/autoload/ale/hover.vim @@ -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 diff --git a/autoload/ale/lsp/message.vim b/autoload/ale/lsp/message.vim index 037e6ce2..5637fa2e 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 55d9d743..d0dbec65 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 |