forked from coder/claudecode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrations.lua
More file actions
264 lines (225 loc) · 8.24 KB
/
Copy pathintegrations.lua
File metadata and controls
264 lines (225 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
---
-- Tree integration module for ClaudeCode.nvim
-- Handles detection and selection of files from nvim-tree, neo-tree, and oil.nvim
-- @module claudecode.integrations
local M = {}
--- Get selected files from the current tree explorer
--- @return table|nil files List of file paths, or nil if error
--- @return string|nil error Error message if operation failed
function M.get_selected_files_from_tree()
local current_ft = vim.bo.filetype
if current_ft == "NvimTree" then
return M._get_nvim_tree_selection()
elseif current_ft == "neo-tree" then
return M._get_neotree_selection()
elseif current_ft == "oil" then
return M._get_oil_selection()
else
return nil, "Not in a supported tree buffer (current filetype: " .. current_ft .. ")"
end
end
--- Get selected files from nvim-tree
--- Supports both multi-selection (marks) and single file under cursor
--- @return table files List of file paths
--- @return string|nil error Error message if operation failed
function M._get_nvim_tree_selection()
local success, nvim_tree_api = pcall(require, "nvim-tree.api")
if not success then
return {}, "nvim-tree not available"
end
local files = {}
local marks = nvim_tree_api.marks.list()
if marks and #marks > 0 then
for i, mark in ipairs(marks) do
if mark.type == "file" and mark.absolute_path and mark.absolute_path ~= "" then
-- Check if it's not a root-level file (basic protection)
if not string.match(mark.absolute_path, "^/[^/]*$") then
table.insert(files, mark.absolute_path)
end
end
end
if #files > 0 then
return files, nil
end
end
local node = nvim_tree_api.tree.get_node_under_cursor()
if node then
if node.type == "file" and node.absolute_path and node.absolute_path ~= "" then
-- Check if it's not a root-level file (basic protection)
if not string.match(node.absolute_path, "^/[^/]*$") then
return { node.absolute_path }, nil
else
return {}, "Cannot add root-level file. Please select a file in a subdirectory."
end
elseif node.type == "directory" and node.absolute_path and node.absolute_path ~= "" then
return { node.absolute_path }, nil
end
end
return {}, "No file found under cursor"
end
--- Get selected files from neo-tree
--- Uses neo-tree's own visual selection method when in visual mode
--- @return table files List of file paths
--- @return string|nil error Error message if operation failed
function M._get_neotree_selection()
local success, manager = pcall(require, "neo-tree.sources.manager")
if not success then
return {}, "neo-tree not available"
end
local state = manager.get_state("filesystem")
if not state then
return {}, "neo-tree filesystem state not available"
end
local files = {}
-- Use neo-tree's own visual selection method (like their copy/paste feature)
local mode = vim.fn.mode()
if mode == "V" or mode == "v" or mode == "\22" then
local current_win = vim.api.nvim_get_current_win()
if state.winid and state.winid == current_win then
-- Use neo-tree's exact method to get visual range (from their get_selected_nodes implementation)
local start_pos = vim.fn.getpos("'<")[2]
local end_pos = vim.fn.getpos("'>")[2]
-- Fallback to current cursor and anchor if marks are not valid
if start_pos == 0 or end_pos == 0 then
local cursor_pos = vim.api.nvim_win_get_cursor(0)[1]
local anchor_pos = vim.fn.getpos("v")[2]
if anchor_pos > 0 then
start_pos = math.min(cursor_pos, anchor_pos)
end_pos = math.max(cursor_pos, anchor_pos)
else
start_pos = cursor_pos
end_pos = cursor_pos
end
end
if end_pos < start_pos then
start_pos, end_pos = end_pos, start_pos
end
local selected_nodes = {}
for line = start_pos, end_pos do
local node = state.tree:get_node(line)
if node then
-- Add validation for node types before adding to selection
if node.type and node.type ~= "message" then
table.insert(selected_nodes, node)
end
end
end
for i, node in ipairs(selected_nodes) do
-- Enhanced validation: check for file type and valid path
if node.type == "file" and node.path and node.path ~= "" then
-- Additional check: ensure it's not a root node (depth protection)
local depth = (node.get_depth and node:get_depth()) and node:get_depth() or 0
if depth > 1 then
table.insert(files, node.path)
end
end
end
if #files > 0 then
return files, nil
end
end
end
if state.tree then
local selection = nil
if state.tree.get_selection then
selection = state.tree:get_selection()
end
if (not selection or #selection == 0) and state.selected_nodes then
selection = state.selected_nodes
end
if selection and #selection > 0 then
for i, node in ipairs(selection) do
if node.type == "file" and node.path then
table.insert(files, node.path)
end
end
if #files > 0 then
return files, nil
end
end
end
if state.tree then
local node = state.tree:get_node()
if node then
if node.type == "file" and node.path then
return { node.path }, nil
elseif node.type == "directory" and node.path then
return { node.path }, nil
end
end
end
return {}, "No file found under cursor"
end
--- Get selected files from oil.nvim
--- Supports both visual selection and single file under cursor
--- @return table files List of file paths
--- @return string|nil error Error message if operation failed
function M._get_oil_selection()
local success, oil = pcall(require, "oil")
if not success then
return {}, "oil.nvim not available"
end
local bufnr = vim.api.nvim_get_current_buf() --[[@as number]]
local files = {}
-- Check if we're in visual mode
local mode = vim.fn.mode()
if mode == "V" or mode == "v" or mode == "\22" then
-- Visual mode: use the common visual range function
local visual_commands = require("claudecode.visual_commands")
local start_line, end_line = visual_commands.get_visual_range()
-- Get current directory once
local dir_ok, current_dir = pcall(oil.get_current_dir, bufnr)
if not dir_ok or not current_dir then
return {}, "Failed to get current directory"
end
-- Process each line in the visual selection
for line = start_line, end_line do
local entry_ok, entry = pcall(oil.get_entry_on_line, bufnr, line)
if entry_ok and entry and entry.name then
-- Skip parent directory entries
if entry.name ~= ".." and entry.name ~= "." then
local full_path = current_dir .. entry.name
-- Handle various entry types
if entry.type == "file" or entry.type == "link" then
table.insert(files, full_path)
elseif entry.type == "directory" then
-- Ensure directory paths end with /
table.insert(files, full_path:match("/$") and full_path or full_path .. "/")
else
-- For unknown types, return the path anyway
table.insert(files, full_path)
end
end
end
end
if #files > 0 then
return files, nil
end
else
-- Normal mode: get file under cursor with error handling
local ok, entry = pcall(oil.get_cursor_entry)
if not ok or not entry then
return {}, "Failed to get cursor entry"
end
local dir_ok, current_dir = pcall(oil.get_current_dir, bufnr)
if not dir_ok or not current_dir then
return {}, "Failed to get current directory"
end
-- Process the entry
if entry.name and entry.name ~= ".." and entry.name ~= "." then
local full_path = current_dir .. entry.name
-- Handle various entry types
if entry.type == "file" or entry.type == "link" then
return { full_path }, nil
elseif entry.type == "directory" then
-- Ensure directory paths end with /
return { full_path:match("/$") and full_path or full_path .. "/" }, nil
else
-- For unknown types, return the path anyway
return { full_path }, nil
end
end
end
return {}, "No file found under cursor"
end
return M