From 15a4a439cdf74c7204ffbd526e213748a32f62f5 Mon Sep 17 00:00:00 2001
From: Steve Walker <65963536+etherswangel@users.noreply.github.com>
Date: Fri, 8 Nov 2024 20:47:20 +0800
Subject: [PATCH 1/3] feat: disable preview for large files

fix: update oil.PreviewWindowConfig
---
 README.md          |  2 ++
 doc/oil.txt        |  2 ++
 lua/oil/config.lua | 13 +++++++++++--
 lua/oil/init.lua   |  7 +++++++
 4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 6bf02823..79b96362 100644
--- a/README.md
+++ b/README.md
@@ -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 = {
diff --git a/doc/oil.txt b/doc/oil.txt
index b4ebf1fd..32830365 100644
--- a/doc/oil.txt
+++ b/doc/oil.txt
@@ -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 = {
diff --git a/lua/oil/config.lua b/lua/oil/config.lua
index 31e25f2a..9eae3557 100644
--- a/lua/oil/config.lua
+++ b/lua/oil/config.lua
@@ -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 = {
@@ -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
 
@@ -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
diff --git a/lua/oil/init.lua b/lua/oil/init.lua
index 600ccc5e..69f97802 100644
--- a/lua/oil/init.lua
+++ b/lua/oil/init.lua
@@ -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 .. "/"

From 788e22a0d451bb243a4fae512b7178b69282abf4 Mon Sep 17 00:00:00 2001
From: Steven Arcangeli <506791+stevearc@users.noreply.github.com>
Date: Tue, 12 Nov 2024 08:21:13 -0800
Subject: [PATCH 2/3] refactor: remove unnecessary shim in config.lua

---
 lua/oil/config.lua | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/lua/oil/config.lua b/lua/oil/config.lua
index 9eae3557..d81dff05 100644
--- a/lua/oil/config.lua
+++ b/lua/oil/config.lua
@@ -325,7 +325,7 @@ 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
+---@field max_file_size_mb number
 
 ---@class (exact) oil.ConfirmationWindowConfig : oil.WindowConfig
 
@@ -390,9 +390,6 @@ M.setup = function(opts)
     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

From 0eca3397c3f9eaf75356f58ccfb6310302ed6d21 Mon Sep 17 00:00:00 2001
From: Steven Arcangeli <506791+stevearc@users.noreply.github.com>
Date: Tue, 12 Nov 2024 08:22:42 -0800
Subject: [PATCH 3/3] refactor: revert changes to shim

---
 lua/oil/config.lua | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/lua/oil/config.lua b/lua/oil/config.lua
index d81dff05..17874b7c 100644
--- a/lua/oil/config.lua
+++ b/lua/oil/config.lua
@@ -386,10 +386,8 @@ 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 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 and opts.preview.update_on_cursor_moved ~= nil then
+    new_conf.preview_win.update_on_cursor_moved = opts.preview.update_on_cursor_moved
   end
 
   if new_conf.lsp_rename_autosave ~= nil then