Skip to content

Commit 101d8db

Browse files
committed
feat: support netrw file selection
1 parent 91357d8 commit 101d8db

File tree

7 files changed

+356
-3
lines changed

7 files changed

+356
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ When Anthropic released Claude Code, they only supported VS Code and JetBrains.
4444
"<leader>as",
4545
"<cmd>ClaudeCodeTreeAdd<cr>",
4646
desc = "Add file",
47-
ft = { "NvimTree", "neo-tree", "oil" },
47+
ft = { "NvimTree", "neo-tree", "oil", "netrw" },
4848
},
4949
-- Diff management
5050
{ "<leader>aa", "<cmd>ClaudeCodeDiffAccept<cr>", desc = "Accept diff" },
@@ -80,7 +80,7 @@ That's it! The plugin will auto-configure everything else.
8080
1. **Launch Claude**: Run `:ClaudeCode` to open Claude in a split terminal
8181
2. **Send context**:
8282
- Select text in visual mode and use `<leader>as` to send it to Claude
83-
- In `nvim-tree`/`neo-tree`/`oil.nvim`, press `<leader>as` on a file to add it to Claude's context
83+
- In `nvim-tree`/`neo-tree`/`oil.nvim`/`netrw`, press `<leader>as` on a file to add it to Claude's context
8484
3. **Let Claude work**: Claude can now:
8585
- See your current file and selections in real-time
8686
- Open files in your editor

dev-config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ return {
2323
"<leader>as",
2424
"<cmd>ClaudeCodeTreeAdd<cr>",
2525
desc = "Add file from tree",
26-
ft = { "NvimTree", "neo-tree", "oil" },
26+
ft = { "NvimTree", "neo-tree", "oil", "netrw" },
2727
},
2828

2929
-- Development helpers

lua/claudecode/diff.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ local function find_main_editor_window()
5050
or filetype == "ClaudeCode"
5151
or filetype == "NvimTree"
5252
or filetype == "oil"
53+
or filetype == "netrw"
5354
or filetype == "aerial"
5455
or filetype == "tagbar"
5556
)

lua/claudecode/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ function M._create_commands()
611611
local is_tree_buffer = current_ft == "NvimTree"
612612
or current_ft == "neo-tree"
613613
or current_ft == "oil"
614+
or current_ft == "netrw"
614615
or string.match(current_bufname, "neo%-tree")
615616
or string.match(current_bufname, "NvimTree")
616617

lua/claudecode/integrations.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function M.get_selected_files_from_tree()
1616
return M._get_neotree_selection()
1717
elseif current_ft == "oil" then
1818
return M._get_oil_selection()
19+
elseif current_ft == "netrw" then
20+
return M._get_netrw_selection()
1921
else
2022
return nil, "Not in a supported tree buffer (current filetype: " .. current_ft .. ")"
2123
end
@@ -261,4 +263,54 @@ function M._get_oil_selection()
261263
return {}, "No file found under cursor"
262264
end
263265

266+
--- Get selected files from netrw
267+
--- Supports both marked files and single file under cursor
268+
--- Reference: :help netrw-mf, :help markfilelist
269+
--- @return table files List of file paths
270+
--- @return string|nil error Error message if operation failed
271+
function M._get_netrw_selection()
272+
-- 1. Check for marked files
273+
local mf_ok, mf_result = pcall(function()
274+
if vim.fn.exists("*netrw#Expose") == 1 then
275+
return vim.fn.call("netrw#Expose", { "netrwmarkfilelist" })
276+
end
277+
return nil
278+
end)
279+
280+
local marked_files = {}
281+
282+
if mf_ok and mf_result and type(mf_result) == "table" and #mf_result > 0 then
283+
for _, file_path in ipairs(mf_result) do
284+
if vim.fn.filereadable(file_path) == 1 or vim.fn.isdirectory(file_path) == 1 then
285+
table.insert(marked_files, file_path)
286+
end
287+
end
288+
end
289+
290+
if #marked_files > 0 then
291+
return marked_files, nil
292+
end
293+
294+
-- 2. No marked files. Check for a file or dir under cursor
295+
local path_ok, path_result = pcall(function()
296+
if vim.fn.exists("*netrw#Call") == 1 then
297+
local word = vim.fn.call("netrw#Call", { "NetrwGetWord" })
298+
if word ~= "" then
299+
return vim.fn.call("netrw#Call", { "NetrwFile", word })
300+
end
301+
end
302+
return nil
303+
end)
304+
305+
if not path_ok or not path_result or path_result == "" then
306+
return {}, "Failed to get path from netrw"
307+
end
308+
309+
if vim.fn.filereadable(path_result) == 1 or vim.fn.isdirectory(path_result) == 1 then
310+
return { path_result }, nil
311+
end
312+
313+
return {}, "Invalid file or directory path: " .. path_result
314+
end
315+
264316
return M

lua/claudecode/tools/open_file.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ local function find_main_editor_window()
7676
or filetype == "ClaudeCode"
7777
or filetype == "NvimTree"
7878
or filetype == "oil"
79+
or filetype == "netrw"
7980
or filetype == "aerial"
8081
or filetype == "tagbar"
8182
)

0 commit comments

Comments
 (0)