Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to use bytes not MiB for filesize #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ The plugin ships with common default options. No further setup is required.
```lua
-- default config
require("bigfile").setup {
filesize = 2, -- size of the file in MiB, the plugin round file sizes to the closest MiB
filesize = 2, -- size of the file in filesize_unit, the plugin will round file sizes to the closest MiB (not bytes)
filesize_unit = "MiB", -- default, also available "bytes"
pattern = { "*" }, -- autocmd pattern or function see <### Overriding the detection of big files>
features = { -- features to disable
"indent_blankline",
Expand Down Expand Up @@ -73,7 +74,7 @@ example:
```lua
require("bigfile").setup {
-- detect long python files
pattern = function(bufnr, filesize_mib)
pattern = function(bufnr, filesize)
-- 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
Expand Down
30 changes: 22 additions & 8 deletions lua/bigfile/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ local M = {}

local features = require "bigfile.features"

---@alias filesize_unit "MiB"|"bytes"

---@class config
---@field filesize integer size in MiB
---@field pattern string|string[]|fun(bufnr: number, filesize_mib: number): boolean an |autocmd-pattern| or callback to override detection of big files
---@field filesize integer size in filesize_unit, default MiB
---@field filesize_unit filesize_unit filesize unit, default MiB
---@field pattern string|string[]|fun(bufnr: number, filesize: number): boolean an |autocmd-pattern| or callback to override detection of big files
---@field features string[] array of features
local default_config = {
filesize = 2,
filesize = 2, -- 2 MiB
filesize_unit = "MiB",
pattern = { "*" },
features = {
"indent_blankline",
Expand All @@ -22,7 +26,7 @@ local default_config = {
}

---@param bufnr number
---@return integer|nil size in MiB if buffer is valid, nil otherwise
---@return integer|nil size in bytes if buffer is valid, nil otherwise
local function get_buf_size(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local ok, stats = pcall(function()
Expand All @@ -31,7 +35,16 @@ local function get_buf_size(bufnr)
if not (ok and stats) then
return
end
return math.floor(0.5 + (stats.size / (1024 * 1024)))
return stats.size
end

---@param size number filesize in filesize_unit
---@param target_unit filesize_unit
local function convert_to_filesize_unit(size, target_unit)
if target_unit == "MiB" then
return math.floor(0.5 + size / (1024 * 1024))
end
return size
end

---@param bufnr number
Expand All @@ -42,7 +55,7 @@ local function pre_bufread_callback(bufnr, config)
return -- buffer has already been processed
end

local filesize = get_buf_size(bufnr) or 0
local filesize = convert_to_filesize_unit(get_buf_size(bufnr) or 0, config.filesize_unit)
local bigfile_detected = filesize >= config.filesize
if type(config.pattern) == "function" then
bigfile_detected = config.pattern(bufnr, filesize) or bigfile_detected
Expand Down Expand Up @@ -95,8 +108,9 @@ function M.setup(overrides)
pre_bufread_callback(args.buf, config)
end,
desc = string.format(
"[bigfile.nvim] Performance rule for handling files over %sMiB",
config.filesize
"[bigfile.nvim] Performance rule for handling files over %s %s",
config.filesize,
config.filesize_unit
),
})

Expand Down