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
480 lines (419 loc) · 15.6 KB
/
integrations.lua
File metadata and controls
480 lines (419 loc) · 15.6 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
--- Tree integration module for OpenCode.nvim
--- Handles detection and selection of files from nvim-tree, neo-tree, mini.files, and oil.nvim
---@module 'opencode.integrations'
local M = {}
local logger = require("opencode.logger")
---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()
elseif current_ft == "minifiles" then
return M._get_mini_files_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
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 _, 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
logger.debug("integrations/neotree", "neo-tree not available (require failed)")
return {}, "neo-tree not available"
end
local state = manager.get_state("filesystem")
if not state then
logger.debug("integrations/neotree", "filesystem state not available from manager")
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()
local current_win = vim.api.nvim_get_current_win()
logger.debug(
"integrations/neotree",
"begin selection",
"mode=",
mode,
"current_win=",
current_win,
"state.winid=",
tostring(state.winid)
)
if mode == "V" or mode == "v" or mode == "\22" then
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
logger.debug("integrations/neotree", "visual selection range", start_pos, "to", end_pos)
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)
local depth = (node.get_depth and node:get_depth()) and node:get_depth() or 0
logger.debug(
"integrations/neotree",
"line",
line,
"node type=",
tostring(node.type),
"depth=",
depth,
"path=",
tostring(node.path)
)
else
logger.debug("integrations/neotree", "line", line, "node rejected (type)", tostring(node and node.type))
end
else
logger.debug("integrations/neotree", "line", line, "no node returned from state.tree:get_node")
end
end
logger.debug("integrations/neotree", "selected_nodes count=", #selected_nodes)
for _, 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)
logger.debug("integrations/neotree", "accepted file", node.path)
else
logger.debug("integrations/neotree", "rejected file (depth<=1)", node.path)
end
elseif node.type == "directory" and node.path and node.path ~= "" then
local depth = (node.get_depth and node:get_depth()) and node:get_depth() or 0
if depth > 1 then
table.insert(files, node.path)
logger.debug("integrations/neotree", "accepted directory", node.path)
else
logger.debug("integrations/neotree", "rejected directory (depth<=1)", node.path)
end
else
logger.debug(
"integrations/neotree",
"rejected node (missing path or unsupported type)",
tostring(node and node.type),
tostring(node and node.path)
)
end
end
if #files > 0 then
logger.debug("integrations/neotree", "files from visual selection:", files)
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
logger.debug("integrations/neotree", "using state selection count=", #selection)
for _, node in ipairs(selection) do
if node.type == "file" and node.path then
table.insert(files, node.path)
logger.debug("integrations/neotree", "accepted file from state selection", node.path)
else
logger.debug(
"integrations/neotree",
"ignored non-file in state selection",
tostring(node and node.type),
tostring(node and node.path)
)
end
end
if #files > 0 then
logger.debug("integrations/neotree", "files from state selection:", files)
return files, nil
end
end
end
if state.tree then
local node = state.tree:get_node()
if node then
logger.debug(
"integrations/neotree",
"fallback single node",
"type=",
tostring(node.type),
"path=",
tostring(node.path)
)
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
logger.debug("integrations/neotree", "no file found under cursor/selection")
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("opencode.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
-- Helper function to get mini.files selection using explicit range
function M._get_mini_files_selection_with_range(start_line, end_line)
local success, mini_files = pcall(require, "mini.files")
if not success then
return {}, "mini.files not available"
end
local files = {}
local bufnr = vim.api.nvim_get_current_buf()
-- Process each line in the range
for line = start_line, end_line do
local entry_ok, entry = pcall(mini_files.get_fs_entry, bufnr, line)
if entry_ok and entry and entry.path and entry.path ~= "" then
-- Extract real filesystem path from mini.files buffer path
local real_path = entry.path
-- Remove mini.files buffer protocol prefix if present
if real_path:match("^minifiles://") then
real_path = real_path:gsub("^minifiles://[^/]*/", "")
end
-- Validate that the path exists
if vim.fn.filereadable(real_path) == 1 or vim.fn.isdirectory(real_path) == 1 then
table.insert(files, real_path)
end
end
end
if #files > 0 then
return files, nil
else
return {}, "No files found in range"
end
end
---Get selected files from mini.files
---Supports both visual selection and single file under cursor
---Reference: mini.files API MiniFiles.get_fs_entry()
---@return table files List of file paths
---@return string|nil error Error message if operation failed
function M._get_mini_files_selection()
local success, mini_files = pcall(require, "mini.files")
if not success then
return {}, "mini.files not available"
end
local bufnr = vim.api.nvim_get_current_buf()
-- Normal mode: get file under cursor
local entry_ok, entry = pcall(mini_files.get_fs_entry, bufnr)
if not entry_ok or not entry then
return {}, "Failed to get entry from mini.files"
end
if entry.path and entry.path ~= "" then
-- Extract real filesystem path from mini.files buffer path
local real_path = entry.path
-- Remove mini.files buffer protocol prefix if present
if real_path:match("^minifiles://") then
real_path = real_path:gsub("^minifiles://[^/]*/", "")
end
-- Validate that the path exists
if vim.fn.filereadable(real_path) == 1 or vim.fn.isdirectory(real_path) == 1 then
return { real_path }, nil
else
return {}, "Invalid file or directory path: " .. real_path
end
end
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()
local has_call = (vim.fn.exists("*netrw#Call") == 1)
local has_expose = (vim.fn.exists("*netrw#Expose") == 1)
if not (has_call and has_expose) then
return {}, "netrw not available"
end
-- function to resolve a 'word' (filename in netrw listing) to an absolute path using b:netrw_curdir
local function resolve_word_to_path(word)
if type(word) ~= "string" or word == "" then
return nil
end
if word == "." or word == ".." or word == "../" then
return nil
end
local curdir = vim.b.netrw_curdir or vim.fn.getcwd()
local joined = curdir .. "/" .. word
return vim.fn.fnamemodify(joined, ":p")
end
-- 1. Check for marked files
do
local mf_ok, mf_result = pcall(function()
if has_expose then
return vim.fn.call("netrw#Expose", { "netrwmarkfilelist" })
end
return nil
end)
local marked_files = {}
if mf_ok 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, vim.fn.fnamemodify(file_path, ":p"))
end
end
end
if #marked_files > 0 then
return marked_files, nil
end
end
-- 2. No marked files. Check for a file or dir under cursor
local path_ok, path_result = pcall(function()
if has_call then
local word = vim.fn.call("netrw#Call", { "NetrwGetWord" })
local p = resolve_word_to_path(word)
return p
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