From 3c946346d5bafe5e1cf7c63c7e51ec9be40bef22 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Wed, 23 Nov 2016 13:44:35 -0500 Subject: [PATCH] Add ability to specify command for executable Also added some more logging and debugging items. Added some tests for the configuration dictionary. Updated documentation --- autoload/langserver/client.vim | 8 ++ autoload/langserver/default.vim | 22 +++- autoload/langserver/initialize.vim | 13 +- autoload/langserver/log.vim | 203 +++++++++++++++-------------- doc/langserver.txt | 8 ++ tests/test_defaults.vader | 46 +++++++ 6 files changed, 189 insertions(+), 111 deletions(-) diff --git a/autoload/langserver/client.vim b/autoload/langserver/client.vim index 0cdd4e1..2e64e54 100644 --- a/autoload/langserver/client.vim +++ b/autoload/langserver/client.vim @@ -190,6 +190,14 @@ function! s:lsp_get_last_request_id(id) abort endfunction function! s:lsp_is_error(notification) abort + " if type(a:notification) != type({}) + " return v:true + " endif + + " if !has_key(a:notification, 'response') || !has_key(a:notification.response, 'result') + " return v:true + " endif + return has_key(a:notification, 'error') endfunction diff --git a/autoload/langserver/default.vim b/autoload/langserver/default.vim index 2c381a0..a799a88 100644 --- a/autoload/langserver/default.vim +++ b/autoload/langserver/default.vim @@ -2,16 +2,24 @@ let s:langserver_executabe = 'langserver-go' "" " Get the default command for starting the server -function! langserver#default#cmd() abort +function! langserver#default#cmd(...) 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 + let l:bad_cmd = [-1] - if has_key(g:langserver_executables, &filetype) - let l:tmp_cmd = g:langserver_executables[&filetype]['cmd'] + if has_key(g:langserver_executables, l:filetype_key) + " Has to be uppercase because of function naming + " Sorry for mixed case :/ + let l:TmpCmd = g:langserver_executables[l:filetype_key]['cmd'] - if type(l:tmp_cmd) == type([]) - return l:tmp_cmd - elseif type(l:tmp_cmd) == type(function('tr')) - let l:result = l:tmp_cmd() + if type(l:TmpCmd) == type([]) + return l:TmpCmd + elseif type(l:TmpCmd) == type(function('tr')) + let l:result = l:TmpCmd() if type(l:result) == type([]) return l:result endif diff --git a/autoload/langserver/initialize.vim b/autoload/langserver/initialize.vim index 6104dd4..c4a2326 100644 --- a/autoload/langserver/initialize.vim +++ b/autoload/langserver/initialize.vim @@ -31,9 +31,16 @@ function! langserver#initialize#get_client_capabilities() abort endfunction function! langserver#initialize#callback(id, data, event) abort - call langserver#initialize#response(a:id, a:data.response.result) - - call langserver#log#log('info', 'Succesfully connected to: ' . string(a:id), v:true) + if langserver#client#is_error(a:data.response) + call langserver#log#log('error', + \ 'Could not connect to: ' . string(a:id) . "\n" . + \ 'Message was: ' . string(a:data), + \ v:true, + \ ) + else + call langserver#initialize#response(a:id, a:data.response.result) + call langserver#log#log('info', 'Succesfully connected to: ' . string(a:id), v:true) + endif endfunction "" diff --git a/autoload/langserver/log.vim b/autoload/langserver/log.vim index a0d0823..91c9676 100644 --- a/autoload/langserver/log.vim +++ b/autoload/langserver/log.vim @@ -2,119 +2,120 @@ let s:log_file = expand('~/langserver-vim.log') let s:current_level = 4 let s:log_level_map = { - \ 'error': 0, - \ 'warning': 1, - \ 'info': 2, - \ 'debug': 3, - \ 'micro': 4, - \ } + \ 'error': 0, + \ 'warning': 1, + \ 'info': 2, + \ 'debug': 3, + \ 'micro': 4, + \ } let s:clear_log = v:true "" " Logging helper function! langserver#log#log(level, message, ...) abort - if s:clear_log - call writefile([], s:log_file, '') - let s:clear_log = v:false - endif - - if a:0 > 0 - let l:echo_choice = a:1 - else - let l:echo_choice = v:false - endif - - let l:numeric_level = s:log_level_map[a:level] - - if type(a:message) == type('') - let l:msg = [a:message] - elseif type(a:message) == type({}) - let l:msg = [string(a:message)] - elseif type(a:message) != type([]) - " TODO: Handle other types of messages? - else - let l:msg = a:message - endif - - let l:final_msg = [] - for l:item in l:msg - call add(l:final_msg, printf('%5s: %s', - \ a:level, - \ l:item, - \ )) - endfor - - - if l:numeric_level < s:current_level - if l:echo_choice - echom string(l:final_msg) - endif - - call writefile(l:final_msg, s:log_file, 'a') - endif + if s:clear_log + call writefile([], s:log_file, '') + let s:clear_log = v:false + endif + + if a:0 > 0 + let l:echo_choice = a:1 + else + let l:echo_choice = v:false + endif + + let l:numeric_level = s:log_level_map[a:level] + + if type(a:message) == type('') + let l:msg = [a:message] + elseif type(a:message) == type({}) + let l:msg = [string(a:message)] + elseif type(a:message) != type([]) + " TODO: Handle other types of messages? + else + let l:msg = a:message + endif + + let l:final_msg = [] + for l:item in l:msg + call add(l:final_msg, printf('%5s: %s', + \ a:level, + \ l:item, + \ )) + endfor + + + if l:numeric_level < s:current_level + if l:echo_choice + echom string(l:final_msg) + endif + + call writefile(l:final_msg, s:log_file, 'a') + endif endfunction "" " Log response helper function! langserver#log#response(id, data, event) abort - let g:last_response = a:data - - if type(a:data) != type({}) - call langserver#log#log('debug', - \ printf('(%3s:%15s): %s', - \ a:id, - \ a:event, - \ string(a:data) - \ ), - \ langserver#util#debug(), - \ ) - return - endif - - if has_key(a:data, 'response') - call langserver#log#log('debug', - \ printf('(%3s:%15s): Response -> M(%20s), D(%s)', - \ a:id, - \ a:event, - \ string(a:data.request.method), - \ string(a:data.response.result), - \ ), - \ langserver#util#debug(), - \ ) - elseif has_key(a:data, 'request') - call langserver#log#log('debug', - \ printf('(%3s:%15s): Request -> M(%20s), D(%s)', - \ a:id, - \ a:event, - \ string(a:data.request.method), - \ string(a:data.request.params), - \ ), - \ langserver#util#debug(), - \ ) - else - call langserver#log#log('debug', - \ printf('(%3s:%15s): Unknown -> D(%s)', - \ a:id, - \ a:event, - \ string(a:data), - \ ), - \ langserver#util#debug(), - \ ) - endif + let g:last_response = a:data + + if type(a:data) != type({}) + call langserver#log#log('debug', + \ printf('(%3s:%15s): %s', + \ a:id, + \ a:event, + \ string(a:data) + \ ), + \ langserver#util#debug(), + \ ) + return + endif + + if has_key(a:data, 'response') && has_key(a:data, 'request') + \ && has_key(a:data.response, 'result') && has_key(a:data.request, 'method') + call langserver#log#log('debug', + \ printf('(%3s:%15s): Response -> M(%20s), D(%s)', + \ a:id, + \ a:event, + \ string(a:data.request.method), + \ string(a:data.response.result), + \ ), + \ langserver#util#debug(), + \ ) + elseif has_key(a:data, 'request') && type(a:data.request) == type({}) + call langserver#log#log('debug', + \ printf('(%3s:%15s): Request -> M(%20s), D(%s)', + \ a:id, + \ a:event, + \ string(a:data.request.method), + \ string(a:data.request.params), + \ ), + \ langserver#util#debug(), + \ ) + else + call langserver#log#log('debug', + \ printf('(%3s:%15s): Unknown -> D(%s)', + \ a:id, + \ a:event, + \ string(a:data), + \ ), + \ langserver#util#debug(), + \ ) + endif endfunction "" " Log only at debug level function! langserver#log#callback(id, data, event) abort - call langserver#log#log('debug', - \ printf('(%3s:%15s): %s', - \ a:id, - \ a:event, - \ string(a:data) - \ ), - \ v:false - \ ) + call langserver#log#log('debug', + \ printf('(%3s:%15s): %s', + \ a:id, + \ a:event, + \ string(a:data) + \ ), + \ v:false + \ ) endfunction "" @@ -122,10 +123,10 @@ endfunction 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) - \ )) + \ a:id, + \ a:event, + \ string(a:data) + \ )) endfunction function! langserver#log#pretty_print(json_dict) abort diff --git a/doc/langserver.txt b/doc/langserver.txt index 4a3f635..bb7d9fa 100644 --- a/doc/langserver.txt +++ b/doc/langserver.txt @@ -19,11 +19,19 @@ CONFIGURATION *langserver-configuration* Define a dictionary like this in your vimrc. > + function! GetMyJSCommand() abort + return ['executable', 'options'] + endfunction + let g:langserver_executables = { \ 'go': { \ 'name': 'sourcegraph/langserver-go', \ 'cmd': ['langserver-go', '-trace', '-logfile', expand('~/Desktop/langserver-go.log')], \ }, + \ 'javascript,typescript': { + \ 'name': 'myjsserver', + \ 'cmd': function('GetMyJSCommand'), + \ }, \ } TODO: Mappings diff --git a/tests/test_defaults.vader b/tests/test_defaults.vader index 5d73065..565d4cb 100644 --- a/tests/test_defaults.vader +++ b/tests/test_defaults.vader @@ -18,3 +18,49 @@ Execute (Testing executable configuration): AssertEqual 'javascript,typescript,jsx,tsx', langserver#util#get_executable_key('jsx') let g:langserver_executables = g:__temp_exec + + +Execute (Testing executable function as cmd): + let g:__temp_exec = get(g:, 'langserver_executables', {}) + + let g:__vader_test__global_var = 'not updated' + function! s:set_global_var() abort + let g:__vader_test__global_var = 'updated' + return ['executable', 'options'] + endfunction + + let g:langserver_executables = { + \ 'example': { + \ 'name': 'sourcegraph/langserver-go', + \ 'cmd': function('s:set_global_var'), + \ }, + \ } + + AssertEqual ['executable', 'options'], langserver#default#cmd('example') + AssertEqual g:__vader_test__global_var, 'updated' + + let g:langserver_executables = g:__temp_exec + + +Execute (Testing executable function as cmd with cs list): + let g:__temp_exec = get(g:, 'langserver_executables', {}) + + let g:__vader_test__global_var = 'not updated' + function! s:set_global_var() abort + let g:__vader_test__global_var = 'updated' + return ['executable', 'options'] + endfunction + + let g:langserver_executables = { + \ 'example,this,that,the_other': { + \ 'name': 'sourcegraph/langserver-go', + \ 'cmd': function('s:set_global_var'), + \ }, + \ } + + AssertEqual ['executable', 'options'], langserver#default#cmd('example') + AssertEqual g:__vader_test__global_var, 'updated' + + let g:langserver_executables = g:__temp_exec + +