Skip to content

Commit d2d2574

Browse files
authored
refactor(files): simplify file filtering and default to text (#1541)
- Remove filetype-based filtering from file list, now only filters out empty entries. - Add simple binary detection in get_file by rejecting files with null bytes. - filetype() now defaults to 'text' if detection fails, letting content validation handle unreadable files. This improves robustness and performance by avoiding unnecessary filetype checks and handling binary files more gracefully. Closes #1533
1 parent ca9a428 commit d2d2574

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

lua/CopilotChat/resources.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function M.get_file(filename)
2727
if not content or content == '' then
2828
return nil
2929
end
30+
-- Simple binary detection: reject files with null bytes
31+
if content:find('\0') then
32+
return nil
33+
end
3034
data = {
3135
content = content,
3236
_modified = modified,

lua/CopilotChat/utils/files.lua

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,11 @@ M.scan_args = {
99
}
1010

1111
local function filter_files(files, max_count)
12-
local filetype = require('plenary.filetype')
13-
12+
-- Filter out empty entries
1413
files = vim.tbl_filter(function(file)
15-
if file == nil or file == '' then
16-
return false
17-
end
18-
19-
local ft = filetype.detect(file, {
20-
fs_access = false,
21-
})
22-
23-
if ft == '' or not ft then
24-
return false
25-
end
26-
27-
return true
14+
return file ~= nil and file ~= ''
2815
end, files)
16+
2917
if max_count and max_count > 0 then
3018
files = vim.list_slice(files, 1, max_count)
3119
end
@@ -268,7 +256,13 @@ function M.filetype(filename)
268256
})
269257

270258
if ft == '' or not ft and not vim.in_fast_event() then
271-
return vim.filetype.match({ filename = filename })
259+
ft = vim.filetype.match({ filename = filename })
260+
end
261+
262+
-- If filetype still not detected, default to 'text'
263+
-- Let content validation handle whether it's actually readable
264+
if not ft or ft == '' then
265+
return 'text'
272266
end
273267

274268
return ft

0 commit comments

Comments
 (0)