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

Commit

Permalink
Add a way to preconfigure callbacks for lang servers
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries committed Nov 26, 2016
1 parent 0d4af02 commit 8bf321c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
24 changes: 23 additions & 1 deletion autoload/langserver/default.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
let s:langserver_executabe = 'langserver-go'
let s:preconfigured_location = expand('<sfile>:h') . '/../../preconfigured/'
let s:callbacks_name = '/callbacks.vim'


""
" Get the default command for starting the server
Expand Down Expand Up @@ -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
20 changes: 20 additions & 0 deletions preconfigured/freebroccolo/ocaml-language-server/callbacks.vim
Original file line number Diff line number Diff line change
@@ -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'),
\ }
9 changes: 9 additions & 0 deletions preconfigured/test/test/callbacks.vim
Original file line number Diff line number Diff line change
@@ -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
20 changes: 19 additions & 1 deletion tests/test_defaults.vader
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
\ },
\ }
Expand All @@ -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


0 comments on commit 8bf321c

Please sign in to comment.