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

Commit

Permalink
Start working on ability for server to send commands
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ DeVries committed Nov 18, 2016
1 parent cf7cf98 commit 9eb9c8d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 29 deletions.
68 changes: 41 additions & 27 deletions autoload/langserver/callbacks.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,59 @@ function! langserver#callbacks#on_stdout(id, data, event) abort
endfunction

function! langserver#callbacks#on_stderr(id, data, event) abort
echom 'stderr ...'
call langserver#log#response(a:id, a:data, a:event)

echom string(a:data)
echom '...stderr'
endfunction

function! langserver#callbacks#on_exit(id, status, event) abort
echom 'lsp('.a:id.'):exit:'.a:status
endfunction

function! langserver#callbacks#on_notification(id, data, event) abort
if a:event ==? 'on_request'
call langserver#extension#command#callback(a:id, a:data, a:event)
elseif a:event ==? 'on_notification'
if has_key(a:data, 'response')
call langserver#log#response(a:id, a:data, a:event)

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)
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
" if langserver#extension#command#callback(a:id, a:data, a:event)
" call langserver#log#log('debug', 'Handled: ' . l:last_topic)
" else
call langserver#log#log('warning', 'LAST REQUEST: ' . l:last_topic, v:true)
" endif
endif
elseif has_key(a:data, 'request')
echom 'notification...'
echom string(a:data)
echom '...notification'
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 a:data.request.method ==? 'textDocument/references'
echom 'LSP NOTIFI References Method matched: ' . string(a:data.request)
" call langserver#references#callback(a:data.request)
if langserver#client#is_error(a:data.response)
call langserver#log#log('debug',
\ 'lsp('.a:id.'):notification:notification error receieved for '.a:data.request.method,
\ v:true,
\ )
else
call langserver#log#log('debug',
\ 'lsp('.a:id.'):notification:notification success receieved for '.a:data.request.method,
\ langserver#util#debug(),
\ )
endif
endif
endfunction
Expand Down
18 changes: 16 additions & 2 deletions autoload/langserver/client.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ function! s:_on_lsp_stdout(id, data, event) abort
call l:client.on_notifications[l:response_msg.id](a:id, l:on_notification_data, 'on_notification')
endif
call remove(l:client.on_notifications, l:response_msg.id)
else
echom string(l:client)
let l:on_notification_data = {
\ 'request': l:response_msg,
\ }
call l:client.opts.on_notification(a:id, l:on_notification_data, 'on_request')
endif
continue
else
Expand Down Expand Up @@ -128,8 +134,12 @@ function! s:lsp_send_request(id, opts) abort " opts = { method, params?, on_noti
if has_key(s:lsp_clients, a:id)
let l:client = s:lsp_clients[a:id]

let l:client.req_seq = l:client.req_seq + 1
let l:req_seq = l:client.req_seq
if has_key(a:opts, 'req_id')
let l:req_seq = a:opts.req_id
else
let l:client.req_seq = l:client.req_seq + 1
let l:req_seq = l:client.req_seq
endif

let l:msg = { 'jsonrpc': '2.0', 'id': l:req_seq, 'method': a:opts.method }
if has_key(a:opts, 'params')
Expand All @@ -148,6 +158,10 @@ function! s:lsp_send_request(id, opts) abort " opts = { method, params?, on_noti
let l:client.opts.on_stderr = a:opts.on_stderr
endif

call langserver#log#log('debug', printf('Sending request: %s, %s',
\ a:id,
\ string(l:msg)
\ ))
call langserver#job#send(l:client.id, l:req_data)

return l:req_seq
Expand Down
29 changes: 29 additions & 0 deletions autoload/langserver/extension/command.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Command handler
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

function! langserver#extension#command#callback(id, data, event) abort
call langserver#log#server_request(a:id, a:data, a:event)

if type(a:data) != type({})
return
endif

let l:method = a:data.request.method

if l:method ==? 'fs/readFile'
let l:response = langserver#extension#fs#readFille(a:data.request.params)
else
call langserver#log#log('warning', 'No matching callback found for: ' . l:method)
return v:false
endif

call langserver#client#send(a:id, {
\ 'req_id': a:data.request.id,
\ 'method': 'fs/readFile',
\ 'params': {
\ 'result': l:response,
\ },
\ })
return v:true
endfunction
11 changes: 11 additions & 0 deletions autoload/langserver/extension/fs.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Extension to handle "fl/#" commands
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

""
" Respond with the full file reading
function! langserver#extension#fs#readFille(filename) abort
echom a:filename

return join(readfile(a:filename), "\n")
endfunction
11 changes: 11 additions & 0 deletions autoload/langserver/log.vim
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ function! langserver#log#callback(id, data, event) abort
\))
endfunction

""
" Log a request from the server
function! langserver#log#server_request(id, data, event) abort
call langserver#log#log('info',
\ printf('(%3s:%15s): %s',
\ a:id,
\ a:event,
\ 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([
Expand Down

0 comments on commit 9eb9c8d

Please sign in to comment.