Allow initialization options to be passed to language servers

This commit is contained in:
Ben Falconer 2018-06-06 16:58:49 +01:00
parent c49ea1a5e3
commit 1a4b08539b
6 changed files with 75 additions and 8 deletions

View File

@ -227,6 +227,21 @@ function! ale#linter#PreProcess(linter) abort
throw '`completion_filter` must be a callback' throw '`completion_filter` must be a callback'
endif endif
endif endif
if has_key(a:linter, 'initialization_options_callback')
if has_key(a:linter, 'initialization_options')
throw 'Only one of `initialization_options` or '
\ . '`initialization_options_callback` should be set'
endif
let l:obj.initialization_options_callback = a:linter.initialization_options_callback
if !s:IsCallback(l:obj.initialization_options_callback)
throw '`initialization_options_callback` must be a callback if defined'
endif
elseif has_key(a:linter, 'initialization_options')
let l:obj.initialization_options = a:linter.initialization_options
endif
endif endif
let l:obj.output_stream = get(a:linter, 'output_stream', 'stdout') let l:obj.output_stream = get(a:linter, 'output_stream', 'stdout')
@ -451,12 +466,20 @@ function! ale#linter#StartLSP(buffer, linter, callback) abort
return {} return {}
endif endif
let l:initialization_options = {}
if has_key(a:linter, 'initialization_options_callback')
let l:initialization_options = ale#util#GetFunction(a:linter.initialization_options_callback)(a:buffer)
elseif has_key(a:linter, 'initialization_options')
let l:initialization_options = a:linter.initialization_options
endif
if a:linter.lsp is# 'socket' if a:linter.lsp is# 'socket'
let l:address = ale#linter#GetAddress(a:buffer, a:linter) let l:address = ale#linter#GetAddress(a:buffer, a:linter)
let l:conn_id = ale#lsp#ConnectToAddress( let l:conn_id = ale#lsp#ConnectToAddress(
\ l:address, \ l:address,
\ l:root, \ l:root,
\ a:callback, \ a:callback,
\ l:initialization_options,
\) \)
else else
let l:executable = ale#linter#GetExecutable(a:buffer, a:linter) let l:executable = ale#linter#GetExecutable(a:buffer, a:linter)
@ -474,6 +497,7 @@ function! ale#linter#StartLSP(buffer, linter, callback) abort
\ l:command, \ l:command,
\ l:root, \ l:root,
\ a:callback, \ a:callback,
\ l:initialization_options,
\) \)
endif endif

View File

@ -6,7 +6,7 @@
let s:connections = [] let s:connections = []
let g:ale_lsp_next_message_id = 1 let g:ale_lsp_next_message_id = 1
function! s:NewConnection() abort function! s:NewConnection(initialization_options) abort
" id: The job ID as a Number, or the server address as a string. " id: The job ID as a Number, or the server address as a string.
" data: The message data received so far. " data: The message data received so far.
" executable: An executable only set for program connections. " executable: An executable only set for program connections.
@ -18,6 +18,7 @@ function! s:NewConnection() abort
\ 'projects': {}, \ 'projects': {},
\ 'open_documents': [], \ 'open_documents': [],
\ 'callback_list': [], \ 'callback_list': [],
\ 'initialization_options': a:initialization_options,
\} \}
call add(s:connections, l:conn) call add(s:connections, l:conn)
@ -271,7 +272,7 @@ endfunction
" "
" The job ID will be returned for for the program if it ran, otherwise " The job ID will be returned for for the program if it ran, otherwise
" 0 will be returned. " 0 will be returned.
function! ale#lsp#StartProgram(executable, command, project_root, callback) abort function! ale#lsp#StartProgram(executable, command, project_root, callback, initialization_options) abort
if !executable(a:executable) if !executable(a:executable)
return 0 return 0
endif endif
@ -279,7 +280,7 @@ function! ale#lsp#StartProgram(executable, command, project_root, callback) abor
let l:conn = s:FindConnection('executable', a:executable) let l:conn = s:FindConnection('executable', a:executable)
" Get the current connection or a new one. " Get the current connection or a new one.
let l:conn = !empty(l:conn) ? l:conn : s:NewConnection() let l:conn = !empty(l:conn) ? l:conn : s:NewConnection(a:initialization_options)
let l:conn.executable = a:executable let l:conn.executable = a:executable
if !has_key(l:conn, 'id') || !ale#job#IsRunning(l:conn.id) if !has_key(l:conn, 'id') || !ale#job#IsRunning(l:conn.id)
@ -305,10 +306,10 @@ function! ale#lsp#StartProgram(executable, command, project_root, callback) abor
endfunction endfunction
" Connect to an address and set up a callback for handling responses. " Connect to an address and set up a callback for handling responses.
function! ale#lsp#ConnectToAddress(address, project_root, callback) abort function! ale#lsp#ConnectToAddress(address, project_root, callback, initialization_options) abort
let l:conn = s:FindConnection('id', a:address) let l:conn = s:FindConnection('id', a:address)
" Get the current connection or a new one. " Get the current connection or a new one.
let l:conn = !empty(l:conn) ? l:conn : s:NewConnection() let l:conn = !empty(l:conn) ? l:conn : s:NewConnection(a:initialization_options)
if !has_key(l:conn, 'channel') || ch_status(l:conn.channel) isnot# 'open' if !has_key(l:conn, 'channel') || ch_status(l:conn.channel) isnot# 'open'
let l:conn.channnel = ch_open(a:address, { let l:conn.channnel = ch_open(a:address, {
@ -383,7 +384,7 @@ function! ale#lsp#Send(conn_id, message, ...) abort
" Only send the init message once. " Only send the init message once.
if !l:project.init_request_id if !l:project.init_request_id
let [l:init_id, l:init_data] = ale#lsp#CreateMessageData( let [l:init_id, l:init_data] = ale#lsp#CreateMessageData(
\ ale#lsp#message#Initialize(l:project_root), \ ale#lsp#message#Initialize(l:project_root, l:conn.initialization_options),
\) \)
let l:project.init_request_id = l:init_id let l:project.init_request_id = l:init_id

View File

@ -24,12 +24,13 @@ function! ale#lsp#message#GetNextVersionID() abort
return l:id return l:id
endfunction endfunction
function! ale#lsp#message#Initialize(root_path) abort function! ale#lsp#message#Initialize(root_path, initialization_options) abort
" TODO: Define needed capabilities. " TODO: Define needed capabilities.
return [0, 'initialize', { return [0, 'initialize', {
\ 'processId': getpid(), \ 'processId': getpid(),
\ 'rootPath': a:root_path, \ 'rootPath': a:root_path,
\ 'capabilities': {}, \ 'capabilities': {},
\ 'initializationOptions': a:initialization_options,
\}] \}]
endfunction endfunction

View File

@ -2331,6 +2331,10 @@ ale#linter#Define(filetype, linter) *ale#linter#Define()*
An optional `completion_filter` callback may be An optional `completion_filter` callback may be
defined for filtering completion results. defined for filtering completion results.
An optional `initialization_options` or
`initialization_options_callback` may be defined to
pass initialization options to the LSP.
`project_root_callback` A |String| or |Funcref| for a callback function `project_root_callback` A |String| or |Funcref| for a callback function
accepting a buffer number. A |String| should be accepting a buffer number. A |String| should be
returned representing the path to the project for the returned representing the path to the project for the
@ -2372,6 +2376,17 @@ ale#linter#Define(filetype, linter) *ale#linter#Define()*
setting can make it easier to guess the linter name setting can make it easier to guess the linter name
by offering a few alternatives. by offering a few alternatives.
`initialization_options` A |Dictionary| of initialization options for LSPs.
This will be fed (as JSON) to the LSP in the
initialize command.
`initialization_options_callback`
A |String| or |Funcref| for a callback function
accepting a buffer number. A |Dictionary| should be
returned for initialization options to pass the LSP.
This can be used in place of `initialization_options`
when more complicated processing is needed.
Only one of `command`, `command_callback`, or `command_chain` should be Only one of `command`, `command_callback`, or `command_chain` should be
specified. `command_callback` is generally recommended when a command string specified. `command_callback` is generally recommended when a command string
needs to be generated dynamically, or any global options are used. needs to be generated dynamically, or any global options are used.

View File

@ -16,9 +16,10 @@ Execute(ale#lsp#message#Initialize() should return correct messages):
\ 'processId': getpid(), \ 'processId': getpid(),
\ 'rootPath': '/foo/bar', \ 'rootPath': '/foo/bar',
\ 'capabilities': {}, \ 'capabilities': {},
\ 'initializationOptions': {'foo': 'bar'},
\ } \ }
\ ], \ ],
\ ale#lsp#message#Initialize('/foo/bar') \ ale#lsp#message#Initialize('/foo/bar', {'foo': 'bar'})
Execute(ale#lsp#message#Initialized() should return correct messages): Execute(ale#lsp#message#Initialized() should return correct messages):
AssertEqual [1, 'initialized'], ale#lsp#message#Initialized() AssertEqual [1, 'initialized'], ale#lsp#message#Initialized()

View File

@ -465,3 +465,28 @@ Execute(PreProcess should complain about address_callback for non-LSP linters):
AssertThrows call ale#linter#PreProcess(g:linter) AssertThrows call ale#linter#PreProcess(g:linter)
AssertEqual '`address_callback` cannot be used when lsp != ''socket''', g:vader_exception AssertEqual '`address_callback` cannot be used when lsp != ''socket''', g:vader_exception
Execute(PreProcess should complain about using initialization_options and initialization_options_callback together):
let g:linter = {
\ 'name': 'x',
\ 'lsp': 'socket',
\ 'address_callback': 'X',
\ 'language': 'x',
\ 'project_root_callback': 'x',
\ 'initialization_options': 'x',
\ 'initialization_options_callback': 'x',
\}
AssertThrows call ale#linter#PreProcess(g:linter)
AssertEqual 'Only one of `initialization_options` or `initialization_options_callback` should be set', g:vader_exception
Execute (PreProcess should throw when initialization_options_callback is not a callback):
AssertThrows call ale#linter#PreProcess({
\ 'name': 'foo',
\ 'lsp': 'socket',
\ 'address_callback': 'X',
\ 'language': 'x',
\ 'project_root_callback': 'x',
\ 'initialization_options_callback': {},
\})
AssertEqual '`initialization_options_callback` must be a callback if defined', g:vader_exception