Skip to content

Commit 2000186

Browse files
committed
feat: add validation of configuration
1 parent d93f32b commit 2000186

File tree

8 files changed

+116
-14
lines changed

8 files changed

+116
-14
lines changed

lua/copilot/config/init.lua

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local logger = require("copilot.logger")
88
---@field filetypes table<string, boolean> Filetypes to enable Copilot for
99
---@field auth_provider_url string|nil URL for the authentication provider
1010
---@field workspace_folders string[] Workspace folders to enable Copilot for
11-
---@field server_opts_overrides table<string, any> Options to override for the server
11+
---@field server_opts_overrides? table<string, any> Options to override for the server
1212
---@field copilot_model string|nil Model to use for Copilot, LSP server dictates the default
1313
---@field root_dir RootDirFuncOrString Root directory for the project, defaults to the nearest .git directory
1414
---@field should_attach ShouldAttachFunc Function to determine if Copilot should attach to the buffer
@@ -32,6 +32,7 @@ local M = {
3232
copilot_node_command = "node",
3333
}
3434

35+
---@param user_configs CopilotConfig
3536
function M.merge_with_user_configs(user_configs)
3637
logger.trace("setting up configuration, opts", user_configs)
3738

@@ -45,11 +46,32 @@ function M.merge_with_user_configs(user_configs)
4546
M[k] = v
4647
end
4748

48-
if M.server.custom_server_filepath then
49-
M.server.custom_server_filepath = vim.fs.normalize(M.server.custom_server_filepath)
50-
end
49+
M.validate(M)
5150

5251
initialized = true
5352
end
5453

54+
---@param config CopilotConfig
55+
function M.validate(config)
56+
vim.validate("panel", config.panel, "table")
57+
vim.validate("suggestion", config.suggestion, "table")
58+
vim.validate("logger", config.logger, "table")
59+
vim.validate("server", config.server, "table")
60+
vim.validate("filetypes", config.filetypes, "table")
61+
vim.validate("auth_provider_url", config.auth_provider_url, { "string", "nil" })
62+
vim.validate("workspace_folders", config.workspace_folders, "table")
63+
vim.validate("server_opts_overrides", config.server_opts_overrides, "table", true)
64+
vim.validate("copilot_model", config.copilot_model, { "string", "nil" })
65+
vim.validate("root_dir", config.root_dir, { "string", "function" })
66+
vim.validate("should_attach", config.should_attach, "function")
67+
vim.validate("copilot_node_command", config.copilot_node_command, "string")
68+
69+
require("copilot.config.panel").validate(config.panel)
70+
require("copilot.config.suggestion").validate(config.suggestion)
71+
require("copilot.config.logger").validate(config.logger)
72+
require("copilot.config.server").validate(config.server)
73+
require("copilot.config.root_dir").validate(config.root_dir)
74+
require("copilot.config.should_attach").validate(config.should_attach)
75+
end
76+
5577
return M

lua/copilot/config/logger.lua

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@class LoggerConfig
1+
---@class (exact) LoggerConfig
22
---@field file string Path to the log file
33
---@field file_log_level integer Log level for the log file, matches vim.log.levels
44
---@field print_log_level integer Log level for printing to the console, matches vim.log.levels
@@ -18,4 +18,30 @@ local logger = {
1818
},
1919
}
2020

21+
local function validate_log_level(level)
22+
return type(level) == "number"
23+
and (
24+
level == vim.log.levels.OFF
25+
or level == vim.log.levels.ERROR
26+
or level == vim.log.levels.WARN
27+
or level == vim.log.levels.INFO
28+
or level == vim.log.levels.DEBUG
29+
or level == vim.log.levels.TRACE
30+
)
31+
end
32+
33+
---@param config LoggerConfig
34+
function logger.validate(config)
35+
vim.validate("file", config.file, "string")
36+
config.file = vim.fs.normalize(config.file)
37+
38+
vim.validate("file_log_level", config.file_log_level, validate_log_level, false, "any of the vim.log.levels")
39+
vim.validate("print_log_level", config.print_log_level, validate_log_level, false, "any of the vim.log.levels")
40+
vim.validate("trace_lsp", config.trace_lsp, function(level)
41+
return level == "off" or level == "verbose" or level == "debug"
42+
end, false, "off, verbose or debug")
43+
vim.validate("trace_lsp_progress", config.trace_lsp_progress, "boolean")
44+
vim.validate("log_lsp_messages", config.log_lsp_messages, "boolean")
45+
end
46+
2147
return logger

lua/copilot/config/panel.lua

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
---@class PanelConfig
1+
---@class (exact) PanelConfig
22
---@field enabled boolean Whether to enable the panel
33
---@field auto_refresh boolean Whether to automatically refresh the panel
44
---@field keymap PanelKeymapConfig Keymap for the panel
5-
---@field layout config_panel_layout Layout of the panel
5+
---@field layout PanelLayoutConfig Layout of the panel
66

7-
---@class PanelKeymapConfig
7+
---@class (exact) PanelKeymapConfig
88
---@field jump_prev string|false Keymap for jumping to the previous suggestion
99
---@field jump_next string|false Keymap for jumping to the next suggestion
1010
---@field accept string|false Keymap for accepting the suggestion
1111
---@field refresh string|false Keymap for refreshing the suggestion
1212
---@field open string|false Keymap for opening the suggestion
1313

14-
---@class config_panel_layout
14+
---@class (exact) PanelLayoutConfig
1515
---@field position string<'left'|'right'|'top'|'bottom'> Position of the panel
1616
---@field ratio number Ratio of the panel size, between 0 and 1
1717

@@ -34,4 +34,23 @@ local panel = {
3434
},
3535
}
3636

37+
---@param config PanelConfig
38+
function panel.validate(config)
39+
vim.validate("enabled", config.enabled, "boolean")
40+
vim.validate("auto_refresh", config.auto_refresh, "boolean")
41+
vim.validate("keymap", config.keymap, "table")
42+
vim.validate("layout", config.layout, "table")
43+
vim.validate("keymap.jump_prev", config.keymap.jump_prev, { "string", "boolean" })
44+
vim.validate("keymap.jump_next", config.keymap.jump_next, { "string", "boolean" })
45+
vim.validate("keymap.accept", config.keymap.accept, { "string", "boolean" })
46+
vim.validate("keymap.refresh", config.keymap.refresh, { "string", "boolean" })
47+
vim.validate("keymap.open", config.keymap.open, { "string", "boolean" })
48+
vim.validate("layout.position", config.layout.position, function(value)
49+
return value == "left" or value == "right" or value == "top" or value == "bottom"
50+
end, false, "left, right, top or bottom")
51+
vim.validate("layout.ratio", config.layout.ratio, function(value)
52+
return type(value) == "number" and value >= 0 and value <= 1
53+
end, false, "number between 0 and 1")
54+
end
55+
3756
return panel

lua/copilot/config/root_dir.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ local root_dir = {
77
end,
88
}
99

10+
---@param config RootDirFuncOrString
11+
function root_dir.validate(config)
12+
vim.validate("root_dir", config, { "string", "function" })
13+
end
14+
1015
return root_dir

lua/copilot/config/server.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
---@class ServerConfig
1+
---@class (exact) ServerConfig
22
---@field type string<'nodejs', 'binary'> Type of the server
3-
---@field custom_server_filepath string|nil Path to the custom server file
3+
---@field custom_server_filepath? string|nil Path to the custom server file
44

55
local server = {
66
---@type ServerConfig
@@ -10,4 +10,16 @@ local server = {
1010
},
1111
}
1212

13+
---@param config ServerConfig
14+
function server.validate(config)
15+
vim.validate("type", config.type, function(server_type)
16+
return type(server_type) == "string" and (server_type == "nodejs" or server_type == "binary")
17+
end, false, "nodejs or binary")
18+
vim.validate("custom_server_filepath", config.custom_server_filepath, { "string", "nil" })
19+
20+
if config.custom_server_filepath then
21+
config.custom_server_filepath = vim.fs.normalize(config.custom_server_filepath)
22+
end
23+
end
24+
1325
return server

lua/copilot/config/should_attach.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ local should_attach = {
1919
end,
2020
}
2121

22+
---@param config ShouldAttachFunc
23+
function should_attach.validate(config)
24+
vim.validate("should_attach", config, "function")
25+
end
26+
2227
return should_attach

lua/copilot/config/suggestion.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
---@class SuggestionConfig
1+
---@class (exact) SuggestionConfig
22
---@field enabled boolean Whether to enable the suggestion
33
---@field auto_trigger boolean Whether to trigger the suggestion automatically
44
---@field hide_during_completion boolean Whether to hide the suggestion during completion
55
---@field debounce integer Debounce time in milliseconds
66
---@field keymap SuggestionKeymapConfig Keymap for the suggestion
77

8-
---@class SuggestionKeymapConfig
8+
---@class (exact) SuggestionKeymapConfig
99
---@field accept string|false Keymap for accepting the suggestion
1010
---@field accept_word string|false Keymap for accepting the word
1111
---@field accept_line string|false Keymap for accepting the line
@@ -31,4 +31,18 @@ local suggestion = {
3131
},
3232
}
3333

34+
function suggestion.validate(config)
35+
vim.validate("enabled", config.enabled, "boolean")
36+
vim.validate("auto_trigger", config.auto_trigger, "boolean")
37+
vim.validate("hide_during_completion", config.hide_during_completion, "boolean")
38+
vim.validate("debounce", config.debounce, { "number", "nil" })
39+
vim.validate("keymap", config.keymap, "table")
40+
vim.validate("keymap.accept", config.keymap.accept, { "string", "boolean" })
41+
vim.validate("keymap.accept_word", config.keymap.accept_word, { "string", "boolean" })
42+
vim.validate("keymap.accept_line", config.keymap.accept_line, { "string", "boolean" })
43+
vim.validate("keymap.next", config.keymap.next, { "string", "boolean" })
44+
vim.validate("keymap.prev", config.keymap.prev, { "string", "boolean" })
45+
vim.validate("keymap.dismiss", config.keymap.dismiss, { "string", "boolean" })
46+
end
47+
3448
return suggestion

lua/copilot/workspace/utils.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ function M.add_workspace_folder(folder_path)
1515
return false
1616
end
1717

18-
-- Normalize path
1918
folder_path = vim.fn.fnamemodify(folder_path, ":p")
2019

2120
--- @type workspace_folder

0 commit comments

Comments
 (0)