Skip to content

Commit d93f32b

Browse files
committed
refactor: split out files further
1 parent 397a38d commit d93f32b

File tree

13 files changed

+469
-444
lines changed

13 files changed

+469
-444
lines changed

lua/copilot/api/init.lua

Lines changed: 31 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
local logger = require("copilot.logger")
2-
local mod = {}
2+
---@class CopilotApi
3+
local M = {}
34

45
---@param callback? fun(err: any|nil, data: table, ctx: table): nil
56
---@return any|nil err
67
---@return any data
78
---@return table ctx
8-
function mod.request(client, method, params, callback)
9+
function M.request(client, method, params, callback)
910
logger.trace("api request:", method, params)
1011
-- hack to convert empty table to json object,
1112
-- empty table is convert to json array by default.
@@ -26,7 +27,7 @@ function mod.request(client, method, params, callback)
2627
end
2728

2829
---@return boolean sent
29-
function mod.notify(client, method, params)
30+
function M.notify(client, method, params)
3031
logger.trace("api notify:", method, params)
3132

3233
if vim.fn.has("nvim-0.11") == 1 then
@@ -50,15 +51,15 @@ end
5051
---@alias copilot_workspace_configurations { settings: copilot_workspace_configuration }
5152

5253
---@param params copilot_workspace_configurations
53-
function mod.notify_change_configuration(client, params)
54-
return mod.notify(client, "workspace/didChangeConfiguration", params)
54+
function M.notify_change_configuration(client, params)
55+
return M.notify(client, "workspace/didChangeConfiguration", params)
5556
end
5657

5758
---@alias copilot_nofify_set_trace_params { value: 'off'|'messages'|'verbose' }
5859

5960
---@param params copilot_nofify_set_trace_params
60-
function mod.notify_set_trace(client, params)
61-
return mod.notify(client, "$/setTrace", params)
61+
function M.notify_set_trace(client, params)
62+
return M.notify(client, "$/setTrace", params)
6263
end
6364

6465
---@alias copilot_check_status_params { options?: { localChecksOnly?: boolean } }
@@ -68,21 +69,21 @@ end
6869
---@return any|nil err
6970
---@return copilot_check_status_data data
7071
---@return table ctx
71-
function mod.check_status(client, params, callback)
72+
function M.check_status(client, params, callback)
7273
if type(params) == "function" then
7374
callback = params
7475
params = {}
7576
end
76-
return mod.request(client, "checkStatus", params or {}, callback)
77+
return M.request(client, "checkStatus", params or {}, callback)
7778
end
7879

7980
---@alias copilot_sign_in_initiate_data { verificationUri?: string, userCode?: string }
8081

8182
---@return any|nil err
8283
---@return copilot_sign_in_initiate_data data
8384
---@return table ctx
84-
function mod.sign_in_initiate(client, callback)
85-
return mod.request(client, "signInInitiate", {}, callback)
85+
function M.sign_in_initiate(client, callback)
86+
return M.request(client, "signInInitiate", {}, callback)
8687
end
8788

8889
---@alias copilot_sign_in_confirm_params { userId: string }
@@ -92,42 +93,42 @@ end
9293
---@return any|nil err
9394
---@return copilot_sign_in_confirm_data data
9495
---@return table ctx
95-
function mod.sign_in_confirm(client, params, callback)
96-
return mod.request(client, "signInConfirm", params, callback)
96+
function M.sign_in_confirm(client, params, callback)
97+
return M.request(client, "signInConfirm", params, callback)
9798
end
9899

99-
function mod.sign_out(client, callback)
100-
return mod.request(client, "signOut", {}, callback)
100+
function M.sign_out(client, callback)
101+
return M.request(client, "signOut", {}, callback)
101102
end
102103

103104
---@alias copilot_get_version_data { version: string }
104105

105106
---@return any|nil err
106107
---@return copilot_get_version_data data
107108
---@return table ctx
108-
function mod.get_version(client, callback)
109-
return mod.request(client, "getVersion", {}, callback)
109+
function M.get_version(client, callback)
110+
return M.request(client, "getVersion", {}, callback)
110111
end
111112

112113
---@alias copilot_notify_accepted_params { uuid: string, acceptedLength?: integer }
113114

114115
---@param params copilot_notify_accepted_params
115-
function mod.notify_accepted(client, params, callback)
116-
return mod.request(client, "notifyAccepted", params, callback)
116+
function M.notify_accepted(client, params, callback)
117+
return M.request(client, "notifyAccepted", params, callback)
117118
end
118119

119120
---@alias copilot_notify_rejected_params { uuids: string[] }
120121

121122
---@param params copilot_notify_rejected_params
122-
function mod.notify_rejected(client, params, callback)
123-
return mod.request(client, "notifyRejected", params, callback)
123+
function M.notify_rejected(client, params, callback)
124+
return M.request(client, "notifyRejected", params, callback)
124125
end
125126

126127
---@alias copilot_notify_shown_params { uuid: string }
127128

128129
---@param params copilot_notify_shown_params
129-
function mod.notify_shown(client, params, callback)
130-
return mod.request(client, "notifyShown", params, callback)
130+
function M.notify_shown(client, params, callback)
131+
return M.request(client, "notifyShown", params, callback)
131132
end
132133

133134
---@alias copilot_get_completions_data_completion { displayText: string, position: { character: integer, line: integer }, range: { ['end']: { character: integer, line: integer }, start: { character: integer, line: integer } }, text: string, uuid: string }
@@ -136,110 +137,22 @@ end
136137
---@return any|nil err
137138
---@return copilot_get_completions_data data
138139
---@return table ctx
139-
function mod.get_completions(client, params, callback)
140-
return mod.request(client, "getCompletions", params, callback)
140+
function M.get_completions(client, params, callback)
141+
return M.request(client, "getCompletions", params, callback)
141142
end
142143

143-
function mod.get_completions_cycling(client, params, callback)
144-
return mod.request(client, "getCompletionsCycling", params, callback)
144+
function M.get_completions_cycling(client, params, callback)
145+
return M.request(client, "getCompletionsCycling", params, callback)
145146
end
146147

147-
---@alias copilot_panel_solution_data { panelId: string, completionText: string, displayText: string, range: { ['end']: { character: integer, line: integer }, start: { character: integer, line: integer } }, score: number, solutionId: string }
148-
---@alias copilot_panel_on_solution_handler fun(result: copilot_panel_solution_data): nil
149-
---@alias copilot_panel_solutions_done_data { panelId: string, status: 'OK'|'Error', message?: string }
150-
---@alias copilot_panel_on_solutions_done_handler fun(result: copilot_panel_solutions_done_data): nil
151-
152148
---@return any|nil err
153149
---@return integer data
154150
---@return table ctx
155-
function mod.get_panel_completions(client, params, callback)
156-
return mod.request(client, "getPanelCompletions", params, callback)
157-
end
158-
159-
local panel = {
160-
callback = {
161-
PanelSolution = {},
162-
PanelSolutionsDone = {},
163-
},
164-
}
165-
166-
panel.handlers = {
167-
---@param result copilot_panel_solution_data
168-
PanelSolution = function(_, result)
169-
if panel.callback.PanelSolution[result.panelId] then
170-
panel.callback.PanelSolution[result.panelId](result)
171-
end
172-
end,
173-
174-
---@param result copilot_panel_solutions_done_data
175-
PanelSolutionsDone = function(_, result)
176-
if panel.callback.PanelSolutionsDone[result.panelId] then
177-
panel.callback.PanelSolutionsDone[result.panelId](result)
178-
end
179-
end,
180-
}
181-
182-
---@param panelId string
183-
---@param handlers { on_solution: copilot_panel_on_solution_handler, on_solutions_done: copilot_panel_on_solutions_done_handler }
184-
function mod.register_panel_handlers(panelId, handlers)
185-
assert(type(panelId) == "string", "missing panelId")
186-
panel.callback.PanelSolution[panelId] = handlers.on_solution
187-
panel.callback.PanelSolutionsDone[panelId] = handlers.on_solutions_done
188-
end
189-
190-
---@param panelId string
191-
function mod.unregister_panel_handlers(panelId)
192-
assert(type(panelId) == "string", "missing panelId")
193-
panel.callback.PanelSolution[panelId] = nil
194-
panel.callback.PanelSolutionsDone[panelId] = nil
195-
end
196-
197-
---@alias copilot_status_notification_data { status: ''|'Normal'|'InProgress'|'Warning', message: string }
198-
199-
local status = {
200-
client_id = nil,
201-
---@type copilot_status_notification_data
202-
data = {
203-
status = "",
204-
message = "",
205-
},
206-
callback = {},
207-
}
208-
209-
status.handlers = {
210-
---@param result copilot_status_notification_data
211-
---@param ctx { client_id: integer, method: string }
212-
statusNotification = function(_, result, ctx)
213-
status.client_id = ctx.client_id
214-
status.data = result
215-
216-
for callback in pairs(status.callback) do
217-
callback(status.data)
218-
end
219-
end,
220-
}
221-
222-
---@param handler fun(data: copilot_status_notification_data): nil
223-
function mod.register_status_notification_handler(handler)
224-
status.callback[handler] = true
225-
handler(status.data)
226-
end
227-
228-
---@param handler fun(data: copilot_status_notification_data): nil
229-
function mod.unregister_status_notification_handler(handler)
230-
status.callback[handler] = nil
151+
function M.get_panel_completions(client, params, callback)
152+
return M.request(client, "getPanelCompletions", params, callback)
231153
end
232154

233155
---@alias copilot_window_show_document { uri: string, external?: boolean, takeFocus?: boolean, selection?: boolean }
234156
---@alias copilot_window_show_document_result { success: boolean }
235157

236-
mod.handlers = {
237-
PanelSolution = panel.handlers.PanelSolution,
238-
PanelSolutionsDone = panel.handlers.PanelSolutionsDone,
239-
statusNotification = status.handlers.statusNotification,
240-
}
241-
242-
mod.panel = panel
243-
mod.status = status
244-
245-
return mod
158+
return M

lua/copilot/client/config.lua

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
local api = require("copilot.api")
2+
local config = require("copilot.config")
3+
local util = require("copilot.util")
4+
local logger = require("copilot.logger")
5+
local lsp = require("copilot.lsp")
6+
local utils = require("copilot.client.utils")
7+
local M = {}
8+
9+
---@param overrides table<string, any>
10+
---@param client CopilotClient
11+
function M.prepare_client_config(overrides, client)
12+
if lsp.binary.initialization_failed then
13+
client.startup_error = "initialization of copilot-language-server failed"
14+
return
15+
end
16+
17+
client.startup_error = nil
18+
19+
local server_path = nil
20+
local cmd = nil
21+
22+
if config.server.custom_server_filepath and vim.fn.filereadable(config.server.custom_server_filepath) then
23+
server_path = config.server.custom_server_filepath
24+
end
25+
26+
if config.server.type == "nodejs" then
27+
cmd = {
28+
lsp.nodejs.node_command,
29+
server_path or lsp.nodejs.get_server_path(),
30+
"--stdio",
31+
}
32+
elseif config.server.type == "binary" then
33+
cmd = {
34+
server_path or lsp.binary.get_server_path(),
35+
"--stdio",
36+
}
37+
end
38+
39+
if not cmd then
40+
logger.error("copilot server type not supported")
41+
return
42+
end
43+
44+
local capabilities = vim.lsp.protocol.make_client_capabilities()
45+
capabilities.window.showDocument.support = true
46+
47+
capabilities.workspace = {
48+
workspaceFolders = true,
49+
}
50+
51+
local root_dir = utils.get_root_dir(config.root_dir)
52+
local workspace_folders = {
53+
--- @type workspace_folder
54+
{
55+
uri = vim.uri_from_fname(root_dir),
56+
-- important to keep root_dir as-is for the name as lsp.lua uses this to check the workspace has not changed
57+
name = root_dir,
58+
},
59+
}
60+
61+
local config_workspace_folders = config.workspace_folders
62+
63+
for _, config_workspace_folder in ipairs(config_workspace_folders) do
64+
if config_workspace_folder ~= "" then
65+
table.insert(
66+
workspace_folders,
67+
--- @type workspace_folder
68+
{
69+
uri = vim.uri_from_fname(config_workspace_folder),
70+
name = config_workspace_folder,
71+
}
72+
)
73+
end
74+
end
75+
76+
local editor_info = util.get_editor_info()
77+
local provider_url = config.auth_provider_url
78+
local proxy_uri = vim.g.copilot_proxy
79+
80+
local settings = { ---@type copilot_settings
81+
telemetry = { ---@type github_settings_telemetry
82+
telemetryLevel = "all",
83+
},
84+
}
85+
86+
if proxy_uri then
87+
vim.tbl_extend("force", settings, {
88+
http = { ---@type copilot_settings_http
89+
proxy = proxy_uri,
90+
proxyStrictSSL = vim.g.copilot_proxy_strict_ssl or false,
91+
proxyKerberosServicePrincipal = nil,
92+
},
93+
})
94+
end
95+
96+
if provider_url then
97+
vim.tbl_extend("force", settings, {
98+
["github-enterprise"] = { ---@type copilot_settings_github-enterprise
99+
uri = provider_url,
100+
},
101+
})
102+
end
103+
104+
-- LSP config, not to be confused with config.lua
105+
return vim.tbl_deep_extend("force", {
106+
cmd = cmd,
107+
root_dir = root_dir,
108+
name = "copilot",
109+
capabilities = capabilities,
110+
get_language_id = function(_, filetype)
111+
return require("copilot.client.filetypes").language_for_file_type(filetype)
112+
end,
113+
on_init = function(lsp_client, initialize_result)
114+
if client.id == lsp_client.id then
115+
client.capabilities = initialize_result.capabilities
116+
end
117+
118+
vim.schedule(function()
119+
local configurations = utils.get_workspace_configurations()
120+
api.notify_change_configuration(lsp_client, configurations)
121+
logger.trace("workspace configuration", configurations)
122+
123+
-- to activate tracing if we want it
124+
local logger_conf = config.logger
125+
local trace_params = { value = logger_conf.trace_lsp } --[[@as copilot_nofify_set_trace_params]]
126+
api.notify_set_trace(lsp_client, trace_params)
127+
128+
-- prevent requests to copilot prior to being initialized
129+
client.initialized = true
130+
end)
131+
end,
132+
on_exit = function(code, _, client_id)
133+
if client.id == client_id then
134+
vim.schedule(function()
135+
client.teardown()
136+
client.id = nil
137+
client.capabilities = nil
138+
end)
139+
end
140+
if code > 0 then
141+
vim.schedule(function()
142+
require("copilot.command").status()
143+
end)
144+
end
145+
end,
146+
handlers = require("copilot.client.handlers").get_handlers(),
147+
init_options = {
148+
editorInfo = editor_info.editorInfo,
149+
editorPluginInfo = editor_info.editorPluginInfo,
150+
},
151+
settings = settings,
152+
workspace_folders = workspace_folders,
153+
}, overrides)
154+
end
155+
156+
return M

0 commit comments

Comments
 (0)