From 4b962188102f2e0f8d8e04f28830190c0f91b43e Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 22 May 2024 23:34:47 +0100 Subject: [PATCH 1/2] feat: add option to use bytes not MiB for filesize --- README.md | 5 +++-- lua/bigfile/init.lua | 30 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f76c05b..2e63dba 100644 --- a/README.md +++ b/README.md @@ -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", @@ -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 diff --git a/lua/bigfile/init.lua b/lua/bigfile/init.lua index 4f72843..8f0c3be 100644 --- a/lua/bigfile/init.lua +++ b/lua/bigfile/init.lua @@ -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", @@ -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() @@ -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 @@ -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 @@ -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 ), }) From 1879d8d0e1a03267f8573741dcad664235706f2a Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 1 Aug 2024 10:43:58 +0100 Subject: [PATCH 2/2] Add missing parenthesis Co-authored-by: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> --- lua/bigfile/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/bigfile/init.lua b/lua/bigfile/init.lua index 8f0c3be..d972911 100644 --- a/lua/bigfile/init.lua +++ b/lua/bigfile/init.lua @@ -42,7 +42,7 @@ end ---@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) + return math.floor(0.5 + size / (1024 * 1024)) end return size end