Skip to content

Commit efaebab

Browse files
committed
feat: add type annotations and prevent diff on dirty buffers
Change-Id: I3bdd815d22a9571b35161f26fdf9b4abb8941a1d Signed-off-by: Thomas Kosiewski <tk@coder.com>
1 parent 70fa226 commit efaebab

38 files changed

+943
-897
lines changed

.luarc.json

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

dev-config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ return {
1818
{ "<leader>am", "<cmd>ClaudeCodeSelectModel<cr>", desc = "Select Claude model" },
1919

2020
-- Context sending
21-
{ "<leader>ab", "<cmd>ClaudeCodeAdd %<cr>", desc = "Add current buffer" },
21+
{ "<leader>as", "<cmd>ClaudeCodeAdd %<cr>", mode = "n", desc = "Add current buffer" },
2222
{ "<leader>as", "<cmd>ClaudeCodeSend<cr>", mode = "v", desc = "Send to Claude" },
2323
{
2424
"<leader>as",

lua/claudecode/config.lua

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1+
---@brief [[
12
--- 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+
37
local M = {}
48

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
539
M.defaults = {
640
port_range = { min = 10000, max = 65535 },
741
auto_start = true,
@@ -24,12 +58,13 @@ M.defaults = {
2458
{ name = "Claude Opus 4 (Latest)", value = "opus" },
2559
{ name = "Claude Sonnet 4 (Latest)", value = "sonnet" },
2660
},
61+
terminal = nil, -- Will be lazy-loaded to avoid circular dependency
2762
}
2863

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.
3368
function M.validate(config)
3469
assert(
3570
type(config.port_range) == "table"
@@ -97,17 +132,34 @@ function M.validate(config)
97132
assert(type(model.name) == "string" and model.name ~= "", "models[" .. i .. "].name must be a non-empty string")
98133
assert(type(model.value) == "string" and model.value ~= "", "models[" .. i .. "].value must be a non-empty string")
99134
end
135+
100136
return true
101137
end
102138

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.
106142
function M.apply(user_config)
107143
local config = vim.deepcopy(M.defaults)
108144

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+
109153
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
111163
end
112164

113165
M.validate(config)

0 commit comments

Comments
 (0)