From 248b081b2f7ed0479f190512f3cb03ed5a2b9b01 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Sat, 26 Nov 2016 19:06:15 -0500 Subject: [PATCH] Improve on preconfigured callbacks --- autoload/langserver/callbacks.vim | 33 ++++++++++++++++++++++++++++--- autoload/langserver/default.vim | 15 +++++++++----- autoload/langserver/log.vim | 8 ++++---- tests/test_defaults.vader | 13 ++++++++++++ 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/autoload/langserver/callbacks.vim b/autoload/langserver/callbacks.vim index 05ccde7..d8a0f2f 100644 --- a/autoload/langserver/callbacks.vim +++ b/autoload/langserver/callbacks.vim @@ -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 @@ -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) @@ -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...' @@ -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 diff --git a/autoload/langserver/default.vim b/autoload/langserver/default.vim index 00360e6..14e2959 100644 --- a/autoload/langserver/default.vim +++ b/autoload/langserver/default.vim @@ -34,11 +34,15 @@ 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'] @@ -46,6 +50,7 @@ function! langserver#default#extension_callbacks(...) abort 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 {} diff --git a/autoload/langserver/log.vim b/autoload/langserver/log.vim index 91c9676..06450b9 100644 --- a/autoload/langserver/log.vim +++ b/autoload/langserver/log.vim @@ -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 diff --git a/tests/test_defaults.vader b/tests/test_defaults.vader index 2888da6..12385ec 100644 --- a/tests/test_defaults.vader +++ b/tests/test_defaults.vader @@ -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