Skip to content

Commit 8c69f58

Browse files
authored
feat(preview)!: add opts.preview.highlight_limit with default 1MB (#2715)
1 parent 7d51950 commit 8c69f58

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

lua/telescope/config.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ append(
535535
{
536536
check_mime_type = not has_win,
537537
filesize_limit = 25,
538+
highlight_limit = 1,
538539
timeout = 250,
539540
treesitter = true,
540541
msg_bg_fillchar = "",
@@ -559,6 +560,9 @@ append(
559560
- filesize_limit: The maximum file size in MB attempted to be previewed.
560561
Set to false to attempt to preview any file size.
561562
Default: 25
563+
- highlight_limit: The maximum file size in MB attempted to be highlighted.
564+
Set to false to attempt to highlight any file size.
565+
Default: 1
562566
- timeout: Timeout the previewer if the preview did not
563567
complete within `timeout` milliseconds.
564568
Set to false to not timeout preview.

lua/telescope/previewers/buffer_previewer.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ local handle_file_preview = function(filepath, bufnr, stat, opts)
182182
end
183183
end
184184

185+
local mb_filesize = stat.size / bytes_to_megabytes
185186
if opts.preview.filesize_limit then
186-
local mb_filesize = math.floor(stat.size / bytes_to_megabytes)
187187
if mb_filesize > opts.preview.filesize_limit then
188188
if type(opts.preview.filesize_hook) == "function" then
189189
opts.preview.filesize_hook(filepath, bufnr, opts)
@@ -228,7 +228,10 @@ local handle_file_preview = function(filepath, bufnr, stat, opts)
228228
if opts.callback then
229229
opts.callback(bufnr)
230230
end
231-
putils.highlighter(bufnr, opts.ft, opts)
231+
232+
if not (opts.preview.highlight_limit and mb_filesize > opts.preview.highlight_limit) then
233+
putils.highlighter(bufnr, opts.ft, opts)
234+
end
232235
else
233236
if type(opts.preview.timeout_hook) == "function" then
234237
opts.preview.timeout_hook(filepath, bufnr, opts)
@@ -243,12 +246,14 @@ end
243246

244247
local PREVIEW_TIMEOUT_MS = 250
245248
local PREVIEW_FILESIZE_MB = 25
249+
local PREVIEW_HIGHLIGHT_MB = 1
246250

247251
previewers.file_maker = function(filepath, bufnr, opts)
248252
opts = vim.F.if_nil(opts, {})
249253
opts.preview = vim.F.if_nil(opts.preview, {})
250254
opts.preview.timeout = vim.F.if_nil(opts.preview.timeout, PREVIEW_TIMEOUT_MS)
251255
opts.preview.filesize_limit = vim.F.if_nil(opts.preview.filesize_limit, PREVIEW_FILESIZE_MB)
256+
opts.preview.highlight_limit = vim.F.if_nil(opts.preview.highlight_limit, PREVIEW_HIGHLIGHT_MB)
252257
opts.preview.msg_bg_fillchar = vim.F.if_nil(opts.preview.msg_bg_fillchar, "")
253258
opts.preview.treesitter = vim.F.if_nil(opts.preview.treesitter, true)
254259
if opts.use_ft_detect == nil then

0 commit comments

Comments
 (0)