Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

Commit

Permalink
Update callbacks and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ DeVries committed Nov 18, 2016
1 parent ceb48c6 commit 41b95c8
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 59 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
\ },
\ }
```

Expand Down
77 changes: 31 additions & 46 deletions autoload/langserver/callbacks.vim
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions autoload/langserver/default.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion autoload/langserver/goto.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions autoload/langserver/hover.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions autoload/langserver/log.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion autoload/langserver/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions testing/vim_connection.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'),
Expand All @@ -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': {},
Expand Down Expand Up @@ -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),
Expand All @@ -99,4 +99,4 @@ function! SendDefinitionRequest() abort
\ })
endfunction

" call lsp#lspClient#stop(g:lsp_id)
" call langserver#client#stop(g:lsp_id)

0 comments on commit 41b95c8

Please sign in to comment.