diff --git a/CLAUDE.md b/CLAUDE.md index 2699f37..d76d5c3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -161,13 +161,15 @@ This modular architecture provides several advantages: ### Available MCP Tools -The server provides these tools (implemented with `#[tool]` attribute): +The server provides these 13 tools (implemented with `#[tool]` attribute): **Connection Management:** -1. **`connect`**: Connect via Unix socket/named pipe, returns deterministic `connection_id` -2. **`connect_tcp`**: Connect via TCP address, returns deterministic `connection_id` -3. **`disconnect`**: Disconnect from specific Neovim instance by `connection_id` +1. **`get_targets`**: Discover available Neovim socket paths created by the + nvim-mcp plugin +2. **`connect`**: Connect via Unix socket/named pipe, returns deterministic `connection_id` +3. **`connect_tcp`**: Connect via TCP address, returns deterministic `connection_id` +4. **`disconnect`**: Disconnect from specific Neovim instance by `connection_id` **Connection-Aware Tools** (require `connection_id` parameter): @@ -185,6 +187,10 @@ The server provides these tools (implemented with `#[tool]` attribute): identification (supports buffer IDs, project-relative paths, and absolute paths) 9. **`lsp_references`**: Get LSP references with universal document identification (supports buffer IDs, project-relative paths, and absolute paths) +10. **`lsp_resolve_code_action`**: Resolve code actions that may have + incomplete data +11. **`lsp_apply_edit`**: Apply workspace edits using Neovim's LSP utility + functions ### Universal Document Identifier System @@ -240,6 +246,14 @@ BLAKE3 hashes of the target string for consistent identification. - **`regex`**: Pattern matching for connection-scoped resource URI parsing - **`blake3`**: Fast, deterministic hashing for connection ID generation +**Testing and Development Dependencies:** + +- **`tempfile`**: Temporary file and directory management for integration tests +- **`serde_json`**: JSON serialization/deserialization with enhanced Claude + Code compatibility +- **Enhanced deserialization**: Support for both string and struct formats + in CodeAction and WorkspaceEdit types + ## Testing Architecture - **Integration tests**: Located in `src/server/integration_tests.rs` and @@ -247,6 +261,11 @@ BLAKE3 hashes of the target string for consistent identification. - **Global mutex**: Prevents port conflicts during concurrent test execution - **Automated setup**: Tests spawn and manage Neovim instances automatically - **Full MCP flow**: Tests cover complete client-server communication +- **LSP testing**: Comprehensive Go integration tests with gopls language server +- **Code action testing**: End-to-end tests for lsp_resolve_code_action and + lsp_apply_edit +- **Test data**: Includes Go source files and LSP configuration for realistic + testing scenarios ## Error Handling @@ -278,6 +297,34 @@ To add a new connection-aware tool to the server: 6. **Registration**: The tool is automatically registered by the `#[tool_router]` macro +**New Tool Parameter Structures:** + +For the recently added LSP tools, the following parameter structures are used: + +```rust +/// Resolve code action parameters +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +pub struct ResolveCodeActionParams { + /// Unique identifier for the target Neovim instance + pub connection_id: String, + /// LSP client name + pub lsp_client_name: String, + /// Code action to resolve + pub code_action: CodeAction, +} + +/// Apply workspace edit parameters +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +pub struct ApplyWorkspaceEditParams { + /// Unique identifier for the target Neovim instance + pub connection_id: String, + /// LSP client name (used for position encoding detection) + pub lsp_client_name: String, + /// Workspace edit to apply using vim.lsp.util.apply_workspace_edit() + pub workspace_edit: WorkspaceEdit, +} +``` + **Example connection-aware tool pattern:** ```rust diff --git a/README.md b/README.md index 0f7f2e1..4ffa899 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ Once both the MCP server and Neovim are running, here's a typical workflow: ## Available Tools -The server provides 11 MCP tools for interacting with Neovim: +The server provides 13 MCP tools for interacting with Neovim: ### Connection Management @@ -181,6 +181,14 @@ establishment phase: `lsp_client_name` (string), `line` (number), `character` (number), `include_declaration` (boolean) +- **`lsp_resolve_code_action`**: Resolve code actions with incomplete data + - Parameters: `connection_id` (string), `lsp_client_name` (string), + `code_action` (CodeAction object) - Code action to resolve + +- **`lsp_apply_edit`**: Apply workspace edits using Neovim's LSP utility functions + - Parameters: `connection_id` (string), `lsp_client_name` (string), + `workspace_edit` (WorkspaceEdit object) - Workspace edit to apply + ### Universal Document Identifier The `document` parameter in the universal LSP tools accepts a `DocumentIdentifier` @@ -203,6 +211,30 @@ providing enhanced flexibility for code analysis and navigation. - **`exec_lua`**: Execute Lua code in Neovim - Parameters: `connection_id` (string), `code` (string) - Lua code to execute +### Complete LSP Code Action Workflow + +The server now supports the full LSP code action lifecycle: + +1. **Get Available Actions**: Use `lsp_code_actions` to retrieve available + code actions for a specific range +2. **Resolve Action**: Use `lsp_resolve_code_action` to resolve any code + action that may have incomplete data +3. **Apply Changes**: Use `lsp_apply_edit` to apply the workspace edit from + the resolved code action + +**Example Workflow**: + +```text +1. lsp_code_actions → Get available actions +2. lsp_resolve_code_action → Resolve incomplete action data +3. lsp_apply_edit → Apply the workspace edit to files +``` + +This enables AI assistants to perform complete code refactoring, quick fixes, +and other LSP-powered transformations. The implementation uses Neovim's native +`vim.lsp.util.apply_workspace_edit()` function with proper position encoding +handling, ensuring reliable and accurate file modifications. + ## MCP Resources Access diagnostic and connection information through structured URI schemes: diff --git a/docs/instructions.md b/docs/instructions.md index e2b1c0c..6525fa5 100644 --- a/docs/instructions.md +++ b/docs/instructions.md @@ -4,7 +4,7 @@ ### Tools -The server provides 11 MCP tools for interacting with Neovim instances: +The server provides 13 MCP tools for interacting with Neovim instances: #### Connection Management @@ -117,6 +117,23 @@ All tools below require a `connection_id` parameter from connection establishmen - **Returns**: Array of reference objects with locations - **Usage**: Find all references to a symbol across the workspace in any document +- **`lsp_resolve_code_action`**: Resolve code actions with incomplete data + - **Parameters**: + - `connection_id` (string): Target Neovim instance ID + - `lsp_client_name` (string): LSP client name from lsp_clients + - `code_action` (CodeAction): Code action object to resolve + - **Returns**: Resolved CodeAction object with complete data + - **Usage**: Resolve code actions that may have incomplete edit or command data + +- **`lsp_apply_edit`**: Apply workspace edits using Neovim's LSP utility functions + - **Parameters**: + - `connection_id` (string): Target Neovim instance ID + - `lsp_client_name` (string): LSP client name from lsp_clients + - `workspace_edit` (WorkspaceEdit): Workspace edit object to apply + - **Returns**: Success confirmation + - **Usage**: Apply code changes from resolved code actions to files using + `vim.lsp.util.apply_workspace_edit()` with proper position encoding handling + ### Resources ### Universal Document Identifier System @@ -206,13 +223,22 @@ Connection-scoped diagnostic resources using `nvim-diagnostics://` scheme: 3. Read nvim-diagnostics://{connection_id}/workspace resource 4. Keep connection active for future operations -#### Code Action Workflow +#### Complete LSP Code Action Workflow 1. get_targets → connect → list_buffers (cache connection_id) 2. lsp_clients (to find available language servers, reuse connection_id) 3. lsp_code_actions (with DocumentIdentifier and LSP client, reuse connection_id) -4. exec_lua (to apply selected actions if needed, reuse connection_id) -5. Keep connection active for additional operations +4. lsp_resolve_code_action (resolve any code action with incomplete data, reuse connection_id) +5. lsp_apply_edit (apply the workspace edit from resolved code action, reuse connection_id) +6. Keep connection active for additional operations + +**Enhanced Workflow Benefits:** + +- **Complete automation**: No manual exec_lua required for applying changes +- **Robust resolution**: Handles code actions with incomplete edit or command data +- **Native integration**: Uses Neovim's built-in `vim.lsp.util.apply_workspace_edit()` + for reliable file modifications with proper position encoding handling +- **Error handling**: Proper validation and error reporting throughout the process ### Error Handling Guidelines