Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The plugin ships with common default options. No further setup is required.
-- default config
require("bigfile").setup {
filesize = 2, -- size of the file in MiB, the plugin round file sizes to the closest MiB
pattern = { "*" }, -- autocmd pattern
pattern = { "*" }, -- autocmd pattern or function see <### Overriding the detection of big files>
features = { -- features to disable
"indent_blankline",
"illuminate",
Expand Down Expand Up @@ -62,6 +62,29 @@ require("bigfile").setup {
}
```

### Overriding the detection of big files

You can add your own logic for detecting big files by setting `pattern` in the
config to a function. If the function returns true file will be considered big,
otherwise `filesize` will be used as a fallback

example:

```lua
require("bigfile").setup {
-- detect long python files
pattern = function(bufnr, filesize_mib)
-- you can't use `nvim_buf_line_count` because this runs on BufReadPre
local file_contents = vim.fn.readfile(vim.api.nvim_buf_get_name(bufnr))
local file_length = #file_contents
local filetype = vim.filetype.match({ buf = bufnr })
if file_length > 5000 and filetype == "python" then
return true
end
end
}
```

# Caveats

- `matchparen` stays disabled, even after you close the big file, you can call `:DoMatchParen` manually to enable it
Expand Down
21 changes: 15 additions & 6 deletions lua/bigfile/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local features = require "bigfile.features"

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

local function pre_bufread_callback(bufnr, rule)
---@param bufnr number
---@param config config
local function pre_bufread_callback(bufnr, config)
local status_ok, _ = pcall(vim.api.nvim_buf_get_var, bufnr, "bigfile_detected")
if status_ok then
return -- buffer has already been processed
end

local filesize = get_buf_size(bufnr)
if not filesize or filesize < rule.filesize then
local filesize = get_buf_size(bufnr) or 0
local bigfile_detected = filesize >= config.filesize
if type(config.pattern) == "function" then
bigfile_detected = config.pattern(bufnr, filesize) or bigfile_detected
end

if not bigfile_detected then
vim.api.nvim_buf_set_var(bufnr, "bigfile_detected", 0)
return
end
Expand All @@ -50,7 +57,7 @@ local function pre_bufread_callback(bufnr, rule)

local matched_features = vim.tbl_map(function(feature)
return features.get_feature(feature)
end, rule.features)
end, config.features)

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

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

local pattern = config.pattern

vim.api.nvim_create_autocmd("BufReadPre", {
pattern = config.pattern,
pattern = type(pattern) ~= "function" and pattern or "*",
group = augroup,
callback = function(args)
pre_bufread_callback(args.buf, config)
Expand Down