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

Commit

Permalink
Add ability to specify command for executable
Browse files Browse the repository at this point in the history
Also added some more logging and debugging items.
Added some tests for the configuration dictionary.
Updated documentation
  • Loading branch information
TJ DeVries committed Nov 23, 2016
1 parent 6973ffc commit 3c94634
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 111 deletions.
8 changes: 8 additions & 0 deletions autoload/langserver/client.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 15 additions & 7 deletions autoload/langserver/default.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions autoload/langserver/initialize.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

""
Expand Down
203 changes: 102 additions & 101 deletions autoload/langserver/log.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,131 @@ 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

""
" Log a request from the server
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
Expand Down
8 changes: 8 additions & 0 deletions doc/langserver.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tests/test_defaults.vader
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit 3c94634

Please sign in to comment.