Skip to content

Commit faa2d93

Browse files
committed
refactor: simplify mini.files integration and remove redundant visual mode logic
- Remove visual mode detection from _get_mini_files_selection() since visual mode is now handled by _get_mini_files_selection_with_range() - Remove visual mode tests that are no longer applicable - Keep only normal mode (single file) functionality in _get_mini_files_selection() - Tests now focus on the actual usage patterns
1 parent 404f8d8 commit faa2d93

File tree

2 files changed

+16
-208
lines changed

2 files changed

+16
-208
lines changed

lua/claudecode/integrations.lua

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -316,56 +316,25 @@ function M._get_mini_files_selection()
316316
local bufnr = vim.api.nvim_get_current_buf()
317317

318318

319-
-- Check if we're in visual mode for multi-selection
320-
local mode = vim.fn.mode()
321-
if mode == "V" or mode == "v" or mode == "\22" then
322-
-- Visual mode: get visual range
323-
local visual_commands = require("claudecode.visual_commands")
324-
local start_line, end_line = visual_commands.get_visual_range()
325-
326-
-- Process each line in the visual selection
327-
for line = start_line, end_line do
328-
local entry_ok, entry = pcall(mini_files.get_fs_entry, bufnr, line)
329-
330-
if entry_ok and entry and entry.path and entry.path ~= "" then
331-
-- Extract real filesystem path from mini.files buffer path
332-
local real_path = entry.path
333-
-- Remove mini.files buffer protocol prefix if present
334-
if real_path:match("^minifiles://") then
335-
real_path = real_path:gsub("^minifiles://[^/]*/", "")
336-
end
319+
-- Normal mode: get file under cursor
320+
local entry_ok, entry = pcall(mini_files.get_fs_entry, bufnr)
321+
if not entry_ok or not entry then
322+
return {}, "Failed to get entry from mini.files"
323+
end
337324

338-
-- Validate that the path exists
339-
if vim.fn.filereadable(real_path) == 1 or vim.fn.isdirectory(real_path) == 1 then
340-
table.insert(files, real_path)
341-
end
342-
end
325+
if entry.path and entry.path ~= "" then
326+
-- Extract real filesystem path from mini.files buffer path
327+
local real_path = entry.path
328+
-- Remove mini.files buffer protocol prefix if present
329+
if real_path:match("^minifiles://") then
330+
real_path = real_path:gsub("^minifiles://[^/]*/", "")
343331
end
344332

345-
if #files > 0 then
346-
return files, nil
347-
end
348-
else
349-
-- Normal mode: get file under cursor
350-
local entry_ok, entry = pcall(mini_files.get_fs_entry, bufnr)
351-
if not entry_ok or not entry then
352-
return {}, "Failed to get entry from mini.files"
353-
end
354-
355-
if entry.path and entry.path ~= "" then
356-
-- Extract real filesystem path from mini.files buffer path
357-
local real_path = entry.path
358-
-- Remove mini.files buffer protocol prefix if present
359-
if real_path:match("^minifiles://") then
360-
real_path = real_path:gsub("^minifiles://[^/]*/", "")
361-
end
362-
363-
-- Validate that the path exists
364-
if vim.fn.filereadable(real_path) == 1 or vim.fn.isdirectory(real_path) == 1 then
365-
return { real_path }, nil
366-
else
367-
return {}, "Invalid file or directory path: " .. real_path
368-
end
333+
-- Validate that the path exists
334+
if vim.fn.filereadable(real_path) == 1 or vim.fn.isdirectory(real_path) == 1 then
335+
return { real_path }, nil
336+
else
337+
return {}, "Invalid file or directory path: " .. real_path
369338
end
370339
end
371340

tests/unit/mini_files_integration_spec.lua

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -150,148 +150,6 @@ describe("mini.files integration", function()
150150
end
151151
end)
152152

153-
it("should handle mini.files buffer paths in visual mode", function()
154-
mock_vim.fn.mode = function()
155-
return "V" -- Visual line mode
156-
end
157-
158-
-- Mock mini.files module that returns buffer-style paths
159-
local mock_mini_files = {
160-
get_fs_entry = function(buf_id, line)
161-
if buf_id ~= 1 then
162-
return nil
163-
end
164-
165-
if line == 1 then
166-
return { path = "minifiles://42//Users/test/project/visual1.lua" }
167-
elseif line == 2 then
168-
return { path = "minifiles://42//Users/test/project/visual2.txt" }
169-
elseif line == 3 then
170-
return { path = "minifiles://42//Users/test/project/src" }
171-
end
172-
return nil
173-
end,
174-
}
175-
package.loaded["mini.files"] = mock_mini_files
176-
177-
local files, err = integrations._get_mini_files_selection()
178-
179-
expect(err).to_be_nil()
180-
expect(files).to_be_table()
181-
expect(#files).to_be(3)
182-
expect(files[1]).to_be("/Users/test/project/visual1.lua")
183-
expect(files[2]).to_be("/Users/test/project/visual2.txt")
184-
expect(files[3]).to_be("/Users/test/project/src")
185-
end)
186-
187-
it("should get multiple files in visual mode", function()
188-
mock_vim.fn.mode = function()
189-
return "V" -- Visual line mode
190-
end
191-
192-
-- Mock mini.files module
193-
local mock_mini_files = {
194-
get_fs_entry = function(buf_id, line)
195-
-- Verify buffer ID is passed correctly
196-
if buf_id ~= 1 then
197-
return nil
198-
end
199-
200-
if line == 1 then
201-
return { path = "/Users/test/project/file1.lua" }
202-
elseif line == 2 then
203-
return { path = "/Users/test/project/file2.lua" }
204-
elseif line == 3 then
205-
return { path = "/Users/test/project/src" }
206-
end
207-
return nil
208-
end,
209-
}
210-
package.loaded["mini.files"] = mock_mini_files
211-
212-
local files, err = integrations._get_mini_files_selection()
213-
214-
expect(err).to_be_nil()
215-
expect(files).to_be_table()
216-
expect(#files).to_be(3)
217-
expect(files[1]).to_be("/Users/test/project/file1.lua")
218-
expect(files[2]).to_be("/Users/test/project/file2.lua")
219-
expect(files[3]).to_be("/Users/test/project/src")
220-
end)
221-
222-
it("should filter out invalid files in visual mode", function()
223-
mock_vim.fn.mode = function()
224-
return "V" -- Visual line mode
225-
end
226-
227-
-- Mock mini.files module
228-
local mock_mini_files = {
229-
get_fs_entry = function(buf_id, line)
230-
if line == 1 then
231-
return { path = "/Users/test/project/valid.lua" }
232-
elseif line == 2 then
233-
return { path = "/Users/test/project/invalid.xyz" } -- Won't pass filereadable/isdirectory
234-
elseif line == 3 then
235-
return { path = "/Users/test/project/src" }
236-
end
237-
return nil
238-
end,
239-
}
240-
package.loaded["mini.files"] = mock_mini_files
241-
242-
local files, err = integrations._get_mini_files_selection()
243-
244-
expect(err).to_be_nil()
245-
expect(files).to_be_table()
246-
expect(#files).to_be(2) -- Only valid.lua and src
247-
expect(files[1]).to_be("/Users/test/project/valid.lua")
248-
expect(files[2]).to_be("/Users/test/project/src")
249-
end)
250-
251-
it("should handle visual mode with buffer ID correctly", function()
252-
mock_vim.fn.mode = function()
253-
return "v" -- Visual character mode
254-
end
255-
256-
-- Mock different buffer ID to test parameter passing
257-
mock_vim.api.nvim_get_current_buf = function()
258-
return 42 -- Different buffer ID
259-
end
260-
261-
-- Mock mini.files module that validates buffer ID
262-
local mock_mini_files = {
263-
get_fs_entry = function(buf_id, line)
264-
-- Should receive buffer ID 42
265-
if buf_id ~= 42 then
266-
error("Expected buffer ID 42, got " .. tostring(buf_id))
267-
end
268-
269-
if line == 1 then
270-
return { path = "/Users/test/project/selected1.lua" }
271-
elseif line == 2 then
272-
return { path = "/Users/test/project/selected2.txt" }
273-
elseif line == 3 then
274-
return { path = "/Users/test/project/selected3.lua" }
275-
end
276-
return nil
277-
end,
278-
}
279-
package.loaded["mini.files"] = mock_mini_files
280-
281-
local files, err = integrations._get_mini_files_selection()
282-
283-
expect(err).to_be_nil()
284-
expect(files).to_be_table()
285-
expect(#files).to_be(3)
286-
expect(files[1]).to_be("/Users/test/project/selected1.lua")
287-
expect(files[2]).to_be("/Users/test/project/selected2.txt")
288-
expect(files[3]).to_be("/Users/test/project/selected3.lua")
289-
290-
-- Reset buffer ID for other tests
291-
mock_vim.api.nvim_get_current_buf = function()
292-
return 1
293-
end
294-
end)
295153

296154
it("should handle empty entry under cursor", function()
297155
-- Mock mini.files module
@@ -375,25 +233,6 @@ describe("mini.files integration", function()
375233
expect(#files).to_be(0)
376234
end)
377235

378-
it("should handle visual mode with no valid entries", function()
379-
mock_vim.fn.mode = function()
380-
return "V" -- Visual line mode
381-
end
382-
383-
-- Mock mini.files module
384-
local mock_mini_files = {
385-
get_fs_entry = function(buf_id, line)
386-
return nil -- No entries
387-
end,
388-
}
389-
package.loaded["mini.files"] = mock_mini_files
390-
391-
local files, err = integrations._get_mini_files_selection()
392-
393-
expect(err).to_be("No file found under cursor")
394-
expect(files).to_be_table()
395-
expect(#files).to_be(0)
396-
end)
397236
end)
398237

399238
describe("get_selected_files_from_tree", function()

0 commit comments

Comments
 (0)