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

Commit

Permalink
Improve on preconfigured callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries committed Nov 27, 2016
1 parent 4474a3a commit 248b081
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
33 changes: 30 additions & 3 deletions autoload/langserver/callbacks.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function! s:check_extra_callbacks(last_topic) abort
echom 'Checking custom callbacks'
let l:custom_callbacks = langserver#default#extension_callbacks()
echom 'Custom callbacks are: ' . string(l:custom_callbacks)
if has_key(l:custom_callbacks, a:last_topic)
call langserver#log#log('info', 'Calling custom callback for: ' . a:last_topic, v:true)
return l:custom_callbacks[a:last_topic]
else
call langserver#log#log('warning', 'No callback registered for: ' . a:last_topic, v:true)
return v:false
endif
endfunction

function! langserver#callbacks#on_stdout(id, data, event) abort
echom 'LSP STDOUT(' . a:id . '): ' . string(a:data)
endfunction
Expand All @@ -12,7 +25,15 @@ 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)
let l:last_topic = a:data['request']['method']

let l:ExtraCallbacks = s:check_extra_callbacks(l:last_topic)

if type(l:ExtraCallbacks) == type(function('tr'))
let l:result = call(l:ExtraCallbacks, [a:id, a:data, a:event])
else
call langserver#extension#command#callback(a:id, a:data, a:event)
endif
elseif a:event ==? 'on_notification'
if has_key(a:data, 'response')
call langserver#log#response(a:id, a:data, a:event)
Expand All @@ -32,7 +53,12 @@ function! langserver#callbacks#on_notification(id, data, event) abort
elseif l:last_topic ==? 'workspace/symbol'
call langserver#symbol#workspace#callback(a:id, a:data, a:event)
else
call langserver#log#log('warning', 'No callback registered for: ' . l:last_topic, v:true)
" Check if any extra callbacks exist.
let l:ExtraCallbacks = s:check_extra_callbacks(l:last_topic)

if type(l:ExtraCallbacks) == type(function('tr'))
let l:result = call(l:ExtraCallbacks, [a:id, a:data, a:event])
endif
endif
elseif has_key(a:data, 'request')
echom 'notification...'
Expand All @@ -42,7 +68,8 @@ function! langserver#callbacks#on_notification(id, data, event) abort

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,
\ 'lsp('.a:id.'):notification:notification error receieved for '.a:data.request.method .
\ ': ' . string(a:data),
\ v:true,
\ )
else
Expand Down
15 changes: 10 additions & 5 deletions autoload/langserver/default.vim
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,23 @@ function! langserver#default#cmd(...) abort
endfunction

function! langserver#default#extension_callbacks(...) abort
if a:0 > 0
let l:filetype_key = langserver#util#get_executable_key(a:1)
else
let l:filetype_key = langserver#util#get_executable_key(&filetype)
endif
try
if a:0 > 0
let l:filetype_key = langserver#util#get_executable_key(a:1)
else
let l:filetype_key = langserver#util#get_executable_key(&filetype)
endif
catch /.*Unsupported filetype.*/
return {}
endtry

if has_key(g:langserver_executables, l:filetype_key)
let l:location = s:preconfigured_location . g:langserver_executables[l:filetype_key]['name']
let l:file_to_source = l:location . s:callbacks_name
if isdirectory(l:location) && filereadable(l:file_to_source)
execute('source ' . l:file_to_source)
let l:subbed = substitute(g:langserver_executables[l:filetype_key]['name'], '/', '_', 'g')
let l:subbed = substitute(l:subbed, '-', '_', 'g')
return Preconfigured_{l:subbed}()
else
return {}
Expand Down
8 changes: 4 additions & 4 deletions autoload/langserver/log.vim
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ endfunction
function! langserver#log#callback(id, data, event) abort
call langserver#log#log('debug',
\ printf('(%3s:%15s): %s',
\ a:id,
\ a:event,
\ string(a:data)
\ ),
\ a:id,
\ a:event,
\ string(a:data)
\ ),
\ v:false
\ )
endfunction
Expand Down
13 changes: 13 additions & 0 deletions tests/test_defaults.vader
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ Execute (Getting predefined functions and passing args):

let g:langserver_executables = g:__temp_exec

Execute (Returns empty dictionary if unknown filetype):
let g:__temp_exec = get(g:, 'langserver_executables', {})

let g:langserver_executables = {
\ 'testing': {
\ 'name': 'test/test',
\ 'cmd': ['echo', '"hello"'],
\ },
\ }

let test_functions = langserver#default#extension_callbacks('foobar')
AssertEqual {}, test_functions

let g:langserver_executables = g:__temp_exec

0 comments on commit 248b081

Please sign in to comment.