Skip to content

Commit 0461cdc

Browse files
author
Blink
committed
feat: add nvim-tree and neotree integration
1 parent c1cdcd5 commit 0461cdc

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim):
5050
config = true,
5151
keys = {
5252
{ "<leader>ac", "<cmd>ClaudeCode<cr>", desc = "Toggle Claude" },
53-
{ "<leader>as", "<cmd>ClaudeCodeSend<cr>", mode = "v", desc = "Send to Claude" },
53+
{ "<leader>as", "<cmd>ClaudeCodeSendSelection<cr>", mode = "v", desc = "Send to Claude" },
54+
-- Add file from nvim-tree or neo-tree
55+
{ "<leader>ca", "<cmd>ClaudeCodeTreeAddFile<cr>", desc = "ClaudeCode: Add file from tree", ft = { "NvimTree", "neo-tree" } },
5456
},
5557
}
5658
```
@@ -60,7 +62,9 @@ That's it! For more configuration options, see [Advanced Setup](#advanced-setup)
6062
## Usage
6163

6264
1. **Launch Claude**: Run `:ClaudeCode` to open Claude in a split terminal
63-
2. **Send context**: Select text and run `:'<,'>ClaudeCodeSend` to send it to Claude
65+
2. **Send context**:
66+
- Select text and run `:'<,'>ClaudeCodeSendSelection` to send it to Claude
67+
- Use the keymap you configured to add a file from `nvim-tree` or `neo-tree`
6468
3. **Let Claude work**: Claude can now:
6569
- See your current file and selections in real-time
6670
- Open files in your editor

lua/claudecode/init.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,47 @@ function M._create_commands()
246246
})
247247

248248
vim.api.nvim_create_user_command("ClaudeCodeSend", function(opts)
249+
if not M.state.server then
250+
logger.error("command", "ClaudeCodeSend: Claude Code integration is not running.")
251+
return
252+
end
253+
254+
local terminal = require("claudecode.terminal")
255+
local bufnr = terminal.get_active_terminal_bufnr()
256+
if not bufnr then
257+
logger.error("command", "ClaudeCodeSend: Could not find terminal buffer.")
258+
return
259+
end
260+
261+
if opts.fargs and #opts.fargs > 0 then
262+
local file_path = table.concat(opts.fargs, " ")
263+
local formatted_path = "@file " .. file_path
264+
local last_line = vim.api.nvim_buf_line_count(bufnr)
265+
vim.api.nvim_buf_set_lines(bufnr, last_line, -1, false, { formatted_path })
266+
else
267+
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
268+
local content = table.concat(lines, "\n")
269+
M.state.server.send(content)
270+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "" })
271+
end
272+
end, {
273+
desc = "Send the content of the input buffer to Claude Code. Optionally add a file path.",
274+
nargs = "*",
275+
})
276+
277+
vim.api.nvim_create_user_command("ClaudeCodeTreeAddFile", function()
278+
local integrations = require("claudecode.integrations")
279+
local file_path = integrations.get_path_from_tree()
280+
if file_path then
281+
vim.cmd("ClaudeCodeSend " .. file_path)
282+
else
283+
logger.warn("command", "ClaudeCodeTreeAddFile: Could not determine file path from the tree.")
284+
end
285+
end, {
286+
desc = "Add the selected file from a tree explorer to the Claude Code input buffer.",
287+
})
288+
289+
vim.api.nvim_create_user_command("ClaudeCodeSendSelection", function(opts)
249290
if not M.state.server then
250291
logger.error("command", "ClaudeCodeSend: Claude Code integration is not running.")
251292
vim.notify("Claude Code integration is not running", vim.log.levels.ERROR)

lua/claudecode/integrations.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local M = {}
2+
3+
function M.get_path_from_tree()
4+
if vim.bo.filetype == "NvimTree" then
5+
local success, nvim_tree_api = pcall(require, "nvim-tree.api")
6+
if success and nvim_tree_api then
7+
local node = nvim_tree_api.tree.get_node_under_cursor()
8+
if node and node.absolute_path then
9+
return node.absolute_path
10+
end
11+
end
12+
elseif vim.bo.filetype == "neo-tree" then
13+
local success, manager = pcall(require, "neo-tree.sources.manager")
14+
if success and manager then
15+
local state = manager.get_state()
16+
if state and state.tree then
17+
local node = state.tree:get_node()
18+
if node and node.path then
19+
return node.path
20+
end
21+
end
22+
end
23+
end
24+
25+
vim.notify("Could not get file path from tree.", vim.log.levels.WARN, { title = "ClaudeCode" })
26+
return nil
27+
end
28+
29+
return M

0 commit comments

Comments
 (0)