Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(previewer): remove with_preview_window #2563

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 58 additions & 54 deletions lua/telescope/previewers/buffer_previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ previewers.cat = defaulter(function(opts)
return from_entry.path(entry, false)
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
local p = from_entry.path(entry, true)
if p == nil or p == "" then
return
Expand Down Expand Up @@ -518,7 +518,7 @@ previewers.vimgrep = defaulter(function(opts)
return from_entry.path(entry, false)
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
local has_buftype = entry.bufnr and vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "" or false
local p
Expand Down Expand Up @@ -596,7 +596,7 @@ previewers.ctags = defaulter(function(_)
return entry.filename
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
conf.buffer_previewer_maker(entry.filename, self.state.bufnr, {
bufname = self.state.bufname,
winid = self.state.winid,
Expand All @@ -619,7 +619,7 @@ previewers.builtin = defaulter(function(_)
return entry.filename
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 text
if entry.text:sub(1, #module_name) ~= module_name then
Expand Down Expand Up @@ -648,7 +648,7 @@ previewers.help = defaulter(function(_)
return entry.filename
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
local query = entry.cmd
query = query:sub(2)
query = [[\V]] .. query
Expand All @@ -675,7 +675,7 @@ previewers.man = defaulter(function(opts)
return entry.value .. "/" .. entry.section
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
local win_width = vim.api.nvim_win_get_width(self.state.winid)
putils.job_maker(vim.deepcopy(pager), self.state.bufnr, {
writer = { "man", entry.section, entry.value },
Expand Down Expand Up @@ -743,7 +743,7 @@ previewers.git_branch_log = defaulter(function(opts)
return entry.value
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
local cmd = {
"git",
"--no-pager",
Expand Down Expand Up @@ -800,7 +800,7 @@ previewers.git_commit_diff_to_parent = defaulter(function(opts)
return entry.value
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
local cmd = { "git", "--no-pager", "diff", entry.value .. "^!" }
if opts.current_file then
table.insert(cmd, "--")
Expand Down Expand Up @@ -831,7 +831,7 @@ previewers.git_commit_diff_to_head = defaulter(function(opts)
return entry.value
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
local cmd = { "git", "--no-pager", "diff", "--cached", entry.value }
if opts.current_file then
table.insert(cmd, "--")
Expand Down Expand Up @@ -862,7 +862,7 @@ previewers.git_commit_diff_as_was = defaulter(function(opts)
return entry.value
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
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
Expand Down Expand Up @@ -896,7 +896,7 @@ previewers.git_commit_message = defaulter(function(opts)
return entry.value
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
local cmd = { "git", "--no-pager", "log", "-n 1", entry.value }

putils.job_maker(cmd, self.state.bufnr, {
Expand Down Expand Up @@ -926,7 +926,7 @@ previewers.git_file_diff = defaulter(function(opts)
return entry.value
end,

define_preview = function(self, entry, status)
define_preview = function(self, entry)
if entry.status and (entry.status == "??" or entry.status == "A ") then
local p = from_entry.path(entry, true)
if p == nil or p == "" then
Expand Down Expand Up @@ -1029,44 +1029,48 @@ previewers.highlights = defaulter(function(_)
return "highlights"
end,

define_preview = function(self, entry, status)
putils.with_preview_window(status, nil, function()
if not self.state.bufname then
local output = vim.split(vim.fn.execute "highlight", "\n")
local hl_groups = {}
for _, v in ipairs(output) do
if v ~= "" then
if v:sub(1, 1) == " " then
local part_of_old = v:match "%s+(.*)"
hl_groups[#hl_groups] = hl_groups[#hl_groups] .. part_of_old
else
table.insert(hl_groups, v)
end
define_preview = function(self, entry)
if not self.state.bufname then
local output = vim.split(vim.fn.execute "highlight", "\n")
local hl_groups = {}
for _, v in ipairs(output) do
if v ~= "" then
if v:sub(1, 1) == " " then
local part_of_old = v:match "%s+(.*)"
hl_groups[#hl_groups] = hl_groups[#hl_groups] .. part_of_old
else
table.insert(hl_groups, v)
end
end
end

vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, hl_groups)
for k, v in ipairs(hl_groups) do
local startPos = string.find(v, "xxx", 1, true) - 1
local endPos = startPos + 3
local hlgroup = string.match(v, "([^ ]*)%s+.*")
pcall(vim.api.nvim_buf_add_highlight, self.state.bufnr, 0, hlgroup, k - 1, startPos, endPos)
end
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, hl_groups)
for k, v in ipairs(hl_groups) do
local startPos = string.find(v, "xxx", 1, true) - 1
local endPos = startPos + 3
local hlgroup = string.match(v, "([^ ]*)%s+.*")
pcall(vim.api.nvim_buf_add_highlight, self.state.bufnr, 0, hlgroup, k - 1, startPos, endPos)
end
end

pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1)
vim.cmd "norm! gg"
vim.fn.search(entry.value .. " ")
local lnum = vim.fn.line "."
-- That one is actually a match but its better to use it like that then matchadd
vim.api.nvim_buf_add_highlight(
self.state.bufnr,
ns_previewer,
"TelescopePreviewMatch",
lnum - 1,
0,
#entry.value
)
vim.schedule(function()
vim.api.nvim_buf_call(self.state.bufnr, function()
vim.cmd "norm! gg"
vim.fn.search(entry.value .. " ")
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
pcall(vim.api.nvim_buf_clear_namespace, self.state.bufnr, ns_previewer, 0, -1)
vim.api.nvim_buf_add_highlight(
self.state.bufnr,
ns_previewer,
"TelescopePreviewMatch",
lnum - 1,
0,
#entry.value
)
-- we need to zz after the highlighting otherwise highlighting doesnt work
vim.cmd "norm! zz"
end)
end)
end,
}
Expand Down Expand Up @@ -1100,10 +1104,10 @@ previewers.pickers = defaulter(function(_)
end
end,

define_preview = function(self, entry, status)
putils.with_preview_window(status, nil, function()
define_preview = function(self, entry)
vim.api.nvim_buf_call(self.state.bufnr, function()
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
return
Expand Down Expand Up @@ -1158,12 +1162,12 @@ end, {})

previewers.display_content = defaulter(function(_)
return previewers.new_buffer_previewer {
define_preview = function(self, entry, status)
putils.with_preview_window(status, nil, function()
assert(
type(entry.preview_command) == "function",
"entry must provide a preview_command function which will put the content into the buffer"
)
define_preview = function(self, entry)
assert(
type(entry.preview_command) == "function",
"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)
end)
end,
Expand Down
13 changes: 0 additions & 13 deletions lua/telescope/previewers/utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local context_manager = require "plenary.context_manager"
local ts_utils = require "telescope.utils"
local strings = require "plenary.strings"
local conf = require("telescope.config").values
Expand Down Expand Up @@ -53,18 +52,6 @@ utils.filetype_detect = function(filepath)
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
--- Job maker for buffer previewer
utils.job_maker = function(cmd, bufnr, opts)
Expand Down