Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require("bigfile").setup {
"vimopts",
"filetype",
},
override_detection = nil -- see <### Overriding the detection of big files> below
}
```

Expand All @@ -62,6 +63,30 @@ require("bigfile").setup {
}
```

### Overriding the detection of big files

You can add your own logic for detecting big files by setting the
`override_detection` callback in the config. If the function doesn't return
anything, just the filesize will be used, otherwise the returned boolean will
be used (true -> file is big, disable features | false -> do nothing)

example:

```lua
require("bigfile").setup {
-- detect long python files
override_detection = 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
20 changes: 16 additions & 4 deletions lua/bigfile/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local features = require "bigfile.features"
---@field filesize integer size in MiB
---@field pattern string|string[] see |autocmd-pattern|
---@field features string[] array of features
---@field override_detection nil|fun(bufnr: number, filesize_mib: number): boolean|nil callback to override detection of big files
local default_config = {
filesize = 2,
pattern = { "*" },
Expand All @@ -19,6 +20,7 @@ local default_config = {
"vimopts",
"filetype",
},
override_detection = nil,
}

---@param bufnr number
Expand All @@ -34,14 +36,24 @@ 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.override_detection) == "function" then
local user_override = config.override_detection(bufnr, filesize)
if user_override ~= nil then
bigfile_detected = user_override
end
end

if not bigfile_detected then
vim.api.nvim_buf_set_var(bufnr, "bigfile_detected", 0)
return
end
Expand All @@ -50,7 +62,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