feat(preview): add max_file_size config to skip large file previews

Problem: previewing large files (e.g. 500 MB logs, binaries) loads them
into a buffer and can freeze or OOM Neovim. `disable_preview` only
receives the filename, so users cannot gate on file size.

Solution: add `preview_win.max_file_size` (number, MB, default 10). In
`open_preview`, check `entry.meta.stat.size` and fall back to
`vim.uv.fs_stat` when the cached stat is absent. If the file exceeds
the limit and a preview window is already open, render "File too large
to preview" in it; if not, emit a WARN notify and return early. The
cursor-moved auto-update path only fires when a window already exists,
so no flag threading is needed to distinguish explicit from implicit.

Based on: stevearc/oil.nvim#213
This commit is contained in:
Barrett Ruth 2026-03-07 16:49:08 -05:00
parent a9a06b8f3b
commit 4b32ada2d9
Signed by: barrett
GPG key ID: A6C96C9349D2FC81
4 changed files with 47 additions and 3 deletions

View file

@ -642,7 +642,23 @@ M.open_preview = function(opts, callback)
local entry_is_file = not vim.endswith(normalized_url, '/')
local filebufnr
if entry_is_file then
if config.preview_win.disable_preview(normalized_url) then
local max_mb = config.preview_win.max_file_size
local _size = entry.meta and entry.meta.stat and entry.meta.stat.size
if not _size then
local _st = vim.uv.fs_stat(normalized_url)
_size = _st and _st.size
end
if max_mb and _size and _size > max_mb * 1024 * 1024 then
if preview_win then
filebufnr = vim.api.nvim_create_buf(false, true)
vim.bo[filebufnr].bufhidden = 'wipe'
vim.bo[filebufnr].buftype = 'nofile'
util.render_text(filebufnr, 'File too large to preview', { winid = preview_win })
else
vim.notify('File too large to preview', vim.log.levels.WARN)
return finish()
end
elseif config.preview_win.disable_preview(normalized_url) then
filebufnr = vim.api.nvim_create_buf(false, true)
vim.bo[filebufnr].bufhidden = 'wipe'
vim.bo[filebufnr].buftype = 'nofile'