diff --git a/doc/snacks-bigfile.txt b/doc/snacks-bigfile.txt index 42daca4f..711291c4 100644 --- a/doc/snacks-bigfile.txt +++ b/doc/snacks-bigfile.txt @@ -44,7 +44,8 @@ mini.animate (if used) { notify = true, -- show notification when big file detected size = 1.5 * 1024 * 1024, -- 1.5MB - columns = 500, + line_length = 500, + lines_scan = 2, -- how many lines will scan for line length -- Enable or disable features when big file detected ---@param ctx {buf: number, ft:string} setup = function(ctx) diff --git a/docs/bigfile.md b/docs/bigfile.md index 496d561d..6d6e0ce1 100644 --- a/docs/bigfile.md +++ b/docs/bigfile.md @@ -37,7 +37,8 @@ The default implementation enables `syntax` for the buffer and disables { notify = true, -- show notification when big file detected size = 1.5 * 1024 * 1024, -- 1.5MB - columns = 500, + line_length = 500, + lines_scan = 2, -- how many lines will scan for line length -- Enable or disable features when big file detected ---@param ctx {buf: number, ft:string} setup = function(ctx) diff --git a/lua/snacks/bigfile.lua b/lua/snacks/bigfile.lua index 2fad01a7..7ec1520b 100644 --- a/lua/snacks/bigfile.lua +++ b/lua/snacks/bigfile.lua @@ -12,7 +12,8 @@ M.meta = { local defaults = { notify = true, -- show notification when big file detected size = 1.5 * 1024 * 1024, -- 1.5MB - columns = 500, + line_length = 500, + lines_scan = 2, -- how many lines will scan for line length -- Enable or disable features when big file detected ---@param ctx {buf: number, ft:string} setup = function(ctx) @@ -31,27 +32,25 @@ local defaults = { function M.setup() local opts = Snacks.config.get("bigfile", defaults) + ---@param bufnr integer + local function buf_contains_long_lines(bufnr) + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, opts.lines_scan, false) + for line in lines do + if #line > opts.line_length then + return true + end + end + return false + end + vim.filetype.add({ pattern = { [".*"] = { function(path, buf) - local columns_exceed_limit = false - - local status, file = pcall(io.open, path, "r") - if status ~= nil and file ~= nil then - for line in file:lines() do - if #line > opts.columns then - columns_exceed_limit = true - file:close() - break - end - end - end - return vim.bo[buf] and vim.bo[buf].filetype ~= "bigfile" and path - and (vim.fn.getfsize(path) > opts.size or columns_exceed_limit) + and (vim.fn.getfsize(path) > opts.size or buf_contains_long_lines(buf)) and "bigfile" or nil end,