fix: stale lnum in buffer previewer (#1229)

* fix: lnum to jump to might be stale eg when resuming buffer picker after changes
This commit is contained in:
fdschmidt93
2021-09-10 13:21:04 +02:00
committed by GitHub
parent b4d6eb9a23
commit 1c35ea319e
2 changed files with 14 additions and 8 deletions

View File

@@ -452,6 +452,7 @@ function make_entry.gen_from_buffer(opts)
local readonly = vim.api.nvim_buf_get_option(entry.bufnr, "readonly") and "=" or " " local readonly = vim.api.nvim_buf_get_option(entry.bufnr, "readonly") and "=" or " "
local changed = entry.info.changed == 1 and "+" or " " local changed = entry.info.changed == 1 and "+" or " "
local indicator = entry.flag .. hidden .. readonly .. changed local indicator = entry.flag .. hidden .. readonly .. changed
local line_count = vim.api.nvim_buf_line_count(entry.bufnr)
return { return {
valid = true, valid = true,
@@ -462,8 +463,8 @@ function make_entry.gen_from_buffer(opts)
bufnr = entry.bufnr, bufnr = entry.bufnr,
filename = bufname, filename = bufname,
-- account for potentially stale lnum as getbufinfo might not be updated or from resuming buffers picker
lnum = entry.info.lnum ~= 0 and entry.info.lnum or 1, lnum = entry.info.lnum ~= 0 and math.max(math.min(entry.info.lnum, line_count), 1) or 1,
indicator = indicator, indicator = indicator,
} }
end end

View File

@@ -956,12 +956,13 @@ end, {})
previewers.buffers = defaulter(function(opts) previewers.buffers = defaulter(function(opts)
opts = opts or {} opts = opts or {}
local cwd = opts.cwd or vim.loop.cwd() local cwd = opts.cwd or vim.loop.cwd()
local previewer_active = true -- decouple provider from preview_win local previewer_active -- decouple provider from preview_win
return Previewer:new { return Previewer:new {
title = function() title = function()
return "Buffers" return "Buffers"
end, end,
setup = function(_, status) setup = function(_, status)
previewer_active = true
local win_id = status.picker.original_win_id local win_id = status.picker.original_win_id
-- required because of see `:h local-options` as -- required because of see `:h local-options` as
-- buffers not yet attached to a current window take the options from the `minimal` popup ... -- buffers not yet attached to a current window take the options from the `minimal` popup ...
@@ -1030,16 +1031,20 @@ previewers.buffers = defaulter(function(opts)
preview_fn = function(self, entry, status) preview_fn = function(self, entry, status)
if vim.api.nvim_buf_is_valid(entry.bufnr) then if vim.api.nvim_buf_is_valid(entry.bufnr) then
vim.api.nvim_win_set_buf(status.preview_win, entry.bufnr) vim.api.nvim_win_set_buf(status.preview_win, entry.bufnr)
self.state.bufnr = entry.bufnr
vim.api.nvim_win_set_option(status.preview_win, "winhl", "Normal:TelescopePreviewNormal") 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, "signcolumn", "no")
vim.api.nvim_win_set_option(status.preview_win, "foldlevel", 100) vim.api.nvim_win_set_option(status.preview_win, "foldlevel", 100)
vim.api.nvim_win_set_option(status.preview_win, "wrap", false) vim.api.nvim_win_set_option(status.preview_win, "wrap", false)
self.state.bufnr = entry.bufnr
if self.state.previewed_buffers[entry.bufnr] ~= true then
if entry.lnum then
local lnum, col = unpack(vim.api.nvim_win_get_cursor(status.preview_win))
entry.lnum = lnum
if not entry.col then if not entry.col then
local _, col = unpack(vim.api.nvim_win_get_cursor(status.preview_win))
entry.col = col + 1 entry.col = col + 1
end end
if self.state.previewed_buffers[entry.bufnr] ~= true then end
self.state.previewed_buffers[entry.bufnr] = true self.state.previewed_buffers[entry.bufnr] = true
end end
end end