Skip to content

Commit 79efe15

Browse files
committed
refactor: extract config to separate module
1 parent c65288b commit 79efe15

File tree

5 files changed

+100
-71
lines changed

5 files changed

+100
-71
lines changed

lua/copilot/client.lua

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
local api = require("copilot.api")
2+
local config = require("copilot.config")
23
local util = require("copilot.util")
34

4-
local M = { params = {} }
5+
local M = {}
56

67
local copilot_node_version = nil
78
function M.get_node_version()
89
if not copilot_node_version then
9-
copilot_node_version = string.match(table.concat(vim.fn.systemlist(M.params.copilot_node_command .. " --version", nil, false)), "v(%S+)")
10+
copilot_node_version = string.match(table.concat(vim.fn.systemlist(config.get("copilot_node_command") .. " --version", nil, false)), "v(%S+)")
1011
end
1112
return copilot_node_version
1213
end
@@ -19,16 +20,13 @@ end
1920

2021
---@param force? boolean
2122
function M.buf_attach(client, force)
22-
if not force and not util.should_attach(M.params.filetypes) then
23+
if not force and not util.should_attach() then
2324
return
2425
end
2526

2627
client = client or util.get_copilot_client()
2728
if client and not util.is_attached(client) then
2829
vim.lsp.buf_attach_client(0, client.id)
29-
30-
---@todo unknown property, remove this
31-
client.completion_function = M.params.extensions
3230
end
3331
end
3432

@@ -74,19 +72,8 @@ M.merge_server_opts = function(params)
7472
end
7573

7674
M.start = function(params)
77-
M.params = params
78-
--- for backward compatibility
79-
if M.params.ft_disable then
80-
for _, disabled_ft in ipairs(M.params.ft_disable) do
81-
M.params.filetypes[disabled_ft] = false
82-
end
83-
end
84-
85-
if not M.params.copilot_node_command then
86-
M.params.copilot_node_command = "node"
87-
end
88-
89-
vim.lsp.start_client(M.merge_server_opts(params))
75+
local client_config = M.merge_server_opts(params)
76+
vim.lsp.start_client(client_config)
9077
end
9178

9279
return M

lua/copilot/command.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function mod.status()
7575
return
7676
end
7777

78-
local should_attach, no_attach_reason = u.should_attach(c.params.filetypes)
78+
local should_attach, no_attach_reason = u.should_attach()
7979
local is_attached = u.is_attached(client)
8080
if is_attached then
8181
if not should_attach then
@@ -114,7 +114,7 @@ function mod.toggle(opts)
114114
end
115115

116116
if not opts.force then
117-
local should_attach, no_attach_reason = u.should_attach(c.params.filetypes)
117+
local should_attach, no_attach_reason = u.should_attach()
118118
if not should_attach then
119119
vim.api.nvim_echo({
120120
{ "[Copilot] " .. no_attach_reason .. "\n" },

lua/copilot/config.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---@class copilot_config
2+
local default_config = {
3+
---@class copilot_config_panel
4+
panel = {
5+
enabled = true,
6+
auto_refresh = false,
7+
---@type table<'accept'|'next'|'prev'|'dismiss', false|string>
8+
keymap = {
9+
jump_prev = "[[",
10+
jump_next = "]]",
11+
accept = "<CR>",
12+
refresh = "gr",
13+
open = "<M-CR>",
14+
},
15+
},
16+
---@class copilot_config_suggestion
17+
suggestion = {
18+
enabled = true,
19+
auto_trigger = false,
20+
debounce = 75,
21+
---@type table<'accept'|'next'|'prev'|'dismiss', false|string>
22+
keymap = {
23+
accept = "<M-l>",
24+
next = "<M-]>",
25+
prev = "<M-[>",
26+
dismiss = "<C-]>",
27+
},
28+
},
29+
---@deprecated
30+
ft_disable = nil,
31+
---@type table<string, boolean>
32+
filetypes = {},
33+
copilot_node_command = "node",
34+
server_opts_overrides = {},
35+
}
36+
37+
local mod = {
38+
config = nil,
39+
}
40+
41+
function mod.setup(opts)
42+
if mod.config then
43+
vim.notify("[copilot] config is already set", vim.log.levels.WARN)
44+
return
45+
end
46+
47+
local config = vim.tbl_deep_extend("force", default_config, opts or {})
48+
49+
--- for backward compatibility
50+
if config.ft_disable then
51+
for _, disabled_ft in ipairs(config.ft_disable) do
52+
config.filetypes[disabled_ft] = false
53+
end
54+
55+
config.ft_disable = nil
56+
end
57+
58+
mod.config = config
59+
60+
return mod.config
61+
end
62+
63+
---@param key? string
64+
function mod.get(key)
65+
if mod.config and key then
66+
return mod.config[key]
67+
end
68+
69+
return mod.config
70+
end
71+
72+
return mod

lua/copilot/init.lua

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
11
local M = { client_info = nil }
2+
local config = require("copilot.config")
23
local client = require("copilot.client")
34
local highlight = require("copilot.highlight")
45
local panel = require("copilot.panel")
56
local suggestion = require("copilot.suggestion")
6-
local defaults = {
7-
panel = {
8-
enabled = true,
9-
auto_refresh = false,
10-
keymap = {
11-
jump_prev = "[[",
12-
jump_next = "]]",
13-
accept = "<CR>",
14-
refresh = "gr",
15-
open = "<M-CR>"
16-
}
17-
},
18-
suggestion = {
19-
enabled = true,
20-
auto_trigger = false,
21-
debounce = 75,
22-
keymap = {
23-
accept = "<M-l>",
24-
next = "<M-]>",
25-
prev = "<M-[>",
26-
dismiss = "<C-]>",
27-
}
28-
},
29-
ft_disable = nil,
30-
filetypes = {},
31-
copilot_node_command = "node",
32-
server_opts_overrides = {},
33-
}
347

358
local create_cmds = function (_)
369
vim.api.nvim_create_user_command("CopilotDetach", function()
@@ -57,23 +30,19 @@ local create_cmds = function (_)
5730
end, {})
5831
end
5932

60-
local config_handler = function(opts)
61-
local user_config = opts and vim.tbl_deep_extend("force", defaults, opts) or defaults
62-
return user_config
63-
end
64-
6533
M.setup = function(opts)
66-
local user_config = config_handler(opts)
34+
local conf = config.setup(opts)
35+
6736
vim.schedule(function ()
68-
client.start(user_config)
37+
client.start(conf)
6938

70-
if user_config.panel.enabled then
71-
panel.setup(user_config.panel)
72-
create_cmds(user_config)
39+
if conf.panel.enabled then
40+
panel.setup(conf.panel)
41+
create_cmds(conf)
7342
end
7443

75-
if user_config.suggestion.enabled then
76-
suggestion.setup(user_config.suggestion)
44+
if conf.suggestion.enabled then
45+
suggestion.setup(conf.suggestion)
7746
end
7847
end)
7948

lua/copilot/util.lua

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local config = require("copilot.config")
2+
13
local M = {}
24

35
local id = 0
@@ -95,11 +97,10 @@ local function is_ft_disabled(ft, filetypes)
9597
return false
9698
end
9799

98-
---@param filetypes table<string, boolean>
99100
---@return boolean should_attach
100101
---@return string? no_attach_reason
101-
function M.should_attach(filetypes)
102-
local ft_disabled, ft_disabled_reason = is_ft_disabled(vim.bo.filetype, filetypes)
102+
function M.should_attach()
103+
local ft_disabled, ft_disabled_reason = is_ft_disabled(vim.bo.filetype, config.get("filetypes"))
103104

104105
if ft_disabled then
105106
return not ft_disabled, ft_disabled_reason
@@ -207,14 +208,14 @@ M.get_completion_params = function(opts)
207208
return M.get_doc_params(opts)
208209
end
209210

210-
211211
---@return copilot_editor_configuration
212212
function M.get_editor_configuration()
213-
local c = require("copilot.client")
213+
local conf = config.get()
214+
215+
local filetypes = vim.deepcopy(conf.filetypes)
214216

215-
local filetypes = vim.deepcopy(c.params.filetypes)
216-
if filetypes['*'] == nil then
217-
filetypes = vim.tbl_deep_extend('keep', filetypes, internal_filetypes)
217+
if filetypes["*"] == nil then
218+
filetypes = vim.tbl_deep_extend("keep", filetypes, internal_filetypes)
218219
end
219220

220221
---@type string[]
@@ -224,7 +225,7 @@ function M.get_editor_configuration()
224225
table.sort(disabled_filetypes)
225226

226227
return {
227-
enableAutoCompletions = not not (c.params.panel.enabled or c.params.suggestion.enabled),
228+
enableAutoCompletions = not not (conf.panel.enabled or conf.suggestion.enabled),
228229
disabledLanguages = disabled_filetypes,
229230
}
230231
end
@@ -234,7 +235,7 @@ M.get_copilot_path = function()
234235
if vim.fn.filereadable(copilot_path) ~= 0 then
235236
return copilot_path
236237
else
237-
print("[Copilot] could not read" .. copilot_path)
238+
print("[Copilot] could not read" .. copilot_path)
238239
end
239240
end
240241

0 commit comments

Comments
 (0)