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(bigfile): take into account lines length #576

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
2 changes: 2 additions & 0 deletions doc/snacks-bigfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ mini.animate <https://github.com/echasnovski/mini.animate> (if used)
{
notify = true, -- show notification when big file detected
size = 1.5 * 1024 * 1024, -- 1.5MB
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)
Expand Down
2 changes: 2 additions & 0 deletions docs/bigfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +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
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)
Expand Down
15 changes: 14 additions & 1 deletion lua/snacks/bigfile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ M.meta = {
local defaults = {
notify = true, -- show notification when big file detected
size = 1.5 * 1024 * 1024, -- 1.5MB
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)
Expand All @@ -30,14 +32,25 @@ local defaults = {
function M.setup()
local opts = Snacks.config.get("bigfile", defaults)

---@param buf integer
local function buf_contains_long_lines(buf)
local lines = vim.api.nvim_buf_get_lines(buf, 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)
return vim.bo[buf]
and vim.bo[buf].filetype ~= "bigfile"
and path
and vim.fn.getfsize(path) > opts.size
and (vim.fn.getfsize(path) > opts.size or buf_contains_long_lines(buf))
and "bigfile"
or nil
end,
Expand Down
Loading