Skip to content

Commit

Permalink
feat: disable preview for large files
Browse files Browse the repository at this point in the history
fix: update oil.PreviewWindowConfig
  • Loading branch information
stevalkr committed Nov 12, 2024
1 parent 50c4bd4 commit 15a4a43
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ require("oil").setup({
preview_win = {
-- Whether the preview window is automatically updated when the cursor is moved
update_on_cursor_moved = true,
-- Maximum file size in megabytes to preview
max_file_size_mb = 100,
},
-- Configuration for the floating action confirmation window
confirmation = {
Expand Down
2 changes: 2 additions & 0 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ CONFIG *oil-confi
preview_win = {
-- Whether the preview window is automatically updated when the cursor is moved
update_on_cursor_moved = true,
-- Maximum file size in megabytes to preview
max_file_size_mb = 100,
},
-- Configuration for the floating action confirmation window
confirmation = {
Expand Down
13 changes: 11 additions & 2 deletions lua/oil/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ local default_config = {
preview_win = {
-- Whether the preview window is automatically updated when the cursor is moved
update_on_cursor_moved = true,
-- Maximum file size in megabytes to preview
max_file_size_mb = 100,
},
-- Configuration for the floating action confirmation window
confirmation = {
Expand Down Expand Up @@ -323,11 +325,13 @@ local M = {}

---@class (exact) oil.PreviewWindowConfig
---@field update_on_cursor_moved boolean
---@field max_file_size_mb? number Maximum file size in megabytes to preview

---@class (exact) oil.ConfirmationWindowConfig : oil.WindowConfig

---@class (exact) oil.SetupPreviewWindowConfig
---@field update_on_cursor_moved? boolean Whether the preview window is automatically updated when the cursor is moved
---@field max_file_size_mb? number Maximum file size in megabytes to preview

---@class (exact) oil.SetupConfirmationWindowConfig : oil.SetupWindowConfig

Expand Down Expand Up @@ -382,8 +386,13 @@ M.setup = function(opts)
new_conf.confirmation = vim.tbl_deep_extend("keep", opts.preview, default_config.confirmation)
end
-- Backwards compatibility. We renamed the 'preview' config to 'preview_win'
if opts.preview and opts.preview.update_on_cursor_moved ~= nil then
new_conf.preview_win.update_on_cursor_moved = opts.preview.update_on_cursor_moved
if opts.preview then
if opts.preview.update_on_cursor_moved ~= nil then
new_conf.preview_win.update_on_cursor_moved = opts.preview.update_on_cursor_moved
end
if opts.preview.max_file_size_mb ~= nil then
new_conf.preview_win.max_file_size_mb = opts.preview.max_file_size_mb
end
end

if new_conf.lsp_rename_autosave ~= nil then
Expand Down
7 changes: 7 additions & 0 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ M.open_preview = function(opts, callback)
if not entry then
return finish("Could not find entry under cursor")
end
if entry.meta ~= nil and entry.meta.stat ~= nil then
if entry.meta.stat.size >= config.preview_win.max_file_size_mb * 1e6 then
return finish(
"File over " .. config.preview_win.max_file_size_mb .. "MB is too large to preview"
)
end
end
local entry_title = entry.name
if entry.type == "directory" then
entry_title = entry_title .. "/"
Expand Down

0 comments on commit 15a4a43

Please sign in to comment.