Skip to content

Commit 80fb1e0

Browse files
committed
Improve handling of large files from @pkazmier
1 parent 3f01a15 commit 80fb1e0

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

lua/oil/util.lua

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -905,43 +905,25 @@ M.read_file_to_scratch_buffer = function(path, opts)
905905
vim.bo[bufnr].bufhidden = "wipe"
906906
vim.bo[bufnr].buftype = "nofile"
907907
vim.b[bufnr].oil_preview_buffer = true
908-
vim.loop.fs_open(path, "r", 438, function(err_open, fd)
909-
if err_open then
910-
print("Opening the file failed with following error message: " .. err_open)
911-
return
908+
909+
-- Next two lines lifted from mini.files
910+
local has_lines, read_res = pcall(vim.fn.readfile, path, "", vim.o.lines)
911+
local lines = has_lines and vim.split(table.concat(read_res, "\n"), "\n") or {}
912+
913+
if not vim.api.nvim_buf_is_valid(bufnr) then
914+
return
915+
end
916+
local ok = pcall(vim.api.nvim_buf_set_lines, bufnr, 0, -1, false, lines)
917+
if not ok then
918+
return
919+
end
920+
local ft = vim.filetype.match({ filename = path })
921+
if ft and ft ~= "" then
922+
local lang = vim.treesitter.language.get_lang(ft)
923+
if not pcall(vim.treesitter.start, bufnr, lang) then
924+
vim.bo[bufnr].syntax = ft
912925
end
913-
vim.loop.fs_fstat(fd, function(err_fstat, stat)
914-
assert(not err_fstat, err_fstat)
915-
if not stat or stat.type ~= "file" then
916-
return
917-
end
918-
vim.loop.fs_read(fd, stat.size, 0, function(err_read, data)
919-
assert(not err_read, err_read)
920-
vim.loop.fs_close(fd, function(err_close)
921-
assert(not err_close, err_close)
922-
vim.schedule(function()
923-
if not vim.api.nvim_buf_is_valid(bufnr) then
924-
return
925-
end
926-
local processed_data = vim.split(data or "", "[\r]?\n", opts)
927-
if processed_data then
928-
local ok = pcall(vim.api.nvim_buf_set_lines, bufnr, 0, -1, false, processed_data)
929-
if not ok then
930-
return
931-
end
932-
end
933-
local ft = vim.filetype.match({ filename = path })
934-
if ft and ft ~= "" then
935-
local lang = vim.treesitter.language.get_lang(ft)
936-
if not pcall(vim.treesitter.start, bufnr, lang) then
937-
vim.bo[bufnr].syntax = ft
938-
end
939-
end
940-
end)
941-
end)
942-
end)
943-
end)
944-
end)
926+
end
945927
return bufnr
946928
end
947929

0 commit comments

Comments
 (0)