From 116dbea5800c908de4afa6e793f28f782621c65d Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Sat, 10 Jun 2023 15:55:43 +0200 Subject: [PATCH] fix(previewer): call fthook after we determined the filetype (#2560) --- lua/telescope/previewers/buffer_previewer.lua | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index e591b43..96eb2cb 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -204,11 +204,6 @@ previewers.file_maker = function(filepath, bufnr, opts) if not vim.in_fast_event() then filepath = vim.fn.expand(filepath) end - if type(opts.preview.filetype_hook) == "function" then - if not opts.preview.filetype_hook(filepath, bufnr, opts) then - return - end - end vim.loop.fs_stat(filepath, function(_, stat) if not stat then return @@ -219,6 +214,11 @@ previewers.file_maker = function(filepath, bufnr, opts) vim.schedule(function() opts.ft = opts.use_ft_detect and putils.filetype_detect(filepath) local possible_binary = false + if type(opts.preview.filetype_hook) == "function" and opts.ft ~= nil and opts.ft ~= "" then + if not opts.preview.filetype_hook(filepath, bufnr, opts) then + return + end + end if opts.preview.check_mime_type == true and has_file and (opts.ft == nil or opts.ft == "") then -- avoid SIGABRT in buffer previewer happening with utils.get_os_command_output local output = capture(string.format([[file --mime-type -b "%s"]], filepath)) @@ -270,6 +270,14 @@ previewers.file_maker = function(filepath, bufnr, opts) if opts.ft == nil or opts.ft == "" then opts.ft = vim.filetype.match { filename = filepath, buf = bufnr } end + -- we need to attempt to call filetype hook at this point "again" + -- previously only if we had a valid filetype, now every time + -- also if there will never be a filetype + if type(opts.preview.filetype_hook) == "function" then + if not opts.preview.filetype_hook(filepath, bufnr, opts) then + return + end + end -- if we still dont have a ft we need to display the binary message if opts.ft == nil or opts.ft == "" and possible_binary then putils.set_preview_message(