refactor(previewer): remove with_preview_window (#2563)

This commit is contained in:
Simon Hauser
2023-06-10 21:01:23 +02:00
committed by GitHub
parent 116dbea580
commit 89ca726572
2 changed files with 59 additions and 68 deletions

View File

@@ -478,7 +478,7 @@ previewers.cat = defaulter(function(opts)
return from_entry.path(entry, false) return from_entry.path(entry, false)
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local p = from_entry.path(entry, true) local p = from_entry.path(entry, true)
if p == nil or p == "" then if p == nil or p == "" then
return return
@@ -518,7 +518,7 @@ previewers.vimgrep = defaulter(function(opts)
return from_entry.path(entry, false) return from_entry.path(entry, false)
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
-- builtin.buffers: bypass path validation for terminal buffers that don't have appropriate path -- builtin.buffers: bypass path validation for terminal buffers that don't have appropriate path
local has_buftype = entry.bufnr and vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "" or false local has_buftype = entry.bufnr and vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "" or false
local p local p
@@ -596,7 +596,7 @@ previewers.ctags = defaulter(function(_)
return entry.filename return entry.filename
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
conf.buffer_previewer_maker(entry.filename, self.state.bufnr, { conf.buffer_previewer_maker(entry.filename, self.state.bufnr, {
bufname = self.state.bufname, bufname = self.state.bufname,
winid = self.state.winid, winid = self.state.winid,
@@ -619,7 +619,7 @@ previewers.builtin = defaulter(function(_)
return entry.filename return entry.filename
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local module_name = vim.fn.fnamemodify(vim.fn.fnamemodify(entry.filename, ":h"), ":t") local module_name = vim.fn.fnamemodify(vim.fn.fnamemodify(entry.filename, ":h"), ":t")
local text local text
if entry.text:sub(1, #module_name) ~= module_name then if entry.text:sub(1, #module_name) ~= module_name then
@@ -648,7 +648,7 @@ previewers.help = defaulter(function(_)
return entry.filename return entry.filename
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local query = entry.cmd local query = entry.cmd
query = query:sub(2) query = query:sub(2)
query = [[\V]] .. query query = [[\V]] .. query
@@ -675,7 +675,7 @@ previewers.man = defaulter(function(opts)
return entry.value .. "/" .. entry.section return entry.value .. "/" .. entry.section
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local win_width = vim.api.nvim_win_get_width(self.state.winid) local win_width = vim.api.nvim_win_get_width(self.state.winid)
putils.job_maker(vim.deepcopy(pager), self.state.bufnr, { putils.job_maker(vim.deepcopy(pager), self.state.bufnr, {
writer = { "man", entry.section, entry.value }, writer = { "man", entry.section, entry.value },
@@ -743,7 +743,7 @@ previewers.git_branch_log = defaulter(function(opts)
return entry.value return entry.value
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local cmd = { local cmd = {
"git", "git",
"--no-pager", "--no-pager",
@@ -800,7 +800,7 @@ previewers.git_commit_diff_to_parent = defaulter(function(opts)
return entry.value return entry.value
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local cmd = { "git", "--no-pager", "diff", entry.value .. "^!" } local cmd = { "git", "--no-pager", "diff", entry.value .. "^!" }
if opts.current_file then if opts.current_file then
table.insert(cmd, "--") table.insert(cmd, "--")
@@ -831,7 +831,7 @@ previewers.git_commit_diff_to_head = defaulter(function(opts)
return entry.value return entry.value
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local cmd = { "git", "--no-pager", "diff", "--cached", entry.value } local cmd = { "git", "--no-pager", "diff", "--cached", entry.value }
if opts.current_file then if opts.current_file then
table.insert(cmd, "--") table.insert(cmd, "--")
@@ -862,7 +862,7 @@ previewers.git_commit_diff_as_was = defaulter(function(opts)
return entry.value return entry.value
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local cmd = { "git", "--no-pager", "show" } local cmd = { "git", "--no-pager", "show" }
local cf = opts.current_file and Path:new(opts.current_file):make_relative(opts.cwd) 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 value = cf and (entry.value .. ":" .. cf) or entry.value
@@ -896,7 +896,7 @@ previewers.git_commit_message = defaulter(function(opts)
return entry.value return entry.value
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
local cmd = { "git", "--no-pager", "log", "-n 1", entry.value } local cmd = { "git", "--no-pager", "log", "-n 1", entry.value }
putils.job_maker(cmd, self.state.bufnr, { putils.job_maker(cmd, self.state.bufnr, {
@@ -926,7 +926,7 @@ previewers.git_file_diff = defaulter(function(opts)
return entry.value return entry.value
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
if entry.status and (entry.status == "??" or entry.status == "A ") then if entry.status and (entry.status == "??" or entry.status == "A ") then
local p = from_entry.path(entry, true) local p = from_entry.path(entry, true)
if p == nil or p == "" then if p == nil or p == "" then
@@ -1029,8 +1029,7 @@ previewers.highlights = defaulter(function(_)
return "highlights" return "highlights"
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
putils.with_preview_window(status, nil, function()
if not self.state.bufname then if not self.state.bufname then
local output = vim.split(vim.fn.execute "highlight", "\n") local output = vim.split(vim.fn.execute "highlight", "\n")
local hl_groups = {} local hl_groups = {}
@@ -1054,11 +1053,13 @@ previewers.highlights = defaulter(function(_)
end end
end end
pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1) vim.schedule(function()
vim.api.nvim_buf_call(self.state.bufnr, function()
vim.cmd "norm! gg" vim.cmd "norm! gg"
vim.fn.search(entry.value .. " ") vim.fn.search(entry.value .. " ")
local lnum = vim.fn.line "." local lnum = vim.api.nvim_win_get_cursor(self.state.winid)[1]
-- That one is actually a match but its better to use it like that then matchadd -- That one is actually a match but its better to use it like that then matchadd
pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1)
vim.api.nvim_buf_add_highlight( vim.api.nvim_buf_add_highlight(
self.state.bufnr, self.state.bufnr,
ns_previewer, ns_previewer,
@@ -1067,6 +1068,9 @@ previewers.highlights = defaulter(function(_)
0, 0,
#entry.value #entry.value
) )
-- we need to zz after the highlighting otherwise highlighting doesnt work
vim.cmd "norm! zz"
end)
end) end)
end, end,
} }
@@ -1100,10 +1104,10 @@ previewers.pickers = defaulter(function(_)
end end
end, end,
define_preview = function(self, entry, status) define_preview = function(self, entry)
putils.with_preview_window(status, nil, function() vim.api.nvim_buf_call(self.state.bufnr, function()
local ns_telescope_entry = vim.api.nvim_create_namespace "telescope_entry" local ns_telescope_entry = vim.api.nvim_create_namespace "telescope_entry"
local preview_height = vim.api.nvim_win_get_height(status.preview_win) local preview_height = vim.api.nvim_win_get_height(self.state.winid)
if self.state.bufname then if self.state.bufname then
return return
@@ -1158,12 +1162,12 @@ end, {})
previewers.display_content = defaulter(function(_) previewers.display_content = defaulter(function(_)
return previewers.new_buffer_previewer { return previewers.new_buffer_previewer {
define_preview = function(self, entry, status) define_preview = function(self, entry)
putils.with_preview_window(status, nil, function()
assert( assert(
type(entry.preview_command) == "function", type(entry.preview_command) == "function",
"entry must provide a preview_command function which will put the content into the buffer" "entry must provide a preview_command function which will put the content into the buffer"
) )
vim.api.nvim_buf_call(self.state.bufnr, function()
entry.preview_command(entry, self.state.bufnr) entry.preview_command(entry, self.state.bufnr)
end) end)
end, end,

View File

@@ -1,4 +1,3 @@
local context_manager = require "plenary.context_manager"
local ts_utils = require "telescope.utils" local ts_utils = require "telescope.utils"
local strings = require "plenary.strings" local strings = require "plenary.strings"
local conf = require("telescope.config").values local conf = require("telescope.config").values
@@ -53,18 +52,6 @@ utils.filetype_detect = function(filepath)
end 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)
else
return context_manager.with(function()
vim.cmd(string.format("noautocmd call nvim_set_current_win(%s)", status.preview_win))
coroutine.yield()
vim.cmd(string.format("noautocmd call nvim_set_current_win(%s)", status.prompt_win))
end, callable)
end
end
-- API helper functions for buffer previewer -- API helper functions for buffer previewer
--- Job maker for buffer previewer --- Job maker for buffer previewer
utils.job_maker = function(cmd, bufnr, opts) utils.job_maker = function(cmd, bufnr, opts)