Skip to content

feat: configurable auto-close and enhanced terminal architecture #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@ jobs:
ln -s "$(pwd)" ~/.local/share/nvim/site/pack/vendor/start/claudecode.nvim

- name: Run integration tests
run: nix develop .#ci -c nvim --headless -u tests/minimal_init.lua -c "lua require('plenary.test_harness').test_directory('tests/integration', {minimal_init = 'tests/minimal_init.lua'})"
run: nix develop .#ci -c ./scripts/run_integration_tests_individually.sh
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):
keys = {
{ "<leader>a", nil, desc = "AI/Claude Code" },
{ "<leader>ac", "<cmd>ClaudeCode<cr>", desc = "Toggle Claude" },
{ "<leader>ar", "<cmd>ClaudeCode --resume<cr>", desc = "Resume Claude" },
{ "<leader>aC", "<cmd>ClaudeCode --continue<cr>", desc = "Continue Claude" },
{ "<leader>as", "<cmd>ClaudeCodeSend<cr>", mode = "v", desc = "Send to Claude" },
{
"<leader>as",
Expand Down Expand Up @@ -78,7 +80,9 @@ That's it! For more configuration options, see [Advanced Setup](#advanced-setup)

## Commands

- `:ClaudeCode` - Toggle the Claude Code terminal window
- `:ClaudeCode [arguments]` - Toggle the Claude Code terminal window (arguments are passed to claude command)
- `:ClaudeCode --resume` - Resume a previous Claude conversation
- `:ClaudeCode --continue` - Continue Claude conversation
- `:ClaudeCodeSend` - Send current visual selection to Claude, or add files from tree explorer
- `:ClaudeCodeTreeAdd` - Add selected file(s) from tree explorer to Claude context (also available via ClaudeCodeSend)
- `:ClaudeCodeAdd <file-path> [start-line] [end-line]` - Add a specific file or directory to Claude context by path with optional line range
Expand Down Expand Up @@ -108,7 +112,7 @@ The `:ClaudeCodeAdd` command allows you to add files or directories directly by
:ClaudeCodeAdd ~/projects/myproject/
:ClaudeCodeAdd ./README.md
:ClaudeCodeAdd src/main.lua 50 100 " Lines 50-100 only
:ClaudeCodeAdd config.lua 25 " From line 25 to end of file
:ClaudeCodeAdd config.lua 25 " Only line 25
```

#### Features
Expand All @@ -132,7 +136,7 @@ The `:ClaudeCodeAdd` command allows you to add files or directories directly by

" Add specific line ranges
:ClaudeCodeAdd src/main.lua 50 100 " Lines 50 through 100
:ClaudeCodeAdd config.lua 25 " From line 25 to end of file
:ClaudeCodeAdd config.lua 25 " Only line 25
:ClaudeCodeAdd utils.py 1 50 " First 50 lines
:ClaudeCodeAdd README.md 10 20 " Just lines 10-20

Expand Down Expand Up @@ -196,6 +200,7 @@ See [DEVELOPMENT.md](./DEVELOPMENT.md) for build instructions and development gu
split_side = "right",
split_width_percentage = 0.3,
provider = "snacks", -- or "native"
auto_close = true, -- Auto-close terminal after command completion
},

-- Diff options
Expand Down Expand Up @@ -223,6 +228,29 @@ See [DEVELOPMENT.md](./DEVELOPMENT.md) for build instructions and development gu

</details>

### Terminal Auto-Close Behavior

The `auto_close` option controls what happens when Claude commands finish:

**When `auto_close = true` (default):**

- Terminal automatically closes after command completion
- Error notifications shown for failed commands (non-zero exit codes)
- Clean workflow for quick command execution

**When `auto_close = false`:**

- Terminal stays open after command completion
- Allows reviewing command output and any error messages
- Useful for debugging or when you want to see detailed output

```lua
terminal = {
provider = "snacks",
auto_close = false, -- Keep terminal open to review output
}
```

## Troubleshooting

- **Claude not connecting?** Check `:ClaudeCodeStatus` and verify lock file exists in `~/.claude/ide/`
Expand Down
46 changes: 46 additions & 0 deletions dev-config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- Development configuration for claudecode.nvim
-- This is Thomas's personal config for developing claudecode.nvim
-- Symlink this to your personal Neovim config:
-- ln -s ~/GitHub/claudecode.nvim/dev-config.lua ~/.config/nvim/lua/plugins/dev-claudecode.lua

return {
"coder/claudecode.nvim",
dev = true, -- Use local development version
dir = "~/GitHub/claudecode.nvim", -- Adjust path as needed
keys = {
-- AI/Claude Code prefix
{ "<leader>a", nil, desc = "AI/Claude Code" },

-- Core Claude commands
{ "<leader>ac", "<cmd>ClaudeCode<cr>", desc = "Toggle Claude" },
{ "<leader>ar", "<cmd>ClaudeCode --resume<cr>", desc = "Resume Claude" },
{ "<leader>aC", "<cmd>ClaudeCode --continue<cr>", desc = "Continue Claude" },

-- Context sending
{ "<leader>as", "<cmd>ClaudeCodeSend<cr>", mode = "v", desc = "Send to Claude" },
{
"<leader>as",
"<cmd>ClaudeCodeTreeAdd<cr>",
desc = "Add file from tree",
ft = { "NvimTree", "neo-tree" },
},

-- Development helpers
{ "<leader>ao", "<cmd>ClaudeCodeOpen<cr>", desc = "Open Claude" },
{ "<leader>aq", "<cmd>ClaudeCodeClose<cr>", desc = "Close Claude" },
{ "<leader>ai", "<cmd>ClaudeCodeStatus<cr>", desc = "Claude Status" },
{ "<leader>aS", "<cmd>ClaudeCodeStart<cr>", desc = "Start Claude Server" },
{ "<leader>aQ", "<cmd>ClaudeCodeStop<cr>", desc = "Stop Claude Server" },
},

-- Development configuration
opts = {
-- auto_start = true,
-- log_level = "debug",
-- terminal_cmd = "claude --debug",
-- terminal = {
-- provider = "native",
-- auto_close = false, -- Keep terminals open to see output
-- },
},
}
18 changes: 10 additions & 8 deletions lua/claudecode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,22 +648,24 @@ function M._create_commands()

local terminal_ok, terminal = pcall(require, "claudecode.terminal")
if terminal_ok then
vim.api.nvim_create_user_command("ClaudeCode", function(_opts)
vim.api.nvim_create_user_command("ClaudeCode", function(opts)
local current_mode = vim.fn.mode()
if current_mode == "v" or current_mode == "V" or current_mode == "\22" then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", false)
end
terminal.toggle({})
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
Copy link
Preview

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the command argument parsing logic into a helper function to improve readability and maintainability of the command registration code.

Suggested change
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
local cmd_args = parse_command_args(opts)

Copilot uses AI. Check for mistakes.

terminal.toggle({}, cmd_args)
end, {
nargs = "?",
desc = "Toggle the Claude Code terminal window",
nargs = "*",
desc = "Toggle the Claude Code terminal window with optional arguments",
})

vim.api.nvim_create_user_command("ClaudeCodeOpen", function(_opts)
terminal.open({})
vim.api.nvim_create_user_command("ClaudeCodeOpen", function(opts)
local cmd_args = opts.args and opts.args ~= "" and opts.args or nil
terminal.open({}, cmd_args)
end, {
nargs = "?",
desc = "Open the Claude Code terminal window",
nargs = "*",
desc = "Open the Claude Code terminal window with optional arguments",
})

vim.api.nvim_create_user_command("ClaudeCodeClose", function()
Expand Down
4 changes: 4 additions & 0 deletions lua/claudecode/meta/vim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@
---@field termopen fun(cmd: string|string[], opts?: table):number For vim.fn.termopen()
-- Add other vim.fn functions as needed

---@class vim_v_table
---@field event table Event data containing status and other event information

---@class vim_global_api
---@field notify fun(msg: string | string[], level?: number, opts?: vim_notify_opts):nil
---@field log vim_log
---@field v vim_v_table For vim.v.event access
---@field _last_echo table[]? table of tables, e.g. { {"message", "HighlightGroup"} }
---@field _last_error string?
---@field o vim_options_table For vim.o.option_name
Expand Down
5 changes: 5 additions & 0 deletions lua/claudecode/server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ function M.stop()

tcp_server.stop_server(M.state.server)

-- CRITICAL: Clear global deferred responses to prevent memory leaks and hanging
if _G.claude_deferred_responses then
_G.claude_deferred_responses = {}
end

M.state.server = nil
M.state.port = nil
M.state.clients = {}
Expand Down
Loading