Skip to content

Commit

Permalink
Use nvim_buf_get_lines api
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisands committed Jan 17, 2025
1 parent b6e1e4a commit 480c4ab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion doc/snacks-bigfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +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
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)
Expand Down
3 changes: 2 additions & 1 deletion docs/bigfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 14 additions & 15 deletions lua/snacks/bigfile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 480c4ab

Please sign in to comment.