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

fix: (terminal) buffer previewer #1120

Merged
merged 13 commits into from
Aug 23, 2021
Merged
5 changes: 5 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,11 @@ end
actions.delete_buffer = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:delete_selection(function(selection)
-- avoid preview win from closing by creating tmp buffer
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
local preview_win = state.get_status(prompt_bufnr).preview_win
vim.api.nvim_win_set_buf(preview_win, buf)
vim.api.nvim_buf_delete(selection.bufnr, { force = true })
end)
end
Expand Down
25 changes: 23 additions & 2 deletions lua/telescope/builtin/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,17 @@ internal.reloader = function(opts)
end

internal.buffers = function(opts)
-- TODO upstream issue? if chosen buffer was not yet attached to window, minimal style of previewer is used
local winopts = {
["number"] = vim.api.nvim_win_get_option(0, "number"),
["relativenumber"] = vim.api.nvim_win_get_option(0, "relativenumber"),
["cursorline"] = vim.api.nvim_win_get_option(0, "cursorline"),
["signcolumn"] = vim.api.nvim_win_get_option(0, "signcolumn"),
["spell"] = vim.api.nvim_win_get_option(0, "spell"),
["wrap"] = vim.api.nvim_win_get_option(0, "wrap"),
["list"] = vim.api.nvim_win_get_option(0, "list"),
["colorcolumn"] = vim.api.nvim_win_get_option(0, "colorcolumn"),
}
local bufnrs = filter(function(b)
if 1 ~= vim.fn.buflisted(b) then
return false
Expand Down Expand Up @@ -637,9 +648,20 @@ internal.buffers = function(opts)
results = buffers,
entry_maker = opts.entry_maker or make_entry.gen_from_buffer(opts),
},
previewer = conf.grep_previewer(opts),
previewer = previewers.buffers.new(opts),
sorter = conf.generic_sorter(opts),
default_selection_index = default_selection_idx,
attach_mappings = function(_, _)
action_set.select:enhance {
post = function()
local entry = action_state.get_selected_entry()
for opt, value in pairs(winopts) do
vim.api.nvim_win_set_option(0, opt, value)
end
vim.api.nvim_win_set_cursor(0, { entry.lnum, entry.col })
fdschmidt93 marked this conversation as resolved.
Show resolved Hide resolved
end,
}
end,
}):find()
end

Expand Down Expand Up @@ -826,7 +848,6 @@ internal.keymaps = function(opts)
end,
}):find()
end

fdschmidt93 marked this conversation as resolved.
Show resolved Hide resolved
internal.filetypes = function(opts)
local filetypes = vim.fn.getcompletion("", "filetype")

Expand Down
76 changes: 51 additions & 25 deletions lua/telescope/previewers/buffer_previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ local search_teardown = function(self)
end
end

local scroll_fn = function(self, direction)
if not self.state then
return
end

local input = direction > 0 and [[]] or [[]]
local count = math.abs(direction)

vim.api.nvim_win_call(self.state.winid, function()
vim.cmd([[normal! ]] .. count .. input)
end)
end

previewers.file_maker = function(filepath, bufnr, opts)
opts = opts or {}
if opts.use_ft_detect == nil then
Expand Down Expand Up @@ -282,18 +295,7 @@ previewers.new_buffer_previewer = function(opts)
end

if not opts.scroll_fn then
function opts.scroll_fn(self, direction)
if not self.state then
return
end

local input = direction > 0 and [[]] or [[]]
local count = math.abs(direction)

vim.api.nvim_buf_call(self.state.bufnr, function()
vim.cmd([[normal! ]] .. count .. input)
end)
end
opts.scroll_fn = scroll_fn
end

return Previewer:new(opts)
Expand Down Expand Up @@ -370,19 +372,12 @@ previewers.vimgrep = defaulter(function(opts)
pcall(vim.api.nvim_buf_clear_namespace, self.state.last_set_bufnr, ns_previewer, 0, -1)
end

-- Workaround for unnamed buffer when using builtin.buffer
if entry.bufnr and (p == "[No Name]" or vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "") then
local lines = vim.api.nvim_buf_get_lines(entry.bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
jump_to_line(self, self.state.bufnr, entry.lnum)
else
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname,
callback = function(bufnr)
jump_to_line(self, bufnr, entry.lnum)
end,
})
end
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname,
callback = function(bufnr)
jump_to_line(self, bufnr, entry.lnum)
end,
})
end,
}
end, {})
Expand Down Expand Up @@ -872,4 +867,35 @@ previewers.display_content = defaulter(function(_)
}
end, {})

previewers.buffers = defaulter(function(opts)
opts = opts or {}
local cwd = opts.cwd or vim.loop.cwd()
return Previewer:new {
title = function()
return "Buffers"
end,
setup = function(_, status)
return { ["winid"] = status.preview_win }
end,
fdschmidt93 marked this conversation as resolved.
Show resolved Hide resolved
dyn_title = function(_, entry)
return Path:new(from_entry.path(entry, true)):normalize(cwd)
end,
preview_fn = function(self, entry, status)
if vim.api.nvim_buf_is_valid(entry.bufnr) then
vim.api.nvim_win_set_buf(status.preview_win, entry.bufnr)
fdschmidt93 marked this conversation as resolved.
Show resolved Hide resolved
vim.api.nvim_win_set_option(status.preview_win, "winhl", "Normal:TelescopePreviewNormal")
vim.api.nvim_win_set_option(status.preview_win, "signcolumn", "no")
vim.api.nvim_win_set_option(status.preview_win, "foldlevel", 100)
vim.api.nvim_win_set_option(status.preview_win, "wrap", false)
self.state.bufnr = entry.bufnr
if not entry.col then
local _, col = unpack(vim.api.nvim_win_get_cursor(status.preview_win))
fdschmidt93 marked this conversation as resolved.
Show resolved Hide resolved
entry.col = col + 1
end
end
end,
scroll_fn = scroll_fn,
}
end, {})

return previewers
1 change: 1 addition & 0 deletions lua/telescope/previewers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ previewers.help = buffer_previewer.help
previewers.man = buffer_previewer.man
previewers.autocommands = buffer_previewer.autocommands
previewers.highlights = buffer_previewer.highlights
previewers.buffers = buffer_previewer.buffers

--- A deprecated way of displaying content more easily. Was written at a time,
--- where the buffer_previewer interface wasn't present. Nowadays it's easier
Expand Down
2 changes: 1 addition & 1 deletion lua/telescope/previewers/previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Previewer:preview(entry, status)

if not self.state then
if self._setup_func then
self.state = self:_setup_func()
self.state = self:_setup_func(status)
else
self.state = {}
end
Expand Down
3 changes: 0 additions & 3 deletions lua/telescope/previewers/term_previewer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,6 @@ previewers.vimgrep = defaulter(function(opts)
if p == nil or p == "" then
return
end
if entry.bufnr and (p == "[No Name]" or vim.api.nvim_buf_get_option(entry.bufnr, "buftype") ~= "") then
return
end

local lnum = entry.lnum or 0

Expand Down