Skip to content

Commit d73a464

Browse files
authored
refactor: centralize type definitions in dedicated types.lua module (#108)
# Centralize type definitions in a dedicated module This PR introduces a new `types.lua` module that centralizes all type definitions for the ClaudeCode.nvim plugin. The change: - Creates a dedicated `lua/claudecode/types.lua` file containing all public API types - Removes inline type definitions from config.lua, init.lua, and logger.lua - Updates type references across the codebase to use the new centralized types - Improves type naming consistency with the `ClaudeCode` prefix (e.g., `ClaudeCodeConfig` instead of `ClaudeCode.Config`) - Enhances type documentation with more detailed descriptions This change makes the type system more maintainable and provides a single source of truth for the plugin's API types.
1 parent 24c1f43 commit d73a464

File tree

7 files changed

+140
-101
lines changed

7 files changed

+140
-101
lines changed

lua/claudecode/config.lua

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,7 @@
66

77
local M = {}
88

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
9+
---@type ClaudeCodeConfig
3910
M.defaults = {
4011
port_range = { min = 10000, max = 65535 },
4112
auto_start = true,
@@ -138,7 +109,7 @@ end
138109

139110
---Applies user configuration on top of default settings and validates the result.
140111
---@param user_config table|nil The user-provided configuration table.
141-
---@return ClaudeCode.Config config The final, validated configuration table.
112+
---@return ClaudeCodeConfig config The final, validated configuration table.
142113
function M.apply(user_config)
143114
local config = vim.deepcopy(M.defaults)
144115

lua/claudecode/init.lua

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,8 @@ local M = {}
99

1010
local logger = require("claudecode.logger")
1111

12-
-- Types
13-
14-
---@class ClaudeCode.Version
15-
---@field major integer
16-
---@field minor integer
17-
---@field patch integer
18-
---@field prerelease? string
19-
---@field string fun(self: ClaudeCode.Version):string
20-
21-
-- Narrow facade of the server module used by this file
22-
---@class ClaudeCode.ServerFacade
23-
---@field start fun(config: ClaudeCode.Config, auth_token: string|nil): boolean, number|string
24-
---@field stop fun(): boolean, string|nil
25-
---@field broadcast fun(method: string, params: table|nil): boolean
26-
---@field get_status fun(): { running: boolean, port: integer|nil, client_count: integer, clients?: table }
27-
28-
-- State type for this module
29-
---@class ClaudeCode.State
30-
---@field config ClaudeCode.Config
31-
---@field server ClaudeCode.ServerFacade|nil
32-
---@field port integer|nil
33-
---@field auth_token string|nil
34-
---@field initialized boolean
35-
---@field mention_queue table[]
36-
---@field mention_timer table|nil
37-
---@field connection_timer table|nil
38-
3912
--- Current plugin version
40-
---@type ClaudeCode.Version
13+
---@type ClaudeCodeVersion
4114
M.version = {
4215
major = 0,
4316
minor = 2,
@@ -53,7 +26,7 @@ M.version = {
5326
}
5427

5528
-- Module state
56-
---@type ClaudeCode.State
29+
---@type ClaudeCodeState
5730
M.state = {
5831
config = require("claudecode.config").defaults,
5932
server = nil,
@@ -314,7 +287,7 @@ function M.send_at_mention(file_path, start_line, end_line, context)
314287
end
315288

316289
---Set up the plugin with user configuration
317-
---@param opts ClaudeCode.Config|nil Optional configuration table to override defaults.
290+
---@param opts ClaudeCodeConfig|nil Optional configuration table to override defaults.
318291
---@return table module The plugin module
319292
function M.setup(opts)
320293
opts = opts or {}

lua/claudecode/logger.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ local level_values = {
2222
local current_log_level_value = M.levels.INFO
2323

2424
---Setup the logger module
25-
---@param plugin_config ClaudeCode.Config The configuration table (e.g., from claudecode.init.state.config).
25+
---@param plugin_config ClaudeCodeConfig The configuration table (e.g., from claudecode.init.state.config).
2626
function M.setup(plugin_config)
2727
local conf = plugin_config
2828

@@ -119,7 +119,7 @@ function M.info(component, ...)
119119
end
120120

121121
---Check if a specific log level is enabled
122-
---@param level_name ClaudeCode.LogLevel The level name ("error", "warn", "info", "debug", "trace")
122+
---@param level_name ClaudeCodeLogLevel The level name ("error", "warn", "info", "debug", "trace")
123123
---@return boolean enabled Whether the level is enabled
124124
function M.is_level_enabled(level_name)
125125
local level_value = level_values[level_name]

lua/claudecode/terminal.lua

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,11 @@
22
--- Supports Snacks.nvim or a native Neovim terminal fallback.
33
--- @module 'claudecode.terminal'
44

5-
--- @class TerminalProvider
6-
--- @field setup fun(config: TerminalConfig)
7-
--- @field open fun(cmd_string: string, env_table: table, config: TerminalConfig, focus: boolean?)
8-
--- @field close fun()
9-
--- @field toggle fun(cmd_string: string, env_table: table, effective_config: TerminalConfig)
10-
--- @field simple_toggle fun(cmd_string: string, env_table: table, effective_config: TerminalConfig)
11-
--- @field focus_toggle fun(cmd_string: string, env_table: table, effective_config: TerminalConfig)
12-
--- @field get_active_bufnr fun(): number?
13-
--- @field is_available fun(): boolean
14-
--- @field ensure_visible? function
15-
--- @field _get_terminal_for_test fun(): table?
16-
17-
--- @class TerminalConfig
18-
--- @field split_side "left"|"right"
19-
--- @field split_width_percentage number
20-
--- @field provider "auto"|"snacks"|"native"|TerminalProvider
21-
--- @field show_native_term_exit_tip boolean
22-
--- @field terminal_cmd string|nil
23-
--- @field auto_close boolean
24-
--- @field env table<string, string>
25-
--- @field snacks_win_opts table
26-
275
local M = {}
286

297
local claudecode_server_module = require("claudecode.server.init")
308

31-
--- @type TerminalConfig
9+
---@type ClaudeCodeTerminalConfig
3210
local defaults = {
3311
split_side = "right",
3412
split_width_percentage = 0.30,
@@ -47,7 +25,7 @@ local providers = {}
4725

4826
---Loads a terminal provider module
4927
---@param provider_name string The name of the provider to load
50-
---@return TerminalProvider? provider The provider module, or nil if loading failed
28+
---@return ClaudeCodeTerminalProvider? provider The provider module, or nil if loading failed
5129
local function load_provider(provider_name)
5230
if not providers[provider_name] then
5331
local ok, provider = pcall(require, "claudecode.terminal." .. provider_name)
@@ -61,8 +39,8 @@ local function load_provider(provider_name)
6139
end
6240

6341
---Validates and enhances a custom table provider with smart defaults
64-
---@param provider TerminalProvider The custom provider table to validate
65-
---@return TerminalProvider? provider The enhanced provider, or nil if invalid
42+
---@param provider ClaudeCodeTerminalProvider The custom provider table to validate
43+
---@return ClaudeCodeTerminalProvider? provider The enhanced provider, or nil if invalid
6644
---@return string? error Error message if validation failed
6745
local function validate_and_enhance_provider(provider)
6846
if type(provider) ~= "table" then
@@ -117,13 +95,13 @@ end
11795

11896
---Gets the effective terminal provider, guaranteed to return a valid provider
11997
---Falls back to native provider if configured provider is unavailable
120-
---@return TerminalProvider provider The terminal provider module (never nil)
98+
---@return ClaudeCodeTerminalProvider provider The terminal provider module (never nil)
12199
local function get_provider()
122100
local logger = require("claudecode.logger")
123101

124102
-- Handle custom table provider
125103
if type(defaults.provider) == "table" then
126-
local custom_provider = defaults.provider --[[@as TerminalProvider]]
104+
local custom_provider = defaults.provider --[[@as ClaudeCodeTerminalProvider]]
127105
local enhanced_provider, error_msg = validate_and_enhance_provider(custom_provider)
128106
if enhanced_provider then
129107
-- Check if custom provider is available
@@ -290,7 +268,7 @@ end
290268

291269
---Configures the terminal module.
292270
---Merges user-provided terminal configuration with defaults and sets the terminal command.
293-
---@param user_term_config TerminalConfig? Configuration options for the terminal.
271+
---@param user_term_config ClaudeCodeTerminalConfig? Configuration options for the terminal.
294272
---@param p_terminal_cmd string? The command to run in the terminal (from main config).
295273
---@param p_env table? Custom environment variables to pass to the terminal (from main config).
296274
function M.setup(user_term_config, p_terminal_cmd, p_env)

lua/claudecode/terminal/native.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local winid = nil
1111
local jobid = nil
1212
local tip_shown = false
1313

14-
---@type TerminalConfig
14+
---@type ClaudeCodeTerminalConfig
1515
local config = require("claudecode.terminal").defaults
1616

1717
local function cleanup_state()
@@ -269,7 +269,7 @@ local function find_existing_claude_terminal()
269269
end
270270

271271
---Setup the terminal module
272-
---@param term_config TerminalConfig
272+
---@param term_config ClaudeCodeTerminalConfig
273273
function M.setup(term_config)
274274
config = term_config
275275
end
@@ -320,7 +320,7 @@ end
320320
---Simple toggle: always show/hide terminal regardless of focus
321321
---@param cmd_string string
322322
---@param env_table table
323-
---@param effective_config TerminalConfig
323+
---@param effective_config ClaudeCodeTerminalConfig
324324
function M.simple_toggle(cmd_string, env_table, effective_config)
325325
-- Check if we have a valid terminal buffer (process running)
326326
local has_buffer = bufnr and vim.api.nvim_buf_is_valid(bufnr)
@@ -360,7 +360,7 @@ end
360360
---Smart focus toggle: switches to terminal if not focused, hides if currently focused
361361
---@param cmd_string string
362362
---@param env_table table
363-
---@param effective_config TerminalConfig
363+
---@param effective_config ClaudeCodeTerminalConfig
364364
function M.focus_toggle(cmd_string, env_table, effective_config)
365365
-- Check if we have a valid terminal buffer (process running)
366366
local has_buffer = bufnr and vim.api.nvim_buf_is_valid(bufnr)
@@ -416,7 +416,7 @@ end
416416
--- Legacy toggle function for backward compatibility (defaults to simple_toggle)
417417
--- @param cmd_string string
418418
--- @param env_table table
419-
--- @param effective_config TerminalConfig
419+
--- @param effective_config ClaudeCodeTerminalConfig
420420
function M.toggle(cmd_string, env_table, effective_config)
421421
M.simple_toggle(cmd_string, env_table, effective_config)
422422
end
@@ -434,5 +434,5 @@ function M.is_available()
434434
return true -- Native provider is always available
435435
end
436436

437-
--- @type TerminalProvider
437+
--- @type ClaudeCodeTerminalProvider
438438
return M

lua/claudecode/terminal/snacks.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ local function setup_terminal_events(term_instance, config)
4242
end
4343

4444
---Builds Snacks terminal options with focus control
45-
---@param config TerminalConfig Terminal configuration
45+
---@param config ClaudeCodeTerminalConfig Terminal configuration
4646
---@param env_table table Environment variables to set for the terminal process
4747
---@param focus boolean|nil Whether to focus the terminal when opened (defaults to true)
4848
---@return table options Snacks terminal options with start_insert/auto_insert controlled by focus parameter
@@ -69,7 +69,7 @@ end
6969
---Open a terminal using Snacks.nvim
7070
---@param cmd_string string
7171
---@param env_table table
72-
---@param config TerminalConfig
72+
---@param config ClaudeCodeTerminalConfig
7373
---@param focus boolean?
7474
function M.open(cmd_string, env_table, config, focus)
7575
if not is_available() then
@@ -258,5 +258,5 @@ function M._get_terminal_for_test()
258258
return terminal
259259
end
260260

261-
---@type TerminalProvider
261+
---@type ClaudeCodeTerminalProvider
262262
return M

lua/claudecode/types.lua

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---@brief [[
2+
--- Centralized type definitions for ClaudeCode.nvim public API.
3+
--- This module contains all user-facing types and configuration structures.
4+
---@brief ]]
5+
---@module 'claudecode.types'
6+
7+
-- Version information type
8+
---@class ClaudeCodeVersion
9+
---@field major integer
10+
---@field minor integer
11+
---@field patch integer
12+
---@field prerelease? string
13+
---@field string fun(self: ClaudeCodeVersion): string
14+
15+
-- Diff behavior configuration
16+
---@class ClaudeCodeDiffOptions
17+
---@field auto_close_on_accept boolean
18+
---@field show_diff_stats boolean
19+
---@field vertical_split boolean
20+
---@field open_in_current_tab boolean
21+
---@field keep_terminal_focus boolean
22+
23+
-- Model selection option
24+
---@class ClaudeCodeModelOption
25+
---@field name string
26+
---@field value string
27+
28+
-- Log level type alias
29+
---@alias ClaudeCodeLogLevel "trace"|"debug"|"info"|"warn"|"error"
30+
31+
-- Terminal split side positioning
32+
---@alias ClaudeCodeSplitSide "left"|"right"
33+
34+
-- In-tree terminal provider names
35+
---@alias ClaudeCodeTerminalProviderName "auto"|"snacks"|"native"
36+
37+
-- @ mention queued for Claude Code
38+
---@class ClaudeCodeMention
39+
---@field file_path string The absolute file path to mention
40+
---@field start_line number? Optional start line (0-indexed for Claude compatibility)
41+
---@field end_line number? Optional end line (0-indexed for Claude compatibility)
42+
---@field timestamp number Creation timestamp from vim.loop.now() for expiry tracking
43+
44+
-- Terminal provider interface
45+
---@class ClaudeCodeTerminalProvider
46+
---@field setup fun(config: ClaudeCodeTerminalConfig)
47+
---@field open fun(cmd_string: string, env_table: table, config: ClaudeCodeTerminalConfig, focus: boolean?)
48+
---@field close fun()
49+
---@field toggle fun(cmd_string: string, env_table: table, effective_config: ClaudeCodeTerminalConfig)
50+
---@field simple_toggle fun(cmd_string: string, env_table: table, effective_config: ClaudeCodeTerminalConfig)
51+
---@field focus_toggle fun(cmd_string: string, env_table: table, effective_config: ClaudeCodeTerminalConfig)
52+
---@field get_active_bufnr fun(): number?
53+
---@field is_available fun(): boolean
54+
---@field ensure_visible? function
55+
---@field _get_terminal_for_test fun(): table?
56+
57+
-- Terminal configuration
58+
---@class ClaudeCodeTerminalConfig
59+
---@field split_side ClaudeCodeSplitSide
60+
---@field split_width_percentage number
61+
---@field provider ClaudeCodeTerminalProviderName|ClaudeCodeTerminalProvider
62+
---@field show_native_term_exit_tip boolean
63+
---@field terminal_cmd string?
64+
---@field auto_close boolean
65+
---@field env table<string, string>
66+
---@field snacks_win_opts table
67+
68+
-- Port range configuration
69+
---@class ClaudeCodePortRange
70+
---@field min integer
71+
---@field max integer
72+
73+
-- Server status information
74+
---@class ClaudeCodeServerStatus
75+
---@field running boolean
76+
---@field port integer?
77+
---@field client_count integer
78+
---@field clients? table<string, any>
79+
80+
-- Main configuration structure
81+
---@class ClaudeCodeConfig
82+
---@field port_range ClaudeCodePortRange
83+
---@field auto_start boolean
84+
---@field terminal_cmd string|nil
85+
---@field env table<string, string>
86+
---@field log_level ClaudeCodeLogLevel
87+
---@field track_selection boolean
88+
---@field visual_demotion_delay_ms number
89+
---@field connection_wait_delay number
90+
---@field connection_timeout number
91+
---@field queue_timeout number
92+
---@field diff_opts ClaudeCodeDiffOptions
93+
---@field models ClaudeCodeModelOption[]
94+
---@field disable_broadcast_debouncing? boolean
95+
---@field enable_broadcast_debouncing_in_tests? boolean
96+
---@field terminal ClaudeCodeTerminalConfig?
97+
98+
-- Server interface for main module
99+
---@class ClaudeCodeServerFacade
100+
---@field start fun(config: ClaudeCodeConfig, auth_token: string|nil): (success: boolean, port_or_error: number|string)
101+
---@field stop fun(): (success: boolean, error_message: string?)
102+
---@field broadcast fun(method: string, params: table?): boolean
103+
---@field get_status fun(): ClaudeCodeServerStatus
104+
105+
-- Main module state
106+
---@class ClaudeCodeState
107+
---@field config ClaudeCodeConfig
108+
---@field server ClaudeCodeServerFacade|nil
109+
---@field port integer|nil
110+
---@field auth_token string|nil
111+
---@field initialized boolean
112+
---@field mention_queue ClaudeCodeMention[]
113+
---@field mention_timer uv.uv_timer_t? -- (compatible with vim.loop timer)
114+
---@field connection_timer uv.uv_timer_t? -- (compatible with vim.loop timer)
115+
116+
-- This module only defines types, no runtime functionality
117+
return {}

0 commit comments

Comments
 (0)