From 8bf321c68aedc0f348fcc647888ce055d9b83b08 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Fri, 25 Nov 2016 23:34:48 -0500 Subject: [PATCH] Add a way to preconfigure callbacks for lang servers --- autoload/langserver/default.vim | 24 ++++++++++++++++++- .../ocaml-language-server/callbacks.vim | 20 ++++++++++++++++ .../ocaml-language-server/settings.json} | 0 preconfigured/test/test/callbacks.vim | 9 +++++++ tests/test_defaults.vader | 20 +++++++++++++++- 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 preconfigured/freebroccolo/ocaml-language-server/callbacks.vim rename preconfigured/{freebroccolo_ocaml-language-server.json => freebroccolo/ocaml-language-server/settings.json} (100%) create mode 100644 preconfigured/test/test/callbacks.vim diff --git a/autoload/langserver/default.vim b/autoload/langserver/default.vim index a799a88..00360e6 100644 --- a/autoload/langserver/default.vim +++ b/autoload/langserver/default.vim @@ -1,4 +1,6 @@ -let s:langserver_executabe = 'langserver-go' +let s:preconfigured_location = expand(':h') . '/../../preconfigured/' +let s:callbacks_name = '/callbacks.vim' + "" " Get the default command for starting the server @@ -30,3 +32,23 @@ function! langserver#default#cmd(...) abort echoerr 'Please consult the documentation for how to configure the langserver' return l:bad_cmd 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 + + 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') + return Preconfigured_{l:subbed}() + else + return {} + endif + endif +endfunction diff --git a/preconfigured/freebroccolo/ocaml-language-server/callbacks.vim b/preconfigured/freebroccolo/ocaml-language-server/callbacks.vim new file mode 100644 index 0000000..4ff7196 --- /dev/null +++ b/preconfigured/freebroccolo/ocaml-language-server/callbacks.vim @@ -0,0 +1,20 @@ +function! s:give_word_at_position(context) abort + " 'params': + " {'uri': 'file:///home/tj/Downloads/example-ocaml-merlin/src/main.ml', + " 'position': {'character': 9, 'line': 6} + " } + if !has_key(a:context, 'uri') || !has_key(a:context, 'position') + " TODO: Log an error? + return + endif + + let l:loc_bufnr = bufnr(langserver#util#get_filename(langserver#util#get_lsp_id(), a:context.uri)) + let l:loc_line = langserver#util#get_line(l:loc_bufnr, a:context.uri, a.position.line - 1) + + call langserver#log#log('error', 'LOC LINE: ' . l:loc_line, v:true) +endfunction + + +let s:register_callbacks = { + \ 'reason.client.giveWordAtPosition': function('s:give_word_at_position'), + \ } diff --git a/preconfigured/freebroccolo_ocaml-language-server.json b/preconfigured/freebroccolo/ocaml-language-server/settings.json similarity index 100% rename from preconfigured/freebroccolo_ocaml-language-server.json rename to preconfigured/freebroccolo/ocaml-language-server/settings.json diff --git a/preconfigured/test/test/callbacks.vim b/preconfigured/test/test/callbacks.vim new file mode 100644 index 0000000..452c1b8 --- /dev/null +++ b/preconfigured/test/test/callbacks.vim @@ -0,0 +1,9 @@ +function! s:test_function(context) abort + return 'tested ' . a:context['var'] . '!' +endfunction + +function! Preconfigured_test_test() abort + return { + \ 'method_name': function('s:test_function'), + \ } +endfunction diff --git a/tests/test_defaults.vader b/tests/test_defaults.vader index 565d4cb..2888da6 100644 --- a/tests/test_defaults.vader +++ b/tests/test_defaults.vader @@ -53,7 +53,7 @@ Execute (Testing executable function as cmd with cs list): let g:langserver_executables = { \ 'example,this,that,the_other': { - \ 'name': 'sourcegraph/langserver-go', + \ 'name': 'this/langserver', \ 'cmd': function('s:set_global_var'), \ }, \ } @@ -63,4 +63,22 @@ Execute (Testing executable function as cmd with cs list): let g:langserver_executables = g:__temp_exec +Execute (Getting predefined settings): + " Pass + +Execute (Getting predefined functions and passing args): + 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('testing') + AssertEqual 'tested foo!', call(test_functions['method_name'], [{'var': 'foo'}]) + + let g:langserver_executables = g:__temp_exec +