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

@ -152,6 +152,9 @@ The full list of options with their defaults:
view_options = {
-- Show files and directories that start with "."
show_hidden = false,
-- When true and a directory has no visible entries, show hidden entries
-- instead of an empty listing. is_always_hidden entries are never shown.
show_hidden_when_empty = false,
-- This function defines what is considered a "hidden" file
is_hidden_file = function(name, bufnr)
local m = name:match("^%.")
@ -227,6 +230,10 @@ The full list of options with their defaults:
disable_preview = function(filename)
return false
end,
-- Maximum file size in MB to load into the preview window. Files larger
-- than this limit will show a placeholder message instead of being loaded.
-- Set to nil to disable the limit.
max_file_size = 10,
-- Window-local options to use for preview window buffers
win_options = {},
},
@ -328,6 +335,19 @@ cleanup_buffers_on_delete *canola.cleanup_buffers_on_de
was successfully deleted via canola. This prevents stale buffers from
appearing in the jumplist after a deletion.
show_hidden_when_empty *canola.show_hidden_when_empty*
type: `boolean` default: `false`
When `true` and a directory contains no visible entries (because all
entries are hidden), canola will display the hidden entries anyway.
Entries matching `is_always_hidden` are never shown. Hidden entries
are still rendered with the dimmed hidden style.
preview_win.max_file_size *canola.preview_win*
type: `number` default: `10`
Maximum file size in MB to load into the preview window. Files larger
than this limit will show a placeholder message instead of being loaded.
Set to `nil` to disable the limit.
--------------------------------------------------------------------------------
API *canola-api*