Skip to content

Commit 9616b73

Browse files
feat: allow overriding the detection of big files (#16)
* feat: allow overriding the detection of big files * refactor: use `pattern` instead of `override_detection`
1 parent 67a03ff commit 9616b73

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The plugin ships with common default options. No further setup is required.
2626
-- default config
2727
require("bigfile").setup {
2828
filesize = 2, -- size of the file in MiB, the plugin round file sizes to the closest MiB
29-
pattern = { "*" }, -- autocmd pattern
29+
pattern = { "*" }, -- autocmd pattern or function see <### Overriding the detection of big files>
3030
features = { -- features to disable
3131
"indent_blankline",
3232
"illuminate",
@@ -62,6 +62,29 @@ require("bigfile").setup {
6262
}
6363
```
6464

65+
### Overriding the detection of big files
66+
67+
You can add your own logic for detecting big files by setting `pattern` in the
68+
config to a function. If the function returns true file will be considered big,
69+
otherwise `filesize` will be used as a fallback
70+
71+
example:
72+
73+
```lua
74+
require("bigfile").setup {
75+
-- detect long python files
76+
pattern = function(bufnr, filesize_mib)
77+
-- you can't use `nvim_buf_line_count` because this runs on BufReadPre
78+
local file_contents = vim.fn.readfile(vim.api.nvim_buf_get_name(bufnr))
79+
local file_length = #file_contents
80+
local filetype = vim.filetype.match({ buf = bufnr })
81+
if file_length > 5000 and filetype == "python" then
82+
return true
83+
end
84+
end
85+
}
86+
```
87+
6588
# Caveats
6689

6790
- `matchparen` stays disabled, even after you close the big file, you can call `:DoMatchParen` manually to enable it

lua/bigfile/init.lua

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local features = require "bigfile.features"
44

55
---@class config
66
---@field filesize integer size in MiB
7-
---@field pattern string|string[] see |autocmd-pattern|
7+
---@field pattern string|string[]|fun(bufnr: number, filesize_mib: number): boolean an |autocmd-pattern| or callback to override detection of big files
88
---@field features string[] array of features
99
local default_config = {
1010
filesize = 2,
@@ -34,14 +34,21 @@ local function get_buf_size(bufnr)
3434
return math.floor(0.5 + (stats.size / (1024 * 1024)))
3535
end
3636

37-
local function pre_bufread_callback(bufnr, rule)
37+
---@param bufnr number
38+
---@param config config
39+
local function pre_bufread_callback(bufnr, config)
3840
local status_ok, _ = pcall(vim.api.nvim_buf_get_var, bufnr, "bigfile_detected")
3941
if status_ok then
4042
return -- buffer has already been processed
4143
end
4244

43-
local filesize = get_buf_size(bufnr)
44-
if not filesize or filesize < rule.filesize then
45+
local filesize = get_buf_size(bufnr) or 0
46+
local bigfile_detected = filesize >= config.filesize
47+
if type(config.pattern) == "function" then
48+
bigfile_detected = config.pattern(bufnr, filesize) or bigfile_detected
49+
end
50+
51+
if not bigfile_detected then
4552
vim.api.nvim_buf_set_var(bufnr, "bigfile_detected", 0)
4653
return
4754
end
@@ -50,7 +57,7 @@ local function pre_bufread_callback(bufnr, rule)
5057

5158
local matched_features = vim.tbl_map(function(feature)
5259
return features.get_feature(feature)
53-
end, rule.features)
60+
end, config.features)
5461

5562
-- Categorize features and disable features that don't need deferring
5663
local matched_deferred_features = {}
@@ -79,8 +86,10 @@ function M.setup(overrides)
7986

8087
local augroup = vim.api.nvim_create_augroup("bigfile", {})
8188

89+
local pattern = config.pattern
90+
8291
vim.api.nvim_create_autocmd("BufReadPre", {
83-
pattern = config.pattern,
92+
pattern = type(pattern) ~= "function" and pattern or "*",
8493
group = augroup,
8594
callback = function(args)
8695
pre_bufread_callback(args.buf, config)

0 commit comments

Comments
 (0)