Allow workspace_config to be a function (#1400)

It is sometimes useful to generate `workspace_config` using a callback.
For example, if the produced config needs to include some information
from the actual workspace.
This commit is contained in:
Illia Bobyr
2023-04-03 17:16:42 -07:00
committed by GitHub
parent e82e73a5eb
commit 44608334f9
4 changed files with 218 additions and 5 deletions

View File

@@ -716,7 +716,7 @@ function! s:ensure_conf(buf, server_name, cb) abort
call s:send_notification(a:server_name, {
\ 'method': 'workspace/didChangeConfiguration',
\ 'params': {
\ 'settings': l:server_info['workspace_config'],
\ 'settings': lsp#utils#workspace_config#get(a:server_name),
\ }
\ })
endif
@@ -934,7 +934,8 @@ function! s:on_request(server_name, id, request) abort
call lsp#utils#workspace_edit#apply_workspace_edit(a:request['params']['edit'])
call s:send_response(a:server_name, { 'id': a:request['id'], 'result': { 'applied': v:true } })
elseif a:request['method'] ==# 'workspace/configuration'
let l:response_items = map(a:request['params']['items'], { key, val -> lsp#utils#workspace_config#get_value(a:server_name, val) })
let l:config = lsp#utils#workspace_config#get(a:server_name)
let l:response_items = map(a:request['params']['items'], { key, val -> lsp#utils#workspace_config#projection(l:config, val) })
call s:send_response(a:server_name, { 'id': a:request['id'], 'result': l:response_items })
elseif a:request['method'] ==# 'workspace/workspaceFolders'
let l:server_info = s:servers[a:server_name]['server_info']
@@ -1309,6 +1310,13 @@ function! lsp#update_workspace_config(server_name, workspace_config) abort
let l:server = s:servers[a:server_name]
let l:server_info = l:server['server_info']
if has_key(l:server_info, 'workspace_config')
if type(l:server_info['workspace_config']) == v:t_func
call lsp#utils#error('''workspace_config'' is a function, so
\ lsp#update_workspace_config() can not be used. Either
\ replace function with a dictionary, or adjust the value
\ generated by the function as necessary.')
return
endif
call s:merge_dict(l:server_info['workspace_config'], a:workspace_config)
else
let l:server_info['workspace_config'] = a:workspace_config