{"repo_name": "claude-code.nvim", "file_name": "/claude-code.nvim/lua/claude-code/terminal.lua", "inference_info": {"prefix_code": "---@mod claude-code.terminal Terminal management for claude-code.nvim\n---@brief [[\n--- This module provides terminal buffer management for claude-code.nvim.\n--- It handles creating, toggling, and managing the terminal window.\n---@brief ]]\n\nlocal M = {}\n\n--- Terminal buffer and window management\n-- @table ClaudeCodeTerminal\n-- @field instances table Key-value store of git root to buffer number\n-- @field saved_updatetime number|nil Original updatetime before Claude Code was opened\n-- @field current_instance string|nil Current git root path for active instance\nM.terminal = {\n instances = {},\n saved_updatetime = nil,\n current_instance = nil,\n}\n\n--- Get the current git root or a fallback identifier\n--- @param git table The git module\n--- @return string identifier Git root path or fallback identifier\nlocal function get_instance_identifier(git)\n local git_root = git.get_git_root()\n if git_root then\n return git_root\n else\n -- Fallback to current working directory if not in a git repo\n return vim.fn.getcwd()\n end\nend\n\n--- Calculate floating window dimensions from percentage strings\n--- @param value number|string Dimension value (number or percentage string)\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated dimension\n--- @private\nlocal function calculate_float_dimension(value, max_value)\n if value == nil then\n return math.floor(max_value * 0.8) -- Default to 80% if not specified\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n return math.floor(max_value * percentage / 100)\n end\n return value\nend\n\n--- Calculate floating window position for centering\n--- @param value number|string Position value (number, \"center\", or percentage)\n--- @param window_size number Size of the window\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated position\n--- @private\nlocal function calculate_float_position(value, window_size, max_value)\n local pos\n if value == 'center' then\n pos = math.floor((max_value - window_size) / 2)\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n pos = math.floor(max_value * percentage / 100)\n else\n pos = value or 0\n end\n -- Clamp position to ensure window is visible\n return math.max(0, math.min(pos, max_value - window_size))\nend\n\n--- Create a floating window for Claude Code\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the float (optional)\n--- @return number Window ID of the created floating window\n--- @private\nlocal function create_float(config, existing_bufnr)\n local float_config = config.window.float or {}\n\n -- Get editor dimensions (accounting for command line, status line, etc.)\n local editor_width = vim.o.columns\n local editor_height = vim.o.lines - vim.o.cmdheight - 1 -- Subtract command line and status line\n\n -- Calculate dimensions\n local width = calculate_float_dimension(float_config.width, editor_width)\n local height = calculate_float_dimension(float_config.height, editor_height)\n\n -- Calculate position\n local row = calculate_float_position(float_config.row, height, editor_height)\n local col = calculate_float_position(float_config.col, width, editor_width)\n\n -- Create floating window configuration\n local win_config = {\n relative = float_config.relative or 'editor',\n width = width,\n height = height,\n row = row,\n col = col,\n border = float_config.border or 'rounded',\n style = 'minimal',\n }\n\n -- Create buffer if we don't have an existing one\n local bufnr = existing_bufnr\n if not bufnr then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n -- Validate existing buffer is still valid and a terminal\n if not vim.api.nvim_buf_is_valid(bufnr) then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n local buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n if buftype ~= 'terminal' then\n -- Buffer exists but is no longer a terminal, create a new one\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n end\n end\n end\n\n -- Create and return the floating window\n return vim.api.nvim_open_win(bufnr, true, win_config)\nend\n\n--- Build command with git root directory if configured\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param base_cmd string Base command to run\n--- @return string Command with git root directory change if applicable\n--- @private\nlocal function build_command_with_git_root(config, git, base_cmd)\n if config.git and config.git.use_git_root then\n local git_root = git.get_git_root()\n if git_root then\n local quoted_root = vim.fn.shellescape(git_root)\n -- Use configurable shell commands\n local separator = config.shell.separator\n local pushd_cmd = config.shell.pushd_cmd\n local popd_cmd = config.shell.popd_cmd\n return pushd_cmd\n .. ' '\n .. quoted_root\n .. ' '\n .. separator\n .. ' '\n .. base_cmd\n .. ' '\n .. separator\n .. ' '\n .. popd_cmd\n end\n end\n return base_cmd\nend\n\n--- Configure common window options\n--- @param win_id number Window ID to configure\n--- @param config table Plugin configuration\n--- @private\nlocal function configure_window_options(win_id, config)\n if config.window.hide_numbers then\n vim.api.nvim_set_option_value('number', false, {win = win_id})\n vim.api.nvim_set_option_value('relativenumber', false, {win = win_id})\n end\n\n if config.window.hide_signcolumn then\n vim.api.nvim_set_option_value('signcolumn', 'no', {win = win_id})\n end\nend\n\n--- Generate buffer name for instance\n--- @param instance_id string Instance identifier\n--- @param config table Plugin configuration\n--- @return string Buffer name\n--- @private\nlocal function generate_buffer_name(instance_id, config)\n if config.git.multi_instance then\n return 'claude-code-' .. instance_id:gsub('[^%w%-_]', '-')\n else\n return 'claude-code'\n end\nend\n\n--- Create a split window according to the specified position configuration\n--- @param position string Window position configuration\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the split (optional)\n--- @private\nlocal function create_split(position, config, existing_bufnr)\n -- Handle floating window\n if position == 'float' then\n return create_float(config, existing_bufnr)\n end\n\n local is_vertical = position:match('vsplit') or position:match('vertical')\n\n -- Create the window with the user's specified command\n -- If the command already contains 'split', use it as is\n if position:match('split') then\n vim.cmd(position)\n else\n -- Otherwise append the appropriate split command\n local split_cmd = is_vertical and 'vsplit' or 'split'\n vim.cmd(position .. ' ' .. split_cmd)\n end\n\n -- If we have an existing buffer to display, switch to it\n if existing_bufnr then\n vim.cmd('buffer ' .. existing_bufnr)\n end\n\n -- Resize the window appropriately based on split type\n if is_vertical then\n vim.cmd('vertical resize ' .. math.floor(vim.o.columns * config.window.split_ratio))\n else\n vim.cmd('resize ' .. math.floor(vim.o.lines * config.window.split_ratio))\n end\nend\n\n--- Set up function to force insert mode when entering the Claude Code window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.force_insert_mode(claude_code, config)\n local current_bufnr = vim.fn.bufnr('%')\n\n -- Check if current buffer is any of our Claude instances\n local is_claude_instance = false\n for _, bufnr in pairs(claude_code.claude_code.instances) do\n if bufnr and bufnr == current_bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n is_claude_instance = true\n break\n end\n end\n\n if is_claude_instance then\n -- Only enter insert mode if we're in the terminal buffer and not already in insert mode\n -- and not configured to stay in normal mode\n if config.window.start_in_normal_mode then\n return\n end\n\n local mode = vim.api.nvim_get_mode().mode\n if vim.bo.buftype == 'terminal' and mode ~= 't' and mode ~= 'i' then\n vim.cmd 'silent! stopinsert'\n vim.schedule(function()\n vim.cmd 'silent! startinsert'\n end)\n end\n end\nend\n\n--- Determine instance ID based on configuration\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @return string instance_id Instance identifier\n--- @private\nlocal function get_instance_id(config, git)\n if config.git.multi_instance then\n if config.git.use_git_root then\n return get_instance_identifier(git)\n else\n return vim.fn.getcwd()\n end\n else\n -- Use a fixed ID for single instance mode\n return 'global'\n end\nend\n\n--- Check if buffer is a valid terminal\n--- @param bufnr number Buffer number\n--- @return boolean is_valid True if buffer is a valid terminal\n--- @private\n", "suffix_code": "\n\n--- Handle existing instance (toggle visibility)\n--- @param bufnr number Buffer number\n--- @param config table Plugin configuration\n--- @private\nlocal function handle_existing_instance(bufnr, config)\n local win_ids = vim.fn.win_findbuf(bufnr)\n if #win_ids > 0 then\n -- Claude Code is visible, close the window\n for _, win_id in ipairs(win_ids) do\n vim.api.nvim_win_close(win_id, true)\n end\n else\n -- Claude Code buffer exists but is not visible, open it in a split or float\n if config.window.position == 'float' then\n create_float(config, bufnr)\n else\n create_split(config.window.position, config, bufnr)\n end\n -- Force insert mode more aggressively unless configured to start in normal mode\n if not config.window.start_in_normal_mode then\n vim.schedule(function()\n vim.cmd 'stopinsert | startinsert'\n end)\n end\n end\nend\n\n--- Create new Claude Code instance\n--- @param claude_code table The main plugin module\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param instance_id string Instance identifier\n--- @private\nlocal function create_new_instance(claude_code, config, git, instance_id)\n if config.window.position == 'float' then\n -- For floating window, create buffer first with terminal\n local new_bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n vim.api.nvim_set_option_value('bufhidden', 'hide', {buf = new_bufnr})\n\n -- Create the floating window\n local win_id = create_float(config, new_bufnr)\n\n -- Set current buffer to run terminal command\n vim.api.nvim_win_set_buf(win_id, new_bufnr)\n\n -- Determine command\n local cmd = build_command_with_git_root(config, git, config.command)\n\n -- Run terminal in the buffer\n vim.fn.termopen(cmd)\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.api.nvim_buf_set_name(new_bufnr, buffer_name)\n\n -- Configure window options\n configure_window_options(win_id, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = new_bufnr\n\n -- Enter insert mode if configured\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n else\n -- Regular split window\n create_split(config.window.position, config)\n\n -- Determine if we should use the git root directory\n local base_cmd = build_command_with_git_root(config, git, config.command)\n local cmd = 'terminal ' .. base_cmd\n\n vim.cmd(cmd)\n vim.cmd 'setlocal bufhidden=hide'\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.cmd('file ' .. buffer_name)\n\n -- Configure window options using helper function\n local current_win = vim.api.nvim_get_current_win()\n configure_window_options(current_win, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = vim.fn.bufnr('%')\n\n -- Automatically enter insert mode in terminal unless configured to start in normal mode\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n end\nend\n\n--- Toggle the Claude Code terminal window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\n--- @param git table The git module\nfunction M.toggle(claude_code, config, git)\n -- Determine instance ID based on config\n local instance_id = get_instance_id(config, git)\n claude_code.claude_code.current_instance = instance_id\n\n -- Check if this Claude Code instance is already running\n local bufnr = claude_code.claude_code.instances[instance_id]\n\n -- Validate existing buffer\n if bufnr and not is_valid_terminal_buffer(bufnr) then\n -- Buffer is no longer a valid terminal, reset\n claude_code.claude_code.instances[instance_id] = nil\n bufnr = nil\n end\n\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n -- Handle existing instance (toggle visibility)\n handle_existing_instance(bufnr, config)\n else\n -- Prune invalid buffer entries\n if bufnr and not vim.api.nvim_buf_is_valid(bufnr) then\n claude_code.claude_code.instances[instance_id] = nil\n end\n -- Create new instance\n create_new_instance(claude_code, config, git, instance_id)\n end\nend\n\nreturn M\n", "middle_code": "local function is_valid_terminal_buffer(bufnr)\n if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n local buftype = nil\n pcall(function()\n buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n end)\n local terminal_job_id = nil\n pcall(function()\n terminal_job_id = vim.b[bufnr].terminal_job_id\n end)\n return buftype == 'terminal'\n and terminal_job_id\n and vim.fn.jobwait({ terminal_job_id }, 0)[1] == -1\nend", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "lua", "sub_task_type": null}, "context_code": [["/claude-code.nvim/lua/claude-code/config.lua", "---@mod claude-code.config Configuration management for claude-code.nvim\n---@brief [[\n--- This module handles configuration management and validation for claude-code.nvim.\n--- It provides the default configuration, validation, and merging of user config.\n---@brief ]]\n\nlocal M = {}\n\n--- ClaudeCodeWindow class for window configuration\n-- @table ClaudeCodeWindow\n-- @field split_ratio number Percentage of screen for the terminal window (height for horizontal, width for vertical)\n-- @field position string Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\" etc.\n-- @field enter_insert boolean Whether to enter insert mode when opening Claude Code\n-- @field start_in_normal_mode boolean Whether to start in normal mode instead of insert mode when opening Claude Code\n-- @field hide_numbers boolean Hide line numbers in the terminal window\n-- @field hide_signcolumn boolean Hide the sign column in the terminal window\n-- @field float table|nil Floating window configuration (only used when position is \"float\")\n-- @field float.width number|string Width of floating window (number: columns, string: percentage like \"80%\")\n-- @field float.height number|string Height of floating window (number: rows, string: percentage like \"80%\")\n-- @field float.row number|string|nil Row position (number: absolute, string: \"center\" or percentage)\n-- @field float.col number|string|nil Column position (number: absolute, string: \"center\" or percentage)\n-- @field float.border string Border style: \"none\", \"single\", \"double\", \"rounded\", \"solid\", \"shadow\", or array\n-- @field float.relative string Relative positioning: \"editor\" or \"cursor\"\n\n--- ClaudeCodeRefresh class for file refresh configuration\n-- @table ClaudeCodeRefresh\n-- @field enable boolean Enable file change detection\n-- @field updatetime number updatetime when Claude Code is active (milliseconds)\n-- @field timer_interval number How often to check for file changes (milliseconds)\n-- @field show_notifications boolean Show notification when files are reloaded\n\n--- ClaudeCodeGit class for git integration configuration\n-- @table ClaudeCodeGit\n-- @field use_git_root boolean Set CWD to git root when opening Claude Code (if in git project)\n-- @field multi_instance boolean Use multiple Claude instances (one per git root)\n\n--- ClaudeCodeKeymapsToggle class for toggle keymap configuration\n-- @table ClaudeCodeKeymapsToggle\n-- @field normal string|boolean Normal mode keymap for toggling Claude Code, false to disable\n-- @field terminal string|boolean Terminal mode keymap for toggling Claude Code, false to disable\n\n--- ClaudeCodeKeymaps class for keymap configuration\n-- @table ClaudeCodeKeymaps\n-- @field toggle ClaudeCodeKeymapsToggle Keymaps for toggling Claude Code\n-- @field window_navigation boolean Enable window navigation keymaps\n-- @field scrolling boolean Enable scrolling keymaps\n\n--- ClaudeCodeCommandVariants class for command variant configuration\n-- @table ClaudeCodeCommandVariants\n-- Conversation management:\n-- @field continue string|boolean Resume the most recent conversation\n-- @field resume string|boolean Display an interactive conversation picker\n-- Output options:\n-- @field verbose string|boolean Enable verbose logging with full turn-by-turn output\n-- Additional options can be added as needed\n\n--- ClaudeCodeShell class for shell configuration\n-- @table ClaudeCodeShell\n-- @field separator string Command separator used in shell commands (e.g., '&&', ';', '|')\n-- @field pushd_cmd string Command to push directory onto stack (e.g., 'pushd' for bash/zsh)\n-- @field popd_cmd string Command to pop directory from stack (e.g., 'popd' for bash/zsh)\n\n--- ClaudeCodeConfig class for main configuration\n-- @table ClaudeCodeConfig\n-- @field window ClaudeCodeWindow Terminal window settings\n-- @field refresh ClaudeCodeRefresh File refresh settings\n-- @field git ClaudeCodeGit Git integration settings\n-- @field shell ClaudeCodeShell Shell-specific configuration\n-- @field command string Command used to launch Claude Code\n-- @field command_variants ClaudeCodeCommandVariants Command variants configuration\n-- @field keymaps ClaudeCodeKeymaps Keymaps configuration\n\n--- Default configuration options\n--- @type ClaudeCodeConfig\nM.default_config = {\n -- Terminal window settings\n window = {\n split_ratio = 0.3, -- Percentage of screen for the terminal window (height or width)\n height_ratio = 0.3, -- DEPRECATED: Use split_ratio instead\n position = 'botright', -- Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\", etc.\n enter_insert = true, -- Whether to enter insert mode when opening Claude Code\n start_in_normal_mode = false, -- Whether to start in normal mode instead of insert mode\n hide_numbers = true, -- Hide line numbers in the terminal window\n hide_signcolumn = true, -- Hide the sign column in the terminal window\n -- Default floating window configuration\n float = {\n width = '80%', -- Width as percentage of editor\n height = '80%', -- Height as percentage of editor\n row = 'center', -- Center vertically\n col = 'center', -- Center horizontally\n relative = 'editor', -- Position relative to editor\n border = 'rounded', -- Border style\n },\n },\n -- File refresh settings\n refresh = {\n enable = true, -- Enable file change detection\n updatetime = 100, -- updatetime to use when Claude Code is active (milliseconds)\n timer_interval = 1000, -- How often to check for file changes (milliseconds)\n show_notifications = true, -- Show notification when files are reloaded\n },\n -- Git integration settings\n git = {\n use_git_root = true, -- Set CWD to git root when opening Claude Code (if in git project)\n multi_instance = true, -- Use multiple Claude instances (one per git root)\n },\n -- Shell-specific settings\n shell = {\n separator = '&&', -- Command separator used in shell commands\n pushd_cmd = 'pushd', -- Command to push directory onto stack\n popd_cmd = 'popd', -- Command to pop directory from stack\n },\n -- Command settings\n command = 'claude', -- Command used to launch Claude Code\n -- Command variants\n command_variants = {\n -- Conversation management\n continue = '--continue', -- Resume the most recent conversation\n resume = '--resume', -- Display an interactive conversation picker\n\n -- Output options\n verbose = '--verbose', -- Enable verbose logging with full turn-by-turn output\n },\n -- Keymaps\n keymaps = {\n toggle = {\n normal = '', -- Normal mode keymap for toggling Claude Code\n terminal = '', -- Terminal mode keymap for toggling Claude Code\n variants = {\n continue = 'cC', -- Normal mode keymap for Claude Code with continue flag\n verbose = 'cV', -- Normal mode keymap for Claude Code with verbose flag\n },\n },\n window_navigation = true, -- Enable window navigation keymaps ()\n scrolling = true, -- Enable scrolling keymaps () for page up/down\n },\n}\n\n--- Validate the configuration\n--- Validate window configuration\n--- @param window table Window configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_window_config(window)\n if type(window) ~= 'table' then\n return false, 'window config must be a table'\n end\n\n if type(window.split_ratio) ~= 'number' or window.split_ratio <= 0 or window.split_ratio > 1 then\n return false, 'window.split_ratio must be a number between 0 and 1'\n end\n\n if type(window.position) ~= 'string' then\n return false, 'window.position must be a string'\n end\n\n if type(window.enter_insert) ~= 'boolean' then\n return false, 'window.enter_insert must be a boolean'\n end\n\n if type(window.start_in_normal_mode) ~= 'boolean' then\n return false, 'window.start_in_normal_mode must be a boolean'\n end\n\n if type(window.hide_numbers) ~= 'boolean' then\n return false, 'window.hide_numbers must be a boolean'\n end\n\n if type(window.hide_signcolumn) ~= 'boolean' then\n return false, 'window.hide_signcolumn must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate floating window configuration\n--- @param float table Float configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_float_config(float)\n if type(float) ~= 'table' then\n return false, 'window.float must be a table when position is \"float\"'\n end\n\n -- Validate width (can be number or percentage string)\n if type(float.width) == 'string' then\n if not float.width:match('^%d+%%$') then\n return false, 'window.float.width must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.width) ~= 'number' or float.width <= 0 then\n return false, 'window.float.width must be a positive number or percentage string'\n end\n\n -- Validate height (can be number or percentage string)\n if type(float.height) == 'string' then\n if not float.height:match('^%d+%%$') then\n return false, 'window.float.height must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.height) ~= 'number' or float.height <= 0 then\n return false, 'window.float.height must be a positive number or percentage string'\n end\n\n -- Validate relative (must be \"editor\" or \"cursor\")\n if float.relative ~= 'editor' and float.relative ~= 'cursor' then\n return false, 'window.float.relative must be \"editor\" or \"cursor\"'\n end\n\n -- Validate border (must be valid border style)\n local valid_borders = { 'none', 'single', 'double', 'rounded', 'solid', 'shadow' }\n local is_valid_border = false\n for _, border in ipairs(valid_borders) do\n if float.border == border then\n is_valid_border = true\n break\n end\n end\n -- Also allow array borders\n if not is_valid_border and type(float.border) ~= 'table' then\n return false,\n 'window.float.border must be one of: none, single, double, rounded, solid, shadow, or an array'\n end\n\n -- Validate row and col if they exist\n if float.row ~= nil then\n if type(float.row) == 'string' and float.row ~= 'center' then\n if not float.row:match('^%d+%%$') then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n elseif type(float.row) ~= 'number' and float.row ~= 'center' then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n end\n\n if float.col ~= nil then\n if type(float.col) == 'string' and float.col ~= 'center' then\n if not float.col:match('^%d+%%$') then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n elseif type(float.col) ~= 'number' and float.col ~= 'center' then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n end\n\n return true, nil\nend\n\n--- Validate refresh configuration\n--- @param refresh table Refresh configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_refresh_config(refresh)\n if type(refresh) ~= 'table' then\n return false, 'refresh config must be a table'\n end\n\n if type(refresh.enable) ~= 'boolean' then\n return false, 'refresh.enable must be a boolean'\n end\n\n if type(refresh.updatetime) ~= 'number' or refresh.updatetime <= 0 then\n return false, 'refresh.updatetime must be a positive number'\n end\n\n if type(refresh.timer_interval) ~= 'number' or refresh.timer_interval <= 0 then\n return false, 'refresh.timer_interval must be a positive number'\n end\n\n if type(refresh.show_notifications) ~= 'boolean' then\n return false, 'refresh.show_notifications must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate git configuration\n--- @param git table Git configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_git_config(git)\n if type(git) ~= 'table' then\n return false, 'git config must be a table'\n end\n\n if type(git.use_git_root) ~= 'boolean' then\n return false, 'git.use_git_root must be a boolean'\n end\n\n if type(git.multi_instance) ~= 'boolean' then\n return false, 'git.multi_instance must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate shell configuration\n--- @param shell table Shell configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_shell_config(shell)\n if type(shell) ~= 'table' then\n return false, 'shell config must be a table'\n end\n\n if type(shell.separator) ~= 'string' then\n return false, 'shell.separator must be a string'\n end\n\n if type(shell.pushd_cmd) ~= 'string' then\n return false, 'shell.pushd_cmd must be a string'\n end\n\n if type(shell.popd_cmd) ~= 'string' then\n return false, 'shell.popd_cmd must be a string'\n end\n\n return true, nil\nend\n\n--- Validate keymaps configuration\n--- @param keymaps table Keymaps configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_keymaps_config(keymaps)\n if type(keymaps) ~= 'table' then\n return false, 'keymaps config must be a table'\n end\n\n if type(keymaps.toggle) ~= 'table' then\n return false, 'keymaps.toggle must be a table'\n end\n\n if not (keymaps.toggle.normal == false or type(keymaps.toggle.normal) == 'string') then\n return false, 'keymaps.toggle.normal must be a string or false'\n end\n\n if not (keymaps.toggle.terminal == false or type(keymaps.toggle.terminal) == 'string') then\n return false, 'keymaps.toggle.terminal must be a string or false'\n end\n\n -- Validate variant keymaps if they exist\n if keymaps.toggle.variants then\n if type(keymaps.toggle.variants) ~= 'table' then\n return false, 'keymaps.toggle.variants must be a table'\n end\n\n -- Check each variant keymap\n for variant_name, keymap in pairs(keymaps.toggle.variants) do\n if not (keymap == false or type(keymap) == 'string') then\n return false, 'keymaps.toggle.variants.' .. variant_name .. ' must be a string or false'\n end\n end\n end\n\n if type(keymaps.window_navigation) ~= 'boolean' then\n return false, 'keymaps.window_navigation must be a boolean'\n end\n\n if type(keymaps.scrolling) ~= 'boolean' then\n return false, 'keymaps.scrolling must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate command variants configuration\n--- @param command_variants table Command variants configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_command_variants_config(command_variants)\n if type(command_variants) ~= 'table' then\n return false, 'command_variants config must be a table'\n end\n\n -- Check each command variant\n for variant_name, variant_args in pairs(command_variants) do\n if not (variant_args == false or type(variant_args) == 'string') then\n return false, 'command_variants.' .. variant_name .. ' must be a string or false'\n end\n end\n\n return true, nil\nend\n\n--- Validate configuration options\n--- @param config ClaudeCodeConfig\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_config(config)\n -- Validate window settings\n local valid, err = validate_window_config(config.window)\n if not valid then\n return false, err\n end\n\n -- Validate float configuration if position is \"float\"\n if config.window.position == 'float' then\n valid, err = validate_float_config(config.window.float)\n if not valid then\n return false, err\n end\n end\n\n -- Validate refresh settings\n valid, err = validate_refresh_config(config.refresh)\n if not valid then\n return false, err\n end\n\n -- Validate git settings\n valid, err = validate_git_config(config.git)\n if not valid then\n return false, err\n end\n\n -- Validate shell settings\n valid, err = validate_shell_config(config.shell)\n if not valid then\n return false, err\n end\n\n -- Validate command settings\n if type(config.command) ~= 'string' then\n return false, 'command must be a string'\n end\n\n -- Validate command variants settings\n valid, err = validate_command_variants_config(config.command_variants)\n if not valid then\n return false, err\n end\n\n -- Validate keymaps settings\n valid, err = validate_keymaps_config(config.keymaps)\n if not valid then\n return false, err\n end\n\n -- Cross-validate keymaps with command variants\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n -- Ensure variant exists in command_variants\n if keymap ~= false and not config.command_variants[variant_name] then\n return false,\n 'keymaps.toggle.variants.' .. variant_name .. ' has no corresponding command variant'\n end\n end\n end\n\n return true, nil\nend\n\n--- Parse user configuration and merge with defaults\n--- @param user_config? table\n--- @param silent? boolean Set to true to suppress error notifications (for tests)\n--- @return ClaudeCodeConfig\nfunction M.parse_config(user_config, silent)\n -- Handle backward compatibility first\n if user_config and user_config.window then\n if user_config.window.height_ratio and not user_config.window.split_ratio then\n -- Copy height_ratio to split_ratio for backward compatibility\n user_config.window.split_ratio = user_config.window.height_ratio\n end\n end\n\n local config = vim.tbl_deep_extend('force', {}, M.default_config, user_config or {})\n\n -- If position is float and no float config provided, use default float config\n if\n config.window.position == 'float'\n and not (user_config and user_config.window and user_config.window.float)\n then\n config.window.float = vim.deepcopy(M.default_config.window.float)\n end\n\n local valid, err = validate_config(config)\n if not valid then\n -- Only notify if not in silent mode\n if not silent then\n vim.notify('Claude Code: ' .. err, vim.log.levels.ERROR)\n end\n -- Fall back to default config in case of error\n return vim.deepcopy(M.default_config)\n end\n\n return config\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/init.lua", "---@mod claude-code Claude Code Neovim Integration\n---@brief [[\n--- A plugin for seamless integration between Claude Code AI assistant and Neovim.\n--- This plugin provides a terminal-based interface to Claude Code within Neovim.\n---\n--- Requirements:\n--- - Neovim 0.7.0 or later\n--- - Claude Code CLI tool installed and available in PATH\n--- - plenary.nvim (dependency for git operations)\n---\n--- Usage:\n--- ```lua\n--- require('claude-code').setup({\n--- -- Configuration options (optional)\n--- })\n--- ```\n---@brief ]]\n\n-- Import modules\nlocal config = require('claude-code.config')\nlocal commands = require('claude-code.commands')\nlocal keymaps = require('claude-code.keymaps')\nlocal file_refresh = require('claude-code.file_refresh')\nlocal terminal = require('claude-code.terminal')\nlocal git = require('claude-code.git')\nlocal version = require('claude-code.version')\n\nlocal M = {}\n\n-- Make imported modules available\nM.commands = commands\n\n-- Store the current configuration\n--- @type table\nM.config = {}\n\n-- Terminal buffer and window management\n--- @type table\nM.claude_code = terminal.terminal\n\n--- Force insert mode when entering the Claude Code window\n--- This is a public function used in keymaps\nfunction M.force_insert_mode()\n terminal.force_insert_mode(M, M.config)\nend\n\n--- Get the current active buffer number\n--- @return number|nil bufnr Current Claude instance buffer number or nil\nlocal function get_current_buffer_number()\n -- Get current instance from the instances table\n local current_instance = M.claude_code.current_instance\n if current_instance and type(M.claude_code.instances) == 'table' then\n return M.claude_code.instances[current_instance]\n end\n return nil\nend\n\n--- Toggle the Claude Code terminal window\n--- This is a public function used by commands\nfunction M.toggle()\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\nend\n\n--- Toggle the Claude Code terminal window with a specific command variant\n--- @param variant_name string The name of the command variant to use\nfunction M.toggle_with_variant(variant_name)\n if not variant_name or not M.config.command_variants[variant_name] then\n -- If variant doesn't exist, fall back to regular toggle\n return M.toggle()\n end\n\n -- Store the original command\n local original_command = M.config.command\n\n -- Set the command with the variant args\n M.config.command = original_command .. ' ' .. M.config.command_variants[variant_name]\n\n -- Call the toggle function with the modified command\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\n\n -- Restore the original command\n M.config.command = original_command\nend\n\n--- Get the current version of the plugin\n--- @return string version Current version string\nfunction M.get_version()\n return version.string()\nend\n\n--- Version information\nM.version = version\n\n--- Setup function for the plugin\n--- @param user_config? table User configuration table (optional)\nfunction M.setup(user_config)\n -- Parse and validate configuration\n -- Don't use silent mode for regular usage - users should see config errors\n M.config = config.parse_config(user_config, false)\n\n -- Set up autoread option\n vim.o.autoread = true\n\n -- Set up file refresh functionality\n file_refresh.setup(M, M.config)\n\n -- Register commands\n commands.register_commands(M)\n\n -- Register keymaps\n keymaps.register_keymaps(M, M.config)\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/file_refresh.lua", "---@mod claude-code.file_refresh File refresh functionality for claude-code.nvim\n---@brief [[\n--- This module provides file refresh functionality to detect and reload files\n--- that have been modified by Claude Code or other external processes.\n---@brief ]]\n\nlocal M = {}\n\n--- Timer for checking file changes\n--- @type userdata|nil\nlocal refresh_timer = nil\n\n--- Setup autocommands for file change detection\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup(claude_code, config)\n if not config.refresh.enable then\n return\n end\n\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeFileRefresh', { clear = true })\n\n -- Create an autocommand that checks for file changes more frequently\n vim.api.nvim_create_autocmd({\n 'CursorHold',\n 'CursorHoldI',\n 'FocusGained',\n 'BufEnter',\n 'InsertLeave',\n 'TextChanged',\n 'TermLeave',\n 'TermEnter',\n 'BufWinEnter',\n }, {\n group = augroup,\n pattern = '*',\n callback = function()\n if vim.fn.filereadable(vim.fn.expand '%') == 1 then\n vim.cmd 'checktime'\n end\n end,\n desc = 'Check for file changes on disk',\n })\n\n -- Clean up any existing timer\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\n\n -- Create a timer to check for file changes periodically\n refresh_timer = vim.loop.new_timer()\n if refresh_timer then\n refresh_timer:start(\n 0,\n config.refresh.timer_interval,\n vim.schedule_wrap(function()\n -- Only check time if there's an active Claude Code terminal\n local current_instance = claude_code.claude_code.current_instance\n local bufnr = current_instance and claude_code.claude_code.instances[current_instance]\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) and #vim.fn.win_findbuf(bufnr) > 0 then\n vim.cmd 'silent! checktime'\n end\n end)\n )\n end\n\n -- Create an autocommand that notifies when a file has been changed externally\n if config.refresh.show_notifications then\n vim.api.nvim_create_autocmd('FileChangedShellPost', {\n group = augroup,\n pattern = '*',\n callback = function()\n vim.notify('File changed on disk. Buffer reloaded.', vim.log.levels.INFO)\n end,\n desc = 'Notify when a file is changed externally',\n })\n end\n\n -- Set a shorter updatetime while Claude Code is open\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n\n -- When Claude Code opens, set a shorter updatetime\n vim.api.nvim_create_autocmd('TermOpen', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf = vim.api.nvim_get_current_buf()\n local buf_name = vim.api.nvim_buf_get_name(buf)\n if buf_name:match('claude%-code$') then\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n vim.o.updatetime = config.refresh.updatetime\n end\n end,\n desc = 'Set shorter updatetime when Claude Code is open',\n })\n\n -- When Claude Code closes, restore normal updatetime\n vim.api.nvim_create_autocmd('TermClose', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf_name = vim.api.nvim_buf_get_name(0)\n if buf_name:match('claude%-code$') then\n vim.o.updatetime = claude_code.claude_code.saved_updatetime\n end\n end,\n desc = 'Restore normal updatetime when Claude Code is closed',\n })\nend\n\n--- Clean up the file refresh functionality (stop the timer)\nfunction M.cleanup()\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/keymaps.lua", "---@mod claude-code.keymaps Keymap management for claude-code.nvim\n---@brief [[\n--- This module provides keymap registration and handling for claude-code.nvim.\n--- It handles normal mode, terminal mode, and window navigation keymaps.\n---@brief ]]\n\nlocal M = {}\n\n--- Register keymaps for claude-code.nvim\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.register_keymaps(claude_code, config)\n local map_opts = { noremap = true, silent = true }\n\n -- Normal mode toggle keymaps\n if config.keymaps.toggle.normal then\n vim.api.nvim_set_keymap(\n 'n',\n config.keymaps.toggle.normal,\n [[ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n if config.keymaps.toggle.terminal then\n -- Terminal mode toggle keymap\n -- In terminal mode, special keys like Ctrl need different handling\n -- We use a direct escape sequence approach for more reliable terminal mappings\n vim.api.nvim_set_keymap(\n 't',\n config.keymaps.toggle.terminal,\n [[:ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n -- Register variant keymaps if configured\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_set_keymap(\n 'n',\n keymap,\n string.format([[%s]], cmd_name),\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: ' .. capitalized_name })\n )\n end\n end\n end\n\n -- Register with which-key if it's available\n vim.defer_fn(function()\n local status_ok, which_key = pcall(require, 'which-key')\n if status_ok then\n if config.keymaps.toggle.normal then\n which_key.add {\n mode = 'n',\n { config.keymaps.toggle.normal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.terminal then\n which_key.add {\n mode = 't',\n { config.keymaps.toggle.terminal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n\n -- Register variant keymaps with which-key\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n which_key.add {\n mode = 'n',\n { keymap, desc = 'Claude Code: ' .. capitalized_name, icon = '🤖' },\n }\n end\n end\n end\n end\n end, 100)\nend\n\n--- Set up terminal-specific keymaps for window navigation\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup_terminal_navigation(claude_code, config)\n -- Get current active Claude instance buffer\n local current_instance = claude_code.claude_code.current_instance\n local buf = current_instance and claude_code.claude_code.instances[current_instance]\n if buf and vim.api.nvim_buf_is_valid(buf) then\n -- Create autocommand to enter insert mode when the terminal window gets focus\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeTerminalFocus_' .. buf, { clear = true })\n\n -- Set up multiple events for more reliable focus detection\n vim.api.nvim_create_autocmd(\n { 'WinEnter', 'BufEnter', 'WinLeave', 'FocusGained', 'CmdLineLeave' },\n {\n group = augroup,\n callback = function()\n vim.schedule(claude_code.force_insert_mode)\n end,\n desc = 'Auto-enter insert mode when focusing Claude Code terminal',\n }\n )\n\n -- Window navigation keymaps\n if config.keymaps.window_navigation then\n -- Window navigation keymaps with special handling to force insert mode in the target window\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n\n -- Also add normal mode mappings for when user is in normal mode in the terminal\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n end\n\n -- Add scrolling keymaps\n if config.keymaps.scrolling then\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page up' }\n )\n end\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/commands.lua", "---@mod claude-code.commands Command registration for claude-code.nvim\n---@brief [[\n--- This module provides command registration and handling for claude-code.nvim.\n--- It defines user commands and command handlers.\n---@brief ]]\n\nlocal M = {}\n\n--- @type table List of available commands and their handlers\nM.commands = {}\n\n--- Register commands for the claude-code plugin\n--- @param claude_code table The main plugin module\nfunction M.register_commands(claude_code)\n -- Create the user command for toggling Claude Code\n vim.api.nvim_create_user_command('ClaudeCode', function()\n claude_code.toggle()\n end, { desc = 'Toggle Claude Code terminal' })\n\n -- Create commands for each command variant\n for variant_name, variant_args in pairs(claude_code.config.command_variants) do\n if variant_args ~= false then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_create_user_command(cmd_name, function()\n claude_code.toggle_with_variant(variant_name)\n end, { desc = 'Toggle Claude Code terminal with ' .. variant_name .. ' option' })\n end\n end\n\n -- Add version command\n vim.api.nvim_create_user_command('ClaudeCodeVersion', function()\n vim.notify('Claude Code version: ' .. claude_code.version(), vim.log.levels.INFO)\n end, { desc = 'Display Claude Code version' })\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/git.lua", "---@mod claude-code.git Git integration for claude-code.nvim\n---@brief [[\n--- This module provides git integration functionality for claude-code.nvim.\n--- It detects git repositories and can set the working directory to the git root.\n---@brief ]]\n\nlocal M = {}\n\n--- Helper function to get git root directory\n--- @return string|nil git_root The git root directory path or nil if not in a git repo\nfunction M.get_git_root()\n -- For testing compatibility\n if vim.env.CLAUDE_CODE_TEST_MODE == 'true' then\n return '/home/user/project'\n end\n\n -- Check if we're in a git repository\n local handle = io.popen('git rev-parse --is-inside-work-tree 2>/dev/null')\n if not handle then\n return nil\n end\n\n local result = handle:read('*a')\n handle:close()\n\n -- Strip trailing whitespace and newlines for reliable matching\n result = result:gsub('[\\n\\r%s]*$', '')\n\n if result == 'true' then\n -- Get the git root path\n local root_handle = io.popen('git rev-parse --show-toplevel 2>/dev/null')\n if not root_handle then\n return nil\n end\n\n local git_root = root_handle:read('*a')\n root_handle:close()\n\n -- Remove trailing whitespace and newlines\n git_root = git_root:gsub('[\\n\\r%s]*$', '')\n\n return git_root\n end\n\n return nil\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/version.lua", "---@mod claude-code.version Version information for claude-code.nvim\n---@brief [[\n--- This module provides version information for claude-code.nvim.\n---@brief ]]\n\n--- @table M\n--- Version information for Claude Code\n--- @field major number Major version (breaking changes)\n--- @field minor number Minor version (new features)\n--- @field patch number Patch version (bug fixes)\n--- @field string function Returns formatted version string\n\nlocal M = {}\n\n-- Individual version components\nM.major = 0\nM.minor = 4\nM.patch = 2\n\n-- Combined semantic version\nM.version = string.format('%d.%d.%d', M.major, M.minor, M.patch)\n\n--- Returns the formatted version string (for backward compatibility)\n--- @return string Version string in format \"major.minor.patch\"\nfunction M.string()\n return M.version\nend\n\n--- Prints the current version of the plugin\nfunction M.print_version()\n vim.notify('Claude Code version: ' .. M.string(), vim.log.levels.INFO)\nend\n\nreturn M\n"]], "task_instance_info": {"created_time": "2025-08-21 00:40:22", "created_task_model": "DeepSeek-R1", "code_description": "This code segment functions to: Validate whether a specified resource represents an active terminal session by verifying structural integrity, checking process association status, and confirming operational state through multiple conditional checks."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```lua\n[TASK_BEGIN]\n--- Check if buffer is a valid terminal\n--- @param bufnr number Buffer number\n--- @return boolean is_valid True if buffer is a valid terminal\n--- @private\nlocal function is_valid_terminal_buffer(bufnr)\n if not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n local buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n return buftype == 'terminal'\nend\n[TASK_END]\n```", "inference_time": "2025-08-21 00-40-40"}, "editdistance_info": {"edit_distance": 45.2555, "calculate_time": "2025-08-21 00:40:40", "true_code_clean": "local function is_valid_terminal_buffer(bufnr)\n if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n local buftype = nil\n pcall(function()\n buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n end)\n local terminal_job_id = nil\n pcall(function()\n terminal_job_id = vim.b[bufnr].terminal_job_id\n end)\n return buftype == 'terminal'\n and terminal_job_id\n and vim.fn.jobwait({ terminal_job_id }, 0)[1] == -1\nend", "predict_code_clean": "local function is_valid_terminal_buffer(bufnr)\n if not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n local buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n return buftype == 'terminal'\nend"}} {"repo_name": "claude-code.nvim", "file_name": "/claude-code.nvim/lua/claude-code/terminal.lua", "inference_info": {"prefix_code": "---@mod claude-code.terminal Terminal management for claude-code.nvim\n---@brief [[\n--- This module provides terminal buffer management for claude-code.nvim.\n--- It handles creating, toggling, and managing the terminal window.\n---@brief ]]\n\nlocal M = {}\n\n--- Terminal buffer and window management\n-- @table ClaudeCodeTerminal\n-- @field instances table Key-value store of git root to buffer number\n-- @field saved_updatetime number|nil Original updatetime before Claude Code was opened\n-- @field current_instance string|nil Current git root path for active instance\nM.terminal = {\n instances = {},\n saved_updatetime = nil,\n current_instance = nil,\n}\n\n--- Get the current git root or a fallback identifier\n--- @param git table The git module\n--- @return string identifier Git root path or fallback identifier\nlocal function get_instance_identifier(git)\n local git_root = git.get_git_root()\n if git_root then\n return git_root\n else\n -- Fallback to current working directory if not in a git repo\n return vim.fn.getcwd()\n end\nend\n\n--- Calculate floating window dimensions from percentage strings\n--- @param value number|string Dimension value (number or percentage string)\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated dimension\n--- @private\nlocal function calculate_float_dimension(value, max_value)\n if value == nil then\n return math.floor(max_value * 0.8) -- Default to 80% if not specified\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n return math.floor(max_value * percentage / 100)\n end\n return value\nend\n\n--- Calculate floating window position for centering\n--- @param value number|string Position value (number, \"center\", or percentage)\n--- @param window_size number Size of the window\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated position\n--- @private\nlocal function calculate_float_position(value, window_size, max_value)\n local pos\n if value == 'center' then\n pos = math.floor((max_value - window_size) / 2)\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n pos = math.floor(max_value * percentage / 100)\n else\n pos = value or 0\n end\n -- Clamp position to ensure window is visible\n return math.max(0, math.min(pos, max_value - window_size))\nend\n\n--- Create a floating window for Claude Code\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the float (optional)\n--- @return number Window ID of the created floating window\n--- @private\nlocal function create_float(config, existing_bufnr)\n local float_config = config.window.float or {}\n\n -- Get editor dimensions (accounting for command line, status line, etc.)\n local editor_width = vim.o.columns\n local editor_height = vim.o.lines - vim.o.cmdheight - 1 -- Subtract command line and status line\n\n -- Calculate dimensions\n local width = calculate_float_dimension(float_config.width, editor_width)\n local height = calculate_float_dimension(float_config.height, editor_height)\n\n -- Calculate position\n local row = calculate_float_position(float_config.row, height, editor_height)\n local col = calculate_float_position(float_config.col, width, editor_width)\n\n -- Create floating window configuration\n local win_config = {\n relative = float_config.relative or 'editor',\n width = width,\n height = height,\n row = row,\n col = col,\n border = float_config.border or 'rounded',\n style = 'minimal',\n }\n\n -- Create buffer if we don't have an existing one\n local bufnr = existing_bufnr\n if not bufnr then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n -- Validate existing buffer is still valid and a terminal\n if not vim.api.nvim_buf_is_valid(bufnr) then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n local buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n if buftype ~= 'terminal' then\n -- Buffer exists but is no longer a terminal, create a new one\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n end\n end\n end\n\n -- Create and return the floating window\n return vim.api.nvim_open_win(bufnr, true, win_config)\nend\n\n--- Build command with git root directory if configured\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param base_cmd string Base command to run\n--- @return string Command with git root directory change if applicable\n--- @private\nlocal function build_command_with_git_root(config, git, base_cmd)\n if config.git and config.git.use_git_root then\n local git_root = git.get_git_root()\n if git_root then\n local quoted_root = vim.fn.shellescape(git_root)\n -- Use configurable shell commands\n local separator = config.shell.separator\n local pushd_cmd = config.shell.pushd_cmd\n local popd_cmd = config.shell.popd_cmd\n return pushd_cmd\n .. ' '\n .. quoted_root\n .. ' '\n .. separator\n .. ' '\n .. base_cmd\n .. ' '\n .. separator\n .. ' '\n .. popd_cmd\n end\n end\n return base_cmd\nend\n\n--- Configure common window options\n--- @param win_id number Window ID to configure\n--- @param config table Plugin configuration\n--- @private\nlocal function configure_window_options(win_id, config)\n if config.window.hide_numbers then\n vim.api.nvim_set_option_value('number', false, {win = win_id})\n vim.api.nvim_set_option_value('relativenumber', false, {win = win_id})\n end\n\n if config.window.hide_signcolumn then\n vim.api.nvim_set_option_value('signcolumn', 'no', {win = win_id})\n end\nend\n\n--- Generate buffer name for instance\n--- @param instance_id string Instance identifier\n--- @param config table Plugin configuration\n--- @return string Buffer name\n--- @private\nlocal function generate_buffer_name(instance_id, config)\n if config.git.multi_instance then\n return 'claude-code-' .. instance_id:gsub('[^%w%-_]', '-')\n else\n return 'claude-code'\n end\nend\n\n--- Create a split window according to the specified position configuration\n--- @param position string Window position configuration\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the split (optional)\n--- @private\nlocal function create_split(position, config, existing_bufnr)\n -- Handle floating window\n if position == 'float' then\n return create_float(config, existing_bufnr)\n end\n\n local is_vertical = position:match('vsplit') or position:match('vertical')\n\n -- Create the window with the user's specified command\n -- If the command already contains 'split', use it as is\n if position:match('split') then\n vim.cmd(position)\n else\n -- Otherwise append the appropriate split command\n local split_cmd = is_vertical and 'vsplit' or 'split'\n vim.cmd(position .. ' ' .. split_cmd)\n end\n\n -- If we have an existing buffer to display, switch to it\n if existing_bufnr then\n vim.cmd('buffer ' .. existing_bufnr)\n end\n\n -- Resize the window appropriately based on split type\n if is_vertical then\n vim.cmd('vertical resize ' .. math.floor(vim.o.columns * config.window.split_ratio))\n else\n vim.cmd('resize ' .. math.floor(vim.o.lines * config.window.split_ratio))\n end\nend\n\n--- Set up function to force insert mode when entering the Claude Code window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\n", "suffix_code": "\n\n--- Determine instance ID based on configuration\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @return string instance_id Instance identifier\n--- @private\nlocal function get_instance_id(config, git)\n if config.git.multi_instance then\n if config.git.use_git_root then\n return get_instance_identifier(git)\n else\n return vim.fn.getcwd()\n end\n else\n -- Use a fixed ID for single instance mode\n return 'global'\n end\nend\n\n--- Check if buffer is a valid terminal\n--- @param bufnr number Buffer number\n--- @return boolean is_valid True if buffer is a valid terminal\n--- @private\nlocal function is_valid_terminal_buffer(bufnr)\n if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n\n local buftype = nil\n pcall(function()\n buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n end)\n \n local terminal_job_id = nil\n pcall(function()\n terminal_job_id = vim.b[bufnr].terminal_job_id\n end)\n\n return buftype == 'terminal'\n and terminal_job_id\n and vim.fn.jobwait({ terminal_job_id }, 0)[1] == -1\nend\n\n--- Handle existing instance (toggle visibility)\n--- @param bufnr number Buffer number\n--- @param config table Plugin configuration\n--- @private\nlocal function handle_existing_instance(bufnr, config)\n local win_ids = vim.fn.win_findbuf(bufnr)\n if #win_ids > 0 then\n -- Claude Code is visible, close the window\n for _, win_id in ipairs(win_ids) do\n vim.api.nvim_win_close(win_id, true)\n end\n else\n -- Claude Code buffer exists but is not visible, open it in a split or float\n if config.window.position == 'float' then\n create_float(config, bufnr)\n else\n create_split(config.window.position, config, bufnr)\n end\n -- Force insert mode more aggressively unless configured to start in normal mode\n if not config.window.start_in_normal_mode then\n vim.schedule(function()\n vim.cmd 'stopinsert | startinsert'\n end)\n end\n end\nend\n\n--- Create new Claude Code instance\n--- @param claude_code table The main plugin module\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param instance_id string Instance identifier\n--- @private\nlocal function create_new_instance(claude_code, config, git, instance_id)\n if config.window.position == 'float' then\n -- For floating window, create buffer first with terminal\n local new_bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n vim.api.nvim_set_option_value('bufhidden', 'hide', {buf = new_bufnr})\n\n -- Create the floating window\n local win_id = create_float(config, new_bufnr)\n\n -- Set current buffer to run terminal command\n vim.api.nvim_win_set_buf(win_id, new_bufnr)\n\n -- Determine command\n local cmd = build_command_with_git_root(config, git, config.command)\n\n -- Run terminal in the buffer\n vim.fn.termopen(cmd)\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.api.nvim_buf_set_name(new_bufnr, buffer_name)\n\n -- Configure window options\n configure_window_options(win_id, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = new_bufnr\n\n -- Enter insert mode if configured\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n else\n -- Regular split window\n create_split(config.window.position, config)\n\n -- Determine if we should use the git root directory\n local base_cmd = build_command_with_git_root(config, git, config.command)\n local cmd = 'terminal ' .. base_cmd\n\n vim.cmd(cmd)\n vim.cmd 'setlocal bufhidden=hide'\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.cmd('file ' .. buffer_name)\n\n -- Configure window options using helper function\n local current_win = vim.api.nvim_get_current_win()\n configure_window_options(current_win, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = vim.fn.bufnr('%')\n\n -- Automatically enter insert mode in terminal unless configured to start in normal mode\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n end\nend\n\n--- Toggle the Claude Code terminal window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\n--- @param git table The git module\nfunction M.toggle(claude_code, config, git)\n -- Determine instance ID based on config\n local instance_id = get_instance_id(config, git)\n claude_code.claude_code.current_instance = instance_id\n\n -- Check if this Claude Code instance is already running\n local bufnr = claude_code.claude_code.instances[instance_id]\n\n -- Validate existing buffer\n if bufnr and not is_valid_terminal_buffer(bufnr) then\n -- Buffer is no longer a valid terminal, reset\n claude_code.claude_code.instances[instance_id] = nil\n bufnr = nil\n end\n\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n -- Handle existing instance (toggle visibility)\n handle_existing_instance(bufnr, config)\n else\n -- Prune invalid buffer entries\n if bufnr and not vim.api.nvim_buf_is_valid(bufnr) then\n claude_code.claude_code.instances[instance_id] = nil\n end\n -- Create new instance\n create_new_instance(claude_code, config, git, instance_id)\n end\nend\n\nreturn M\n", "middle_code": "function M.force_insert_mode(claude_code, config)\n local current_bufnr = vim.fn.bufnr('%')\n local is_claude_instance = false\n for _, bufnr in pairs(claude_code.claude_code.instances) do\n if bufnr and bufnr == current_bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n is_claude_instance = true\n break\n end\n end\n if is_claude_instance then\n if config.window.start_in_normal_mode then\n return\n end\n local mode = vim.api.nvim_get_mode().mode\n if vim.bo.buftype == 'terminal' and mode ~= 't' and mode ~= 'i' then\n vim.cmd 'silent! stopinsert'\n vim.schedule(function()\n vim.cmd 'silent! startinsert'\n end)\n end\n end\nend", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "lua", "sub_task_type": null}, "context_code": [["/claude-code.nvim/lua/claude-code/config.lua", "---@mod claude-code.config Configuration management for claude-code.nvim\n---@brief [[\n--- This module handles configuration management and validation for claude-code.nvim.\n--- It provides the default configuration, validation, and merging of user config.\n---@brief ]]\n\nlocal M = {}\n\n--- ClaudeCodeWindow class for window configuration\n-- @table ClaudeCodeWindow\n-- @field split_ratio number Percentage of screen for the terminal window (height for horizontal, width for vertical)\n-- @field position string Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\" etc.\n-- @field enter_insert boolean Whether to enter insert mode when opening Claude Code\n-- @field start_in_normal_mode boolean Whether to start in normal mode instead of insert mode when opening Claude Code\n-- @field hide_numbers boolean Hide line numbers in the terminal window\n-- @field hide_signcolumn boolean Hide the sign column in the terminal window\n-- @field float table|nil Floating window configuration (only used when position is \"float\")\n-- @field float.width number|string Width of floating window (number: columns, string: percentage like \"80%\")\n-- @field float.height number|string Height of floating window (number: rows, string: percentage like \"80%\")\n-- @field float.row number|string|nil Row position (number: absolute, string: \"center\" or percentage)\n-- @field float.col number|string|nil Column position (number: absolute, string: \"center\" or percentage)\n-- @field float.border string Border style: \"none\", \"single\", \"double\", \"rounded\", \"solid\", \"shadow\", or array\n-- @field float.relative string Relative positioning: \"editor\" or \"cursor\"\n\n--- ClaudeCodeRefresh class for file refresh configuration\n-- @table ClaudeCodeRefresh\n-- @field enable boolean Enable file change detection\n-- @field updatetime number updatetime when Claude Code is active (milliseconds)\n-- @field timer_interval number How often to check for file changes (milliseconds)\n-- @field show_notifications boolean Show notification when files are reloaded\n\n--- ClaudeCodeGit class for git integration configuration\n-- @table ClaudeCodeGit\n-- @field use_git_root boolean Set CWD to git root when opening Claude Code (if in git project)\n-- @field multi_instance boolean Use multiple Claude instances (one per git root)\n\n--- ClaudeCodeKeymapsToggle class for toggle keymap configuration\n-- @table ClaudeCodeKeymapsToggle\n-- @field normal string|boolean Normal mode keymap for toggling Claude Code, false to disable\n-- @field terminal string|boolean Terminal mode keymap for toggling Claude Code, false to disable\n\n--- ClaudeCodeKeymaps class for keymap configuration\n-- @table ClaudeCodeKeymaps\n-- @field toggle ClaudeCodeKeymapsToggle Keymaps for toggling Claude Code\n-- @field window_navigation boolean Enable window navigation keymaps\n-- @field scrolling boolean Enable scrolling keymaps\n\n--- ClaudeCodeCommandVariants class for command variant configuration\n-- @table ClaudeCodeCommandVariants\n-- Conversation management:\n-- @field continue string|boolean Resume the most recent conversation\n-- @field resume string|boolean Display an interactive conversation picker\n-- Output options:\n-- @field verbose string|boolean Enable verbose logging with full turn-by-turn output\n-- Additional options can be added as needed\n\n--- ClaudeCodeShell class for shell configuration\n-- @table ClaudeCodeShell\n-- @field separator string Command separator used in shell commands (e.g., '&&', ';', '|')\n-- @field pushd_cmd string Command to push directory onto stack (e.g., 'pushd' for bash/zsh)\n-- @field popd_cmd string Command to pop directory from stack (e.g., 'popd' for bash/zsh)\n\n--- ClaudeCodeConfig class for main configuration\n-- @table ClaudeCodeConfig\n-- @field window ClaudeCodeWindow Terminal window settings\n-- @field refresh ClaudeCodeRefresh File refresh settings\n-- @field git ClaudeCodeGit Git integration settings\n-- @field shell ClaudeCodeShell Shell-specific configuration\n-- @field command string Command used to launch Claude Code\n-- @field command_variants ClaudeCodeCommandVariants Command variants configuration\n-- @field keymaps ClaudeCodeKeymaps Keymaps configuration\n\n--- Default configuration options\n--- @type ClaudeCodeConfig\nM.default_config = {\n -- Terminal window settings\n window = {\n split_ratio = 0.3, -- Percentage of screen for the terminal window (height or width)\n height_ratio = 0.3, -- DEPRECATED: Use split_ratio instead\n position = 'botright', -- Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\", etc.\n enter_insert = true, -- Whether to enter insert mode when opening Claude Code\n start_in_normal_mode = false, -- Whether to start in normal mode instead of insert mode\n hide_numbers = true, -- Hide line numbers in the terminal window\n hide_signcolumn = true, -- Hide the sign column in the terminal window\n -- Default floating window configuration\n float = {\n width = '80%', -- Width as percentage of editor\n height = '80%', -- Height as percentage of editor\n row = 'center', -- Center vertically\n col = 'center', -- Center horizontally\n relative = 'editor', -- Position relative to editor\n border = 'rounded', -- Border style\n },\n },\n -- File refresh settings\n refresh = {\n enable = true, -- Enable file change detection\n updatetime = 100, -- updatetime to use when Claude Code is active (milliseconds)\n timer_interval = 1000, -- How often to check for file changes (milliseconds)\n show_notifications = true, -- Show notification when files are reloaded\n },\n -- Git integration settings\n git = {\n use_git_root = true, -- Set CWD to git root when opening Claude Code (if in git project)\n multi_instance = true, -- Use multiple Claude instances (one per git root)\n },\n -- Shell-specific settings\n shell = {\n separator = '&&', -- Command separator used in shell commands\n pushd_cmd = 'pushd', -- Command to push directory onto stack\n popd_cmd = 'popd', -- Command to pop directory from stack\n },\n -- Command settings\n command = 'claude', -- Command used to launch Claude Code\n -- Command variants\n command_variants = {\n -- Conversation management\n continue = '--continue', -- Resume the most recent conversation\n resume = '--resume', -- Display an interactive conversation picker\n\n -- Output options\n verbose = '--verbose', -- Enable verbose logging with full turn-by-turn output\n },\n -- Keymaps\n keymaps = {\n toggle = {\n normal = '', -- Normal mode keymap for toggling Claude Code\n terminal = '', -- Terminal mode keymap for toggling Claude Code\n variants = {\n continue = 'cC', -- Normal mode keymap for Claude Code with continue flag\n verbose = 'cV', -- Normal mode keymap for Claude Code with verbose flag\n },\n },\n window_navigation = true, -- Enable window navigation keymaps ()\n scrolling = true, -- Enable scrolling keymaps () for page up/down\n },\n}\n\n--- Validate the configuration\n--- Validate window configuration\n--- @param window table Window configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_window_config(window)\n if type(window) ~= 'table' then\n return false, 'window config must be a table'\n end\n\n if type(window.split_ratio) ~= 'number' or window.split_ratio <= 0 or window.split_ratio > 1 then\n return false, 'window.split_ratio must be a number between 0 and 1'\n end\n\n if type(window.position) ~= 'string' then\n return false, 'window.position must be a string'\n end\n\n if type(window.enter_insert) ~= 'boolean' then\n return false, 'window.enter_insert must be a boolean'\n end\n\n if type(window.start_in_normal_mode) ~= 'boolean' then\n return false, 'window.start_in_normal_mode must be a boolean'\n end\n\n if type(window.hide_numbers) ~= 'boolean' then\n return false, 'window.hide_numbers must be a boolean'\n end\n\n if type(window.hide_signcolumn) ~= 'boolean' then\n return false, 'window.hide_signcolumn must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate floating window configuration\n--- @param float table Float configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_float_config(float)\n if type(float) ~= 'table' then\n return false, 'window.float must be a table when position is \"float\"'\n end\n\n -- Validate width (can be number or percentage string)\n if type(float.width) == 'string' then\n if not float.width:match('^%d+%%$') then\n return false, 'window.float.width must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.width) ~= 'number' or float.width <= 0 then\n return false, 'window.float.width must be a positive number or percentage string'\n end\n\n -- Validate height (can be number or percentage string)\n if type(float.height) == 'string' then\n if not float.height:match('^%d+%%$') then\n return false, 'window.float.height must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.height) ~= 'number' or float.height <= 0 then\n return false, 'window.float.height must be a positive number or percentage string'\n end\n\n -- Validate relative (must be \"editor\" or \"cursor\")\n if float.relative ~= 'editor' and float.relative ~= 'cursor' then\n return false, 'window.float.relative must be \"editor\" or \"cursor\"'\n end\n\n -- Validate border (must be valid border style)\n local valid_borders = { 'none', 'single', 'double', 'rounded', 'solid', 'shadow' }\n local is_valid_border = false\n for _, border in ipairs(valid_borders) do\n if float.border == border then\n is_valid_border = true\n break\n end\n end\n -- Also allow array borders\n if not is_valid_border and type(float.border) ~= 'table' then\n return false,\n 'window.float.border must be one of: none, single, double, rounded, solid, shadow, or an array'\n end\n\n -- Validate row and col if they exist\n if float.row ~= nil then\n if type(float.row) == 'string' and float.row ~= 'center' then\n if not float.row:match('^%d+%%$') then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n elseif type(float.row) ~= 'number' and float.row ~= 'center' then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n end\n\n if float.col ~= nil then\n if type(float.col) == 'string' and float.col ~= 'center' then\n if not float.col:match('^%d+%%$') then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n elseif type(float.col) ~= 'number' and float.col ~= 'center' then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n end\n\n return true, nil\nend\n\n--- Validate refresh configuration\n--- @param refresh table Refresh configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_refresh_config(refresh)\n if type(refresh) ~= 'table' then\n return false, 'refresh config must be a table'\n end\n\n if type(refresh.enable) ~= 'boolean' then\n return false, 'refresh.enable must be a boolean'\n end\n\n if type(refresh.updatetime) ~= 'number' or refresh.updatetime <= 0 then\n return false, 'refresh.updatetime must be a positive number'\n end\n\n if type(refresh.timer_interval) ~= 'number' or refresh.timer_interval <= 0 then\n return false, 'refresh.timer_interval must be a positive number'\n end\n\n if type(refresh.show_notifications) ~= 'boolean' then\n return false, 'refresh.show_notifications must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate git configuration\n--- @param git table Git configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_git_config(git)\n if type(git) ~= 'table' then\n return false, 'git config must be a table'\n end\n\n if type(git.use_git_root) ~= 'boolean' then\n return false, 'git.use_git_root must be a boolean'\n end\n\n if type(git.multi_instance) ~= 'boolean' then\n return false, 'git.multi_instance must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate shell configuration\n--- @param shell table Shell configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_shell_config(shell)\n if type(shell) ~= 'table' then\n return false, 'shell config must be a table'\n end\n\n if type(shell.separator) ~= 'string' then\n return false, 'shell.separator must be a string'\n end\n\n if type(shell.pushd_cmd) ~= 'string' then\n return false, 'shell.pushd_cmd must be a string'\n end\n\n if type(shell.popd_cmd) ~= 'string' then\n return false, 'shell.popd_cmd must be a string'\n end\n\n return true, nil\nend\n\n--- Validate keymaps configuration\n--- @param keymaps table Keymaps configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_keymaps_config(keymaps)\n if type(keymaps) ~= 'table' then\n return false, 'keymaps config must be a table'\n end\n\n if type(keymaps.toggle) ~= 'table' then\n return false, 'keymaps.toggle must be a table'\n end\n\n if not (keymaps.toggle.normal == false or type(keymaps.toggle.normal) == 'string') then\n return false, 'keymaps.toggle.normal must be a string or false'\n end\n\n if not (keymaps.toggle.terminal == false or type(keymaps.toggle.terminal) == 'string') then\n return false, 'keymaps.toggle.terminal must be a string or false'\n end\n\n -- Validate variant keymaps if they exist\n if keymaps.toggle.variants then\n if type(keymaps.toggle.variants) ~= 'table' then\n return false, 'keymaps.toggle.variants must be a table'\n end\n\n -- Check each variant keymap\n for variant_name, keymap in pairs(keymaps.toggle.variants) do\n if not (keymap == false or type(keymap) == 'string') then\n return false, 'keymaps.toggle.variants.' .. variant_name .. ' must be a string or false'\n end\n end\n end\n\n if type(keymaps.window_navigation) ~= 'boolean' then\n return false, 'keymaps.window_navigation must be a boolean'\n end\n\n if type(keymaps.scrolling) ~= 'boolean' then\n return false, 'keymaps.scrolling must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate command variants configuration\n--- @param command_variants table Command variants configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_command_variants_config(command_variants)\n if type(command_variants) ~= 'table' then\n return false, 'command_variants config must be a table'\n end\n\n -- Check each command variant\n for variant_name, variant_args in pairs(command_variants) do\n if not (variant_args == false or type(variant_args) == 'string') then\n return false, 'command_variants.' .. variant_name .. ' must be a string or false'\n end\n end\n\n return true, nil\nend\n\n--- Validate configuration options\n--- @param config ClaudeCodeConfig\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_config(config)\n -- Validate window settings\n local valid, err = validate_window_config(config.window)\n if not valid then\n return false, err\n end\n\n -- Validate float configuration if position is \"float\"\n if config.window.position == 'float' then\n valid, err = validate_float_config(config.window.float)\n if not valid then\n return false, err\n end\n end\n\n -- Validate refresh settings\n valid, err = validate_refresh_config(config.refresh)\n if not valid then\n return false, err\n end\n\n -- Validate git settings\n valid, err = validate_git_config(config.git)\n if not valid then\n return false, err\n end\n\n -- Validate shell settings\n valid, err = validate_shell_config(config.shell)\n if not valid then\n return false, err\n end\n\n -- Validate command settings\n if type(config.command) ~= 'string' then\n return false, 'command must be a string'\n end\n\n -- Validate command variants settings\n valid, err = validate_command_variants_config(config.command_variants)\n if not valid then\n return false, err\n end\n\n -- Validate keymaps settings\n valid, err = validate_keymaps_config(config.keymaps)\n if not valid then\n return false, err\n end\n\n -- Cross-validate keymaps with command variants\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n -- Ensure variant exists in command_variants\n if keymap ~= false and not config.command_variants[variant_name] then\n return false,\n 'keymaps.toggle.variants.' .. variant_name .. ' has no corresponding command variant'\n end\n end\n end\n\n return true, nil\nend\n\n--- Parse user configuration and merge with defaults\n--- @param user_config? table\n--- @param silent? boolean Set to true to suppress error notifications (for tests)\n--- @return ClaudeCodeConfig\nfunction M.parse_config(user_config, silent)\n -- Handle backward compatibility first\n if user_config and user_config.window then\n if user_config.window.height_ratio and not user_config.window.split_ratio then\n -- Copy height_ratio to split_ratio for backward compatibility\n user_config.window.split_ratio = user_config.window.height_ratio\n end\n end\n\n local config = vim.tbl_deep_extend('force', {}, M.default_config, user_config or {})\n\n -- If position is float and no float config provided, use default float config\n if\n config.window.position == 'float'\n and not (user_config and user_config.window and user_config.window.float)\n then\n config.window.float = vim.deepcopy(M.default_config.window.float)\n end\n\n local valid, err = validate_config(config)\n if not valid then\n -- Only notify if not in silent mode\n if not silent then\n vim.notify('Claude Code: ' .. err, vim.log.levels.ERROR)\n end\n -- Fall back to default config in case of error\n return vim.deepcopy(M.default_config)\n end\n\n return config\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/init.lua", "---@mod claude-code Claude Code Neovim Integration\n---@brief [[\n--- A plugin for seamless integration between Claude Code AI assistant and Neovim.\n--- This plugin provides a terminal-based interface to Claude Code within Neovim.\n---\n--- Requirements:\n--- - Neovim 0.7.0 or later\n--- - Claude Code CLI tool installed and available in PATH\n--- - plenary.nvim (dependency for git operations)\n---\n--- Usage:\n--- ```lua\n--- require('claude-code').setup({\n--- -- Configuration options (optional)\n--- })\n--- ```\n---@brief ]]\n\n-- Import modules\nlocal config = require('claude-code.config')\nlocal commands = require('claude-code.commands')\nlocal keymaps = require('claude-code.keymaps')\nlocal file_refresh = require('claude-code.file_refresh')\nlocal terminal = require('claude-code.terminal')\nlocal git = require('claude-code.git')\nlocal version = require('claude-code.version')\n\nlocal M = {}\n\n-- Make imported modules available\nM.commands = commands\n\n-- Store the current configuration\n--- @type table\nM.config = {}\n\n-- Terminal buffer and window management\n--- @type table\nM.claude_code = terminal.terminal\n\n--- Force insert mode when entering the Claude Code window\n--- This is a public function used in keymaps\nfunction M.force_insert_mode()\n terminal.force_insert_mode(M, M.config)\nend\n\n--- Get the current active buffer number\n--- @return number|nil bufnr Current Claude instance buffer number or nil\nlocal function get_current_buffer_number()\n -- Get current instance from the instances table\n local current_instance = M.claude_code.current_instance\n if current_instance and type(M.claude_code.instances) == 'table' then\n return M.claude_code.instances[current_instance]\n end\n return nil\nend\n\n--- Toggle the Claude Code terminal window\n--- This is a public function used by commands\nfunction M.toggle()\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\nend\n\n--- Toggle the Claude Code terminal window with a specific command variant\n--- @param variant_name string The name of the command variant to use\nfunction M.toggle_with_variant(variant_name)\n if not variant_name or not M.config.command_variants[variant_name] then\n -- If variant doesn't exist, fall back to regular toggle\n return M.toggle()\n end\n\n -- Store the original command\n local original_command = M.config.command\n\n -- Set the command with the variant args\n M.config.command = original_command .. ' ' .. M.config.command_variants[variant_name]\n\n -- Call the toggle function with the modified command\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\n\n -- Restore the original command\n M.config.command = original_command\nend\n\n--- Get the current version of the plugin\n--- @return string version Current version string\nfunction M.get_version()\n return version.string()\nend\n\n--- Version information\nM.version = version\n\n--- Setup function for the plugin\n--- @param user_config? table User configuration table (optional)\nfunction M.setup(user_config)\n -- Parse and validate configuration\n -- Don't use silent mode for regular usage - users should see config errors\n M.config = config.parse_config(user_config, false)\n\n -- Set up autoread option\n vim.o.autoread = true\n\n -- Set up file refresh functionality\n file_refresh.setup(M, M.config)\n\n -- Register commands\n commands.register_commands(M)\n\n -- Register keymaps\n keymaps.register_keymaps(M, M.config)\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/file_refresh.lua", "---@mod claude-code.file_refresh File refresh functionality for claude-code.nvim\n---@brief [[\n--- This module provides file refresh functionality to detect and reload files\n--- that have been modified by Claude Code or other external processes.\n---@brief ]]\n\nlocal M = {}\n\n--- Timer for checking file changes\n--- @type userdata|nil\nlocal refresh_timer = nil\n\n--- Setup autocommands for file change detection\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup(claude_code, config)\n if not config.refresh.enable then\n return\n end\n\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeFileRefresh', { clear = true })\n\n -- Create an autocommand that checks for file changes more frequently\n vim.api.nvim_create_autocmd({\n 'CursorHold',\n 'CursorHoldI',\n 'FocusGained',\n 'BufEnter',\n 'InsertLeave',\n 'TextChanged',\n 'TermLeave',\n 'TermEnter',\n 'BufWinEnter',\n }, {\n group = augroup,\n pattern = '*',\n callback = function()\n if vim.fn.filereadable(vim.fn.expand '%') == 1 then\n vim.cmd 'checktime'\n end\n end,\n desc = 'Check for file changes on disk',\n })\n\n -- Clean up any existing timer\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\n\n -- Create a timer to check for file changes periodically\n refresh_timer = vim.loop.new_timer()\n if refresh_timer then\n refresh_timer:start(\n 0,\n config.refresh.timer_interval,\n vim.schedule_wrap(function()\n -- Only check time if there's an active Claude Code terminal\n local current_instance = claude_code.claude_code.current_instance\n local bufnr = current_instance and claude_code.claude_code.instances[current_instance]\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) and #vim.fn.win_findbuf(bufnr) > 0 then\n vim.cmd 'silent! checktime'\n end\n end)\n )\n end\n\n -- Create an autocommand that notifies when a file has been changed externally\n if config.refresh.show_notifications then\n vim.api.nvim_create_autocmd('FileChangedShellPost', {\n group = augroup,\n pattern = '*',\n callback = function()\n vim.notify('File changed on disk. Buffer reloaded.', vim.log.levels.INFO)\n end,\n desc = 'Notify when a file is changed externally',\n })\n end\n\n -- Set a shorter updatetime while Claude Code is open\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n\n -- When Claude Code opens, set a shorter updatetime\n vim.api.nvim_create_autocmd('TermOpen', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf = vim.api.nvim_get_current_buf()\n local buf_name = vim.api.nvim_buf_get_name(buf)\n if buf_name:match('claude%-code$') then\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n vim.o.updatetime = config.refresh.updatetime\n end\n end,\n desc = 'Set shorter updatetime when Claude Code is open',\n })\n\n -- When Claude Code closes, restore normal updatetime\n vim.api.nvim_create_autocmd('TermClose', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf_name = vim.api.nvim_buf_get_name(0)\n if buf_name:match('claude%-code$') then\n vim.o.updatetime = claude_code.claude_code.saved_updatetime\n end\n end,\n desc = 'Restore normal updatetime when Claude Code is closed',\n })\nend\n\n--- Clean up the file refresh functionality (stop the timer)\nfunction M.cleanup()\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/keymaps.lua", "---@mod claude-code.keymaps Keymap management for claude-code.nvim\n---@brief [[\n--- This module provides keymap registration and handling for claude-code.nvim.\n--- It handles normal mode, terminal mode, and window navigation keymaps.\n---@brief ]]\n\nlocal M = {}\n\n--- Register keymaps for claude-code.nvim\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.register_keymaps(claude_code, config)\n local map_opts = { noremap = true, silent = true }\n\n -- Normal mode toggle keymaps\n if config.keymaps.toggle.normal then\n vim.api.nvim_set_keymap(\n 'n',\n config.keymaps.toggle.normal,\n [[ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n if config.keymaps.toggle.terminal then\n -- Terminal mode toggle keymap\n -- In terminal mode, special keys like Ctrl need different handling\n -- We use a direct escape sequence approach for more reliable terminal mappings\n vim.api.nvim_set_keymap(\n 't',\n config.keymaps.toggle.terminal,\n [[:ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n -- Register variant keymaps if configured\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_set_keymap(\n 'n',\n keymap,\n string.format([[%s]], cmd_name),\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: ' .. capitalized_name })\n )\n end\n end\n end\n\n -- Register with which-key if it's available\n vim.defer_fn(function()\n local status_ok, which_key = pcall(require, 'which-key')\n if status_ok then\n if config.keymaps.toggle.normal then\n which_key.add {\n mode = 'n',\n { config.keymaps.toggle.normal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.terminal then\n which_key.add {\n mode = 't',\n { config.keymaps.toggle.terminal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n\n -- Register variant keymaps with which-key\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n which_key.add {\n mode = 'n',\n { keymap, desc = 'Claude Code: ' .. capitalized_name, icon = '🤖' },\n }\n end\n end\n end\n end\n end, 100)\nend\n\n--- Set up terminal-specific keymaps for window navigation\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup_terminal_navigation(claude_code, config)\n -- Get current active Claude instance buffer\n local current_instance = claude_code.claude_code.current_instance\n local buf = current_instance and claude_code.claude_code.instances[current_instance]\n if buf and vim.api.nvim_buf_is_valid(buf) then\n -- Create autocommand to enter insert mode when the terminal window gets focus\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeTerminalFocus_' .. buf, { clear = true })\n\n -- Set up multiple events for more reliable focus detection\n vim.api.nvim_create_autocmd(\n { 'WinEnter', 'BufEnter', 'WinLeave', 'FocusGained', 'CmdLineLeave' },\n {\n group = augroup,\n callback = function()\n vim.schedule(claude_code.force_insert_mode)\n end,\n desc = 'Auto-enter insert mode when focusing Claude Code terminal',\n }\n )\n\n -- Window navigation keymaps\n if config.keymaps.window_navigation then\n -- Window navigation keymaps with special handling to force insert mode in the target window\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n\n -- Also add normal mode mappings for when user is in normal mode in the terminal\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n end\n\n -- Add scrolling keymaps\n if config.keymaps.scrolling then\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page up' }\n )\n end\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/commands.lua", "---@mod claude-code.commands Command registration for claude-code.nvim\n---@brief [[\n--- This module provides command registration and handling for claude-code.nvim.\n--- It defines user commands and command handlers.\n---@brief ]]\n\nlocal M = {}\n\n--- @type table List of available commands and their handlers\nM.commands = {}\n\n--- Register commands for the claude-code plugin\n--- @param claude_code table The main plugin module\nfunction M.register_commands(claude_code)\n -- Create the user command for toggling Claude Code\n vim.api.nvim_create_user_command('ClaudeCode', function()\n claude_code.toggle()\n end, { desc = 'Toggle Claude Code terminal' })\n\n -- Create commands for each command variant\n for variant_name, variant_args in pairs(claude_code.config.command_variants) do\n if variant_args ~= false then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_create_user_command(cmd_name, function()\n claude_code.toggle_with_variant(variant_name)\n end, { desc = 'Toggle Claude Code terminal with ' .. variant_name .. ' option' })\n end\n end\n\n -- Add version command\n vim.api.nvim_create_user_command('ClaudeCodeVersion', function()\n vim.notify('Claude Code version: ' .. claude_code.version(), vim.log.levels.INFO)\n end, { desc = 'Display Claude Code version' })\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/git.lua", "---@mod claude-code.git Git integration for claude-code.nvim\n---@brief [[\n--- This module provides git integration functionality for claude-code.nvim.\n--- It detects git repositories and can set the working directory to the git root.\n---@brief ]]\n\nlocal M = {}\n\n--- Helper function to get git root directory\n--- @return string|nil git_root The git root directory path or nil if not in a git repo\nfunction M.get_git_root()\n -- For testing compatibility\n if vim.env.CLAUDE_CODE_TEST_MODE == 'true' then\n return '/home/user/project'\n end\n\n -- Check if we're in a git repository\n local handle = io.popen('git rev-parse --is-inside-work-tree 2>/dev/null')\n if not handle then\n return nil\n end\n\n local result = handle:read('*a')\n handle:close()\n\n -- Strip trailing whitespace and newlines for reliable matching\n result = result:gsub('[\\n\\r%s]*$', '')\n\n if result == 'true' then\n -- Get the git root path\n local root_handle = io.popen('git rev-parse --show-toplevel 2>/dev/null')\n if not root_handle then\n return nil\n end\n\n local git_root = root_handle:read('*a')\n root_handle:close()\n\n -- Remove trailing whitespace and newlines\n git_root = git_root:gsub('[\\n\\r%s]*$', '')\n\n return git_root\n end\n\n return nil\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/version.lua", "---@mod claude-code.version Version information for claude-code.nvim\n---@brief [[\n--- This module provides version information for claude-code.nvim.\n---@brief ]]\n\n--- @table M\n--- Version information for Claude Code\n--- @field major number Major version (breaking changes)\n--- @field minor number Minor version (new features)\n--- @field patch number Patch version (bug fixes)\n--- @field string function Returns formatted version string\n\nlocal M = {}\n\n-- Individual version components\nM.major = 0\nM.minor = 4\nM.patch = 2\n\n-- Combined semantic version\nM.version = string.format('%d.%d.%d', M.major, M.minor, M.patch)\n\n--- Returns the formatted version string (for backward compatibility)\n--- @return string Version string in format \"major.minor.patch\"\nfunction M.string()\n return M.version\nend\n\n--- Prints the current version of the plugin\nfunction M.print_version()\n vim.notify('Claude Code version: ' .. M.string(), vim.log.levels.INFO)\nend\n\nreturn M\n"]], "task_instance_info": {"created_time": "2025-08-21 00:40:22", "created_task_model": "DeepSeek-R1", "code_description": "This code segment functions to: Enforce insertion mode activation upon entering a terminal interface unless configured for normal mode operation, while validating current interaction state against stored session data."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```lua\n[TASK_BEGIN]\nfunction M.setup_autocmds(claude_code, config)\n -- Create autocommand group to avoid duplicates\n vim.api.nvim_create_augroup('ClaudeCodeTerminal', { clear = true })\n\n -- Automatically enter insert mode when focusing terminal window\n vim.api.nvim_create_autocmd('BufEnter', {\n group = 'ClaudeCodeTerminal',\n pattern = 'claude-code*',\n callback = function()\n -- Only force insert mode if not configured to start in normal mode\n if not config.window.start_in_normal_mode then\n -- Use schedule to ensure proper mode transition\n vim.schedule(function()\n if vim.fn.mode() ~= 'i' then\n vim.cmd('startinsert')\n end\n end)\n end\n end\n })\n\n -- Preserve normal mode when leaving terminal window\n vim.api.nvim_create_autocmd('BufLeave', {\n group = 'ClaudeCodeTerminal',\n pattern = 'claude-code*',\n callback = function()\n if vim.fn.mode() == 'i' then\n vim.cmd('stopinsert')\n end\n end\n })\nend\n[TASK_END]\n```", "inference_time": "2025-08-21 00-40-46"}, "editdistance_info": {"edit_distance": 16.2304, "calculate_time": "2025-08-21 00:40:46", "true_code_clean": "function M.force_insert_mode(claude_code, config)\n local current_bufnr = vim.fn.bufnr('%')\n local is_claude_instance = false\n for _, bufnr in pairs(claude_code.claude_code.instances) do\n if bufnr and bufnr == current_bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n is_claude_instance = true\n break\n end\n end\n if is_claude_instance then\n if config.window.start_in_normal_mode then\n return\n end\n local mode = vim.api.nvim_get_mode().mode\n if vim.bo.buftype == 'terminal' and mode ~= 't' and mode ~= 'i' then\n vim.cmd 'silent! stopinsert'\n vim.schedule(function()\n vim.cmd 'silent! startinsert'\n end)\n end\n end\nend", "predict_code_clean": "function M.setup_autocmds(claude_code, config)\n vim.api.nvim_create_augroup('ClaudeCodeTerminal', { clear = true })\n vim.api.nvim_create_autocmd('BufEnter', {\n group = 'ClaudeCodeTerminal',\n pattern = 'claude-code*',\n callback = function()\n if not config.window.start_in_normal_mode then\n vim.schedule(function()\n if vim.fn.mode() ~= 'i' then\n vim.cmd('startinsert')\n end\n end)\n end\n end\n })\n vim.api.nvim_create_autocmd('BufLeave', {\n group = 'ClaudeCodeTerminal',\n pattern = 'claude-code*',\n callback = function()\n if vim.fn.mode() == 'i' then\n vim.cmd('stopinsert')\n end\n end\n })\nend"}} {"repo_name": "claude-code.nvim", "file_name": "/claude-code.nvim/lua/claude-code/commands.lua", "inference_info": {"prefix_code": "---@mod claude-code.commands Command registration for claude-code.nvim\n---@brief [[\n--- This module provides command registration and handling for claude-code.nvim.\n--- It defines user commands and command handlers.\n---@brief ]]\n\nlocal M = {}\n\n--- @type table List of available commands and their handlers\nM.commands = {}\n\n--- Register commands for the claude-code plugin\n--- @param claude_code table The main plugin module\n", "suffix_code": "\n\nreturn M\n", "middle_code": "function M.register_commands(claude_code)\n vim.api.nvim_create_user_command('ClaudeCode', function()\n claude_code.toggle()\n end, { desc = 'Toggle Claude Code terminal' })\n for variant_name, variant_args in pairs(claude_code.config.command_variants) do\n if variant_args ~= false then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n vim.api.nvim_create_user_command(cmd_name, function()\n claude_code.toggle_with_variant(variant_name)\n end, { desc = 'Toggle Claude Code terminal with ' .. variant_name .. ' option' })\n end\n end\n vim.api.nvim_create_user_command('ClaudeCodeVersion', function()\n vim.notify('Claude Code version: ' .. claude_code.version(), vim.log.levels.INFO)\n end, { desc = 'Display Claude Code version' })\nend", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "lua", "sub_task_type": null}, "context_code": [["/claude-code.nvim/lua/claude-code/keymaps.lua", "---@mod claude-code.keymaps Keymap management for claude-code.nvim\n---@brief [[\n--- This module provides keymap registration and handling for claude-code.nvim.\n--- It handles normal mode, terminal mode, and window navigation keymaps.\n---@brief ]]\n\nlocal M = {}\n\n--- Register keymaps for claude-code.nvim\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.register_keymaps(claude_code, config)\n local map_opts = { noremap = true, silent = true }\n\n -- Normal mode toggle keymaps\n if config.keymaps.toggle.normal then\n vim.api.nvim_set_keymap(\n 'n',\n config.keymaps.toggle.normal,\n [[ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n if config.keymaps.toggle.terminal then\n -- Terminal mode toggle keymap\n -- In terminal mode, special keys like Ctrl need different handling\n -- We use a direct escape sequence approach for more reliable terminal mappings\n vim.api.nvim_set_keymap(\n 't',\n config.keymaps.toggle.terminal,\n [[:ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n -- Register variant keymaps if configured\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_set_keymap(\n 'n',\n keymap,\n string.format([[%s]], cmd_name),\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: ' .. capitalized_name })\n )\n end\n end\n end\n\n -- Register with which-key if it's available\n vim.defer_fn(function()\n local status_ok, which_key = pcall(require, 'which-key')\n if status_ok then\n if config.keymaps.toggle.normal then\n which_key.add {\n mode = 'n',\n { config.keymaps.toggle.normal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.terminal then\n which_key.add {\n mode = 't',\n { config.keymaps.toggle.terminal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n\n -- Register variant keymaps with which-key\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n which_key.add {\n mode = 'n',\n { keymap, desc = 'Claude Code: ' .. capitalized_name, icon = '🤖' },\n }\n end\n end\n end\n end\n end, 100)\nend\n\n--- Set up terminal-specific keymaps for window navigation\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup_terminal_navigation(claude_code, config)\n -- Get current active Claude instance buffer\n local current_instance = claude_code.claude_code.current_instance\n local buf = current_instance and claude_code.claude_code.instances[current_instance]\n if buf and vim.api.nvim_buf_is_valid(buf) then\n -- Create autocommand to enter insert mode when the terminal window gets focus\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeTerminalFocus_' .. buf, { clear = true })\n\n -- Set up multiple events for more reliable focus detection\n vim.api.nvim_create_autocmd(\n { 'WinEnter', 'BufEnter', 'WinLeave', 'FocusGained', 'CmdLineLeave' },\n {\n group = augroup,\n callback = function()\n vim.schedule(claude_code.force_insert_mode)\n end,\n desc = 'Auto-enter insert mode when focusing Claude Code terminal',\n }\n )\n\n -- Window navigation keymaps\n if config.keymaps.window_navigation then\n -- Window navigation keymaps with special handling to force insert mode in the target window\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n\n -- Also add normal mode mappings for when user is in normal mode in the terminal\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n end\n\n -- Add scrolling keymaps\n if config.keymaps.scrolling then\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page up' }\n )\n end\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/init.lua", "---@mod claude-code Claude Code Neovim Integration\n---@brief [[\n--- A plugin for seamless integration between Claude Code AI assistant and Neovim.\n--- This plugin provides a terminal-based interface to Claude Code within Neovim.\n---\n--- Requirements:\n--- - Neovim 0.7.0 or later\n--- - Claude Code CLI tool installed and available in PATH\n--- - plenary.nvim (dependency for git operations)\n---\n--- Usage:\n--- ```lua\n--- require('claude-code').setup({\n--- -- Configuration options (optional)\n--- })\n--- ```\n---@brief ]]\n\n-- Import modules\nlocal config = require('claude-code.config')\nlocal commands = require('claude-code.commands')\nlocal keymaps = require('claude-code.keymaps')\nlocal file_refresh = require('claude-code.file_refresh')\nlocal terminal = require('claude-code.terminal')\nlocal git = require('claude-code.git')\nlocal version = require('claude-code.version')\n\nlocal M = {}\n\n-- Make imported modules available\nM.commands = commands\n\n-- Store the current configuration\n--- @type table\nM.config = {}\n\n-- Terminal buffer and window management\n--- @type table\nM.claude_code = terminal.terminal\n\n--- Force insert mode when entering the Claude Code window\n--- This is a public function used in keymaps\nfunction M.force_insert_mode()\n terminal.force_insert_mode(M, M.config)\nend\n\n--- Get the current active buffer number\n--- @return number|nil bufnr Current Claude instance buffer number or nil\nlocal function get_current_buffer_number()\n -- Get current instance from the instances table\n local current_instance = M.claude_code.current_instance\n if current_instance and type(M.claude_code.instances) == 'table' then\n return M.claude_code.instances[current_instance]\n end\n return nil\nend\n\n--- Toggle the Claude Code terminal window\n--- This is a public function used by commands\nfunction M.toggle()\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\nend\n\n--- Toggle the Claude Code terminal window with a specific command variant\n--- @param variant_name string The name of the command variant to use\nfunction M.toggle_with_variant(variant_name)\n if not variant_name or not M.config.command_variants[variant_name] then\n -- If variant doesn't exist, fall back to regular toggle\n return M.toggle()\n end\n\n -- Store the original command\n local original_command = M.config.command\n\n -- Set the command with the variant args\n M.config.command = original_command .. ' ' .. M.config.command_variants[variant_name]\n\n -- Call the toggle function with the modified command\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\n\n -- Restore the original command\n M.config.command = original_command\nend\n\n--- Get the current version of the plugin\n--- @return string version Current version string\nfunction M.get_version()\n return version.string()\nend\n\n--- Version information\nM.version = version\n\n--- Setup function for the plugin\n--- @param user_config? table User configuration table (optional)\nfunction M.setup(user_config)\n -- Parse and validate configuration\n -- Don't use silent mode for regular usage - users should see config errors\n M.config = config.parse_config(user_config, false)\n\n -- Set up autoread option\n vim.o.autoread = true\n\n -- Set up file refresh functionality\n file_refresh.setup(M, M.config)\n\n -- Register commands\n commands.register_commands(M)\n\n -- Register keymaps\n keymaps.register_keymaps(M, M.config)\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/terminal.lua", "---@mod claude-code.terminal Terminal management for claude-code.nvim\n---@brief [[\n--- This module provides terminal buffer management for claude-code.nvim.\n--- It handles creating, toggling, and managing the terminal window.\n---@brief ]]\n\nlocal M = {}\n\n--- Terminal buffer and window management\n-- @table ClaudeCodeTerminal\n-- @field instances table Key-value store of git root to buffer number\n-- @field saved_updatetime number|nil Original updatetime before Claude Code was opened\n-- @field current_instance string|nil Current git root path for active instance\nM.terminal = {\n instances = {},\n saved_updatetime = nil,\n current_instance = nil,\n}\n\n--- Get the current git root or a fallback identifier\n--- @param git table The git module\n--- @return string identifier Git root path or fallback identifier\nlocal function get_instance_identifier(git)\n local git_root = git.get_git_root()\n if git_root then\n return git_root\n else\n -- Fallback to current working directory if not in a git repo\n return vim.fn.getcwd()\n end\nend\n\n--- Calculate floating window dimensions from percentage strings\n--- @param value number|string Dimension value (number or percentage string)\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated dimension\n--- @private\nlocal function calculate_float_dimension(value, max_value)\n if value == nil then\n return math.floor(max_value * 0.8) -- Default to 80% if not specified\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n return math.floor(max_value * percentage / 100)\n end\n return value\nend\n\n--- Calculate floating window position for centering\n--- @param value number|string Position value (number, \"center\", or percentage)\n--- @param window_size number Size of the window\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated position\n--- @private\nlocal function calculate_float_position(value, window_size, max_value)\n local pos\n if value == 'center' then\n pos = math.floor((max_value - window_size) / 2)\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n pos = math.floor(max_value * percentage / 100)\n else\n pos = value or 0\n end\n -- Clamp position to ensure window is visible\n return math.max(0, math.min(pos, max_value - window_size))\nend\n\n--- Create a floating window for Claude Code\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the float (optional)\n--- @return number Window ID of the created floating window\n--- @private\nlocal function create_float(config, existing_bufnr)\n local float_config = config.window.float or {}\n\n -- Get editor dimensions (accounting for command line, status line, etc.)\n local editor_width = vim.o.columns\n local editor_height = vim.o.lines - vim.o.cmdheight - 1 -- Subtract command line and status line\n\n -- Calculate dimensions\n local width = calculate_float_dimension(float_config.width, editor_width)\n local height = calculate_float_dimension(float_config.height, editor_height)\n\n -- Calculate position\n local row = calculate_float_position(float_config.row, height, editor_height)\n local col = calculate_float_position(float_config.col, width, editor_width)\n\n -- Create floating window configuration\n local win_config = {\n relative = float_config.relative or 'editor',\n width = width,\n height = height,\n row = row,\n col = col,\n border = float_config.border or 'rounded',\n style = 'minimal',\n }\n\n -- Create buffer if we don't have an existing one\n local bufnr = existing_bufnr\n if not bufnr then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n -- Validate existing buffer is still valid and a terminal\n if not vim.api.nvim_buf_is_valid(bufnr) then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n local buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n if buftype ~= 'terminal' then\n -- Buffer exists but is no longer a terminal, create a new one\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n end\n end\n end\n\n -- Create and return the floating window\n return vim.api.nvim_open_win(bufnr, true, win_config)\nend\n\n--- Build command with git root directory if configured\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param base_cmd string Base command to run\n--- @return string Command with git root directory change if applicable\n--- @private\nlocal function build_command_with_git_root(config, git, base_cmd)\n if config.git and config.git.use_git_root then\n local git_root = git.get_git_root()\n if git_root then\n local quoted_root = vim.fn.shellescape(git_root)\n -- Use configurable shell commands\n local separator = config.shell.separator\n local pushd_cmd = config.shell.pushd_cmd\n local popd_cmd = config.shell.popd_cmd\n return pushd_cmd\n .. ' '\n .. quoted_root\n .. ' '\n .. separator\n .. ' '\n .. base_cmd\n .. ' '\n .. separator\n .. ' '\n .. popd_cmd\n end\n end\n return base_cmd\nend\n\n--- Configure common window options\n--- @param win_id number Window ID to configure\n--- @param config table Plugin configuration\n--- @private\nlocal function configure_window_options(win_id, config)\n if config.window.hide_numbers then\n vim.api.nvim_set_option_value('number', false, {win = win_id})\n vim.api.nvim_set_option_value('relativenumber', false, {win = win_id})\n end\n\n if config.window.hide_signcolumn then\n vim.api.nvim_set_option_value('signcolumn', 'no', {win = win_id})\n end\nend\n\n--- Generate buffer name for instance\n--- @param instance_id string Instance identifier\n--- @param config table Plugin configuration\n--- @return string Buffer name\n--- @private\nlocal function generate_buffer_name(instance_id, config)\n if config.git.multi_instance then\n return 'claude-code-' .. instance_id:gsub('[^%w%-_]', '-')\n else\n return 'claude-code'\n end\nend\n\n--- Create a split window according to the specified position configuration\n--- @param position string Window position configuration\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the split (optional)\n--- @private\nlocal function create_split(position, config, existing_bufnr)\n -- Handle floating window\n if position == 'float' then\n return create_float(config, existing_bufnr)\n end\n\n local is_vertical = position:match('vsplit') or position:match('vertical')\n\n -- Create the window with the user's specified command\n -- If the command already contains 'split', use it as is\n if position:match('split') then\n vim.cmd(position)\n else\n -- Otherwise append the appropriate split command\n local split_cmd = is_vertical and 'vsplit' or 'split'\n vim.cmd(position .. ' ' .. split_cmd)\n end\n\n -- If we have an existing buffer to display, switch to it\n if existing_bufnr then\n vim.cmd('buffer ' .. existing_bufnr)\n end\n\n -- Resize the window appropriately based on split type\n if is_vertical then\n vim.cmd('vertical resize ' .. math.floor(vim.o.columns * config.window.split_ratio))\n else\n vim.cmd('resize ' .. math.floor(vim.o.lines * config.window.split_ratio))\n end\nend\n\n--- Set up function to force insert mode when entering the Claude Code window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.force_insert_mode(claude_code, config)\n local current_bufnr = vim.fn.bufnr('%')\n\n -- Check if current buffer is any of our Claude instances\n local is_claude_instance = false\n for _, bufnr in pairs(claude_code.claude_code.instances) do\n if bufnr and bufnr == current_bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n is_claude_instance = true\n break\n end\n end\n\n if is_claude_instance then\n -- Only enter insert mode if we're in the terminal buffer and not already in insert mode\n -- and not configured to stay in normal mode\n if config.window.start_in_normal_mode then\n return\n end\n\n local mode = vim.api.nvim_get_mode().mode\n if vim.bo.buftype == 'terminal' and mode ~= 't' and mode ~= 'i' then\n vim.cmd 'silent! stopinsert'\n vim.schedule(function()\n vim.cmd 'silent! startinsert'\n end)\n end\n end\nend\n\n--- Determine instance ID based on configuration\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @return string instance_id Instance identifier\n--- @private\nlocal function get_instance_id(config, git)\n if config.git.multi_instance then\n if config.git.use_git_root then\n return get_instance_identifier(git)\n else\n return vim.fn.getcwd()\n end\n else\n -- Use a fixed ID for single instance mode\n return 'global'\n end\nend\n\n--- Check if buffer is a valid terminal\n--- @param bufnr number Buffer number\n--- @return boolean is_valid True if buffer is a valid terminal\n--- @private\nlocal function is_valid_terminal_buffer(bufnr)\n if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n\n local buftype = nil\n pcall(function()\n buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n end)\n \n local terminal_job_id = nil\n pcall(function()\n terminal_job_id = vim.b[bufnr].terminal_job_id\n end)\n\n return buftype == 'terminal'\n and terminal_job_id\n and vim.fn.jobwait({ terminal_job_id }, 0)[1] == -1\nend\n\n--- Handle existing instance (toggle visibility)\n--- @param bufnr number Buffer number\n--- @param config table Plugin configuration\n--- @private\nlocal function handle_existing_instance(bufnr, config)\n local win_ids = vim.fn.win_findbuf(bufnr)\n if #win_ids > 0 then\n -- Claude Code is visible, close the window\n for _, win_id in ipairs(win_ids) do\n vim.api.nvim_win_close(win_id, true)\n end\n else\n -- Claude Code buffer exists but is not visible, open it in a split or float\n if config.window.position == 'float' then\n create_float(config, bufnr)\n else\n create_split(config.window.position, config, bufnr)\n end\n -- Force insert mode more aggressively unless configured to start in normal mode\n if not config.window.start_in_normal_mode then\n vim.schedule(function()\n vim.cmd 'stopinsert | startinsert'\n end)\n end\n end\nend\n\n--- Create new Claude Code instance\n--- @param claude_code table The main plugin module\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param instance_id string Instance identifier\n--- @private\nlocal function create_new_instance(claude_code, config, git, instance_id)\n if config.window.position == 'float' then\n -- For floating window, create buffer first with terminal\n local new_bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n vim.api.nvim_set_option_value('bufhidden', 'hide', {buf = new_bufnr})\n\n -- Create the floating window\n local win_id = create_float(config, new_bufnr)\n\n -- Set current buffer to run terminal command\n vim.api.nvim_win_set_buf(win_id, new_bufnr)\n\n -- Determine command\n local cmd = build_command_with_git_root(config, git, config.command)\n\n -- Run terminal in the buffer\n vim.fn.termopen(cmd)\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.api.nvim_buf_set_name(new_bufnr, buffer_name)\n\n -- Configure window options\n configure_window_options(win_id, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = new_bufnr\n\n -- Enter insert mode if configured\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n else\n -- Regular split window\n create_split(config.window.position, config)\n\n -- Determine if we should use the git root directory\n local base_cmd = build_command_with_git_root(config, git, config.command)\n local cmd = 'terminal ' .. base_cmd\n\n vim.cmd(cmd)\n vim.cmd 'setlocal bufhidden=hide'\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.cmd('file ' .. buffer_name)\n\n -- Configure window options using helper function\n local current_win = vim.api.nvim_get_current_win()\n configure_window_options(current_win, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = vim.fn.bufnr('%')\n\n -- Automatically enter insert mode in terminal unless configured to start in normal mode\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n end\nend\n\n--- Toggle the Claude Code terminal window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\n--- @param git table The git module\nfunction M.toggle(claude_code, config, git)\n -- Determine instance ID based on config\n local instance_id = get_instance_id(config, git)\n claude_code.claude_code.current_instance = instance_id\n\n -- Check if this Claude Code instance is already running\n local bufnr = claude_code.claude_code.instances[instance_id]\n\n -- Validate existing buffer\n if bufnr and not is_valid_terminal_buffer(bufnr) then\n -- Buffer is no longer a valid terminal, reset\n claude_code.claude_code.instances[instance_id] = nil\n bufnr = nil\n end\n\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n -- Handle existing instance (toggle visibility)\n handle_existing_instance(bufnr, config)\n else\n -- Prune invalid buffer entries\n if bufnr and not vim.api.nvim_buf_is_valid(bufnr) then\n claude_code.claude_code.instances[instance_id] = nil\n end\n -- Create new instance\n create_new_instance(claude_code, config, git, instance_id)\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/config.lua", "---@mod claude-code.config Configuration management for claude-code.nvim\n---@brief [[\n--- This module handles configuration management and validation for claude-code.nvim.\n--- It provides the default configuration, validation, and merging of user config.\n---@brief ]]\n\nlocal M = {}\n\n--- ClaudeCodeWindow class for window configuration\n-- @table ClaudeCodeWindow\n-- @field split_ratio number Percentage of screen for the terminal window (height for horizontal, width for vertical)\n-- @field position string Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\" etc.\n-- @field enter_insert boolean Whether to enter insert mode when opening Claude Code\n-- @field start_in_normal_mode boolean Whether to start in normal mode instead of insert mode when opening Claude Code\n-- @field hide_numbers boolean Hide line numbers in the terminal window\n-- @field hide_signcolumn boolean Hide the sign column in the terminal window\n-- @field float table|nil Floating window configuration (only used when position is \"float\")\n-- @field float.width number|string Width of floating window (number: columns, string: percentage like \"80%\")\n-- @field float.height number|string Height of floating window (number: rows, string: percentage like \"80%\")\n-- @field float.row number|string|nil Row position (number: absolute, string: \"center\" or percentage)\n-- @field float.col number|string|nil Column position (number: absolute, string: \"center\" or percentage)\n-- @field float.border string Border style: \"none\", \"single\", \"double\", \"rounded\", \"solid\", \"shadow\", or array\n-- @field float.relative string Relative positioning: \"editor\" or \"cursor\"\n\n--- ClaudeCodeRefresh class for file refresh configuration\n-- @table ClaudeCodeRefresh\n-- @field enable boolean Enable file change detection\n-- @field updatetime number updatetime when Claude Code is active (milliseconds)\n-- @field timer_interval number How often to check for file changes (milliseconds)\n-- @field show_notifications boolean Show notification when files are reloaded\n\n--- ClaudeCodeGit class for git integration configuration\n-- @table ClaudeCodeGit\n-- @field use_git_root boolean Set CWD to git root when opening Claude Code (if in git project)\n-- @field multi_instance boolean Use multiple Claude instances (one per git root)\n\n--- ClaudeCodeKeymapsToggle class for toggle keymap configuration\n-- @table ClaudeCodeKeymapsToggle\n-- @field normal string|boolean Normal mode keymap for toggling Claude Code, false to disable\n-- @field terminal string|boolean Terminal mode keymap for toggling Claude Code, false to disable\n\n--- ClaudeCodeKeymaps class for keymap configuration\n-- @table ClaudeCodeKeymaps\n-- @field toggle ClaudeCodeKeymapsToggle Keymaps for toggling Claude Code\n-- @field window_navigation boolean Enable window navigation keymaps\n-- @field scrolling boolean Enable scrolling keymaps\n\n--- ClaudeCodeCommandVariants class for command variant configuration\n-- @table ClaudeCodeCommandVariants\n-- Conversation management:\n-- @field continue string|boolean Resume the most recent conversation\n-- @field resume string|boolean Display an interactive conversation picker\n-- Output options:\n-- @field verbose string|boolean Enable verbose logging with full turn-by-turn output\n-- Additional options can be added as needed\n\n--- ClaudeCodeShell class for shell configuration\n-- @table ClaudeCodeShell\n-- @field separator string Command separator used in shell commands (e.g., '&&', ';', '|')\n-- @field pushd_cmd string Command to push directory onto stack (e.g., 'pushd' for bash/zsh)\n-- @field popd_cmd string Command to pop directory from stack (e.g., 'popd' for bash/zsh)\n\n--- ClaudeCodeConfig class for main configuration\n-- @table ClaudeCodeConfig\n-- @field window ClaudeCodeWindow Terminal window settings\n-- @field refresh ClaudeCodeRefresh File refresh settings\n-- @field git ClaudeCodeGit Git integration settings\n-- @field shell ClaudeCodeShell Shell-specific configuration\n-- @field command string Command used to launch Claude Code\n-- @field command_variants ClaudeCodeCommandVariants Command variants configuration\n-- @field keymaps ClaudeCodeKeymaps Keymaps configuration\n\n--- Default configuration options\n--- @type ClaudeCodeConfig\nM.default_config = {\n -- Terminal window settings\n window = {\n split_ratio = 0.3, -- Percentage of screen for the terminal window (height or width)\n height_ratio = 0.3, -- DEPRECATED: Use split_ratio instead\n position = 'botright', -- Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\", etc.\n enter_insert = true, -- Whether to enter insert mode when opening Claude Code\n start_in_normal_mode = false, -- Whether to start in normal mode instead of insert mode\n hide_numbers = true, -- Hide line numbers in the terminal window\n hide_signcolumn = true, -- Hide the sign column in the terminal window\n -- Default floating window configuration\n float = {\n width = '80%', -- Width as percentage of editor\n height = '80%', -- Height as percentage of editor\n row = 'center', -- Center vertically\n col = 'center', -- Center horizontally\n relative = 'editor', -- Position relative to editor\n border = 'rounded', -- Border style\n },\n },\n -- File refresh settings\n refresh = {\n enable = true, -- Enable file change detection\n updatetime = 100, -- updatetime to use when Claude Code is active (milliseconds)\n timer_interval = 1000, -- How often to check for file changes (milliseconds)\n show_notifications = true, -- Show notification when files are reloaded\n },\n -- Git integration settings\n git = {\n use_git_root = true, -- Set CWD to git root when opening Claude Code (if in git project)\n multi_instance = true, -- Use multiple Claude instances (one per git root)\n },\n -- Shell-specific settings\n shell = {\n separator = '&&', -- Command separator used in shell commands\n pushd_cmd = 'pushd', -- Command to push directory onto stack\n popd_cmd = 'popd', -- Command to pop directory from stack\n },\n -- Command settings\n command = 'claude', -- Command used to launch Claude Code\n -- Command variants\n command_variants = {\n -- Conversation management\n continue = '--continue', -- Resume the most recent conversation\n resume = '--resume', -- Display an interactive conversation picker\n\n -- Output options\n verbose = '--verbose', -- Enable verbose logging with full turn-by-turn output\n },\n -- Keymaps\n keymaps = {\n toggle = {\n normal = '', -- Normal mode keymap for toggling Claude Code\n terminal = '', -- Terminal mode keymap for toggling Claude Code\n variants = {\n continue = 'cC', -- Normal mode keymap for Claude Code with continue flag\n verbose = 'cV', -- Normal mode keymap for Claude Code with verbose flag\n },\n },\n window_navigation = true, -- Enable window navigation keymaps ()\n scrolling = true, -- Enable scrolling keymaps () for page up/down\n },\n}\n\n--- Validate the configuration\n--- Validate window configuration\n--- @param window table Window configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_window_config(window)\n if type(window) ~= 'table' then\n return false, 'window config must be a table'\n end\n\n if type(window.split_ratio) ~= 'number' or window.split_ratio <= 0 or window.split_ratio > 1 then\n return false, 'window.split_ratio must be a number between 0 and 1'\n end\n\n if type(window.position) ~= 'string' then\n return false, 'window.position must be a string'\n end\n\n if type(window.enter_insert) ~= 'boolean' then\n return false, 'window.enter_insert must be a boolean'\n end\n\n if type(window.start_in_normal_mode) ~= 'boolean' then\n return false, 'window.start_in_normal_mode must be a boolean'\n end\n\n if type(window.hide_numbers) ~= 'boolean' then\n return false, 'window.hide_numbers must be a boolean'\n end\n\n if type(window.hide_signcolumn) ~= 'boolean' then\n return false, 'window.hide_signcolumn must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate floating window configuration\n--- @param float table Float configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_float_config(float)\n if type(float) ~= 'table' then\n return false, 'window.float must be a table when position is \"float\"'\n end\n\n -- Validate width (can be number or percentage string)\n if type(float.width) == 'string' then\n if not float.width:match('^%d+%%$') then\n return false, 'window.float.width must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.width) ~= 'number' or float.width <= 0 then\n return false, 'window.float.width must be a positive number or percentage string'\n end\n\n -- Validate height (can be number or percentage string)\n if type(float.height) == 'string' then\n if not float.height:match('^%d+%%$') then\n return false, 'window.float.height must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.height) ~= 'number' or float.height <= 0 then\n return false, 'window.float.height must be a positive number or percentage string'\n end\n\n -- Validate relative (must be \"editor\" or \"cursor\")\n if float.relative ~= 'editor' and float.relative ~= 'cursor' then\n return false, 'window.float.relative must be \"editor\" or \"cursor\"'\n end\n\n -- Validate border (must be valid border style)\n local valid_borders = { 'none', 'single', 'double', 'rounded', 'solid', 'shadow' }\n local is_valid_border = false\n for _, border in ipairs(valid_borders) do\n if float.border == border then\n is_valid_border = true\n break\n end\n end\n -- Also allow array borders\n if not is_valid_border and type(float.border) ~= 'table' then\n return false,\n 'window.float.border must be one of: none, single, double, rounded, solid, shadow, or an array'\n end\n\n -- Validate row and col if they exist\n if float.row ~= nil then\n if type(float.row) == 'string' and float.row ~= 'center' then\n if not float.row:match('^%d+%%$') then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n elseif type(float.row) ~= 'number' and float.row ~= 'center' then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n end\n\n if float.col ~= nil then\n if type(float.col) == 'string' and float.col ~= 'center' then\n if not float.col:match('^%d+%%$') then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n elseif type(float.col) ~= 'number' and float.col ~= 'center' then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n end\n\n return true, nil\nend\n\n--- Validate refresh configuration\n--- @param refresh table Refresh configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_refresh_config(refresh)\n if type(refresh) ~= 'table' then\n return false, 'refresh config must be a table'\n end\n\n if type(refresh.enable) ~= 'boolean' then\n return false, 'refresh.enable must be a boolean'\n end\n\n if type(refresh.updatetime) ~= 'number' or refresh.updatetime <= 0 then\n return false, 'refresh.updatetime must be a positive number'\n end\n\n if type(refresh.timer_interval) ~= 'number' or refresh.timer_interval <= 0 then\n return false, 'refresh.timer_interval must be a positive number'\n end\n\n if type(refresh.show_notifications) ~= 'boolean' then\n return false, 'refresh.show_notifications must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate git configuration\n--- @param git table Git configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_git_config(git)\n if type(git) ~= 'table' then\n return false, 'git config must be a table'\n end\n\n if type(git.use_git_root) ~= 'boolean' then\n return false, 'git.use_git_root must be a boolean'\n end\n\n if type(git.multi_instance) ~= 'boolean' then\n return false, 'git.multi_instance must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate shell configuration\n--- @param shell table Shell configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_shell_config(shell)\n if type(shell) ~= 'table' then\n return false, 'shell config must be a table'\n end\n\n if type(shell.separator) ~= 'string' then\n return false, 'shell.separator must be a string'\n end\n\n if type(shell.pushd_cmd) ~= 'string' then\n return false, 'shell.pushd_cmd must be a string'\n end\n\n if type(shell.popd_cmd) ~= 'string' then\n return false, 'shell.popd_cmd must be a string'\n end\n\n return true, nil\nend\n\n--- Validate keymaps configuration\n--- @param keymaps table Keymaps configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_keymaps_config(keymaps)\n if type(keymaps) ~= 'table' then\n return false, 'keymaps config must be a table'\n end\n\n if type(keymaps.toggle) ~= 'table' then\n return false, 'keymaps.toggle must be a table'\n end\n\n if not (keymaps.toggle.normal == false or type(keymaps.toggle.normal) == 'string') then\n return false, 'keymaps.toggle.normal must be a string or false'\n end\n\n if not (keymaps.toggle.terminal == false or type(keymaps.toggle.terminal) == 'string') then\n return false, 'keymaps.toggle.terminal must be a string or false'\n end\n\n -- Validate variant keymaps if they exist\n if keymaps.toggle.variants then\n if type(keymaps.toggle.variants) ~= 'table' then\n return false, 'keymaps.toggle.variants must be a table'\n end\n\n -- Check each variant keymap\n for variant_name, keymap in pairs(keymaps.toggle.variants) do\n if not (keymap == false or type(keymap) == 'string') then\n return false, 'keymaps.toggle.variants.' .. variant_name .. ' must be a string or false'\n end\n end\n end\n\n if type(keymaps.window_navigation) ~= 'boolean' then\n return false, 'keymaps.window_navigation must be a boolean'\n end\n\n if type(keymaps.scrolling) ~= 'boolean' then\n return false, 'keymaps.scrolling must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate command variants configuration\n--- @param command_variants table Command variants configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_command_variants_config(command_variants)\n if type(command_variants) ~= 'table' then\n return false, 'command_variants config must be a table'\n end\n\n -- Check each command variant\n for variant_name, variant_args in pairs(command_variants) do\n if not (variant_args == false or type(variant_args) == 'string') then\n return false, 'command_variants.' .. variant_name .. ' must be a string or false'\n end\n end\n\n return true, nil\nend\n\n--- Validate configuration options\n--- @param config ClaudeCodeConfig\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_config(config)\n -- Validate window settings\n local valid, err = validate_window_config(config.window)\n if not valid then\n return false, err\n end\n\n -- Validate float configuration if position is \"float\"\n if config.window.position == 'float' then\n valid, err = validate_float_config(config.window.float)\n if not valid then\n return false, err\n end\n end\n\n -- Validate refresh settings\n valid, err = validate_refresh_config(config.refresh)\n if not valid then\n return false, err\n end\n\n -- Validate git settings\n valid, err = validate_git_config(config.git)\n if not valid then\n return false, err\n end\n\n -- Validate shell settings\n valid, err = validate_shell_config(config.shell)\n if not valid then\n return false, err\n end\n\n -- Validate command settings\n if type(config.command) ~= 'string' then\n return false, 'command must be a string'\n end\n\n -- Validate command variants settings\n valid, err = validate_command_variants_config(config.command_variants)\n if not valid then\n return false, err\n end\n\n -- Validate keymaps settings\n valid, err = validate_keymaps_config(config.keymaps)\n if not valid then\n return false, err\n end\n\n -- Cross-validate keymaps with command variants\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n -- Ensure variant exists in command_variants\n if keymap ~= false and not config.command_variants[variant_name] then\n return false,\n 'keymaps.toggle.variants.' .. variant_name .. ' has no corresponding command variant'\n end\n end\n end\n\n return true, nil\nend\n\n--- Parse user configuration and merge with defaults\n--- @param user_config? table\n--- @param silent? boolean Set to true to suppress error notifications (for tests)\n--- @return ClaudeCodeConfig\nfunction M.parse_config(user_config, silent)\n -- Handle backward compatibility first\n if user_config and user_config.window then\n if user_config.window.height_ratio and not user_config.window.split_ratio then\n -- Copy height_ratio to split_ratio for backward compatibility\n user_config.window.split_ratio = user_config.window.height_ratio\n end\n end\n\n local config = vim.tbl_deep_extend('force', {}, M.default_config, user_config or {})\n\n -- If position is float and no float config provided, use default float config\n if\n config.window.position == 'float'\n and not (user_config and user_config.window and user_config.window.float)\n then\n config.window.float = vim.deepcopy(M.default_config.window.float)\n end\n\n local valid, err = validate_config(config)\n if not valid then\n -- Only notify if not in silent mode\n if not silent then\n vim.notify('Claude Code: ' .. err, vim.log.levels.ERROR)\n end\n -- Fall back to default config in case of error\n return vim.deepcopy(M.default_config)\n end\n\n return config\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/file_refresh.lua", "---@mod claude-code.file_refresh File refresh functionality for claude-code.nvim\n---@brief [[\n--- This module provides file refresh functionality to detect and reload files\n--- that have been modified by Claude Code or other external processes.\n---@brief ]]\n\nlocal M = {}\n\n--- Timer for checking file changes\n--- @type userdata|nil\nlocal refresh_timer = nil\n\n--- Setup autocommands for file change detection\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup(claude_code, config)\n if not config.refresh.enable then\n return\n end\n\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeFileRefresh', { clear = true })\n\n -- Create an autocommand that checks for file changes more frequently\n vim.api.nvim_create_autocmd({\n 'CursorHold',\n 'CursorHoldI',\n 'FocusGained',\n 'BufEnter',\n 'InsertLeave',\n 'TextChanged',\n 'TermLeave',\n 'TermEnter',\n 'BufWinEnter',\n }, {\n group = augroup,\n pattern = '*',\n callback = function()\n if vim.fn.filereadable(vim.fn.expand '%') == 1 then\n vim.cmd 'checktime'\n end\n end,\n desc = 'Check for file changes on disk',\n })\n\n -- Clean up any existing timer\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\n\n -- Create a timer to check for file changes periodically\n refresh_timer = vim.loop.new_timer()\n if refresh_timer then\n refresh_timer:start(\n 0,\n config.refresh.timer_interval,\n vim.schedule_wrap(function()\n -- Only check time if there's an active Claude Code terminal\n local current_instance = claude_code.claude_code.current_instance\n local bufnr = current_instance and claude_code.claude_code.instances[current_instance]\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) and #vim.fn.win_findbuf(bufnr) > 0 then\n vim.cmd 'silent! checktime'\n end\n end)\n )\n end\n\n -- Create an autocommand that notifies when a file has been changed externally\n if config.refresh.show_notifications then\n vim.api.nvim_create_autocmd('FileChangedShellPost', {\n group = augroup,\n pattern = '*',\n callback = function()\n vim.notify('File changed on disk. Buffer reloaded.', vim.log.levels.INFO)\n end,\n desc = 'Notify when a file is changed externally',\n })\n end\n\n -- Set a shorter updatetime while Claude Code is open\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n\n -- When Claude Code opens, set a shorter updatetime\n vim.api.nvim_create_autocmd('TermOpen', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf = vim.api.nvim_get_current_buf()\n local buf_name = vim.api.nvim_buf_get_name(buf)\n if buf_name:match('claude%-code$') then\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n vim.o.updatetime = config.refresh.updatetime\n end\n end,\n desc = 'Set shorter updatetime when Claude Code is open',\n })\n\n -- When Claude Code closes, restore normal updatetime\n vim.api.nvim_create_autocmd('TermClose', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf_name = vim.api.nvim_buf_get_name(0)\n if buf_name:match('claude%-code$') then\n vim.o.updatetime = claude_code.claude_code.saved_updatetime\n end\n end,\n desc = 'Restore normal updatetime when Claude Code is closed',\n })\nend\n\n--- Clean up the file refresh functionality (stop the timer)\nfunction M.cleanup()\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/version.lua", "---@mod claude-code.version Version information for claude-code.nvim\n---@brief [[\n--- This module provides version information for claude-code.nvim.\n---@brief ]]\n\n--- @table M\n--- Version information for Claude Code\n--- @field major number Major version (breaking changes)\n--- @field minor number Minor version (new features)\n--- @field patch number Patch version (bug fixes)\n--- @field string function Returns formatted version string\n\nlocal M = {}\n\n-- Individual version components\nM.major = 0\nM.minor = 4\nM.patch = 2\n\n-- Combined semantic version\nM.version = string.format('%d.%d.%d', M.major, M.minor, M.patch)\n\n--- Returns the formatted version string (for backward compatibility)\n--- @return string Version string in format \"major.minor.patch\"\nfunction M.string()\n return M.version\nend\n\n--- Prints the current version of the plugin\nfunction M.print_version()\n vim.notify('Claude Code version: ' .. M.string(), vim.log.levels.INFO)\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/git.lua", "---@mod claude-code.git Git integration for claude-code.nvim\n---@brief [[\n--- This module provides git integration functionality for claude-code.nvim.\n--- It detects git repositories and can set the working directory to the git root.\n---@brief ]]\n\nlocal M = {}\n\n--- Helper function to get git root directory\n--- @return string|nil git_root The git root directory path or nil if not in a git repo\nfunction M.get_git_root()\n -- For testing compatibility\n if vim.env.CLAUDE_CODE_TEST_MODE == 'true' then\n return '/home/user/project'\n end\n\n -- Check if we're in a git repository\n local handle = io.popen('git rev-parse --is-inside-work-tree 2>/dev/null')\n if not handle then\n return nil\n end\n\n local result = handle:read('*a')\n handle:close()\n\n -- Strip trailing whitespace and newlines for reliable matching\n result = result:gsub('[\\n\\r%s]*$', '')\n\n if result == 'true' then\n -- Get the git root path\n local root_handle = io.popen('git rev-parse --show-toplevel 2>/dev/null')\n if not root_handle then\n return nil\n end\n\n local git_root = root_handle:read('*a')\n root_handle:close()\n\n -- Remove trailing whitespace and newlines\n git_root = git_root:gsub('[\\n\\r%s]*$', '')\n\n return git_root\n end\n\n return nil\nend\n\nreturn M\n"]], "task_instance_info": {"created_time": "2025-08-21 00:40:22", "created_task_model": "DeepSeek-R1", "code_description": "This code segment functions to: Define multiple user-executable commands that interface with a plugin's core functionality, including dynamic generation of variant commands based on configuration settings and version information display."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```lua\n[TASK_BEGIN]\nM.commands = {\n start = function(claude_code)\n claude_code:start_chat()\n end,\n \n toggle = function(claude_code)\n claude_code:toggle_chat_window()\n end,\n \n submit = function(claude_code)\n claude_code:submit_query()\n end,\n \n stop = function(claude_code)\n claude_code:stop_current_request()\n end,\n \n new = function(claude_code)\n claude_code:create_new_chat_buffer()\n end,\n}\n[TASK_END]\n```", "inference_time": "2025-08-21 00-40-46"}, "editdistance_info": {"edit_distance": 12.8079, "calculate_time": "2025-08-21 00:40:46", "true_code_clean": "function M.register_commands(claude_code)\n vim.api.nvim_create_user_command('ClaudeCode', function()\n claude_code.toggle()\n end, { desc = 'Toggle Claude Code terminal' })\n for variant_name, variant_args in pairs(claude_code.config.command_variants) do\n if variant_args ~= false then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n vim.api.nvim_create_user_command(cmd_name, function()\n claude_code.toggle_with_variant(variant_name)\n end, { desc = 'Toggle Claude Code terminal with ' .. variant_name .. ' option' })\n end\n end\n vim.api.nvim_create_user_command('ClaudeCodeVersion', function()\n vim.notify('Claude Code version: ' .. claude_code.version(), vim.log.levels.INFO)\n end, { desc = 'Display Claude Code version' })\nend", "predict_code_clean": "M.commands = {\n start = function(claude_code)\n claude_code:start_chat()\n end,\n toggle = function(claude_code)\n claude_code:toggle_chat_window()\n end,\n submit = function(claude_code)\n claude_code:submit_query()\n end,\n stop = function(claude_code)\n claude_code:stop_current_request()\n end,\n new = function(claude_code)\n claude_code:create_new_chat_buffer()\n end,\n}"}} {"repo_name": "claude-code.nvim", "file_name": "/claude-code.nvim/lua/claude-code/keymaps.lua", "inference_info": {"prefix_code": "---@mod claude-code.keymaps Keymap management for claude-code.nvim\n---@brief [[\n--- This module provides keymap registration and handling for claude-code.nvim.\n--- It handles normal mode, terminal mode, and window navigation keymaps.\n---@brief ]]\n\nlocal M = {}\n\n--- Register keymaps for claude-code.nvim\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.register_keymaps(claude_code, config)\n local map_opts = { noremap = true, silent = true }\n\n -- Normal mode toggle keymaps\n if config.keymaps.toggle.normal then\n vim.api.nvim_set_keymap(\n 'n',\n config.keymaps.toggle.normal,\n [[ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n if config.keymaps.toggle.terminal then\n -- Terminal mode toggle keymap\n -- In terminal mode, special keys like Ctrl need different handling\n -- We use a direct escape sequence approach for more reliable terminal mappings\n vim.api.nvim_set_keymap(\n 't',\n config.keymaps.toggle.terminal,\n [[:ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n -- Register variant keymaps if configured\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_set_keymap(\n 'n',\n keymap,\n string.format([[%s]], cmd_name),\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: ' .. capitalized_name })\n )\n end\n end\n end\n\n -- Register with which-key if it's available\n vim.defer_fn(", "suffix_code": ", 100)\nend\n\n--- Set up terminal-specific keymaps for window navigation\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup_terminal_navigation(claude_code, config)\n -- Get current active Claude instance buffer\n local current_instance = claude_code.claude_code.current_instance\n local buf = current_instance and claude_code.claude_code.instances[current_instance]\n if buf and vim.api.nvim_buf_is_valid(buf) then\n -- Create autocommand to enter insert mode when the terminal window gets focus\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeTerminalFocus_' .. buf, { clear = true })\n\n -- Set up multiple events for more reliable focus detection\n vim.api.nvim_create_autocmd(\n { 'WinEnter', 'BufEnter', 'WinLeave', 'FocusGained', 'CmdLineLeave' },\n {\n group = augroup,\n callback = function()\n vim.schedule(claude_code.force_insert_mode)\n end,\n desc = 'Auto-enter insert mode when focusing Claude Code terminal',\n }\n )\n\n -- Window navigation keymaps\n if config.keymaps.window_navigation then\n -- Window navigation keymaps with special handling to force insert mode in the target window\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n\n -- Also add normal mode mappings for when user is in normal mode in the terminal\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n end\n\n -- Add scrolling keymaps\n if config.keymaps.scrolling then\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page up' }\n )\n end\n end\nend\n\nreturn M\n", "middle_code": "function()\n local status_ok, which_key = pcall(require, 'which-key')\n if status_ok then\n if config.keymaps.toggle.normal then\n which_key.add {\n mode = 'n',\n { config.keymaps.toggle.normal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.terminal then\n which_key.add {\n mode = 't',\n { config.keymaps.toggle.terminal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n which_key.add {\n mode = 'n',\n { keymap, desc = 'Claude Code: ' .. capitalized_name, icon = '🤖' },\n }\n end\n end\n end\n end\n end", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "lua", "sub_task_type": null}, "context_code": [["/claude-code.nvim/lua/claude-code/config.lua", "---@mod claude-code.config Configuration management for claude-code.nvim\n---@brief [[\n--- This module handles configuration management and validation for claude-code.nvim.\n--- It provides the default configuration, validation, and merging of user config.\n---@brief ]]\n\nlocal M = {}\n\n--- ClaudeCodeWindow class for window configuration\n-- @table ClaudeCodeWindow\n-- @field split_ratio number Percentage of screen for the terminal window (height for horizontal, width for vertical)\n-- @field position string Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\" etc.\n-- @field enter_insert boolean Whether to enter insert mode when opening Claude Code\n-- @field start_in_normal_mode boolean Whether to start in normal mode instead of insert mode when opening Claude Code\n-- @field hide_numbers boolean Hide line numbers in the terminal window\n-- @field hide_signcolumn boolean Hide the sign column in the terminal window\n-- @field float table|nil Floating window configuration (only used when position is \"float\")\n-- @field float.width number|string Width of floating window (number: columns, string: percentage like \"80%\")\n-- @field float.height number|string Height of floating window (number: rows, string: percentage like \"80%\")\n-- @field float.row number|string|nil Row position (number: absolute, string: \"center\" or percentage)\n-- @field float.col number|string|nil Column position (number: absolute, string: \"center\" or percentage)\n-- @field float.border string Border style: \"none\", \"single\", \"double\", \"rounded\", \"solid\", \"shadow\", or array\n-- @field float.relative string Relative positioning: \"editor\" or \"cursor\"\n\n--- ClaudeCodeRefresh class for file refresh configuration\n-- @table ClaudeCodeRefresh\n-- @field enable boolean Enable file change detection\n-- @field updatetime number updatetime when Claude Code is active (milliseconds)\n-- @field timer_interval number How often to check for file changes (milliseconds)\n-- @field show_notifications boolean Show notification when files are reloaded\n\n--- ClaudeCodeGit class for git integration configuration\n-- @table ClaudeCodeGit\n-- @field use_git_root boolean Set CWD to git root when opening Claude Code (if in git project)\n-- @field multi_instance boolean Use multiple Claude instances (one per git root)\n\n--- ClaudeCodeKeymapsToggle class for toggle keymap configuration\n-- @table ClaudeCodeKeymapsToggle\n-- @field normal string|boolean Normal mode keymap for toggling Claude Code, false to disable\n-- @field terminal string|boolean Terminal mode keymap for toggling Claude Code, false to disable\n\n--- ClaudeCodeKeymaps class for keymap configuration\n-- @table ClaudeCodeKeymaps\n-- @field toggle ClaudeCodeKeymapsToggle Keymaps for toggling Claude Code\n-- @field window_navigation boolean Enable window navigation keymaps\n-- @field scrolling boolean Enable scrolling keymaps\n\n--- ClaudeCodeCommandVariants class for command variant configuration\n-- @table ClaudeCodeCommandVariants\n-- Conversation management:\n-- @field continue string|boolean Resume the most recent conversation\n-- @field resume string|boolean Display an interactive conversation picker\n-- Output options:\n-- @field verbose string|boolean Enable verbose logging with full turn-by-turn output\n-- Additional options can be added as needed\n\n--- ClaudeCodeShell class for shell configuration\n-- @table ClaudeCodeShell\n-- @field separator string Command separator used in shell commands (e.g., '&&', ';', '|')\n-- @field pushd_cmd string Command to push directory onto stack (e.g., 'pushd' for bash/zsh)\n-- @field popd_cmd string Command to pop directory from stack (e.g., 'popd' for bash/zsh)\n\n--- ClaudeCodeConfig class for main configuration\n-- @table ClaudeCodeConfig\n-- @field window ClaudeCodeWindow Terminal window settings\n-- @field refresh ClaudeCodeRefresh File refresh settings\n-- @field git ClaudeCodeGit Git integration settings\n-- @field shell ClaudeCodeShell Shell-specific configuration\n-- @field command string Command used to launch Claude Code\n-- @field command_variants ClaudeCodeCommandVariants Command variants configuration\n-- @field keymaps ClaudeCodeKeymaps Keymaps configuration\n\n--- Default configuration options\n--- @type ClaudeCodeConfig\nM.default_config = {\n -- Terminal window settings\n window = {\n split_ratio = 0.3, -- Percentage of screen for the terminal window (height or width)\n height_ratio = 0.3, -- DEPRECATED: Use split_ratio instead\n position = 'botright', -- Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\", etc.\n enter_insert = true, -- Whether to enter insert mode when opening Claude Code\n start_in_normal_mode = false, -- Whether to start in normal mode instead of insert mode\n hide_numbers = true, -- Hide line numbers in the terminal window\n hide_signcolumn = true, -- Hide the sign column in the terminal window\n -- Default floating window configuration\n float = {\n width = '80%', -- Width as percentage of editor\n height = '80%', -- Height as percentage of editor\n row = 'center', -- Center vertically\n col = 'center', -- Center horizontally\n relative = 'editor', -- Position relative to editor\n border = 'rounded', -- Border style\n },\n },\n -- File refresh settings\n refresh = {\n enable = true, -- Enable file change detection\n updatetime = 100, -- updatetime to use when Claude Code is active (milliseconds)\n timer_interval = 1000, -- How often to check for file changes (milliseconds)\n show_notifications = true, -- Show notification when files are reloaded\n },\n -- Git integration settings\n git = {\n use_git_root = true, -- Set CWD to git root when opening Claude Code (if in git project)\n multi_instance = true, -- Use multiple Claude instances (one per git root)\n },\n -- Shell-specific settings\n shell = {\n separator = '&&', -- Command separator used in shell commands\n pushd_cmd = 'pushd', -- Command to push directory onto stack\n popd_cmd = 'popd', -- Command to pop directory from stack\n },\n -- Command settings\n command = 'claude', -- Command used to launch Claude Code\n -- Command variants\n command_variants = {\n -- Conversation management\n continue = '--continue', -- Resume the most recent conversation\n resume = '--resume', -- Display an interactive conversation picker\n\n -- Output options\n verbose = '--verbose', -- Enable verbose logging with full turn-by-turn output\n },\n -- Keymaps\n keymaps = {\n toggle = {\n normal = '', -- Normal mode keymap for toggling Claude Code\n terminal = '', -- Terminal mode keymap for toggling Claude Code\n variants = {\n continue = 'cC', -- Normal mode keymap for Claude Code with continue flag\n verbose = 'cV', -- Normal mode keymap for Claude Code with verbose flag\n },\n },\n window_navigation = true, -- Enable window navigation keymaps ()\n scrolling = true, -- Enable scrolling keymaps () for page up/down\n },\n}\n\n--- Validate the configuration\n--- Validate window configuration\n--- @param window table Window configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_window_config(window)\n if type(window) ~= 'table' then\n return false, 'window config must be a table'\n end\n\n if type(window.split_ratio) ~= 'number' or window.split_ratio <= 0 or window.split_ratio > 1 then\n return false, 'window.split_ratio must be a number between 0 and 1'\n end\n\n if type(window.position) ~= 'string' then\n return false, 'window.position must be a string'\n end\n\n if type(window.enter_insert) ~= 'boolean' then\n return false, 'window.enter_insert must be a boolean'\n end\n\n if type(window.start_in_normal_mode) ~= 'boolean' then\n return false, 'window.start_in_normal_mode must be a boolean'\n end\n\n if type(window.hide_numbers) ~= 'boolean' then\n return false, 'window.hide_numbers must be a boolean'\n end\n\n if type(window.hide_signcolumn) ~= 'boolean' then\n return false, 'window.hide_signcolumn must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate floating window configuration\n--- @param float table Float configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_float_config(float)\n if type(float) ~= 'table' then\n return false, 'window.float must be a table when position is \"float\"'\n end\n\n -- Validate width (can be number or percentage string)\n if type(float.width) == 'string' then\n if not float.width:match('^%d+%%$') then\n return false, 'window.float.width must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.width) ~= 'number' or float.width <= 0 then\n return false, 'window.float.width must be a positive number or percentage string'\n end\n\n -- Validate height (can be number or percentage string)\n if type(float.height) == 'string' then\n if not float.height:match('^%d+%%$') then\n return false, 'window.float.height must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.height) ~= 'number' or float.height <= 0 then\n return false, 'window.float.height must be a positive number or percentage string'\n end\n\n -- Validate relative (must be \"editor\" or \"cursor\")\n if float.relative ~= 'editor' and float.relative ~= 'cursor' then\n return false, 'window.float.relative must be \"editor\" or \"cursor\"'\n end\n\n -- Validate border (must be valid border style)\n local valid_borders = { 'none', 'single', 'double', 'rounded', 'solid', 'shadow' }\n local is_valid_border = false\n for _, border in ipairs(valid_borders) do\n if float.border == border then\n is_valid_border = true\n break\n end\n end\n -- Also allow array borders\n if not is_valid_border and type(float.border) ~= 'table' then\n return false,\n 'window.float.border must be one of: none, single, double, rounded, solid, shadow, or an array'\n end\n\n -- Validate row and col if they exist\n if float.row ~= nil then\n if type(float.row) == 'string' and float.row ~= 'center' then\n if not float.row:match('^%d+%%$') then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n elseif type(float.row) ~= 'number' and float.row ~= 'center' then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n end\n\n if float.col ~= nil then\n if type(float.col) == 'string' and float.col ~= 'center' then\n if not float.col:match('^%d+%%$') then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n elseif type(float.col) ~= 'number' and float.col ~= 'center' then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n end\n\n return true, nil\nend\n\n--- Validate refresh configuration\n--- @param refresh table Refresh configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_refresh_config(refresh)\n if type(refresh) ~= 'table' then\n return false, 'refresh config must be a table'\n end\n\n if type(refresh.enable) ~= 'boolean' then\n return false, 'refresh.enable must be a boolean'\n end\n\n if type(refresh.updatetime) ~= 'number' or refresh.updatetime <= 0 then\n return false, 'refresh.updatetime must be a positive number'\n end\n\n if type(refresh.timer_interval) ~= 'number' or refresh.timer_interval <= 0 then\n return false, 'refresh.timer_interval must be a positive number'\n end\n\n if type(refresh.show_notifications) ~= 'boolean' then\n return false, 'refresh.show_notifications must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate git configuration\n--- @param git table Git configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_git_config(git)\n if type(git) ~= 'table' then\n return false, 'git config must be a table'\n end\n\n if type(git.use_git_root) ~= 'boolean' then\n return false, 'git.use_git_root must be a boolean'\n end\n\n if type(git.multi_instance) ~= 'boolean' then\n return false, 'git.multi_instance must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate shell configuration\n--- @param shell table Shell configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_shell_config(shell)\n if type(shell) ~= 'table' then\n return false, 'shell config must be a table'\n end\n\n if type(shell.separator) ~= 'string' then\n return false, 'shell.separator must be a string'\n end\n\n if type(shell.pushd_cmd) ~= 'string' then\n return false, 'shell.pushd_cmd must be a string'\n end\n\n if type(shell.popd_cmd) ~= 'string' then\n return false, 'shell.popd_cmd must be a string'\n end\n\n return true, nil\nend\n\n--- Validate keymaps configuration\n--- @param keymaps table Keymaps configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_keymaps_config(keymaps)\n if type(keymaps) ~= 'table' then\n return false, 'keymaps config must be a table'\n end\n\n if type(keymaps.toggle) ~= 'table' then\n return false, 'keymaps.toggle must be a table'\n end\n\n if not (keymaps.toggle.normal == false or type(keymaps.toggle.normal) == 'string') then\n return false, 'keymaps.toggle.normal must be a string or false'\n end\n\n if not (keymaps.toggle.terminal == false or type(keymaps.toggle.terminal) == 'string') then\n return false, 'keymaps.toggle.terminal must be a string or false'\n end\n\n -- Validate variant keymaps if they exist\n if keymaps.toggle.variants then\n if type(keymaps.toggle.variants) ~= 'table' then\n return false, 'keymaps.toggle.variants must be a table'\n end\n\n -- Check each variant keymap\n for variant_name, keymap in pairs(keymaps.toggle.variants) do\n if not (keymap == false or type(keymap) == 'string') then\n return false, 'keymaps.toggle.variants.' .. variant_name .. ' must be a string or false'\n end\n end\n end\n\n if type(keymaps.window_navigation) ~= 'boolean' then\n return false, 'keymaps.window_navigation must be a boolean'\n end\n\n if type(keymaps.scrolling) ~= 'boolean' then\n return false, 'keymaps.scrolling must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate command variants configuration\n--- @param command_variants table Command variants configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_command_variants_config(command_variants)\n if type(command_variants) ~= 'table' then\n return false, 'command_variants config must be a table'\n end\n\n -- Check each command variant\n for variant_name, variant_args in pairs(command_variants) do\n if not (variant_args == false or type(variant_args) == 'string') then\n return false, 'command_variants.' .. variant_name .. ' must be a string or false'\n end\n end\n\n return true, nil\nend\n\n--- Validate configuration options\n--- @param config ClaudeCodeConfig\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_config(config)\n -- Validate window settings\n local valid, err = validate_window_config(config.window)\n if not valid then\n return false, err\n end\n\n -- Validate float configuration if position is \"float\"\n if config.window.position == 'float' then\n valid, err = validate_float_config(config.window.float)\n if not valid then\n return false, err\n end\n end\n\n -- Validate refresh settings\n valid, err = validate_refresh_config(config.refresh)\n if not valid then\n return false, err\n end\n\n -- Validate git settings\n valid, err = validate_git_config(config.git)\n if not valid then\n return false, err\n end\n\n -- Validate shell settings\n valid, err = validate_shell_config(config.shell)\n if not valid then\n return false, err\n end\n\n -- Validate command settings\n if type(config.command) ~= 'string' then\n return false, 'command must be a string'\n end\n\n -- Validate command variants settings\n valid, err = validate_command_variants_config(config.command_variants)\n if not valid then\n return false, err\n end\n\n -- Validate keymaps settings\n valid, err = validate_keymaps_config(config.keymaps)\n if not valid then\n return false, err\n end\n\n -- Cross-validate keymaps with command variants\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n -- Ensure variant exists in command_variants\n if keymap ~= false and not config.command_variants[variant_name] then\n return false,\n 'keymaps.toggle.variants.' .. variant_name .. ' has no corresponding command variant'\n end\n end\n end\n\n return true, nil\nend\n\n--- Parse user configuration and merge with defaults\n--- @param user_config? table\n--- @param silent? boolean Set to true to suppress error notifications (for tests)\n--- @return ClaudeCodeConfig\nfunction M.parse_config(user_config, silent)\n -- Handle backward compatibility first\n if user_config and user_config.window then\n if user_config.window.height_ratio and not user_config.window.split_ratio then\n -- Copy height_ratio to split_ratio for backward compatibility\n user_config.window.split_ratio = user_config.window.height_ratio\n end\n end\n\n local config = vim.tbl_deep_extend('force', {}, M.default_config, user_config or {})\n\n -- If position is float and no float config provided, use default float config\n if\n config.window.position == 'float'\n and not (user_config and user_config.window and user_config.window.float)\n then\n config.window.float = vim.deepcopy(M.default_config.window.float)\n end\n\n local valid, err = validate_config(config)\n if not valid then\n -- Only notify if not in silent mode\n if not silent then\n vim.notify('Claude Code: ' .. err, vim.log.levels.ERROR)\n end\n -- Fall back to default config in case of error\n return vim.deepcopy(M.default_config)\n end\n\n return config\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/file_refresh.lua", "---@mod claude-code.file_refresh File refresh functionality for claude-code.nvim\n---@brief [[\n--- This module provides file refresh functionality to detect and reload files\n--- that have been modified by Claude Code or other external processes.\n---@brief ]]\n\nlocal M = {}\n\n--- Timer for checking file changes\n--- @type userdata|nil\nlocal refresh_timer = nil\n\n--- Setup autocommands for file change detection\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup(claude_code, config)\n if not config.refresh.enable then\n return\n end\n\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeFileRefresh', { clear = true })\n\n -- Create an autocommand that checks for file changes more frequently\n vim.api.nvim_create_autocmd({\n 'CursorHold',\n 'CursorHoldI',\n 'FocusGained',\n 'BufEnter',\n 'InsertLeave',\n 'TextChanged',\n 'TermLeave',\n 'TermEnter',\n 'BufWinEnter',\n }, {\n group = augroup,\n pattern = '*',\n callback = function()\n if vim.fn.filereadable(vim.fn.expand '%') == 1 then\n vim.cmd 'checktime'\n end\n end,\n desc = 'Check for file changes on disk',\n })\n\n -- Clean up any existing timer\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\n\n -- Create a timer to check for file changes periodically\n refresh_timer = vim.loop.new_timer()\n if refresh_timer then\n refresh_timer:start(\n 0,\n config.refresh.timer_interval,\n vim.schedule_wrap(function()\n -- Only check time if there's an active Claude Code terminal\n local current_instance = claude_code.claude_code.current_instance\n local bufnr = current_instance and claude_code.claude_code.instances[current_instance]\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) and #vim.fn.win_findbuf(bufnr) > 0 then\n vim.cmd 'silent! checktime'\n end\n end)\n )\n end\n\n -- Create an autocommand that notifies when a file has been changed externally\n if config.refresh.show_notifications then\n vim.api.nvim_create_autocmd('FileChangedShellPost', {\n group = augroup,\n pattern = '*',\n callback = function()\n vim.notify('File changed on disk. Buffer reloaded.', vim.log.levels.INFO)\n end,\n desc = 'Notify when a file is changed externally',\n })\n end\n\n -- Set a shorter updatetime while Claude Code is open\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n\n -- When Claude Code opens, set a shorter updatetime\n vim.api.nvim_create_autocmd('TermOpen', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf = vim.api.nvim_get_current_buf()\n local buf_name = vim.api.nvim_buf_get_name(buf)\n if buf_name:match('claude%-code$') then\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n vim.o.updatetime = config.refresh.updatetime\n end\n end,\n desc = 'Set shorter updatetime when Claude Code is open',\n })\n\n -- When Claude Code closes, restore normal updatetime\n vim.api.nvim_create_autocmd('TermClose', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf_name = vim.api.nvim_buf_get_name(0)\n if buf_name:match('claude%-code$') then\n vim.o.updatetime = claude_code.claude_code.saved_updatetime\n end\n end,\n desc = 'Restore normal updatetime when Claude Code is closed',\n })\nend\n\n--- Clean up the file refresh functionality (stop the timer)\nfunction M.cleanup()\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/terminal.lua", "---@mod claude-code.terminal Terminal management for claude-code.nvim\n---@brief [[\n--- This module provides terminal buffer management for claude-code.nvim.\n--- It handles creating, toggling, and managing the terminal window.\n---@brief ]]\n\nlocal M = {}\n\n--- Terminal buffer and window management\n-- @table ClaudeCodeTerminal\n-- @field instances table Key-value store of git root to buffer number\n-- @field saved_updatetime number|nil Original updatetime before Claude Code was opened\n-- @field current_instance string|nil Current git root path for active instance\nM.terminal = {\n instances = {},\n saved_updatetime = nil,\n current_instance = nil,\n}\n\n--- Get the current git root or a fallback identifier\n--- @param git table The git module\n--- @return string identifier Git root path or fallback identifier\nlocal function get_instance_identifier(git)\n local git_root = git.get_git_root()\n if git_root then\n return git_root\n else\n -- Fallback to current working directory if not in a git repo\n return vim.fn.getcwd()\n end\nend\n\n--- Calculate floating window dimensions from percentage strings\n--- @param value number|string Dimension value (number or percentage string)\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated dimension\n--- @private\nlocal function calculate_float_dimension(value, max_value)\n if value == nil then\n return math.floor(max_value * 0.8) -- Default to 80% if not specified\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n return math.floor(max_value * percentage / 100)\n end\n return value\nend\n\n--- Calculate floating window position for centering\n--- @param value number|string Position value (number, \"center\", or percentage)\n--- @param window_size number Size of the window\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated position\n--- @private\nlocal function calculate_float_position(value, window_size, max_value)\n local pos\n if value == 'center' then\n pos = math.floor((max_value - window_size) / 2)\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n pos = math.floor(max_value * percentage / 100)\n else\n pos = value or 0\n end\n -- Clamp position to ensure window is visible\n return math.max(0, math.min(pos, max_value - window_size))\nend\n\n--- Create a floating window for Claude Code\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the float (optional)\n--- @return number Window ID of the created floating window\n--- @private\nlocal function create_float(config, existing_bufnr)\n local float_config = config.window.float or {}\n\n -- Get editor dimensions (accounting for command line, status line, etc.)\n local editor_width = vim.o.columns\n local editor_height = vim.o.lines - vim.o.cmdheight - 1 -- Subtract command line and status line\n\n -- Calculate dimensions\n local width = calculate_float_dimension(float_config.width, editor_width)\n local height = calculate_float_dimension(float_config.height, editor_height)\n\n -- Calculate position\n local row = calculate_float_position(float_config.row, height, editor_height)\n local col = calculate_float_position(float_config.col, width, editor_width)\n\n -- Create floating window configuration\n local win_config = {\n relative = float_config.relative or 'editor',\n width = width,\n height = height,\n row = row,\n col = col,\n border = float_config.border or 'rounded',\n style = 'minimal',\n }\n\n -- Create buffer if we don't have an existing one\n local bufnr = existing_bufnr\n if not bufnr then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n -- Validate existing buffer is still valid and a terminal\n if not vim.api.nvim_buf_is_valid(bufnr) then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n local buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n if buftype ~= 'terminal' then\n -- Buffer exists but is no longer a terminal, create a new one\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n end\n end\n end\n\n -- Create and return the floating window\n return vim.api.nvim_open_win(bufnr, true, win_config)\nend\n\n--- Build command with git root directory if configured\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param base_cmd string Base command to run\n--- @return string Command with git root directory change if applicable\n--- @private\nlocal function build_command_with_git_root(config, git, base_cmd)\n if config.git and config.git.use_git_root then\n local git_root = git.get_git_root()\n if git_root then\n local quoted_root = vim.fn.shellescape(git_root)\n -- Use configurable shell commands\n local separator = config.shell.separator\n local pushd_cmd = config.shell.pushd_cmd\n local popd_cmd = config.shell.popd_cmd\n return pushd_cmd\n .. ' '\n .. quoted_root\n .. ' '\n .. separator\n .. ' '\n .. base_cmd\n .. ' '\n .. separator\n .. ' '\n .. popd_cmd\n end\n end\n return base_cmd\nend\n\n--- Configure common window options\n--- @param win_id number Window ID to configure\n--- @param config table Plugin configuration\n--- @private\nlocal function configure_window_options(win_id, config)\n if config.window.hide_numbers then\n vim.api.nvim_set_option_value('number', false, {win = win_id})\n vim.api.nvim_set_option_value('relativenumber', false, {win = win_id})\n end\n\n if config.window.hide_signcolumn then\n vim.api.nvim_set_option_value('signcolumn', 'no', {win = win_id})\n end\nend\n\n--- Generate buffer name for instance\n--- @param instance_id string Instance identifier\n--- @param config table Plugin configuration\n--- @return string Buffer name\n--- @private\nlocal function generate_buffer_name(instance_id, config)\n if config.git.multi_instance then\n return 'claude-code-' .. instance_id:gsub('[^%w%-_]', '-')\n else\n return 'claude-code'\n end\nend\n\n--- Create a split window according to the specified position configuration\n--- @param position string Window position configuration\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the split (optional)\n--- @private\nlocal function create_split(position, config, existing_bufnr)\n -- Handle floating window\n if position == 'float' then\n return create_float(config, existing_bufnr)\n end\n\n local is_vertical = position:match('vsplit') or position:match('vertical')\n\n -- Create the window with the user's specified command\n -- If the command already contains 'split', use it as is\n if position:match('split') then\n vim.cmd(position)\n else\n -- Otherwise append the appropriate split command\n local split_cmd = is_vertical and 'vsplit' or 'split'\n vim.cmd(position .. ' ' .. split_cmd)\n end\n\n -- If we have an existing buffer to display, switch to it\n if existing_bufnr then\n vim.cmd('buffer ' .. existing_bufnr)\n end\n\n -- Resize the window appropriately based on split type\n if is_vertical then\n vim.cmd('vertical resize ' .. math.floor(vim.o.columns * config.window.split_ratio))\n else\n vim.cmd('resize ' .. math.floor(vim.o.lines * config.window.split_ratio))\n end\nend\n\n--- Set up function to force insert mode when entering the Claude Code window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.force_insert_mode(claude_code, config)\n local current_bufnr = vim.fn.bufnr('%')\n\n -- Check if current buffer is any of our Claude instances\n local is_claude_instance = false\n for _, bufnr in pairs(claude_code.claude_code.instances) do\n if bufnr and bufnr == current_bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n is_claude_instance = true\n break\n end\n end\n\n if is_claude_instance then\n -- Only enter insert mode if we're in the terminal buffer and not already in insert mode\n -- and not configured to stay in normal mode\n if config.window.start_in_normal_mode then\n return\n end\n\n local mode = vim.api.nvim_get_mode().mode\n if vim.bo.buftype == 'terminal' and mode ~= 't' and mode ~= 'i' then\n vim.cmd 'silent! stopinsert'\n vim.schedule(function()\n vim.cmd 'silent! startinsert'\n end)\n end\n end\nend\n\n--- Determine instance ID based on configuration\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @return string instance_id Instance identifier\n--- @private\nlocal function get_instance_id(config, git)\n if config.git.multi_instance then\n if config.git.use_git_root then\n return get_instance_identifier(git)\n else\n return vim.fn.getcwd()\n end\n else\n -- Use a fixed ID for single instance mode\n return 'global'\n end\nend\n\n--- Check if buffer is a valid terminal\n--- @param bufnr number Buffer number\n--- @return boolean is_valid True if buffer is a valid terminal\n--- @private\nlocal function is_valid_terminal_buffer(bufnr)\n if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n\n local buftype = nil\n pcall(function()\n buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n end)\n \n local terminal_job_id = nil\n pcall(function()\n terminal_job_id = vim.b[bufnr].terminal_job_id\n end)\n\n return buftype == 'terminal'\n and terminal_job_id\n and vim.fn.jobwait({ terminal_job_id }, 0)[1] == -1\nend\n\n--- Handle existing instance (toggle visibility)\n--- @param bufnr number Buffer number\n--- @param config table Plugin configuration\n--- @private\nlocal function handle_existing_instance(bufnr, config)\n local win_ids = vim.fn.win_findbuf(bufnr)\n if #win_ids > 0 then\n -- Claude Code is visible, close the window\n for _, win_id in ipairs(win_ids) do\n vim.api.nvim_win_close(win_id, true)\n end\n else\n -- Claude Code buffer exists but is not visible, open it in a split or float\n if config.window.position == 'float' then\n create_float(config, bufnr)\n else\n create_split(config.window.position, config, bufnr)\n end\n -- Force insert mode more aggressively unless configured to start in normal mode\n if not config.window.start_in_normal_mode then\n vim.schedule(function()\n vim.cmd 'stopinsert | startinsert'\n end)\n end\n end\nend\n\n--- Create new Claude Code instance\n--- @param claude_code table The main plugin module\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param instance_id string Instance identifier\n--- @private\nlocal function create_new_instance(claude_code, config, git, instance_id)\n if config.window.position == 'float' then\n -- For floating window, create buffer first with terminal\n local new_bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n vim.api.nvim_set_option_value('bufhidden', 'hide', {buf = new_bufnr})\n\n -- Create the floating window\n local win_id = create_float(config, new_bufnr)\n\n -- Set current buffer to run terminal command\n vim.api.nvim_win_set_buf(win_id, new_bufnr)\n\n -- Determine command\n local cmd = build_command_with_git_root(config, git, config.command)\n\n -- Run terminal in the buffer\n vim.fn.termopen(cmd)\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.api.nvim_buf_set_name(new_bufnr, buffer_name)\n\n -- Configure window options\n configure_window_options(win_id, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = new_bufnr\n\n -- Enter insert mode if configured\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n else\n -- Regular split window\n create_split(config.window.position, config)\n\n -- Determine if we should use the git root directory\n local base_cmd = build_command_with_git_root(config, git, config.command)\n local cmd = 'terminal ' .. base_cmd\n\n vim.cmd(cmd)\n vim.cmd 'setlocal bufhidden=hide'\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.cmd('file ' .. buffer_name)\n\n -- Configure window options using helper function\n local current_win = vim.api.nvim_get_current_win()\n configure_window_options(current_win, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = vim.fn.bufnr('%')\n\n -- Automatically enter insert mode in terminal unless configured to start in normal mode\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n end\nend\n\n--- Toggle the Claude Code terminal window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\n--- @param git table The git module\nfunction M.toggle(claude_code, config, git)\n -- Determine instance ID based on config\n local instance_id = get_instance_id(config, git)\n claude_code.claude_code.current_instance = instance_id\n\n -- Check if this Claude Code instance is already running\n local bufnr = claude_code.claude_code.instances[instance_id]\n\n -- Validate existing buffer\n if bufnr and not is_valid_terminal_buffer(bufnr) then\n -- Buffer is no longer a valid terminal, reset\n claude_code.claude_code.instances[instance_id] = nil\n bufnr = nil\n end\n\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n -- Handle existing instance (toggle visibility)\n handle_existing_instance(bufnr, config)\n else\n -- Prune invalid buffer entries\n if bufnr and not vim.api.nvim_buf_is_valid(bufnr) then\n claude_code.claude_code.instances[instance_id] = nil\n end\n -- Create new instance\n create_new_instance(claude_code, config, git, instance_id)\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/init.lua", "---@mod claude-code Claude Code Neovim Integration\n---@brief [[\n--- A plugin for seamless integration between Claude Code AI assistant and Neovim.\n--- This plugin provides a terminal-based interface to Claude Code within Neovim.\n---\n--- Requirements:\n--- - Neovim 0.7.0 or later\n--- - Claude Code CLI tool installed and available in PATH\n--- - plenary.nvim (dependency for git operations)\n---\n--- Usage:\n--- ```lua\n--- require('claude-code').setup({\n--- -- Configuration options (optional)\n--- })\n--- ```\n---@brief ]]\n\n-- Import modules\nlocal config = require('claude-code.config')\nlocal commands = require('claude-code.commands')\nlocal keymaps = require('claude-code.keymaps')\nlocal file_refresh = require('claude-code.file_refresh')\nlocal terminal = require('claude-code.terminal')\nlocal git = require('claude-code.git')\nlocal version = require('claude-code.version')\n\nlocal M = {}\n\n-- Make imported modules available\nM.commands = commands\n\n-- Store the current configuration\n--- @type table\nM.config = {}\n\n-- Terminal buffer and window management\n--- @type table\nM.claude_code = terminal.terminal\n\n--- Force insert mode when entering the Claude Code window\n--- This is a public function used in keymaps\nfunction M.force_insert_mode()\n terminal.force_insert_mode(M, M.config)\nend\n\n--- Get the current active buffer number\n--- @return number|nil bufnr Current Claude instance buffer number or nil\nlocal function get_current_buffer_number()\n -- Get current instance from the instances table\n local current_instance = M.claude_code.current_instance\n if current_instance and type(M.claude_code.instances) == 'table' then\n return M.claude_code.instances[current_instance]\n end\n return nil\nend\n\n--- Toggle the Claude Code terminal window\n--- This is a public function used by commands\nfunction M.toggle()\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\nend\n\n--- Toggle the Claude Code terminal window with a specific command variant\n--- @param variant_name string The name of the command variant to use\nfunction M.toggle_with_variant(variant_name)\n if not variant_name or not M.config.command_variants[variant_name] then\n -- If variant doesn't exist, fall back to regular toggle\n return M.toggle()\n end\n\n -- Store the original command\n local original_command = M.config.command\n\n -- Set the command with the variant args\n M.config.command = original_command .. ' ' .. M.config.command_variants[variant_name]\n\n -- Call the toggle function with the modified command\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\n\n -- Restore the original command\n M.config.command = original_command\nend\n\n--- Get the current version of the plugin\n--- @return string version Current version string\nfunction M.get_version()\n return version.string()\nend\n\n--- Version information\nM.version = version\n\n--- Setup function for the plugin\n--- @param user_config? table User configuration table (optional)\nfunction M.setup(user_config)\n -- Parse and validate configuration\n -- Don't use silent mode for regular usage - users should see config errors\n M.config = config.parse_config(user_config, false)\n\n -- Set up autoread option\n vim.o.autoread = true\n\n -- Set up file refresh functionality\n file_refresh.setup(M, M.config)\n\n -- Register commands\n commands.register_commands(M)\n\n -- Register keymaps\n keymaps.register_keymaps(M, M.config)\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/commands.lua", "---@mod claude-code.commands Command registration for claude-code.nvim\n---@brief [[\n--- This module provides command registration and handling for claude-code.nvim.\n--- It defines user commands and command handlers.\n---@brief ]]\n\nlocal M = {}\n\n--- @type table List of available commands and their handlers\nM.commands = {}\n\n--- Register commands for the claude-code plugin\n--- @param claude_code table The main plugin module\nfunction M.register_commands(claude_code)\n -- Create the user command for toggling Claude Code\n vim.api.nvim_create_user_command('ClaudeCode', function()\n claude_code.toggle()\n end, { desc = 'Toggle Claude Code terminal' })\n\n -- Create commands for each command variant\n for variant_name, variant_args in pairs(claude_code.config.command_variants) do\n if variant_args ~= false then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_create_user_command(cmd_name, function()\n claude_code.toggle_with_variant(variant_name)\n end, { desc = 'Toggle Claude Code terminal with ' .. variant_name .. ' option' })\n end\n end\n\n -- Add version command\n vim.api.nvim_create_user_command('ClaudeCodeVersion', function()\n vim.notify('Claude Code version: ' .. claude_code.version(), vim.log.levels.INFO)\n end, { desc = 'Display Claude Code version' })\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/git.lua", "---@mod claude-code.git Git integration for claude-code.nvim\n---@brief [[\n--- This module provides git integration functionality for claude-code.nvim.\n--- It detects git repositories and can set the working directory to the git root.\n---@brief ]]\n\nlocal M = {}\n\n--- Helper function to get git root directory\n--- @return string|nil git_root The git root directory path or nil if not in a git repo\nfunction M.get_git_root()\n -- For testing compatibility\n if vim.env.CLAUDE_CODE_TEST_MODE == 'true' then\n return '/home/user/project'\n end\n\n -- Check if we're in a git repository\n local handle = io.popen('git rev-parse --is-inside-work-tree 2>/dev/null')\n if not handle then\n return nil\n end\n\n local result = handle:read('*a')\n handle:close()\n\n -- Strip trailing whitespace and newlines for reliable matching\n result = result:gsub('[\\n\\r%s]*$', '')\n\n if result == 'true' then\n -- Get the git root path\n local root_handle = io.popen('git rev-parse --show-toplevel 2>/dev/null')\n if not root_handle then\n return nil\n end\n\n local git_root = root_handle:read('*a')\n root_handle:close()\n\n -- Remove trailing whitespace and newlines\n git_root = git_root:gsub('[\\n\\r%s]*$', '')\n\n return git_root\n end\n\n return nil\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/version.lua", "---@mod claude-code.version Version information for claude-code.nvim\n---@brief [[\n--- This module provides version information for claude-code.nvim.\n---@brief ]]\n\n--- @table M\n--- Version information for Claude Code\n--- @field major number Major version (breaking changes)\n--- @field minor number Minor version (new features)\n--- @field patch number Patch version (bug fixes)\n--- @field string function Returns formatted version string\n\nlocal M = {}\n\n-- Individual version components\nM.major = 0\nM.minor = 4\nM.patch = 2\n\n-- Combined semantic version\nM.version = string.format('%d.%d.%d', M.major, M.minor, M.patch)\n\n--- Returns the formatted version string (for backward compatibility)\n--- @return string Version string in format \"major.minor.patch\"\nfunction M.string()\n return M.version\nend\n\n--- Prints the current version of the plugin\nfunction M.print_version()\n vim.notify('Claude Code version: ' .. M.string(), vim.log.levels.INFO)\nend\n\nreturn M\n"]], "task_instance_info": {"created_time": "2025-08-21 00:40:22", "created_task_model": "DeepSeek-R1", "function_skeleton": "function () end", "code_description": "This code segment functions to: Integrate with an external keybinding visualization system by dynamically registering command descriptions and icons for different interaction modes, ensuring consistent documentation across user interface elements."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```lua\n[TASK_BEGIN]\n function()\n local wk_ok, wk = pcall(require, 'which-key')\n if wk_ok then\n -- Register main toggle keymap\n if config.keymaps.toggle.normal then\n wk.register({\n [config.keymaps.toggle.normal] = { 'ClaudeCode', 'Claude Code: Toggle' },\n }, { mode = 'n', prefix = '', plugin = 'Claude Code' })\n end\n\n -- Register terminal toggle keymap\n if config.keymaps.toggle.terminal then\n wk.register({\n [config.keymaps.toggle.terminal] = { 'ClaudeCode', 'Claude Code: Toggle' },\n }, { mode = 't', prefix = '', plugin = 'Claude Code' })\n end\n\n -- Register variant keymaps\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n \n wk.register({\n [keymap] = { string.format('%s', cmd_name), 'Claude Code: ' .. capitalized_name },\n }, { mode = 'n', prefix = '', plugin = 'Claude Code' })\n end\n end\n end\n end\n end\n[TASK_END]\n```", "inference_time": "2025-08-21 00-40-47"}, "editdistance_info": {"edit_distance": 45.5197, "calculate_time": "2025-08-21 00:40:47", "true_code_clean": "function()\n local status_ok, which_key = pcall(require, 'which-key')\n if status_ok then\n if config.keymaps.toggle.normal then\n which_key.add {\n mode = 'n',\n { config.keymaps.toggle.normal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.terminal then\n which_key.add {\n mode = 't',\n { config.keymaps.toggle.terminal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n which_key.add {\n mode = 'n',\n { keymap, desc = 'Claude Code: ' .. capitalized_name, icon = '🤖' },\n }\n end\n end\n end\n end\n end", "predict_code_clean": "function()\n local wk_ok, wk = pcall(require, 'which-key')\n if wk_ok then\n if config.keymaps.toggle.normal then\n wk.register({\n [config.keymaps.toggle.normal] = { 'ClaudeCode', 'Claude Code: Toggle' },\n }, { mode = 'n', prefix = '', plugin = 'Claude Code' })\n end\n if config.keymaps.toggle.terminal then\n wk.register({\n [config.keymaps.toggle.terminal] = { 'ClaudeCode', 'Claude Code: Toggle' },\n }, { mode = 't', prefix = '', plugin = 'Claude Code' })\n end\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n wk.register({\n [keymap] = { string.format('%s', cmd_name), 'Claude Code: ' .. capitalized_name },\n }, { mode = 'n', prefix = '', plugin = 'Claude Code' })\n end\n end\n end\n end\n end"}} {"repo_name": "claude-code.nvim", "file_name": "/claude-code.nvim/lua/claude-code/git.lua", "inference_info": {"prefix_code": "---@mod claude-code.git Git integration for claude-code.nvim\n---@brief [[\n--- This module provides git integration functionality for claude-code.nvim.\n--- It detects git repositories and can set the working directory to the git root.\n---@brief ]]\n\nlocal M = {}\n\n--- Helper function to get git root directory\n--- @return string|nil git_root The git root directory path or nil if not in a git repo\n", "suffix_code": "\n\nreturn M\n", "middle_code": "function M.get_git_root()\n if vim.env.CLAUDE_CODE_TEST_MODE == 'true' then\n return '/home/user/project'\n end\n local handle = io.popen('git rev-parse \n if not handle then\n return nil\n end\n local result = handle:read('*a')\n handle:close()\n result = result:gsub('[\\n\\r%s]*$', '')\n if result == 'true' then\n local root_handle = io.popen('git rev-parse \n if not root_handle then\n return nil\n end\n local git_root = root_handle:read('*a')\n root_handle:close()\n git_root = git_root:gsub('[\\n\\r%s]*$', '')\n return git_root\n end\n return nil\nend", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "lua", "sub_task_type": null}, "context_code": [["/claude-code.nvim/lua/claude-code/terminal.lua", "---@mod claude-code.terminal Terminal management for claude-code.nvim\n---@brief [[\n--- This module provides terminal buffer management for claude-code.nvim.\n--- It handles creating, toggling, and managing the terminal window.\n---@brief ]]\n\nlocal M = {}\n\n--- Terminal buffer and window management\n-- @table ClaudeCodeTerminal\n-- @field instances table Key-value store of git root to buffer number\n-- @field saved_updatetime number|nil Original updatetime before Claude Code was opened\n-- @field current_instance string|nil Current git root path for active instance\nM.terminal = {\n instances = {},\n saved_updatetime = nil,\n current_instance = nil,\n}\n\n--- Get the current git root or a fallback identifier\n--- @param git table The git module\n--- @return string identifier Git root path or fallback identifier\nlocal function get_instance_identifier(git)\n local git_root = git.get_git_root()\n if git_root then\n return git_root\n else\n -- Fallback to current working directory if not in a git repo\n return vim.fn.getcwd()\n end\nend\n\n--- Calculate floating window dimensions from percentage strings\n--- @param value number|string Dimension value (number or percentage string)\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated dimension\n--- @private\nlocal function calculate_float_dimension(value, max_value)\n if value == nil then\n return math.floor(max_value * 0.8) -- Default to 80% if not specified\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n return math.floor(max_value * percentage / 100)\n end\n return value\nend\n\n--- Calculate floating window position for centering\n--- @param value number|string Position value (number, \"center\", or percentage)\n--- @param window_size number Size of the window\n--- @param max_value number Maximum value (columns or lines)\n--- @return number Calculated position\n--- @private\nlocal function calculate_float_position(value, window_size, max_value)\n local pos\n if value == 'center' then\n pos = math.floor((max_value - window_size) / 2)\n elseif type(value) == 'string' and value:match('^%d+%%$') then\n local percentage = tonumber(value:match('^(%d+)%%$'))\n pos = math.floor(max_value * percentage / 100)\n else\n pos = value or 0\n end\n -- Clamp position to ensure window is visible\n return math.max(0, math.min(pos, max_value - window_size))\nend\n\n--- Create a floating window for Claude Code\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the float (optional)\n--- @return number Window ID of the created floating window\n--- @private\nlocal function create_float(config, existing_bufnr)\n local float_config = config.window.float or {}\n\n -- Get editor dimensions (accounting for command line, status line, etc.)\n local editor_width = vim.o.columns\n local editor_height = vim.o.lines - vim.o.cmdheight - 1 -- Subtract command line and status line\n\n -- Calculate dimensions\n local width = calculate_float_dimension(float_config.width, editor_width)\n local height = calculate_float_dimension(float_config.height, editor_height)\n\n -- Calculate position\n local row = calculate_float_position(float_config.row, height, editor_height)\n local col = calculate_float_position(float_config.col, width, editor_width)\n\n -- Create floating window configuration\n local win_config = {\n relative = float_config.relative or 'editor',\n width = width,\n height = height,\n row = row,\n col = col,\n border = float_config.border or 'rounded',\n style = 'minimal',\n }\n\n -- Create buffer if we don't have an existing one\n local bufnr = existing_bufnr\n if not bufnr then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n -- Validate existing buffer is still valid and a terminal\n if not vim.api.nvim_buf_is_valid(bufnr) then\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n else\n local buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n if buftype ~= 'terminal' then\n -- Buffer exists but is no longer a terminal, create a new one\n bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n end\n end\n end\n\n -- Create and return the floating window\n return vim.api.nvim_open_win(bufnr, true, win_config)\nend\n\n--- Build command with git root directory if configured\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param base_cmd string Base command to run\n--- @return string Command with git root directory change if applicable\n--- @private\nlocal function build_command_with_git_root(config, git, base_cmd)\n if config.git and config.git.use_git_root then\n local git_root = git.get_git_root()\n if git_root then\n local quoted_root = vim.fn.shellescape(git_root)\n -- Use configurable shell commands\n local separator = config.shell.separator\n local pushd_cmd = config.shell.pushd_cmd\n local popd_cmd = config.shell.popd_cmd\n return pushd_cmd\n .. ' '\n .. quoted_root\n .. ' '\n .. separator\n .. ' '\n .. base_cmd\n .. ' '\n .. separator\n .. ' '\n .. popd_cmd\n end\n end\n return base_cmd\nend\n\n--- Configure common window options\n--- @param win_id number Window ID to configure\n--- @param config table Plugin configuration\n--- @private\nlocal function configure_window_options(win_id, config)\n if config.window.hide_numbers then\n vim.api.nvim_set_option_value('number', false, {win = win_id})\n vim.api.nvim_set_option_value('relativenumber', false, {win = win_id})\n end\n\n if config.window.hide_signcolumn then\n vim.api.nvim_set_option_value('signcolumn', 'no', {win = win_id})\n end\nend\n\n--- Generate buffer name for instance\n--- @param instance_id string Instance identifier\n--- @param config table Plugin configuration\n--- @return string Buffer name\n--- @private\nlocal function generate_buffer_name(instance_id, config)\n if config.git.multi_instance then\n return 'claude-code-' .. instance_id:gsub('[^%w%-_]', '-')\n else\n return 'claude-code'\n end\nend\n\n--- Create a split window according to the specified position configuration\n--- @param position string Window position configuration\n--- @param config table Plugin configuration containing window settings\n--- @param existing_bufnr number|nil Buffer number of existing buffer to show in the split (optional)\n--- @private\nlocal function create_split(position, config, existing_bufnr)\n -- Handle floating window\n if position == 'float' then\n return create_float(config, existing_bufnr)\n end\n\n local is_vertical = position:match('vsplit') or position:match('vertical')\n\n -- Create the window with the user's specified command\n -- If the command already contains 'split', use it as is\n if position:match('split') then\n vim.cmd(position)\n else\n -- Otherwise append the appropriate split command\n local split_cmd = is_vertical and 'vsplit' or 'split'\n vim.cmd(position .. ' ' .. split_cmd)\n end\n\n -- If we have an existing buffer to display, switch to it\n if existing_bufnr then\n vim.cmd('buffer ' .. existing_bufnr)\n end\n\n -- Resize the window appropriately based on split type\n if is_vertical then\n vim.cmd('vertical resize ' .. math.floor(vim.o.columns * config.window.split_ratio))\n else\n vim.cmd('resize ' .. math.floor(vim.o.lines * config.window.split_ratio))\n end\nend\n\n--- Set up function to force insert mode when entering the Claude Code window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.force_insert_mode(claude_code, config)\n local current_bufnr = vim.fn.bufnr('%')\n\n -- Check if current buffer is any of our Claude instances\n local is_claude_instance = false\n for _, bufnr in pairs(claude_code.claude_code.instances) do\n if bufnr and bufnr == current_bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n is_claude_instance = true\n break\n end\n end\n\n if is_claude_instance then\n -- Only enter insert mode if we're in the terminal buffer and not already in insert mode\n -- and not configured to stay in normal mode\n if config.window.start_in_normal_mode then\n return\n end\n\n local mode = vim.api.nvim_get_mode().mode\n if vim.bo.buftype == 'terminal' and mode ~= 't' and mode ~= 'i' then\n vim.cmd 'silent! stopinsert'\n vim.schedule(function()\n vim.cmd 'silent! startinsert'\n end)\n end\n end\nend\n\n--- Determine instance ID based on configuration\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @return string instance_id Instance identifier\n--- @private\nlocal function get_instance_id(config, git)\n if config.git.multi_instance then\n if config.git.use_git_root then\n return get_instance_identifier(git)\n else\n return vim.fn.getcwd()\n end\n else\n -- Use a fixed ID for single instance mode\n return 'global'\n end\nend\n\n--- Check if buffer is a valid terminal\n--- @param bufnr number Buffer number\n--- @return boolean is_valid True if buffer is a valid terminal\n--- @private\nlocal function is_valid_terminal_buffer(bufnr)\n if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then\n return false\n end\n\n local buftype = nil\n pcall(function()\n buftype = vim.api.nvim_get_option_value('buftype', {buf = bufnr})\n end)\n \n local terminal_job_id = nil\n pcall(function()\n terminal_job_id = vim.b[bufnr].terminal_job_id\n end)\n\n return buftype == 'terminal'\n and terminal_job_id\n and vim.fn.jobwait({ terminal_job_id }, 0)[1] == -1\nend\n\n--- Handle existing instance (toggle visibility)\n--- @param bufnr number Buffer number\n--- @param config table Plugin configuration\n--- @private\nlocal function handle_existing_instance(bufnr, config)\n local win_ids = vim.fn.win_findbuf(bufnr)\n if #win_ids > 0 then\n -- Claude Code is visible, close the window\n for _, win_id in ipairs(win_ids) do\n vim.api.nvim_win_close(win_id, true)\n end\n else\n -- Claude Code buffer exists but is not visible, open it in a split or float\n if config.window.position == 'float' then\n create_float(config, bufnr)\n else\n create_split(config.window.position, config, bufnr)\n end\n -- Force insert mode more aggressively unless configured to start in normal mode\n if not config.window.start_in_normal_mode then\n vim.schedule(function()\n vim.cmd 'stopinsert | startinsert'\n end)\n end\n end\nend\n\n--- Create new Claude Code instance\n--- @param claude_code table The main plugin module\n--- @param config table Plugin configuration\n--- @param git table Git module\n--- @param instance_id string Instance identifier\n--- @private\nlocal function create_new_instance(claude_code, config, git, instance_id)\n if config.window.position == 'float' then\n -- For floating window, create buffer first with terminal\n local new_bufnr = vim.api.nvim_create_buf(false, true) -- unlisted, scratch\n vim.api.nvim_set_option_value('bufhidden', 'hide', {buf = new_bufnr})\n\n -- Create the floating window\n local win_id = create_float(config, new_bufnr)\n\n -- Set current buffer to run terminal command\n vim.api.nvim_win_set_buf(win_id, new_bufnr)\n\n -- Determine command\n local cmd = build_command_with_git_root(config, git, config.command)\n\n -- Run terminal in the buffer\n vim.fn.termopen(cmd)\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.api.nvim_buf_set_name(new_bufnr, buffer_name)\n\n -- Configure window options\n configure_window_options(win_id, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = new_bufnr\n\n -- Enter insert mode if configured\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n else\n -- Regular split window\n create_split(config.window.position, config)\n\n -- Determine if we should use the git root directory\n local base_cmd = build_command_with_git_root(config, git, config.command)\n local cmd = 'terminal ' .. base_cmd\n\n vim.cmd(cmd)\n vim.cmd 'setlocal bufhidden=hide'\n\n -- Create a unique buffer name\n local buffer_name = generate_buffer_name(instance_id, config)\n vim.cmd('file ' .. buffer_name)\n\n -- Configure window options using helper function\n local current_win = vim.api.nvim_get_current_win()\n configure_window_options(current_win, config)\n\n -- Store buffer number for this instance\n claude_code.claude_code.instances[instance_id] = vim.fn.bufnr('%')\n\n -- Automatically enter insert mode in terminal unless configured to start in normal mode\n if config.window.enter_insert and not config.window.start_in_normal_mode then\n vim.cmd 'startinsert'\n end\n end\nend\n\n--- Toggle the Claude Code terminal window\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\n--- @param git table The git module\nfunction M.toggle(claude_code, config, git)\n -- Determine instance ID based on config\n local instance_id = get_instance_id(config, git)\n claude_code.claude_code.current_instance = instance_id\n\n -- Check if this Claude Code instance is already running\n local bufnr = claude_code.claude_code.instances[instance_id]\n\n -- Validate existing buffer\n if bufnr and not is_valid_terminal_buffer(bufnr) then\n -- Buffer is no longer a valid terminal, reset\n claude_code.claude_code.instances[instance_id] = nil\n bufnr = nil\n end\n\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n -- Handle existing instance (toggle visibility)\n handle_existing_instance(bufnr, config)\n else\n -- Prune invalid buffer entries\n if bufnr and not vim.api.nvim_buf_is_valid(bufnr) then\n claude_code.claude_code.instances[instance_id] = nil\n end\n -- Create new instance\n create_new_instance(claude_code, config, git, instance_id)\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/config.lua", "---@mod claude-code.config Configuration management for claude-code.nvim\n---@brief [[\n--- This module handles configuration management and validation for claude-code.nvim.\n--- It provides the default configuration, validation, and merging of user config.\n---@brief ]]\n\nlocal M = {}\n\n--- ClaudeCodeWindow class for window configuration\n-- @table ClaudeCodeWindow\n-- @field split_ratio number Percentage of screen for the terminal window (height for horizontal, width for vertical)\n-- @field position string Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\" etc.\n-- @field enter_insert boolean Whether to enter insert mode when opening Claude Code\n-- @field start_in_normal_mode boolean Whether to start in normal mode instead of insert mode when opening Claude Code\n-- @field hide_numbers boolean Hide line numbers in the terminal window\n-- @field hide_signcolumn boolean Hide the sign column in the terminal window\n-- @field float table|nil Floating window configuration (only used when position is \"float\")\n-- @field float.width number|string Width of floating window (number: columns, string: percentage like \"80%\")\n-- @field float.height number|string Height of floating window (number: rows, string: percentage like \"80%\")\n-- @field float.row number|string|nil Row position (number: absolute, string: \"center\" or percentage)\n-- @field float.col number|string|nil Column position (number: absolute, string: \"center\" or percentage)\n-- @field float.border string Border style: \"none\", \"single\", \"double\", \"rounded\", \"solid\", \"shadow\", or array\n-- @field float.relative string Relative positioning: \"editor\" or \"cursor\"\n\n--- ClaudeCodeRefresh class for file refresh configuration\n-- @table ClaudeCodeRefresh\n-- @field enable boolean Enable file change detection\n-- @field updatetime number updatetime when Claude Code is active (milliseconds)\n-- @field timer_interval number How often to check for file changes (milliseconds)\n-- @field show_notifications boolean Show notification when files are reloaded\n\n--- ClaudeCodeGit class for git integration configuration\n-- @table ClaudeCodeGit\n-- @field use_git_root boolean Set CWD to git root when opening Claude Code (if in git project)\n-- @field multi_instance boolean Use multiple Claude instances (one per git root)\n\n--- ClaudeCodeKeymapsToggle class for toggle keymap configuration\n-- @table ClaudeCodeKeymapsToggle\n-- @field normal string|boolean Normal mode keymap for toggling Claude Code, false to disable\n-- @field terminal string|boolean Terminal mode keymap for toggling Claude Code, false to disable\n\n--- ClaudeCodeKeymaps class for keymap configuration\n-- @table ClaudeCodeKeymaps\n-- @field toggle ClaudeCodeKeymapsToggle Keymaps for toggling Claude Code\n-- @field window_navigation boolean Enable window navigation keymaps\n-- @field scrolling boolean Enable scrolling keymaps\n\n--- ClaudeCodeCommandVariants class for command variant configuration\n-- @table ClaudeCodeCommandVariants\n-- Conversation management:\n-- @field continue string|boolean Resume the most recent conversation\n-- @field resume string|boolean Display an interactive conversation picker\n-- Output options:\n-- @field verbose string|boolean Enable verbose logging with full turn-by-turn output\n-- Additional options can be added as needed\n\n--- ClaudeCodeShell class for shell configuration\n-- @table ClaudeCodeShell\n-- @field separator string Command separator used in shell commands (e.g., '&&', ';', '|')\n-- @field pushd_cmd string Command to push directory onto stack (e.g., 'pushd' for bash/zsh)\n-- @field popd_cmd string Command to pop directory from stack (e.g., 'popd' for bash/zsh)\n\n--- ClaudeCodeConfig class for main configuration\n-- @table ClaudeCodeConfig\n-- @field window ClaudeCodeWindow Terminal window settings\n-- @field refresh ClaudeCodeRefresh File refresh settings\n-- @field git ClaudeCodeGit Git integration settings\n-- @field shell ClaudeCodeShell Shell-specific configuration\n-- @field command string Command used to launch Claude Code\n-- @field command_variants ClaudeCodeCommandVariants Command variants configuration\n-- @field keymaps ClaudeCodeKeymaps Keymaps configuration\n\n--- Default configuration options\n--- @type ClaudeCodeConfig\nM.default_config = {\n -- Terminal window settings\n window = {\n split_ratio = 0.3, -- Percentage of screen for the terminal window (height or width)\n height_ratio = 0.3, -- DEPRECATED: Use split_ratio instead\n position = 'botright', -- Position of the window: \"botright\", \"topleft\", \"vertical\", \"float\", etc.\n enter_insert = true, -- Whether to enter insert mode when opening Claude Code\n start_in_normal_mode = false, -- Whether to start in normal mode instead of insert mode\n hide_numbers = true, -- Hide line numbers in the terminal window\n hide_signcolumn = true, -- Hide the sign column in the terminal window\n -- Default floating window configuration\n float = {\n width = '80%', -- Width as percentage of editor\n height = '80%', -- Height as percentage of editor\n row = 'center', -- Center vertically\n col = 'center', -- Center horizontally\n relative = 'editor', -- Position relative to editor\n border = 'rounded', -- Border style\n },\n },\n -- File refresh settings\n refresh = {\n enable = true, -- Enable file change detection\n updatetime = 100, -- updatetime to use when Claude Code is active (milliseconds)\n timer_interval = 1000, -- How often to check for file changes (milliseconds)\n show_notifications = true, -- Show notification when files are reloaded\n },\n -- Git integration settings\n git = {\n use_git_root = true, -- Set CWD to git root when opening Claude Code (if in git project)\n multi_instance = true, -- Use multiple Claude instances (one per git root)\n },\n -- Shell-specific settings\n shell = {\n separator = '&&', -- Command separator used in shell commands\n pushd_cmd = 'pushd', -- Command to push directory onto stack\n popd_cmd = 'popd', -- Command to pop directory from stack\n },\n -- Command settings\n command = 'claude', -- Command used to launch Claude Code\n -- Command variants\n command_variants = {\n -- Conversation management\n continue = '--continue', -- Resume the most recent conversation\n resume = '--resume', -- Display an interactive conversation picker\n\n -- Output options\n verbose = '--verbose', -- Enable verbose logging with full turn-by-turn output\n },\n -- Keymaps\n keymaps = {\n toggle = {\n normal = '', -- Normal mode keymap for toggling Claude Code\n terminal = '', -- Terminal mode keymap for toggling Claude Code\n variants = {\n continue = 'cC', -- Normal mode keymap for Claude Code with continue flag\n verbose = 'cV', -- Normal mode keymap for Claude Code with verbose flag\n },\n },\n window_navigation = true, -- Enable window navigation keymaps ()\n scrolling = true, -- Enable scrolling keymaps () for page up/down\n },\n}\n\n--- Validate the configuration\n--- Validate window configuration\n--- @param window table Window configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_window_config(window)\n if type(window) ~= 'table' then\n return false, 'window config must be a table'\n end\n\n if type(window.split_ratio) ~= 'number' or window.split_ratio <= 0 or window.split_ratio > 1 then\n return false, 'window.split_ratio must be a number between 0 and 1'\n end\n\n if type(window.position) ~= 'string' then\n return false, 'window.position must be a string'\n end\n\n if type(window.enter_insert) ~= 'boolean' then\n return false, 'window.enter_insert must be a boolean'\n end\n\n if type(window.start_in_normal_mode) ~= 'boolean' then\n return false, 'window.start_in_normal_mode must be a boolean'\n end\n\n if type(window.hide_numbers) ~= 'boolean' then\n return false, 'window.hide_numbers must be a boolean'\n end\n\n if type(window.hide_signcolumn) ~= 'boolean' then\n return false, 'window.hide_signcolumn must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate floating window configuration\n--- @param float table Float configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_float_config(float)\n if type(float) ~= 'table' then\n return false, 'window.float must be a table when position is \"float\"'\n end\n\n -- Validate width (can be number or percentage string)\n if type(float.width) == 'string' then\n if not float.width:match('^%d+%%$') then\n return false, 'window.float.width must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.width) ~= 'number' or float.width <= 0 then\n return false, 'window.float.width must be a positive number or percentage string'\n end\n\n -- Validate height (can be number or percentage string)\n if type(float.height) == 'string' then\n if not float.height:match('^%d+%%$') then\n return false, 'window.float.height must be a number or percentage (e.g., \"80%\")'\n end\n elseif type(float.height) ~= 'number' or float.height <= 0 then\n return false, 'window.float.height must be a positive number or percentage string'\n end\n\n -- Validate relative (must be \"editor\" or \"cursor\")\n if float.relative ~= 'editor' and float.relative ~= 'cursor' then\n return false, 'window.float.relative must be \"editor\" or \"cursor\"'\n end\n\n -- Validate border (must be valid border style)\n local valid_borders = { 'none', 'single', 'double', 'rounded', 'solid', 'shadow' }\n local is_valid_border = false\n for _, border in ipairs(valid_borders) do\n if float.border == border then\n is_valid_border = true\n break\n end\n end\n -- Also allow array borders\n if not is_valid_border and type(float.border) ~= 'table' then\n return false,\n 'window.float.border must be one of: none, single, double, rounded, solid, shadow, or an array'\n end\n\n -- Validate row and col if they exist\n if float.row ~= nil then\n if type(float.row) == 'string' and float.row ~= 'center' then\n if not float.row:match('^%d+%%$') then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n elseif type(float.row) ~= 'number' and float.row ~= 'center' then\n return false, 'window.float.row must be a number, \"center\", or percentage string'\n end\n end\n\n if float.col ~= nil then\n if type(float.col) == 'string' and float.col ~= 'center' then\n if not float.col:match('^%d+%%$') then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n elseif type(float.col) ~= 'number' and float.col ~= 'center' then\n return false, 'window.float.col must be a number, \"center\", or percentage string'\n end\n end\n\n return true, nil\nend\n\n--- Validate refresh configuration\n--- @param refresh table Refresh configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_refresh_config(refresh)\n if type(refresh) ~= 'table' then\n return false, 'refresh config must be a table'\n end\n\n if type(refresh.enable) ~= 'boolean' then\n return false, 'refresh.enable must be a boolean'\n end\n\n if type(refresh.updatetime) ~= 'number' or refresh.updatetime <= 0 then\n return false, 'refresh.updatetime must be a positive number'\n end\n\n if type(refresh.timer_interval) ~= 'number' or refresh.timer_interval <= 0 then\n return false, 'refresh.timer_interval must be a positive number'\n end\n\n if type(refresh.show_notifications) ~= 'boolean' then\n return false, 'refresh.show_notifications must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate git configuration\n--- @param git table Git configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_git_config(git)\n if type(git) ~= 'table' then\n return false, 'git config must be a table'\n end\n\n if type(git.use_git_root) ~= 'boolean' then\n return false, 'git.use_git_root must be a boolean'\n end\n\n if type(git.multi_instance) ~= 'boolean' then\n return false, 'git.multi_instance must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate shell configuration\n--- @param shell table Shell configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_shell_config(shell)\n if type(shell) ~= 'table' then\n return false, 'shell config must be a table'\n end\n\n if type(shell.separator) ~= 'string' then\n return false, 'shell.separator must be a string'\n end\n\n if type(shell.pushd_cmd) ~= 'string' then\n return false, 'shell.pushd_cmd must be a string'\n end\n\n if type(shell.popd_cmd) ~= 'string' then\n return false, 'shell.popd_cmd must be a string'\n end\n\n return true, nil\nend\n\n--- Validate keymaps configuration\n--- @param keymaps table Keymaps configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_keymaps_config(keymaps)\n if type(keymaps) ~= 'table' then\n return false, 'keymaps config must be a table'\n end\n\n if type(keymaps.toggle) ~= 'table' then\n return false, 'keymaps.toggle must be a table'\n end\n\n if not (keymaps.toggle.normal == false or type(keymaps.toggle.normal) == 'string') then\n return false, 'keymaps.toggle.normal must be a string or false'\n end\n\n if not (keymaps.toggle.terminal == false or type(keymaps.toggle.terminal) == 'string') then\n return false, 'keymaps.toggle.terminal must be a string or false'\n end\n\n -- Validate variant keymaps if they exist\n if keymaps.toggle.variants then\n if type(keymaps.toggle.variants) ~= 'table' then\n return false, 'keymaps.toggle.variants must be a table'\n end\n\n -- Check each variant keymap\n for variant_name, keymap in pairs(keymaps.toggle.variants) do\n if not (keymap == false or type(keymap) == 'string') then\n return false, 'keymaps.toggle.variants.' .. variant_name .. ' must be a string or false'\n end\n end\n end\n\n if type(keymaps.window_navigation) ~= 'boolean' then\n return false, 'keymaps.window_navigation must be a boolean'\n end\n\n if type(keymaps.scrolling) ~= 'boolean' then\n return false, 'keymaps.scrolling must be a boolean'\n end\n\n return true, nil\nend\n\n--- Validate command variants configuration\n--- @param command_variants table Command variants configuration\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_command_variants_config(command_variants)\n if type(command_variants) ~= 'table' then\n return false, 'command_variants config must be a table'\n end\n\n -- Check each command variant\n for variant_name, variant_args in pairs(command_variants) do\n if not (variant_args == false or type(variant_args) == 'string') then\n return false, 'command_variants.' .. variant_name .. ' must be a string or false'\n end\n end\n\n return true, nil\nend\n\n--- Validate configuration options\n--- @param config ClaudeCodeConfig\n--- @return boolean valid\n--- @return string? error_message\nlocal function validate_config(config)\n -- Validate window settings\n local valid, err = validate_window_config(config.window)\n if not valid then\n return false, err\n end\n\n -- Validate float configuration if position is \"float\"\n if config.window.position == 'float' then\n valid, err = validate_float_config(config.window.float)\n if not valid then\n return false, err\n end\n end\n\n -- Validate refresh settings\n valid, err = validate_refresh_config(config.refresh)\n if not valid then\n return false, err\n end\n\n -- Validate git settings\n valid, err = validate_git_config(config.git)\n if not valid then\n return false, err\n end\n\n -- Validate shell settings\n valid, err = validate_shell_config(config.shell)\n if not valid then\n return false, err\n end\n\n -- Validate command settings\n if type(config.command) ~= 'string' then\n return false, 'command must be a string'\n end\n\n -- Validate command variants settings\n valid, err = validate_command_variants_config(config.command_variants)\n if not valid then\n return false, err\n end\n\n -- Validate keymaps settings\n valid, err = validate_keymaps_config(config.keymaps)\n if not valid then\n return false, err\n end\n\n -- Cross-validate keymaps with command variants\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n -- Ensure variant exists in command_variants\n if keymap ~= false and not config.command_variants[variant_name] then\n return false,\n 'keymaps.toggle.variants.' .. variant_name .. ' has no corresponding command variant'\n end\n end\n end\n\n return true, nil\nend\n\n--- Parse user configuration and merge with defaults\n--- @param user_config? table\n--- @param silent? boolean Set to true to suppress error notifications (for tests)\n--- @return ClaudeCodeConfig\nfunction M.parse_config(user_config, silent)\n -- Handle backward compatibility first\n if user_config and user_config.window then\n if user_config.window.height_ratio and not user_config.window.split_ratio then\n -- Copy height_ratio to split_ratio for backward compatibility\n user_config.window.split_ratio = user_config.window.height_ratio\n end\n end\n\n local config = vim.tbl_deep_extend('force', {}, M.default_config, user_config or {})\n\n -- If position is float and no float config provided, use default float config\n if\n config.window.position == 'float'\n and not (user_config and user_config.window and user_config.window.float)\n then\n config.window.float = vim.deepcopy(M.default_config.window.float)\n end\n\n local valid, err = validate_config(config)\n if not valid then\n -- Only notify if not in silent mode\n if not silent then\n vim.notify('Claude Code: ' .. err, vim.log.levels.ERROR)\n end\n -- Fall back to default config in case of error\n return vim.deepcopy(M.default_config)\n end\n\n return config\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/init.lua", "---@mod claude-code Claude Code Neovim Integration\n---@brief [[\n--- A plugin for seamless integration between Claude Code AI assistant and Neovim.\n--- This plugin provides a terminal-based interface to Claude Code within Neovim.\n---\n--- Requirements:\n--- - Neovim 0.7.0 or later\n--- - Claude Code CLI tool installed and available in PATH\n--- - plenary.nvim (dependency for git operations)\n---\n--- Usage:\n--- ```lua\n--- require('claude-code').setup({\n--- -- Configuration options (optional)\n--- })\n--- ```\n---@brief ]]\n\n-- Import modules\nlocal config = require('claude-code.config')\nlocal commands = require('claude-code.commands')\nlocal keymaps = require('claude-code.keymaps')\nlocal file_refresh = require('claude-code.file_refresh')\nlocal terminal = require('claude-code.terminal')\nlocal git = require('claude-code.git')\nlocal version = require('claude-code.version')\n\nlocal M = {}\n\n-- Make imported modules available\nM.commands = commands\n\n-- Store the current configuration\n--- @type table\nM.config = {}\n\n-- Terminal buffer and window management\n--- @type table\nM.claude_code = terminal.terminal\n\n--- Force insert mode when entering the Claude Code window\n--- This is a public function used in keymaps\nfunction M.force_insert_mode()\n terminal.force_insert_mode(M, M.config)\nend\n\n--- Get the current active buffer number\n--- @return number|nil bufnr Current Claude instance buffer number or nil\nlocal function get_current_buffer_number()\n -- Get current instance from the instances table\n local current_instance = M.claude_code.current_instance\n if current_instance and type(M.claude_code.instances) == 'table' then\n return M.claude_code.instances[current_instance]\n end\n return nil\nend\n\n--- Toggle the Claude Code terminal window\n--- This is a public function used by commands\nfunction M.toggle()\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\nend\n\n--- Toggle the Claude Code terminal window with a specific command variant\n--- @param variant_name string The name of the command variant to use\nfunction M.toggle_with_variant(variant_name)\n if not variant_name or not M.config.command_variants[variant_name] then\n -- If variant doesn't exist, fall back to regular toggle\n return M.toggle()\n end\n\n -- Store the original command\n local original_command = M.config.command\n\n -- Set the command with the variant args\n M.config.command = original_command .. ' ' .. M.config.command_variants[variant_name]\n\n -- Call the toggle function with the modified command\n terminal.toggle(M, M.config, git)\n\n -- Set up terminal navigation keymaps after toggling\n local bufnr = get_current_buffer_number()\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) then\n keymaps.setup_terminal_navigation(M, M.config)\n end\n\n -- Restore the original command\n M.config.command = original_command\nend\n\n--- Get the current version of the plugin\n--- @return string version Current version string\nfunction M.get_version()\n return version.string()\nend\n\n--- Version information\nM.version = version\n\n--- Setup function for the plugin\n--- @param user_config? table User configuration table (optional)\nfunction M.setup(user_config)\n -- Parse and validate configuration\n -- Don't use silent mode for regular usage - users should see config errors\n M.config = config.parse_config(user_config, false)\n\n -- Set up autoread option\n vim.o.autoread = true\n\n -- Set up file refresh functionality\n file_refresh.setup(M, M.config)\n\n -- Register commands\n commands.register_commands(M)\n\n -- Register keymaps\n keymaps.register_keymaps(M, M.config)\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/file_refresh.lua", "---@mod claude-code.file_refresh File refresh functionality for claude-code.nvim\n---@brief [[\n--- This module provides file refresh functionality to detect and reload files\n--- that have been modified by Claude Code or other external processes.\n---@brief ]]\n\nlocal M = {}\n\n--- Timer for checking file changes\n--- @type userdata|nil\nlocal refresh_timer = nil\n\n--- Setup autocommands for file change detection\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup(claude_code, config)\n if not config.refresh.enable then\n return\n end\n\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeFileRefresh', { clear = true })\n\n -- Create an autocommand that checks for file changes more frequently\n vim.api.nvim_create_autocmd({\n 'CursorHold',\n 'CursorHoldI',\n 'FocusGained',\n 'BufEnter',\n 'InsertLeave',\n 'TextChanged',\n 'TermLeave',\n 'TermEnter',\n 'BufWinEnter',\n }, {\n group = augroup,\n pattern = '*',\n callback = function()\n if vim.fn.filereadable(vim.fn.expand '%') == 1 then\n vim.cmd 'checktime'\n end\n end,\n desc = 'Check for file changes on disk',\n })\n\n -- Clean up any existing timer\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\n\n -- Create a timer to check for file changes periodically\n refresh_timer = vim.loop.new_timer()\n if refresh_timer then\n refresh_timer:start(\n 0,\n config.refresh.timer_interval,\n vim.schedule_wrap(function()\n -- Only check time if there's an active Claude Code terminal\n local current_instance = claude_code.claude_code.current_instance\n local bufnr = current_instance and claude_code.claude_code.instances[current_instance]\n if bufnr and vim.api.nvim_buf_is_valid(bufnr) and #vim.fn.win_findbuf(bufnr) > 0 then\n vim.cmd 'silent! checktime'\n end\n end)\n )\n end\n\n -- Create an autocommand that notifies when a file has been changed externally\n if config.refresh.show_notifications then\n vim.api.nvim_create_autocmd('FileChangedShellPost', {\n group = augroup,\n pattern = '*',\n callback = function()\n vim.notify('File changed on disk. Buffer reloaded.', vim.log.levels.INFO)\n end,\n desc = 'Notify when a file is changed externally',\n })\n end\n\n -- Set a shorter updatetime while Claude Code is open\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n\n -- When Claude Code opens, set a shorter updatetime\n vim.api.nvim_create_autocmd('TermOpen', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf = vim.api.nvim_get_current_buf()\n local buf_name = vim.api.nvim_buf_get_name(buf)\n if buf_name:match('claude%-code$') then\n claude_code.claude_code.saved_updatetime = vim.o.updatetime\n vim.o.updatetime = config.refresh.updatetime\n end\n end,\n desc = 'Set shorter updatetime when Claude Code is open',\n })\n\n -- When Claude Code closes, restore normal updatetime\n vim.api.nvim_create_autocmd('TermClose', {\n group = augroup,\n pattern = '*',\n callback = function()\n local buf_name = vim.api.nvim_buf_get_name(0)\n if buf_name:match('claude%-code$') then\n vim.o.updatetime = claude_code.claude_code.saved_updatetime\n end\n end,\n desc = 'Restore normal updatetime when Claude Code is closed',\n })\nend\n\n--- Clean up the file refresh functionality (stop the timer)\nfunction M.cleanup()\n if refresh_timer then\n refresh_timer:stop()\n refresh_timer:close()\n refresh_timer = nil\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/keymaps.lua", "---@mod claude-code.keymaps Keymap management for claude-code.nvim\n---@brief [[\n--- This module provides keymap registration and handling for claude-code.nvim.\n--- It handles normal mode, terminal mode, and window navigation keymaps.\n---@brief ]]\n\nlocal M = {}\n\n--- Register keymaps for claude-code.nvim\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.register_keymaps(claude_code, config)\n local map_opts = { noremap = true, silent = true }\n\n -- Normal mode toggle keymaps\n if config.keymaps.toggle.normal then\n vim.api.nvim_set_keymap(\n 'n',\n config.keymaps.toggle.normal,\n [[ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n if config.keymaps.toggle.terminal then\n -- Terminal mode toggle keymap\n -- In terminal mode, special keys like Ctrl need different handling\n -- We use a direct escape sequence approach for more reliable terminal mappings\n vim.api.nvim_set_keymap(\n 't',\n config.keymaps.toggle.terminal,\n [[:ClaudeCode]],\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: Toggle' })\n )\n end\n\n -- Register variant keymaps if configured\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_set_keymap(\n 'n',\n keymap,\n string.format([[%s]], cmd_name),\n vim.tbl_extend('force', map_opts, { desc = 'Claude Code: ' .. capitalized_name })\n )\n end\n end\n end\n\n -- Register with which-key if it's available\n vim.defer_fn(function()\n local status_ok, which_key = pcall(require, 'which-key')\n if status_ok then\n if config.keymaps.toggle.normal then\n which_key.add {\n mode = 'n',\n { config.keymaps.toggle.normal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n if config.keymaps.toggle.terminal then\n which_key.add {\n mode = 't',\n { config.keymaps.toggle.terminal, desc = 'Claude Code: Toggle', icon = '🤖' },\n }\n end\n\n -- Register variant keymaps with which-key\n if config.keymaps.toggle.variants then\n for variant_name, keymap in pairs(config.keymaps.toggle.variants) do\n if keymap then\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n which_key.add {\n mode = 'n',\n { keymap, desc = 'Claude Code: ' .. capitalized_name, icon = '🤖' },\n }\n end\n end\n end\n end\n end, 100)\nend\n\n--- Set up terminal-specific keymaps for window navigation\n--- @param claude_code table The main plugin module\n--- @param config table The plugin configuration\nfunction M.setup_terminal_navigation(claude_code, config)\n -- Get current active Claude instance buffer\n local current_instance = claude_code.claude_code.current_instance\n local buf = current_instance and claude_code.claude_code.instances[current_instance]\n if buf and vim.api.nvim_buf_is_valid(buf) then\n -- Create autocommand to enter insert mode when the terminal window gets focus\n local augroup = vim.api.nvim_create_augroup('ClaudeCodeTerminalFocus_' .. buf, { clear = true })\n\n -- Set up multiple events for more reliable focus detection\n vim.api.nvim_create_autocmd(\n { 'WinEnter', 'BufEnter', 'WinLeave', 'FocusGained', 'CmdLineLeave' },\n {\n group = augroup,\n callback = function()\n vim.schedule(claude_code.force_insert_mode)\n end,\n desc = 'Auto-enter insert mode when focusing Claude Code terminal',\n }\n )\n\n -- Window navigation keymaps\n if config.keymaps.window_navigation then\n -- Window navigation keymaps with special handling to force insert mode in the target window\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n\n -- Also add normal mode mappings for when user is in normal mode in the terminal\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[h:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move left' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[j:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[k:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move up' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 'n',\n '',\n [[l:lua require(\"claude-code\").force_insert_mode()]],\n { noremap = true, silent = true, desc = 'Window: move right' }\n )\n end\n\n -- Add scrolling keymaps\n if config.keymaps.scrolling then\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page down' }\n )\n vim.api.nvim_buf_set_keymap(\n buf,\n 't',\n '',\n [[i]],\n { noremap = true, silent = true, desc = 'Scroll full page up' }\n )\n end\n end\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/commands.lua", "---@mod claude-code.commands Command registration for claude-code.nvim\n---@brief [[\n--- This module provides command registration and handling for claude-code.nvim.\n--- It defines user commands and command handlers.\n---@brief ]]\n\nlocal M = {}\n\n--- @type table List of available commands and their handlers\nM.commands = {}\n\n--- Register commands for the claude-code plugin\n--- @param claude_code table The main plugin module\nfunction M.register_commands(claude_code)\n -- Create the user command for toggling Claude Code\n vim.api.nvim_create_user_command('ClaudeCode', function()\n claude_code.toggle()\n end, { desc = 'Toggle Claude Code terminal' })\n\n -- Create commands for each command variant\n for variant_name, variant_args in pairs(claude_code.config.command_variants) do\n if variant_args ~= false then\n -- Convert variant name to PascalCase for command name (e.g., \"continue\" -> \"Continue\")\n local capitalized_name = variant_name:gsub('^%l', string.upper)\n local cmd_name = 'ClaudeCode' .. capitalized_name\n\n vim.api.nvim_create_user_command(cmd_name, function()\n claude_code.toggle_with_variant(variant_name)\n end, { desc = 'Toggle Claude Code terminal with ' .. variant_name .. ' option' })\n end\n end\n\n -- Add version command\n vim.api.nvim_create_user_command('ClaudeCodeVersion', function()\n vim.notify('Claude Code version: ' .. claude_code.version(), vim.log.levels.INFO)\n end, { desc = 'Display Claude Code version' })\nend\n\nreturn M\n"], ["/claude-code.nvim/lua/claude-code/version.lua", "---@mod claude-code.version Version information for claude-code.nvim\n---@brief [[\n--- This module provides version information for claude-code.nvim.\n---@brief ]]\n\n--- @table M\n--- Version information for Claude Code\n--- @field major number Major version (breaking changes)\n--- @field minor number Minor version (new features)\n--- @field patch number Patch version (bug fixes)\n--- @field string function Returns formatted version string\n\nlocal M = {}\n\n-- Individual version components\nM.major = 0\nM.minor = 4\nM.patch = 2\n\n-- Combined semantic version\nM.version = string.format('%d.%d.%d', M.major, M.minor, M.patch)\n\n--- Returns the formatted version string (for backward compatibility)\n--- @return string Version string in format \"major.minor.patch\"\nfunction M.string()\n return M.version\nend\n\n--- Prints the current version of the plugin\nfunction M.print_version()\n vim.notify('Claude Code version: ' .. M.string(), vim.log.levels.INFO)\nend\n\nreturn M\n"]], "task_instance_info": {"created_time": "2025-08-21 00:40:22", "created_task_model": "DeepSeek-R1", "code_description": "This code segment functions to: Determine the root directory of a version control repository while supporting test environment simulation, execute system commands to verify repository status, process command outputs with whitespace sanitization, and conditionally retrieve path information based on command success verification."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```lua\n[TASK_BEGIN]\nfunction M.get_git_root()\n local output = vim.fn.system('git rev-parse --show-toplevel 2>/dev/null')\n if vim.v.shell_error ~= 0 then\n return nil\n end\n local git_root = vim.trim(output)\n return git_root ~= '' and git_root or nil\nend\n[TASK_END]\n```", "inference_time": "2025-08-21 00-40-52"}, "editdistance_info": {"edit_distance": 21.7647, "calculate_time": "2025-08-21 00:40:52", "true_code_clean": "function M.get_git_root()\n if vim.env.CLAUDE_CODE_TEST_MODE == 'true' then\n return '/home/user/project'\n end\n local handle = io.popen('git rev-parse \n if not handle then\n return nil\n end\n local result = handle:read('*a')\n handle:close()\n result = result:gsub('[\\n\\r%s]*$', '')\n if result == 'true' then\n local root_handle = io.popen('git rev-parse \n if not root_handle then\n return nil\n end\n local git_root = root_handle:read('*a')\n root_handle:close()\n git_root = git_root:gsub('[\\n\\r%s]*$', '')\n return git_root\n end\n return nil\nend", "predict_code_clean": "function M.get_git_root()\n local output = vim.fn.system('git rev-parse \n if vim.v.shell_error ~= 0 then\n return nil\n end\n local git_root = vim.trim(output)\n return git_root ~= '' and git_root or nil\nend"}}