Skip to content

feat: support netrw file selection #62

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ When Anthropic released Claude Code, they only supported VS Code and JetBrains.
"<leader>as",
"<cmd>ClaudeCodeTreeAdd<cr>",
desc = "Add file",
ft = { "NvimTree", "neo-tree", "oil" },
ft = { "NvimTree", "neo-tree", "oil", "netrw" },
},
-- Diff management
{ "<leader>aa", "<cmd>ClaudeCodeDiffAccept<cr>", desc = "Accept diff" },
Expand Down Expand Up @@ -80,7 +80,7 @@ That's it! The plugin will auto-configure everything else.
1. **Launch Claude**: Run `:ClaudeCode` to open Claude in a split terminal
2. **Send context**:
- Select text in visual mode and use `<leader>as` to send it to Claude
- In `nvim-tree`/`neo-tree`/`oil.nvim`, press `<leader>as` on a file to add it to Claude's context
- In `nvim-tree`/`neo-tree`/`oil.nvim`/`netrw`, press `<leader>as` on a file to add it to Claude's context
3. **Let Claude work**: Claude can now:
- See your current file and selections in real-time
- Open files in your editor
Expand Down
2 changes: 1 addition & 1 deletion dev-config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return {
"<leader>as",
"<cmd>ClaudeCodeTreeAdd<cr>",
desc = "Add file from tree",
ft = { "NvimTree", "neo-tree", "oil" },
ft = { "NvimTree", "neo-tree", "oil", "netrw" },
},

-- Development helpers
Expand Down
1 change: 1 addition & 0 deletions lua/claudecode/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ local function find_main_editor_window()
or filetype == "ClaudeCode"
or filetype == "NvimTree"
or filetype == "oil"
or filetype == "netrw"
or filetype == "aerial"
or filetype == "tagbar"
)
Expand Down
1 change: 1 addition & 0 deletions lua/claudecode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ function M._create_commands()
local is_tree_buffer = current_ft == "NvimTree"
or current_ft == "neo-tree"
or current_ft == "oil"
or current_ft == "netrw"
or string.match(current_bufname, "neo%-tree")
or string.match(current_bufname, "NvimTree")

Expand Down
52 changes: 52 additions & 0 deletions lua/claudecode/integrations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function M.get_selected_files_from_tree()
return M._get_neotree_selection()
elseif current_ft == "oil" then
return M._get_oil_selection()
elseif current_ft == "netrw" then
return M._get_netrw_selection()
else
return nil, "Not in a supported tree buffer (current filetype: " .. current_ft .. ")"
end
Expand Down Expand Up @@ -261,4 +263,54 @@ function M._get_oil_selection()
return {}, "No file found under cursor"
end

--- Get selected files from netrw
--- Supports both marked files and single file under cursor
--- Reference: :help netrw-mf, :help markfilelist
--- @return table files List of file paths
--- @return string|nil error Error message if operation failed
function M._get_netrw_selection()
-- 1. Check for marked files
local mf_ok, mf_result = pcall(function()
if vim.fn.exists("*netrw#Expose") == 1 then
return vim.fn.call("netrw#Expose", { "netrwmarkfilelist" })
end
return nil
end)

local marked_files = {}

if mf_ok and mf_result and type(mf_result) == "table" and #mf_result > 0 then
for _, file_path in ipairs(mf_result) do
if vim.fn.filereadable(file_path) == 1 or vim.fn.isdirectory(file_path) == 1 then
table.insert(marked_files, file_path)
end
end
end

if #marked_files > 0 then
return marked_files, nil
end

-- 2. No marked files. Check for a file or dir under cursor
local path_ok, path_result = pcall(function()
if vim.fn.exists("*netrw#Call") == 1 then
local word = vim.fn.call("netrw#Call", { "NetrwGetWord" })
if word ~= "" then
return vim.fn.call("netrw#Call", { "NetrwFile", word })
end
end
return nil
end)

if not path_ok or not path_result or path_result == "" then
return {}, "Failed to get path from netrw"
end

if vim.fn.filereadable(path_result) == 1 or vim.fn.isdirectory(path_result) == 1 then
return { path_result }, nil
end

return {}, "Invalid file or directory path: " .. path_result
end

return M
1 change: 1 addition & 0 deletions lua/claudecode/tools/open_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ local function find_main_editor_window()
or filetype == "ClaudeCode"
or filetype == "NvimTree"
or filetype == "oil"
or filetype == "netrw"
or filetype == "aerial"
or filetype == "tagbar"
)
Expand Down
Loading