Skip to content

Commit e69323a

Browse files
committed
refactor!: major rewrite of project structure
1 parent d661d65 commit e69323a

File tree

22 files changed

+352
-295
lines changed

22 files changed

+352
-295
lines changed
File renamed without changes.
File renamed without changes.

lua/copilot/client.lua renamed to lua/copilot/client/init.lua

Lines changed: 25 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,20 @@ local api = require("copilot.api")
22
local config = require("copilot.config")
33
local util = require("copilot.util")
44
local logger = require("copilot.logger")
5-
local lsp_binary = require("copilot.lsp_binary")
6-
local lsp_nodesj = require("copilot.lsp_nodejs")
5+
local lsp = require("copilot.lsp")
6+
local utils = require("copilot.client.utils")
77

88
local is_disabled = false
99

1010
local M = {
1111
augroup = "copilot.client",
1212
id = nil,
1313
--- @class copilot_capabilities:lsp.ClientCapabilities
14-
--- @field copilot table<'openURL', boolean>
1514
--- @field workspace table<'workspaceFolders', boolean>
1615
capabilities = nil,
1716
config = nil,
1817
startup_error = nil,
1918
initialized = false,
20-
---@type copilot_should_attach
21-
should_attach = nil,
22-
---@type string<'nodejs', 'binary'>
23-
---@class copilot_config_server
24-
server = {
25-
---@type string<'nodejs', 'binary'>
26-
type = "nodejs",
27-
---@type string|nil
28-
custom_server_filepath = nil,
29-
},
3019
}
3120

3221
---@param id integer
@@ -46,7 +35,7 @@ end
4635

4736
---@param force? boolean
4837
function M.buf_attach(force)
49-
if lsp_binary.initialization_failed then
38+
if lsp.binary.initialization_failed then
5039
M.startup_error = "initialization of copilot-language-server failed"
5140
return
5241
end
@@ -59,7 +48,7 @@ function M.buf_attach(force)
5948
local bufnr = vim.api.nvim_get_current_buf()
6049
local bufname = vim.api.nvim_buf_get_name(bufnr)
6150

62-
if not (force or (M.should_attach(bufnr, bufname) and util.should_attach())) then
51+
if not (force or (config.should_attach(bufnr, bufname) and util.should_attach())) then
6352
logger.debug("not attaching to buffer based on force and should_attach criteria")
6453
return
6554
end
@@ -70,7 +59,7 @@ function M.buf_attach(force)
7059
end
7160

7261
-- In case it has changed, we update it
73-
M.config.root_dir = config.get_root_dir()
62+
M.config.root_dir = utils.get_root_dir(config.root_dir)
7463

7564
local ok, client_id_or_err = pcall(vim.lsp.start, M.config)
7665
if not ok then
@@ -161,7 +150,7 @@ local function get_handlers()
161150
}
162151

163152
-- optional handlers
164-
local logger_conf = config.config.logger
153+
local logger_conf = config.logger
165154
if logger_conf.trace_lsp ~= "off" then
166155
handlers = vim.tbl_extend("force", handlers, {
167156
["$/logTrace"] = logger.handle_lsp_trace,
@@ -184,7 +173,7 @@ local function get_handlers()
184173
end
185174

186175
local function prepare_client_config(overrides)
187-
if lsp_binary.initialization_failed then
176+
if lsp.binary.initialization_failed then
188177
M.startup_error = "initialization of copilot-language-server failed"
189178
return
190179
end
@@ -194,19 +183,19 @@ local function prepare_client_config(overrides)
194183
local server_path = nil
195184
local cmd = nil
196185

197-
if M.server.custom_server_filepath and vim.fn.filereadable(M.server.custom_server_filepath) then
198-
server_path = M.server.custom_server_filepath
186+
if config.server.custom_server_filepath and vim.fn.filereadable(config.server.custom_server_filepath) then
187+
server_path = config.server.custom_server_filepath
199188
end
200189

201-
if M.server.type == "nodejs" then
190+
if config.server.type == "nodejs" then
202191
cmd = {
203-
lsp_nodesj.node_command,
204-
server_path or lsp_nodesj.get_server_path(),
192+
lsp.nodejs.node_command,
193+
server_path or lsp.nodejs.get_server_path(),
205194
"--stdio",
206195
}
207-
elseif M.server.type == "binary" then
196+
elseif config.server.type == "binary" then
208197
cmd = {
209-
server_path or lsp_binary.get_server_path(),
198+
server_path or lsp.binary.get_server_path(),
210199
"--stdio",
211200
}
212201
end
@@ -223,7 +212,7 @@ local function prepare_client_config(overrides)
223212
workspaceFolders = true,
224213
}
225214

226-
local root_dir = config.get_root_dir()
215+
local root_dir = utils.get_root_dir(config.root_dir)
227216
local workspace_folders = {
228217
--- @type workspace_folder
229218
{
@@ -233,7 +222,7 @@ local function prepare_client_config(overrides)
233222
},
234223
}
235224

236-
local config_workspace_folders = config.config.workspace_folders
225+
local config_workspace_folders = config.workspace_folders
237226

238227
for _, config_workspace_folder in ipairs(config_workspace_folders) do
239228
if config_workspace_folder ~= "" then
@@ -249,7 +238,7 @@ local function prepare_client_config(overrides)
249238
end
250239

251240
local editor_info = util.get_editor_info()
252-
local provider_url = config.config.auth_provider_url
241+
local provider_url = config.auth_provider_url
253242
local proxy_uri = vim.g.copilot_proxy
254243

255244
local settings = { ---@type copilot_settings
@@ -296,7 +285,7 @@ local function prepare_client_config(overrides)
296285
logger.trace("workspace configuration", configurations)
297286

298287
-- to activate tracing if we want it
299-
local logger_conf = config.config.logger
288+
local logger_conf = config.logger
300289
local trace_params = { value = logger_conf.trace_lsp } --[[@as copilot_nofify_set_trace_params]]
301290
api.notify_set_trace(client, trace_params)
302291

@@ -329,22 +318,16 @@ local function prepare_client_config(overrides)
329318
end
330319

331320
function M.setup()
332-
M.should_attach = config.config.should_attach
333-
local server_config = config.config.server
334-
local node_command = config.config.copilot_node_command
335-
M.server = vim.tbl_deep_extend("force", M.server, server_config)
321+
local node_command = config.copilot_node_command
336322

337-
if M.server.custom_server_filepath then
338-
M.server.custom_server_filepath = vim.fs.normalize(M.server.custom_server_filepath)
323+
--TODO: merge the two types into an indirection
324+
if config.server.type == "nodejs" then
325+
lsp.nodejs.setup(node_command, config.server.custom_server_filepath)
326+
elseif config.server.type == "binary" then
327+
lsp.binary.setup(config.server.custom_server_filepath)
339328
end
340329

341-
if M.server.type == "nodejs" then
342-
lsp_nodesj.setup(node_command, M.server.custom_server_filepath)
343-
elseif M.server.type == "binary" then
344-
lsp_binary.setup(M.server.custom_server_filepath)
345-
end
346-
347-
M.config = prepare_client_config(config.config.server_opts_overrides)
330+
M.config = prepare_client_config(config.server_opts_overrides)
348331

349332
if not M.config then
350333
is_disabled = true
@@ -378,54 +361,4 @@ function M.teardown()
378361
end
379362
end
380363

381-
function M.add_workspace_folder(folder_path)
382-
if type(folder_path) ~= "string" then
383-
logger.error("workspace folder path must be a string")
384-
return false
385-
end
386-
387-
if vim.fn.isdirectory(folder_path) ~= 1 then
388-
logger.error("invalid workspace folder: " .. folder_path)
389-
return false
390-
end
391-
392-
-- Normalize path
393-
folder_path = vim.fn.fnamemodify(folder_path, ":p")
394-
395-
--- @type workspace_folder
396-
local workspace_folder = {
397-
uri = vim.uri_from_fname(folder_path),
398-
name = folder_path,
399-
}
400-
401-
local workspace_folders = config.config.workspace_folders
402-
if not workspace_folders then
403-
workspace_folders = {}
404-
end
405-
406-
for _, existing_folder in ipairs(workspace_folders) do
407-
if existing_folder == folder_path then
408-
return
409-
end
410-
end
411-
412-
table.insert(workspace_folders, { folder_path })
413-
config.set("workspace_folders", workspace_folders)
414-
415-
local client = M.get()
416-
if client and client.initialized then
417-
api.notify(client, "workspace/didChangeWorkspaceFolders", {
418-
event = {
419-
added = { workspace_folder },
420-
removed = {},
421-
},
422-
})
423-
logger.notify("added workspace folder: " .. folder_path)
424-
else
425-
logger.notify("workspace folder will be added on next session: " .. folder_path)
426-
end
427-
428-
return true
429-
end
430-
431364
return M

lua/copilot/client/utils.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local M = {}
2+
3+
---@param config_root_dir RootDirFuncOrString
4+
function M.get_root_dir(config_root_dir)
5+
local root_dir --[[@as string]]
6+
7+
if type(config_root_dir) == "function" then
8+
root_dir = config_root_dir()
9+
else
10+
root_dir = config_root_dir
11+
end
12+
13+
if not root_dir or root_dir == "" then
14+
root_dir = "."
15+
end
16+
17+
root_dir = vim.fn.fnamemodify(root_dir, ":p:h")
18+
return root_dir
19+
end
20+
21+
return M

lua/copilot/config.lua

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)