Skip to content

Commit

Permalink
feat: using vim.filetype match
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed May 24, 2023
1 parent dda9a39 commit 3bc13c4
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 87 deletions.
167 changes: 80 additions & 87 deletions lua/telescope/previewers/buffer_previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local putils = require "telescope.previewers.utils"
local Previewer = require "telescope.previewers.previewer"
local conf = require("telescope.config").values

local pfiletype = require "plenary.filetype"
local pscan = require "plenary.scandir"

local buf_delete = utils.buf_delete
Expand Down Expand Up @@ -201,106 +200,100 @@ previewers.file_maker = function(filepath, bufnr, opts)
if opts.use_ft_detect == nil then
opts.use_ft_detect = true
end
opts.ft = opts.use_ft_detect and pfiletype.detect(filepath)
if opts.bufname ~= filepath then
if not vim.in_fast_event() then
vim.schedule(function()
opts.ft = opts.use_ft_detect and putils.filetype_detect(filepath)
if opts.bufname ~= filepath 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
if type(opts.preview.filetype_hook) == "function" then
if not opts.preview.filetype_hook(filepath, bufnr, opts) then
return
end
end
if stat.type == "directory" then
handle_directory_preview(filepath, bufnr, opts)
else
if opts.preview.check_mime_type == true and has_file and 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))
local mime_type = vim.split(output, "/")
if mime_type[1] ~= "text" and mime_type[1] ~= "inode" and mime_type[2] ~= "json" then
if type(opts.preview.mime_hook) == "function" then
vim.schedule_wrap(opts.preview.mime_hook)(filepath, bufnr, opts)
else
vim.schedule_wrap(putils.set_preview_message)(
bufnr,
opts.winid,
"Binary cannot be previewed",
opts.preview.msg_bg_fillchar
)
end
return
end
if mime_type[2] == "json" then
opts.ft = "json"
end
vim.loop.fs_stat(filepath, function(_, stat)
if not stat then
return
end

if opts.preview.filesize_limit then
local mb_filesize = math.floor(stat.size / bytes_to_megabytes)
if mb_filesize > opts.preview.filesize_limit then
if type(opts.preview.filesize_hook) == "function" then
vim.schedule_wrap(opts.preview.filesize_hook)(filepath, bufnr, opts)
else
vim.schedule_wrap(putils.set_preview_message)(
bufnr,
opts.winid,
"File exceeds preview size limit",
opts.preview.msg_bg_fillchar
)
if stat.type == "directory" then
handle_directory_preview(filepath, bufnr, opts)
else
if opts.preview.check_mime_type == true and has_file and 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))
local mime_type = vim.split(output, "/")
if mime_type[1] ~= "text" and mime_type[1] ~= "inode" and mime_type[2] ~= "json" then
if type(opts.preview.mime_hook) == "function" then
vim.schedule_wrap(opts.preview.mime_hook)(filepath, bufnr, opts)
else
vim.schedule_wrap(putils.set_preview_message)(
bufnr,
opts.winid,
"Binary cannot be previewed",
opts.preview.msg_bg_fillchar
)
end
return
end
if mime_type[2] == "json" then
opts.ft = "json"
end
return
end
end

opts.start_time = vim.loop.hrtime()
Path:new(filepath):_read_async(vim.schedule_wrap(function(data)
if not vim.api.nvim_buf_is_valid(bufnr) then
return
if opts.preview.filesize_limit then
local mb_filesize = math.floor(stat.size / bytes_to_megabytes)
if mb_filesize > opts.preview.filesize_limit then
if type(opts.preview.filesize_hook) == "function" then
vim.schedule_wrap(opts.preview.filesize_hook)(filepath, bufnr, opts)
else
vim.schedule_wrap(putils.set_preview_message)(
bufnr,
opts.winid,
"File exceeds preview size limit",
opts.preview.msg_bg_fillchar
)
end
return
end
end
local processed_data = split(data, "[\r]?\n", _, opts)

if processed_data then
local ok = pcall(vim.api.nvim_buf_set_lines, bufnr, 0, -1, false, processed_data)
if not ok then
opts.start_time = vim.loop.hrtime()
Path:new(filepath):_read_async(vim.schedule_wrap(function(data)
if not vim.api.nvim_buf_is_valid(bufnr) then
return
end
local processed_data = split(data, "[\r]?\n", _, opts)

if opts.callback then
opts.callback(bufnr)
end
putils.highlighter(bufnr, opts.ft, opts)
else
if type(opts.preview.timeout_hook) == "function" then
vim.schedule_wrap(opts.preview.timeout_hook)(filepath, bufnr, opts)
if processed_data then
local ok = pcall(vim.api.nvim_buf_set_lines, bufnr, 0, -1, false, processed_data)
if not ok then
return
end

if opts.callback then
opts.callback(bufnr)
end
putils.highlighter(bufnr, opts.ft, opts)
else
vim.schedule_wrap(putils.set_preview_message)(
bufnr,
opts.winid,
"Previewer timed out",
opts.preview.msg_bg_fillchar
)
if type(opts.preview.timeout_hook) == "function" then
vim.schedule_wrap(opts.preview.timeout_hook)(filepath, bufnr, opts)
else
vim.schedule_wrap(putils.set_preview_message)(
bufnr,
opts.winid,
"Previewer timed out",
opts.preview.msg_bg_fillchar
)
end
return
end
return
end
end))
end
end)
else
if opts.callback then
if vim.in_fast_event() then
vim.schedule(function()
opts.callback(bufnr)
end)
else
end))
end
end)
else
if opts.callback then
opts.callback(bufnr)
end
end
end
end)
end

previewers.new_buffer_previewer = function(opts)
Expand Down Expand Up @@ -431,7 +424,7 @@ previewers.new_buffer_previewer = function(opts)
data = {
title = entry.preview_title,
bufname = self.state.bufname,
filetype = pfiletype.detect(self.state.bufname),
filetype = putils.filetype_detect(self.state.bufname),
},
})
end)
Expand Down Expand Up @@ -851,7 +844,7 @@ previewers.git_commit_diff_as_was = defaulter(function(opts)
local cmd = { "git", "--no-pager", "show" }
local cf = opts.current_file and Path:new(opts.current_file):make_relative(opts.cwd)
local value = cf and (entry.value .. ":" .. cf) or entry.value
local ft = cf and pfiletype.detect(value) or "diff"
local ft = cf and putils.filetype_detect(value) or "diff"
table.insert(cmd, value)

putils.job_maker(cmd, self.state.bufnr, {
Expand Down
47 changes: 47 additions & 0 deletions lua/telescope/previewers/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,56 @@ local strings = require "plenary.strings"
local conf = require("telescope.config").values

local Job = require "plenary.job"
local Path = require "plenary.path"

local utils = {}

local detect_from_shebang = function(p)
local s = p:readbyterange(0, 256)
if not s then
return nil
end
local lines = vim.split(s, "\n")
return vim.filetype.match { contents = lines }
end

local parse_modeline = function(tail)
if tail:find "vim:" then
return tail:match ".*:ft=([^: ]*):.*$" or ""
end
end

local detect_from_modeline = function(p)
local s = p:readbyterange(-256, 256)
if s then
local lines = vim.split(s, "\n")
local idx = lines[#lines] ~= "" and #lines or #lines - 1
if idx >= 1 then
return parse_modeline(lines[idx])
end
end
end

utils.filetype_detect = function(filepath)
local match = vim.filetype.match { filename = filepath }
if match and match ~= "" then
return match
end

local p = Path:new(filepath)
if p and p:exists() then
match = detect_from_shebang(p)
if match and match ~= "" then
return match
end

match = detect_from_modeline(p)
if match and match ~= "" then
return match
end
end
end

utils.with_preview_window = function(status, bufnr, callable)
if bufnr and vim.api.nvim_buf_call and false then
vim.api.nvim_buf_call(bufnr, callable)
Expand Down

0 comments on commit 3bc13c4

Please sign in to comment.