Skip to content

Commit ccc10f8

Browse files
committed
debug: add range-based mini.files selection and extensive logging
- Add _get_mini_files_selection_with_range function - Check for range parameters and last visual selection range - Add debug logging to see what parameters are received - Fall back to mode detection if no range available This should help identify why multi-file selection isn't working.
1 parent ee9954d commit ccc10f8

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

lua/claudecode/integrations.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,65 @@ end
268268
--- Reference: mini.files API MiniFiles.get_fs_entry()
269269
--- @return table files List of file paths
270270
--- @return string|nil error Error message if operation failed
271+
-- Helper function to get mini.files selection using explicit range
272+
function M._get_mini_files_selection_with_range(start_line, end_line)
273+
local success, mini_files = pcall(require, "mini.files")
274+
if not success then
275+
return {}, "mini.files not available"
276+
end
277+
278+
local files = {}
279+
local bufnr = vim.api.nvim_get_current_buf()
280+
281+
local logger = require("claudecode.logger")
282+
logger.debug("integrations", "mini.files range selection:")
283+
logger.debug("integrations", " start_line: " .. tostring(start_line))
284+
logger.debug("integrations", " end_line: " .. tostring(end_line))
285+
286+
-- Process each line in the range
287+
for line = start_line, end_line do
288+
logger.debug("integrations", " processing line " .. tostring(line))
289+
local entry_ok, entry = pcall(mini_files.get_fs_entry, bufnr, line)
290+
291+
if entry_ok and entry then
292+
logger.debug("integrations", " entry found: " .. tostring(entry.path))
293+
if entry.path and entry.path ~= "" then
294+
-- Extract real filesystem path from mini.files buffer path
295+
local real_path = entry.path
296+
-- Remove mini.files buffer protocol prefix if present
297+
if real_path:match("^minifiles://") then
298+
real_path = real_path:gsub("^minifiles://[^/]*/", "")
299+
end
300+
301+
logger.debug("integrations", " real_path: " .. tostring(real_path))
302+
303+
-- Validate that the path exists
304+
if vim.fn.filereadable(real_path) == 1 or vim.fn.isdirectory(real_path) == 1 then
305+
logger.debug("integrations", " adding to files: " .. real_path)
306+
table.insert(files, real_path)
307+
else
308+
logger.debug("integrations", " path not readable/directory: " .. real_path)
309+
end
310+
else
311+
logger.debug("integrations", " entry has no path or empty path")
312+
end
313+
else
314+
logger.debug("integrations", " no entry or pcall failed for line " .. tostring(line))
315+
end
316+
end
317+
318+
logger.debug("integrations", "mini.files range selection result: " .. #files .. " files found")
319+
for i, file in ipairs(files) do
320+
logger.debug("integrations", " file " .. i .. ": " .. file)
321+
end
322+
323+
if #files > 0 then
324+
return files, nil
325+
else
326+
return {}, "No file found under cursor"
327+
end
328+
end
329+
271330
function M._get_mini_files_selection()
272331
local success, mini_files = pcall(require, "mini.files")
273332
if not success then

lua/claudecode/selection.lua

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,11 @@ end
647647
-- @param line1 number|nil Optional start line for range-based selection
648648
-- @param line2 number|nil Optional end line for range-based selection
649649
function M.send_at_mention_for_visual_selection(line1, line2)
650+
-- Debug logging to see what parameters we received
651+
logger.debug("selection", "send_at_mention_for_visual_selection called with:")
652+
logger.debug("selection", " line1: " .. tostring(line1))
653+
logger.debug("selection", " line2: " .. tostring(line2))
654+
650655
if not M.state.tracking_enabled then
651656
logger.error("selection", "Selection tracking is not enabled.")
652657
return false
@@ -671,8 +676,36 @@ function M.send_at_mention_for_visual_selection(line1, line2)
671676
logger.debug("selection", " buf_name has minifiles?: " .. tostring(current_buf_name:match("^minifiles://") ~= nil))
672677

673678
if current_ft == "minifiles" or current_buf_name:match("^minifiles://") then
679+
-- Check what mode we're in when calling integrations
680+
local current_mode = vim.fn.mode()
681+
logger.debug("selection", "Calling integrations with mode: '" .. tostring(current_mode) .. "'")
682+
683+
-- For mini.files, we need to handle the range selection manually since visual mode might have ended
674684
local integrations = require("claudecode.integrations")
675-
local files, err = integrations.get_selected_files_from_tree()
685+
local files, err
686+
687+
if line1 and line2 then
688+
-- Range-based selection, get files directly using the range
689+
logger.debug("selection", "Using range selection: " .. line1 .. " to " .. line2)
690+
files, err = integrations._get_mini_files_selection_with_range(line1, line2)
691+
else
692+
-- No range provided, try to get last visual selection range
693+
local last_visual_start = vim.fn.line("'<")
694+
local last_visual_end = vim.fn.line("'>")
695+
696+
logger.debug("selection", "No range provided, checking last visual selection:")
697+
logger.debug("selection", " last_visual_start: " .. tostring(last_visual_start))
698+
logger.debug("selection", " last_visual_end: " .. tostring(last_visual_end))
699+
700+
if last_visual_start > 0 and last_visual_end > 0 and last_visual_start <= last_visual_end then
701+
logger.debug("selection", "Using last visual selection range: " .. last_visual_start .. " to " .. last_visual_end)
702+
files, err = integrations._get_mini_files_selection_with_range(last_visual_start, last_visual_end)
703+
else
704+
-- Fall back to current mode detection
705+
logger.debug("selection", "No valid visual range, falling back to mode detection")
706+
files, err = integrations.get_selected_files_from_tree()
707+
end
708+
end
676709

677710
if err then
678711
logger.error("selection", "Failed to get mini.files selection: " .. err)

0 commit comments

Comments
 (0)