telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

feat(previewer): show unwritten buffer previewers for some pickers

Open jamestrew opened this issue 1 year ago • 11 comments

Use actual buffer contents for buffer previewers for some pickers (buffers, treesitter, current_buffer_fuzzy_find, for starters). Not to be confused with reusing buffers for preview buffers.

This lets users view previews of buffers with unwritten changes. For treesitter and current_buffer_fuzzy_find, not showing actual buffer content is arguably a bug considering the finder operates over the buffer (written or not). So there are instances where you can search over unwritten content in the buffer only to see an outdated, written only content in the preview.

Closes https://github.com/nvim-telescope/telescope.nvim/issues/2481

jamestrew avatar Feb 25 '24 01:02 jamestrew

buffers preview needs it too.

przepompownia avatar Feb 25 '24 13:02 przepompownia

It includes it :grin:

some pickers (buffers,

jamestrew avatar Feb 25 '24 13:02 jamestrew

Maybe I need to verify it on minimal env first.

przepompownia avatar Feb 25 '24 14:02 przepompownia

image

with minimal init.lua:

local thisInitFile = debug.getinfo(1).source:match('@?(.*)')
local configDir = vim.fs.dirname(thisInitFile)

vim.env['XDG_CONFIG_HOME'] = configDir
vim.env['XDG_DATA_HOME'] = vim.fs.joinpath(configDir, '.xdg/data')
vim.env['XDG_STATE_HOME'] = vim.fs.joinpath(configDir, '.xdg/state')
vim.env['XDG_CACHE_HOME'] = vim.fs.joinpath(configDir, '.xdg/cache')
local stdPathConfig = vim.fn.stdpath('config')

vim.opt.runtimepath:prepend(stdPathConfig)
vim.opt.packpath:prepend(stdPathConfig)

local function gitClone(url, installPath, branch)
  if vim.fn.isdirectory(installPath) ~= 0 then
    return
  end

  local command = {'git', 'clone', '--', url, installPath}
  if branch then
    table.insert(command, 3, '--branch')
    table.insert(command, 4, branch)
  end
  local sysObj = vim.system(command, {}):wait()
  if sysObj.code ~= 0 then
    error(sysObj.stderr)
  end
  vim.notify(sysObj.stdout)
  vim.notify(sysObj.stderr, vim.log.levels.WARN)
end

local pluginsPath = 'plugins'
vim.fn.mkdir(pluginsPath, 'p')
pluginsPath = vim.uv.fs_realpath(pluginsPath)

local plugins = {
  ['plenary.nvim'] = {url = 'https://github.com/nvim-lua/plenary.nvim'},
  ['telescope.nvim'] = {url = 'https://github.com/nvim-telescope/telescope.nvim', branch = 'feat/unwritten-buffer-previewer'},
}

for name, repo in pairs(plugins) do
  local installPath = vim.fs.joinpath(pluginsPath, name)
  gitClone(repo.url, installPath, repo.branch)
  vim.opt.runtimepath:append(installPath)
end

require('telescope').setup {
  defaults = {
    preview = {},
    layout_strategy = 'horizontal',
    layout_config = {
      preview_cutoff = 1,
    },
    mappings = {
      i = {
        ['<A-/>'] = function (promptBufnr)
          return require 'telescope.actions.layout'.toggle_preview(promptBufnr)
        end,
        ['<Esc>'] = 'close',
      },
    },
  },
}

local function init()
  vim.api.nvim_buf_set_lines(0, 0, 0, true, {'changed'})
  vim.schedule(function ()
    vim.cmd.Telescope 'buffers'
  end)
end

vim.api.nvim_create_autocmd('UIEnter', {
  once = true,
  callback = init,
})

For comparison current_buffer_fuzzy_find shows the contents only in the result window (whether or not it makes sense in this case):

image

przepompownia avatar Feb 25 '24 14:02 przepompownia

i'd like to review this in the next couple of days, dont think i can make it today

Conni2461 avatar Feb 25 '24 14:02 Conni2461

Thanks for opening this PR anyway! I spent few hours for verify if it's possible at the user side before reporting an issue.

I have updated the minimal repro to reduce typing.

przepompownia avatar Feb 25 '24 14:02 przepompownia

Thanks for opening this PR anyway! I spent few hours for verify if it's possible at the user side before reporting an issue.

I have updated the minimal repro to reduce typing.

Ahh sorry it looks like it's an edge case with [No Name] buffers. Even on master branch, it should use the buffer contents but it looks like it's not working. This PR so far specifically covers non-[No Name] "normal buffers" (empty buftype) with valid filetypes. I'll see if I can patch up this [No Name] case in this PR as well when I get some more time later.

But in the mean time, I was able to verify this by modifying for minimal config here

local function init()
  vim.cmd("edit init.lua")  -- edit this file so it's not a no name buffer
  vim.api.nvim_buf_set_lines(0, -1, -1, true, {'changed'})
  vim.schedule(function ()
    vim.cmd.Telescope 'buffers'
  end)
end

jamestrew avatar Feb 25 '24 15:02 jamestrew

I did not mention (but it's visible on the screenshot) that I run the minimal nvim --clean -u init.lua f1.lua (where file f1.lua doesn't exist) so it's not [No Name] buffer there.

Notice that init.lua exists and try to :edit any nonexisting file.

Indeed, for init.lua it works for me (probably because the file exists).

image

image

przepompownia avatar Feb 25 '24 15:02 przepompownia

Now it works!

przepompownia avatar Feb 25 '24 18:02 przepompownia

@jamestrew could you update this branch, please?

przepompownia avatar Mar 22 '24 12:03 przepompownia