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

Conversation

ThomasK33
Copy link
Member

Summary

  • Add configurable auto_close option (default: true) to control terminal lifecycle behavior
  • Refactor terminal module into clean provider architecture with separate snacks and native implementations
  • Simplify state management using reliable :buf_valid() approach instead of complex state tracking
  • Remove Snacks.terminal.get() usage to avoid invalid instance caching bugs
  • Add comprehensive error reporting with detailed diagnostic context
  • Fixes [FEATURE] Open Claude code with --continue or --resume option #17: Add support for --continue and --resume command arguments

Key Features

Command Arguments Support (Fixes #17)

Configurable Auto-Close Behavior

  • auto_close = true (default): Terminal automatically closes after command completion with error notifications for failed commands
  • auto_close = false: Terminal stays open for output review, useful for debugging and seeing detailed command results

Enhanced Terminal Architecture

  • Modular provider system: Clean separation between snacks and native terminal providers
  • Single creation path: Both open() and toggle() use consistent terminal creation logic through M.open()
  • Reliable state management: Uses :buf_valid() for terminal validity checks instead of complex state variables
  • Improved error handling: Detailed diagnostic context when terminal creation fails

Developer Experience Improvements

  • Enhanced error reporting: Specific failure reasons with full context (command, options, validation details)
  • LSP type definitions: Added vim.v definitions to eliminate LSP warnings
  • Complete documentation: README updated with auto_close examples and usage guidance
  • Development configuration: Updated dev-config.lua with auto_close example

Technical Changes

  • Eliminated problematic Snacks.terminal.get() calls that returned invalid cached instances
  • Removed terminal_state complexity in favor of simple buffer validity checks
  • Added conditional event handlers that only create TermClose listeners when auto_close is enabled
  • Maintained 100% API compatibility while improving reliability and user experience
  • Added command argument parsing to support --resume, --continue, and any other claude CLI options

Test Results

  • All tests passing: 237 successes / 0 failures / 0 errors
  • Code quality clean: 0 warnings / 0 errors in 64 files
  • Backward compatible: Default behavior unchanged, all existing functionality preserved

Files Changed

  • lua/claudecode/terminal.lua: Refactored to provider dispatcher with command argument support
  • lua/claudecode/terminal/snacks.lua: Clean snacks provider implementation
  • lua/claudecode/terminal/native.lua: Clean native provider implementation
  • lua/claudecode/init.lua: Updated command registration to support arguments
  • README.md: Added auto_close documentation and command argument examples
  • dev-config.lua: Added development configuration template
  • lua/claudecode/meta/vim.lua: Added vim.v type definitions
  • Updated tests and integration specs for new architecture

This refactoring significantly improves terminal reliability while adding user-requested configurability for terminal lifecycle management and command argument support.

ThomasK33 added 4 commits June 9, 2025 11:02
- Extended :ClaudeCode and :ClaudeCodeOpen commands to accept arguments
- Arguments are appended to the configured terminal_cmd when launching Claude
- Updated terminal module to support cmd_args parameter in open() and toggle()
- Added comprehensive test coverage for argument handling and edge cases
- Maintained full backward compatibility with existing usage

Usage examples:
- :ClaudeCode --resume
- :ClaudeCodeOpen --help --verbose
- :ClaudeCode (works as before with no arguments)

Change-Id: I5ff2867e884f234a6b4fbaac065a39aa59cbf533
Signed-off-by: Thomas Kosiewski <[email protected]>
- Add <leader>ar for :ClaudeCode --resume
- Add <leader>aC for :ClaudeCode --continue
- Document new command argument support in Commands section
- Maintain existing <leader>ac keybind for basic :ClaudeCode toggle

Change-Id: I1ac31cb81750f420aef682aaccfa9068c67808b9
Signed-off-by: Thomas Kosiewski <[email protected]>
- Add my personal config for dev

Change-Id: I68160a8c011cc46c2a954e11701217be649443bd
Signed-off-by: Thomas Kosiewski <[email protected]>
…e management

- Add auto_close configuration option (default: true) to control terminal lifecycle
- Refactor terminal.lua into modular provider architecture with separate snacks and native providers
- Simplify terminal state management using :buf_valid() instead of complex state tracking
- Remove Snacks.terminal.get() usage to avoid invalid instance caching bugs
- Add comprehensive error reporting with detailed context for terminal creation failures
- Update README.md with auto_close documentation and usage examples
- Add vim.v type definitions to eliminate LSP warnings
- Update dev-config.lua with auto_close example
- Maintain 100% API compatibility while improving reliability

Change-Id: Iabc80707bf5f23890075c20d0f2abbdfa443e34e
Signed-off-by: Thomas Kosiewski <[email protected]>
@ThomasK33 ThomasK33 requested a review from Copilot June 10, 2025 17:52
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the terminal module to introduce a configurable auto-close behavior, a modular provider system supporting both snacks and native implementations, and enhanced command argument support. Key changes include adjusting tests to reflect the new provider API, updating command registrations and documentation, and simplifying state management in terminal operations.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/unit/terminal_spec.lua Updated tests to use the new provider interface and verify command argument handling.
tests/unit/init_spec.lua Added tests for command registration and argument parsing for ClaudeCode commands.
tests/integration/command_args_spec.lua Integration tests ensuring proper appending of command arguments and native terminal fallback.
lua/claudecode/terminal/snacks.lua Redesigned snacks provider with auto-close and error handling improvements.
lua/claudecode/terminal/native.lua Introduced native terminal provider with command argument support and state recovery logic.
lua/claudecode/meta/vim.lua Added vim.v typing to support LSP type hints.
lua/claudecode/init.lua Updated command registrations to pass along command arguments.
dev-config.lua Provided updated development configuration examples.
README.md Documentation updated with new command usage examples and auto_close behavior details.
Comments suppressed due to low confidence (1)

tests/unit/terminal_spec.lua:460

  • [nitpick] Consider updating the test case description to clearly reflect the expected behavior of focusing an existing terminal and invoking provider open accordingly for clarity and maintainability.
it("should call provider open twice when terminal exists", function()

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.

…ally

- Add scripts/run_integration_tests_individually.sh to run each integration test file separately
- Update GitHub Actions workflow to use the new script instead of plenary test_directory
- Fix terminal lifecycle mocking in command_args_spec.lua to properly simulate exit callbacks
- Add cleanup of global deferred responses in server stop() function
- Skip command_args_spec.lua in CI as it consistently hangs with plenary test_directory

This is a workaround for plenary.test_harness.test_directory() hanging in headless mode
when running integration tests that load claudecode. Individual test_file() calls work
properly and allow all tests to complete successfully.

Change-Id: Iec21e0db276d478c403a6dc703bdd1e8ef540ac8
Signed-off-by: Thomas Kosiewski <[email protected]>
@ThomasK33 ThomasK33 merged commit b822036 into main Jun 11, 2025
3 checks passed
@ThomasK33 ThomasK33 deleted the thomask33/claudecode-command-args branch June 11, 2025 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Open Claude code with --continue or --resume option
1 participant