diff --git a/autoload/langserver/goto.vim b/autoload/langserver/goto.vim index c33d65d..78ac311 100644 --- a/autoload/langserver/goto.vim +++ b/autoload/langserver/goto.vim @@ -29,13 +29,19 @@ function! langserver#goto#callback(id, data, event) abort call langserver#goto#goto_defintion(a:id, l:parsed_data['uri'], l:parsed_data['range'], {}) endfunction -function! langserver#goto#request(name) abort - call langserver#client#send(a:name, { +function! langserver#goto#request(...) abort + if a:0 > 0 + let l:sever_name = a:1 + else + let l:server_name = langserver#util#get_lsp_id() + endif + + call langserver#client#send(l:server_name, { \ 'method': 'textDocument/definition', \ 'params': { - \ 'textDocument': langserver#util#get_text_document_identifier(a:name), + \ 'textDocument': langserver#util#get_text_document_identifier(l:server_name), \ 'position': langserver#util#get_position(), - \ }, + \ }, \ }) endfunction diff --git a/autoload/langserver/references.vim b/autoload/langserver/references.vim index 950c50b..fa3798a 100644 --- a/autoload/langserver/references.vim +++ b/autoload/langserver/references.vim @@ -6,11 +6,12 @@ function! langserver#references#transform_reply(message) abort let l:location_list = [] for l:location in a:message let l:loc_bufnr = bufnr(langserver#util#get_filename(langserver#util#get_lsp_id(), l:location['uri'])) + let l:loc_filename = langserver#util#get_filename(langserver#util#get_lsp_id(), l:location['uri']) let l:loc_line = l:location['range']['start']['line'] + 1 let l:loc_text = bufnr('%') == l:loc_bufnr ? getline(l:loc_line) : '' let l:location_dict = { - \ 'bufnr': l:loc_bufnr, + \ 'filename': l:loc_filename, \ 'lnum': l:loc_line, \ 'col': l:location['range']['start']['character'] + 1, \ 'text': l:loc_text, @@ -53,15 +54,16 @@ function! langserver#references#request() abort endfunction function! langserver#references#display(loc_list) abort - if langserver#util#debug() - echo 'Displaying references' - echo string(a:loc_list) - endif + call langserver#log#log('debug', "Displaying references...\n" . string(a:loc_list)) " TODO: Highlight the references, and turn them off somehow call setloclist(0, \ a:loc_list, \ 'r', - \ '[Location References]', + \ '[References]', \ ) + + if !empty(a:loc_list) + lwindow + endif endfunction diff --git a/autoload/langserver/symbol/util.vim b/autoload/langserver/symbol/util.vim index c721470..46eb8cb 100644 --- a/autoload/langserver/symbol/util.vim +++ b/autoload/langserver/symbol/util.vim @@ -1,44 +1,72 @@ -let s:symbol_kind: { - \ 'name': { - \ 'File': 1, - \ 'Module': 2, - \ 'Namespace': 3, - \ 'Package': 4, - \ 'Class': 5, - \ 'Method': 6, - \ 'Property': 7, - \ 'Field': 8, - \ 'Constructor': 9, - \ 'Enum': 10, - \ 'Interface': 11, - \ 'Function': 12, - \ 'Variable': 13, - \ 'Constant': 14, - \ 'String': 15, - \ 'Number': 16, - \ 'Boolean': 17, - \ 'Array': 18, - \ } - \ 'number': { - \ 1: 'File', - \ 2: 'Module', - \ 3: 'Namespace', - \ 4: 'Package', - \ 5: 'Class', - \ 6: 'Method', - \ 7: 'Property', - \ 8: 'Field', - \ 9: 'Constructor', - \ 10: 'Enum', - \ 11: 'Interface', - \ 12: 'Function', - \ 13: 'Variable', - \ 14: 'Constant', - \ 15: 'String', - \ 16: 'Number', - \ 17: 'Boolean', - \ 18: 'Array', - \ } - \} +let s:symbol_kind = { + \ 'name': { + \ 'File': 1, + \ 'Module': 2, + \ 'Namespace': 3, + \ 'Package': 4, + \ 'Class': 5, + \ 'Method': 6, + \ 'Property': 7, + \ 'Field': 8, + \ 'Constructor': 9, + \ 'Enum': 10, + \ 'Interface': 11, + \ 'Function': 12, + \ 'Variable': 13, + \ 'Constant': 14, + \ 'String': 15, + \ 'Number': 16, + \ 'Boolean': 17, + \ 'Array': 18, + \ }, + \ 'number': { + \ 1: 'File', + \ 2: 'Module', + \ 3: 'Namespace', + \ 4: 'Package', + \ 5: 'Class', + \ 6: 'Method', + \ 7: 'Property', + \ 8: 'Field', + \ 9: 'Constructor', + \ 10: 'Enum', + \ 11: 'Interface', + \ 12: 'Function', + \ 13: 'Variable', + \ 14: 'Constant', + \ 15: 'String', + \ 16: 'Number', + \ 17: 'Boolean', + \ 18: 'Array', + \ }, + \ } +function! langserver#symbol#util#transform_reply(message) abort + let l:server_name = langserver#util#get_lsp_id() + let l:qf_list = [] + + for l:msg in a:message + let l:loc_filename = langserver#util#get_filename(l:server_name, l:msg['location']['uri']) + let l:loc_line = l:msg['location']['range']['start']['line'] + 1 + let l:loc_col = l:msg['location']['range']['start']['character'] + 1 + + let l:loc_kind = s:symbol_kind['number'][l:msg['kind']] + + let l:msg_dict = { + \ 'filename': l:loc_filename, + \ 'lnum': l:loc_line, + \ 'col': l:loc_col, + \ 'pattern': l:msg['name'], + \ 'text': printf('%10s: %s', + \ l:loc_kind, + \ l:msg['name'], + \ ), + \ } + + call add(l:qf_list, l:msg_dict) + endfor + + return l:qf_list +endfunction + diff --git a/autoload/langserver/symbol/workspace.vim b/autoload/langserver/symbol/workspace.vim index ea979da..cc57963 100644 --- a/autoload/langserver/symbol/workspace.vim +++ b/autoload/langserver/symbol/workspace.vim @@ -6,7 +6,8 @@ function! langserver#symbol#workspace#callback(id, data, event) abort return endif - call langserver#log#log('info', a:data, v:true) + let l:transformed = langserver#symbol#util#transform_reply(l:parsed_data) + call langserver#symbol#workspace#display(l:transformed) endfunction function!langserver#symbol#workspace#request(...) abort @@ -21,3 +22,9 @@ function!langserver#symbol#workspace#request(...) abort \ 'params': {'query': l:query} \ }) endfunction + +function! langserver#symbol#workspace#display(loc_list) abort + call langserver#log#log('debug', string(a:loc_list)) + + call setqflist(a:loc_list) +endfunction diff --git a/plugin/langserver.vim b/plugin/langserver.vim index 9c7301c..f0ce6df 100644 --- a/plugin/langserver.vim +++ b/plugin/langserver.vim @@ -1,7 +1,7 @@ let g:langserver_configuration = { - \ 'json_rpc_version': "2.0", + \ 'json_rpc_version': '2.0', \ } @@ -11,4 +11,13 @@ command! LSPStart call langserver#start({}) " Open a text document, and alert the language server command! LSPOpen call langserver#didOpenTextDocument() -nnoremap gd :call langserver#goto#request(langserver#util#get_lsp_id()) +" Request a goto +command! LSPGoto call langserver#goto#request() + +" Request a hover +command! LSPHover call langserver#hover#request() + + + +nnoremap gd :call langserver#goto#request() +nnoremap gh :call langserver#hovoer#request() diff --git a/tests/test_message_parse.vader b/tests/test_message_parse.vader index e0582b7..1dcb5ff 100644 --- a/tests/test_message_parse.vader +++ b/tests/test_message_parse.vader @@ -34,5 +34,11 @@ Execute (Test parse of references): let test_message = '<-- result #25: textDocument/references: [{"uri":"file:///home/tj/go/src/github.com/sourcegraph/go-langserver/langserver/util.go","range":{"start":{"line":7,"character":22},"end":{"line":7,"character":28}}},{"uri":"file:///home/tj/go/src/github.com/sourcegraph/go-langserver/langserver/util.go","range":{"start":{"line":9,"character":4},"end":{"line":9,"character":10}}},{"uri":"file:///home/tj/go/src/github.com/sourcegraph/go-langserver/langserver/util.go","range":{"start":{"line":9,"character":39},"end":{"line":9,"character":45}}},{"uri":"file:///home/tj/go/src/github.com/sourcegraph/go-langserver/langserver/util.go","range":{"start":{"line":10,"character":16},"end":{"line":10,"character":22}}},{"uri":"file:///home/tj/go/src/github.com/sourcegraph/go-langserver/langserver/util.go","range":{"start":{"line":12,"character":13},"end":{"line":12,"character":19}}}]' let parsed = langserver#util#parse_message(test_message) - let transformed = langserver#references#transform_reply(parsed) + " let transformed = langserver#references#transform_reply(parsed) + " Log transformed + +Execute (Test parse of symbols): + let test_message = {'response': {'id': 6, 'result': [{'location': {'uri': 'file:///usr/local/go/src/os/path_plan9.go', 'range': {'end': {'character': 4, 'line': 7}, 'start': {'character': 0, 'line': 6}}}, 'name': 'PathSeparator', 'kind': 14, 'containerName': 'os'}, {'location': {'uri': 'file:///usr/local/go/src/os/path_unix.go', 'range': {'end': {'character': 4, 'line': 9}, 'start': {'character': 0, 'line': 8}}}, 'name': 'PathSeparator', 'kind': 14, 'containerName': 'os'}, {'location': {'uri': 'file:///usr/local/go/src/os/path_windows.go', 'range': {'end': {'character': 4, 'line': 7}, 'start': {'character': 0, 'line': 6}}}, 'name': 'PathSeparator', 'kind': 14, 'containerName': 'os'}], 'jsonrpc': '2.0'}, 'request': {'id': 6, 'jsonrpc': '2.0', 'method': 'workspace/symbol', 'params': {'query': 'PathSeparator'}}} + + let transformed = langserver#symbol#util#transform_reply(test_message['response']['result']) Log transformed