Skip to content

Commit 25ab971

Browse files
authored
feat: add support for dynamic root_dir resolving (#382)
the configuration supports overriding the default function as root_dir, with a fallback to the current directory. fixes #371
1 parent 330ffb9 commit 25ab971

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ require('copilot').setup({
8888
["."] = false,
8989
},
9090
copilot_node_command = 'node', -- Node.js version must be > 18.x
91-
copilot_model = "", -- Current LSP default is gpt-35-turbo, supports gpt-4o-copilot
9291
workspace_folders = {},
92+
copilot_model = "", -- Current LSP default is gpt-35-turbo, supports gpt-4o-copilot
93+
root_dir = function()
94+
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
95+
end,
9396
server_opts_overrides = {},
9497
})
9598
```
@@ -228,6 +231,11 @@ workspace_folders = {
228231

229232
They can also be added runtime, using the command `:Copilot workspace add [folderpath]` where `[folderpath]` is the workspace folder.
230233

234+
### root_dir
235+
236+
This allows changing the function that gets the root folder, the default looks for a parent folder that contains the folder `.git`.
237+
If none is found, it will use the current working directory.
238+
231239
## Commands
232240

233241
`copilot.lua` defines the `:Copilot` command that can perform various actions. It has completion support, so try it out.

lua/copilot/client.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ function M.buf_attach(force)
105105
return
106106
end
107107

108+
-- In case it has changed, we update it
109+
M.config.root_dir = config.get_root_dir()
110+
108111
local ok, client_id_or_err = pcall(lsp_start, M.config)
109112
if not ok then
110113
vim.notify(string.format("[Copilot] Failed to start LSP client: %s", client_id_or_err), vim.log.levels.ERROR)
@@ -218,11 +221,7 @@ local function prepare_client_config(overrides)
218221
["copilot/openURL"] = api.handlers["copilot/openURL"],
219222
}
220223

221-
local root_dir = vim.loop.cwd()
222-
if not root_dir then
223-
root_dir = vim.fn.getcwd()
224-
end
225-
224+
local root_dir = config.get_root_dir()
226225
local workspace_folders = {
227226
--- @type workspace_folder
228227
{
@@ -247,6 +246,7 @@ local function prepare_client_config(overrides)
247246
end
248247
end
249248

249+
-- LSP config, not to be confused with config.lua
250250
return vim.tbl_deep_extend("force", {
251251
cmd = {
252252
node,

lua/copilot/config.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ local default_config = {
4545
server_opts_overrides = {},
4646
---@type string|nil
4747
copilot_model = nil,
48+
---@type function|string
49+
root_dir = function()
50+
return vim.fs.dirname(vim.fs.find(".git", { upward = true })[1])
51+
end,
4852
}
4953

5054
local mod = {
@@ -96,4 +100,26 @@ function mod.set(key, value)
96100
mod.config[key] = value
97101
end
98102

103+
function mod.get_root_dir()
104+
if not mod.config then
105+
error("[Copilot] not initialized")
106+
end
107+
108+
local config_root_dir = mod.config.root_dir
109+
local root_dir --[[@as string]]
110+
111+
if type(config_root_dir) == "function" then
112+
root_dir = config_root_dir()
113+
else
114+
root_dir = config_root_dir
115+
end
116+
117+
if not root_dir or root_dir == "" then
118+
root_dir = "."
119+
end
120+
121+
root_dir = vim.fn.fnamemodify(root_dir, ":p:h")
122+
return root_dir
123+
end
124+
99125
return mod

0 commit comments

Comments
 (0)