1
+ --- @brief [[
1
2
--- Manages configuration for the Claude Code Neovim integration.
2
- -- Provides default settings, validation, and application of user-defined configurations.
3
+ --- Provides default settings, validation, and application of user-defined configurations.
4
+ --- @brief ]]
5
+ --- @module ' claudecode.config'
6
+
3
7
local M = {}
4
8
9
+ -- Types (authoritative for configuration shape):
10
+ --- @class ClaudeCode.DiffOptions
11
+ --- @field auto_close_on_accept boolean
12
+ --- @field show_diff_stats boolean
13
+ --- @field vertical_split boolean
14
+ --- @field open_in_current_tab boolean
15
+ --- @field keep_terminal_focus boolean
16
+
17
+ --- @class ClaudeCode.ModelOption
18
+ --- @field name string
19
+ --- @field value string
20
+
21
+ --- @alias ClaudeCode.LogLevel " trace" | " debug" | " info" | " warn" | " error"
22
+
23
+ --- @class ClaudeCode.Config
24
+ --- @field port_range { min : integer , max : integer }
25
+ --- @field auto_start boolean
26
+ --- @field terminal_cmd string | nil
27
+ --- @field env table<string , string>
28
+ --- @field log_level ClaudeCode.LogLevel
29
+ --- @field track_selection boolean
30
+ --- @field visual_demotion_delay_ms number
31
+ --- @field connection_wait_delay number
32
+ --- @field connection_timeout number
33
+ --- @field queue_timeout number
34
+ --- @field diff_opts ClaudeCode.DiffOptions
35
+ --- @field models ClaudeCode.ModelOption[]
36
+ --- @field disable_broadcast_debouncing ? boolean
37
+ --- @field enable_broadcast_debouncing_in_tests ? boolean
38
+ --- @field terminal TerminalConfig | nil
5
39
M .defaults = {
6
40
port_range = { min = 10000 , max = 65535 },
7
41
auto_start = true ,
@@ -24,12 +58,13 @@ M.defaults = {
24
58
{ name = " Claude Opus 4 (Latest)" , value = " opus" },
25
59
{ name = " Claude Sonnet 4 (Latest)" , value = " sonnet" },
26
60
},
61
+ terminal = nil , -- Will be lazy-loaded to avoid circular dependency
27
62
}
28
63
29
- --- Validates the provided configuration table.
30
- -- @ param config table The configuration table to validate .
31
- -- @ return boolean true if the configuration is valid .
32
- -- @error string if any configuration option is invalid .
64
+ --- Validates the provided configuration table.
65
+ --- Throws an error if any validation fails .
66
+ --- @param config table The configuration table to validate .
67
+ --- @return boolean true if the configuration is valid .
33
68
function M .validate (config )
34
69
assert (
35
70
type (config .port_range ) == " table"
@@ -97,17 +132,34 @@ function M.validate(config)
97
132
assert (type (model .name ) == " string" and model .name ~= " " , " models[" .. i .. " ].name must be a non-empty string" )
98
133
assert (type (model .value ) == " string" and model .value ~= " " , " models[" .. i .. " ].value must be a non-empty string" )
99
134
end
135
+
100
136
return true
101
137
end
102
138
103
- --- Applies user configuration on top of default settings and validates the result.
104
- -- @param user_config table|nil The user-provided configuration table.
105
- -- @return table The final, validated configuration table.
139
+ --- Applies user configuration on top of default settings and validates the result.
140
+ --- @param user_config table | nil The user-provided configuration table.
141
+ --- @return ClaudeCode.Config config The final, validated configuration table.
106
142
function M .apply (user_config )
107
143
local config = vim .deepcopy (M .defaults )
108
144
145
+ -- Lazy-load terminal defaults to avoid circular dependency
146
+ if config .terminal == nil then
147
+ local terminal_ok , terminal_module = pcall (require , " claudecode.terminal" )
148
+ if terminal_ok and terminal_module .defaults then
149
+ config .terminal = terminal_module .defaults
150
+ end
151
+ end
152
+
109
153
if user_config then
110
- config = vim .tbl_deep_extend (" force" , config , user_config )
154
+ -- Use vim.tbl_deep_extend if available, otherwise simple merge
155
+ if vim .tbl_deep_extend then
156
+ config = vim .tbl_deep_extend (" force" , config , user_config )
157
+ else
158
+ -- Simple fallback for testing environment
159
+ for k , v in pairs (user_config ) do
160
+ config [k ] = v
161
+ end
162
+ end
111
163
end
112
164
113
165
M .validate (config )
0 commit comments