diff --git a/README.md b/README.md index 12f84e9..58dced6 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,10 @@ You will need to put this somewhere that is sourced on startup. ```vim let g:langserver_executables = { - \ 'go': ['langserver-go', '-trace', '-logfile', expand('~/Desktop/langserver-go.log')], + \ 'go': { + \ 'name': 'sourcegraph/langserver-go', + \ 'cmd': ['langserver-go', '-trace', '-logfile', expand('~/Desktop/langserver-go.log')], + \ }, \ } ``` diff --git a/autoload/langserver/callbacks.vim b/autoload/langserver/callbacks.vim index 79a4a8e..cb5f2c4 100644 --- a/autoload/langserver/callbacks.vim +++ b/autoload/langserver/callbacks.vim @@ -1,64 +1,49 @@ function! langserver#callbacks#on_stdout(id, data, event) abort - echom 'LSP STDOUT(' . a:id . '): ' . string(a:data) + echom 'LSP STDOUT(' . a:id . '): ' . string(a:data) endfunction function! langserver#callbacks#on_stderr(id, data, event) abort - " TODO: some function that parses this easily. - " let split_data = split(a:data[0], ':') - " echom '{' . join(split_data[1:], ':') . '}' - " return - - " if v:true || has_key(a:data, 'textDocument/definition') - " echom 'lsp has found definition' - " else - echom 'lsp('.a:id.'):stderr: Event:' . a:event . ' ==> ' . join(a:data, "\r\n") - - " let parsed = langserver#util#parse_message(a:data) - " echom 'Resulting data is: ' . string(parsed) - " echom 'STDERR: Request ' . string(a:data.request) + call langserver#log#response(a:id, a:data, a:event) endfunction function! langserver#callbacks#on_exit(id, status, event) abort - echom 'lsp('.a:id.'):exit:'.a:status + echom 'lsp('.a:id.'):exit:'.a:status endfunction function! langserver#callbacks#on_notification(id, data, event) abort - if has_key(a:data, 'response') - if langserver#util#debug() - let g:last_response = a:data - echom 'LSP RSP: ' . string(a:data) - endif + if has_key(a:data, 'response') + call langserver#log#response(a:id, a:data, a:event) - let l:last_topic = a:data['request']['method'] + let l:last_topic = a:data['request']['method'] - if l:last_topic ==? 'textDocument/references' - call langserver#references#callback(a:id, a:data, a:event) - elseif l:last_topic ==? 'textDocument/definition' - call langserver#goto#callback(a:id, a:data, a:event) - elseif l:last_topic ==? 'textDocument/hover' - call langserver#hover#callback(a:id, a:data, a:event) - elseif l:last_topic ==? 'textDocument/didOpen' - call langserver#documents#callback_did_open(a:id, a:data, a:event) - elseif l:last_topic ==? 'workspace/symbol' - call langserver#symbol#workspace#callback(a:id, a:data, a:event) - else - call langserver#log#log('warning', 'LAST REQUEST: ' . l:last_topic, v:true) - endif - endif + if l:last_topic ==? 'textDocument/references' + call langserver#references#callback(a:id, a:data, a:event) + elseif l:last_topic ==? 'textDocument/definition' + call langserver#goto#callback(a:id, a:data, a:event) + elseif l:last_topic ==? 'textDocument/hover' + call langserver#hover#callback(a:id, a:data, a:event) + elseif l:last_topic ==? 'textDocument/didOpen' + call langserver#documents#callback_did_open(a:id, a:data, a:event) + elseif l:last_topic ==? 'workspace/symbol' + call langserver#symbol#workspace#callback(a:id, a:data, a:event) + else + call langserver#log#log('warning', 'LAST REQUEST: ' . l:last_topic, v:true) + endif + endif - if langserver#client#is_error(a:data.response) - echom 'lsp('.a:id.'):notification:notification error receieved for '.a:data.request.method - else - if langserver#util#debug() - " echom 'lsp('.a:id.'):notification:notification success receieved for '.a:data.request.method - endif + if langserver#client#is_error(a:data.response) + echom 'lsp('.a:id.'):notification:notification error receieved for '.a:data.request.method + else + if langserver#util#debug() + " echom 'lsp('.a:id.'):notification:notification success receieved for '.a:data.request.method + endif - if a:data.request.method ==? 'textDocument/references' - echom 'LSP NOTIFI References Method matched: ' . string(a:data.request) - " call langserver#references#callback(a:data.request) - endif - endif + if a:data.request.method ==? 'textDocument/references' + echom 'LSP NOTIFI References Method matched: ' . string(a:data.request) + " call langserver#references#callback(a:data.request) + endif + endif endfunction function! langserver#callbacks#data(id, data, event) abort diff --git a/autoload/langserver/default.vim b/autoload/langserver/default.vim index 5813df1..404e4ab 100644 --- a/autoload/langserver/default.vim +++ b/autoload/langserver/default.vim @@ -2,9 +2,9 @@ let s:langserver_executabe = 'langserver-go' "" " Get the default command for starting the server -function! langserver#default#cmd() +function! langserver#default#cmd() abort if has_key(g:langserver_executables, &filetype) - return g:langserver_executables[&filetype] + return g:langserver_executables[&filetype]['cmd'] else return [-1] endif diff --git a/autoload/langserver/goto.vim b/autoload/langserver/goto.vim index a6b82dd..c33d65d 100644 --- a/autoload/langserver/goto.vim +++ b/autoload/langserver/goto.vim @@ -6,7 +6,11 @@ function! langserver#goto#callback(id, data, event) abort endif if has_key(a:data, 'response') - let l:parsed_data = a:data['response']['result'][0] + if type(a:data['response']['result']) == type([]) + let l:parsed_data = a:data['response']['result'][0] + else + let l:parsed_data = a:data['response']['result'] + endif else return endif diff --git a/autoload/langserver/hover.vim b/autoload/langserver/hover.vim index f8027c2..0d357bb 100644 --- a/autoload/langserver/hover.vim +++ b/autoload/langserver/hover.vim @@ -5,7 +5,12 @@ function! langserver#hover#callback(id, data, event) abort endif " {'data': {'textDocument/hover': {'range': {'end': {'character': 20, 'line': 44}, 'start': {'character': 9, 'line': 44}}, 'contents': [{'language': 'go', 'value': 'type LangHandler struct'}, {'language': 'markdown', 'value': 'LangHandler is a Go language server LSP/JSON-RPC handler.'}]}}, 'type': 'result'} - let l:range = l:parsed_data['range'] + if has_key(l:parsed_data, 'range') + let l:range = l:parsed_data['range'] + else + let l:range = {} + endif + let l:data = l:parsed_data['contents'] call langserver#hover#display(l:range, l:data) @@ -19,8 +24,9 @@ function! langserver#hover#request() abort endfunction function! langserver#hover#display(range, data) abort - let s:my_last_highlight = matchaddpos('WarningMsg', langserver#highlight#matchaddpos_range(a:range)) - redraw + if !empty(a:range) + let s:my_last_highlight = matchaddpos('WarningMsg', langserver#highlight#matchaddpos_range(a:range)) + endif let l:hover_string = '' for l:explanation in a:data diff --git a/autoload/langserver/log.vim b/autoload/langserver/log.vim index 301b5c3..d512189 100644 --- a/autoload/langserver/log.vim +++ b/autoload/langserver/log.vim @@ -55,6 +55,55 @@ function! langserver#log#log(level, message, ...) abort endif endfunction +"" +" Log response helper +function! langserver#log#response(id, data, event) abort + let g:last_response = a:data + + if type(a:data) != type({}) + call langserver#log#log('debug', + \ printf('(%3s:%15s): %s', + \ a:id, + \ a:event, + \ string(a:data) + \ ), + \ langserver#util#debug(), + \ ) + return + endif + + if has_key(a:data, 'response') + call langserver#log#log('debug', + \ printf('(%3s:%15s): Response -> M(%20s), D(%s)', + \ a:id, + \ a:event, + \ string(a:data.request.method), + \ string(a:data.response.result), + \ ), + \ langserver#util#debug(), + \ ) + elseif has_key(a:data, 'request') + call langserver#log#log('debug', + \ printf('(%3s:%15s): Request -> M(%20s), D(%s)', + \ a:id, + \ a:event, + \ string(a:data.request.method), + \ string(a:data.request.params), + \ ), + \ langserver#util#debug(), + \ ) + else + call langserver#log#log('debug', + \ printf('(%3s:%15s): Unknown -> D(%s)', + \ a:id, + \ a:event, + \ string(a:data), + \ ), + \ langserver#util#debug(), + \ ) + endif +endfunction + "" " Log only at debug level function! langserver#log#callback(id, data, event) abort @@ -65,3 +114,13 @@ function! langserver#log#callback(id, data, event) abort \ string(a:data) \)) endfunction + +function! langserver#log#pretty_print(json_dict) abort + " TODO: Get pretty printing of json dictionaries if possible + let g:my_var = system([ + \ 'echo', + \ shellescape(g:last_response[3]), + \ '|', + \ 'python', '-m', 'json.tool', + \ ]) +endfunction diff --git a/autoload/langserver/util.vim b/autoload/langserver/util.vim index 4b5553a..9ab65c1 100644 --- a/autoload/langserver/util.vim +++ b/autoload/langserver/util.vim @@ -238,7 +238,7 @@ function! langserver#util#parse_message(message) abort endfunction function! langserver#util#debug() abort - return v:true + return v:false endfunction function! langserver#util#get_lsp_id() abort diff --git a/testing/vim_connection.vim b/testing/vim_connection.vim index 3b0e352..11d5532 100644 --- a/testing/vim_connection.vim +++ b/testing/vim_connection.vim @@ -23,7 +23,7 @@ function! s:on_exit(id, status, event) endfunction function! s:on_notification(id, data, event) - if lsp#lspClient#is_error(a:data.response) + if langserver#client#is_error(a:data.response) echom 'lsp('.a:id.'):notification:notification error receieved for '.a:data.request.method else echom 'lsp('.a:id.'):notification:notification success receieved for '.a:data.request.method @@ -35,7 +35,7 @@ function! s:on_notification1(id, data, event) endfunction " go get github.com/sourcegraph/go-langserver/langserver/cmd/langserver-go -let g:lsp_id = lsp#lspClient#start({ +let g:lsp_id = langserver#client#start({ \ 'cmd': [s:langserver_executabe, '-trace', '-logfile', expand('~/Desktop/langserver-go.log')], \ 'on_stderr': function('s:on_stderr'), \ 'on_exit': function('s:on_exit'), @@ -44,7 +44,7 @@ let g:lsp_id = lsp#lspClient#start({ if g:lsp_id > 0 echom 'lsp server running' - call lsp#lspClient#send(g:lsp_id, { + call langserver#client#send(g:lsp_id, { \ 'method': 'initialize', \ 'params': { \ 'capabilities': {}, @@ -88,7 +88,7 @@ function! s:on_definition_request(id, data, event) endfunction function! SendDefinitionRequest() abort - call lsp#lspClient#send(g:lsp_id, { + call langserver#client#send(g:lsp_id, { \ 'method': 'textDocument/definition', \ 'params': { \ 'textDocument': langserver#util#get_text_document_identifier(s:langserver_name), @@ -99,4 +99,4 @@ function! SendDefinitionRequest() abort \ }) endfunction -" call lsp#lspClient#stop(g:lsp_id) +" call langserver#client#stop(g:lsp_id)