2024-01-19 22:17:55 +01:00
|
|
|
local copilot = {}
|
|
|
|
|
2024-11-23 16:01:51 +01:00
|
|
|
local showDocument = function(err, result, ctx, _)
|
|
|
|
local fallback = vim.lsp.handlers['window/showDocument']
|
|
|
|
if not fallback or (result.external and vim.g.copilot_browser) then
|
|
|
|
return vim.fn['copilot#handlers#window_showDocument'](result)
|
|
|
|
else
|
|
|
|
return fallback(err, result, ctx, _)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
copilot.lsp_start_client = function(cmd, handler_names, opts, settings)
|
|
|
|
local handlers = {['window/showDocument'] = showDocument}
|
2024-01-19 22:17:55 +01:00
|
|
|
local id
|
|
|
|
for _, name in ipairs(handler_names) do
|
2024-11-23 16:01:51 +01:00
|
|
|
handlers[name] = function(err, result, ctx, _)
|
2024-01-19 22:17:55 +01:00
|
|
|
if result then
|
2024-11-23 16:01:51 +01:00
|
|
|
local retval = vim.call('copilot#client#LspHandle', id, { method = name, params = result })
|
2024-01-19 22:17:55 +01:00
|
|
|
if type(retval) == 'table' then
|
|
|
|
return retval.result, retval.error
|
2024-11-23 16:01:51 +01:00
|
|
|
elseif vim.lsp.handlers[name] then
|
|
|
|
return vim.lsp.handlers[name](err, result, ctx, _)
|
2024-01-19 22:17:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-11-23 16:01:51 +01:00
|
|
|
end
|
|
|
|
local workspace_folders = opts.workspaceFolders
|
|
|
|
if #workspace_folders == 0 then
|
|
|
|
workspace_folders = nil
|
2024-01-19 22:17:55 +01:00
|
|
|
end
|
|
|
|
id = vim.lsp.start_client({
|
|
|
|
cmd = cmd,
|
|
|
|
cmd_cwd = vim.call('copilot#job#Cwd'),
|
2024-11-23 16:01:51 +01:00
|
|
|
name = 'GitHub Copilot',
|
|
|
|
init_options = opts.initializationOptions,
|
|
|
|
workspace_folders = workspace_folders,
|
|
|
|
settings = settings,
|
2024-01-19 22:17:55 +01:00
|
|
|
handlers = handlers,
|
|
|
|
on_init = function(client, initialize_result)
|
2024-11-23 16:01:51 +01:00
|
|
|
vim.call('copilot#client#LspInit', client.id, initialize_result)
|
|
|
|
if vim.fn.has('nvim-0.8') == 0 then
|
|
|
|
client.notify('workspace/didChangeConfiguration', { settings = settings })
|
|
|
|
end
|
2024-01-19 22:17:55 +01:00
|
|
|
end,
|
|
|
|
on_exit = function(code, signal, client_id)
|
|
|
|
vim.schedule(function()
|
2024-11-23 16:01:51 +01:00
|
|
|
vim.call('copilot#client#LspExit', client_id, code, signal)
|
2024-01-19 22:17:55 +01:00
|
|
|
end)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
return id
|
|
|
|
end
|
|
|
|
|
2024-11-23 16:01:51 +01:00
|
|
|
copilot.lsp_request = function(client_id, method, params, bufnr)
|
2024-01-19 22:17:55 +01:00
|
|
|
local client = vim.lsp.get_client_by_id(client_id)
|
|
|
|
if not client then
|
|
|
|
return
|
|
|
|
end
|
2024-11-23 16:01:51 +01:00
|
|
|
if bufnr == vim.NIL then
|
|
|
|
bufnr = nil
|
2024-01-19 22:17:55 +01:00
|
|
|
end
|
|
|
|
local _, id
|
|
|
|
_, id = client.request(method, params, function(err, result)
|
2024-11-23 16:01:51 +01:00
|
|
|
vim.call('copilot#client#LspResponse', client_id, { id = id, error = err, result = result })
|
|
|
|
end, bufnr)
|
2024-01-19 22:17:55 +01:00
|
|
|
return id
|
|
|
|
end
|
|
|
|
|
|
|
|
copilot.rpc_request = function(client_id, method, params)
|
|
|
|
local client = vim.lsp.get_client_by_id(client_id)
|
|
|
|
if not client then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local _, id
|
|
|
|
_, id = client.rpc.request(method, params, function(err, result)
|
2024-11-23 16:01:51 +01:00
|
|
|
vim.call('copilot#client#LspResponse', client_id, { id = id, error = err, result = result })
|
2024-01-19 22:17:55 +01:00
|
|
|
end)
|
|
|
|
return id
|
|
|
|
end
|
|
|
|
|
|
|
|
copilot.rpc_notify = function(client_id, method, params)
|
|
|
|
local client = vim.lsp.get_client_by_id(client_id)
|
|
|
|
if not client then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return client.rpc.notify(method, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
return copilot
|