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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jamestrew
Copy link
Contributor

@jamestrew jamestrew commented Feb 25, 2024

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 #2481

@przepompownia
Copy link

buffers preview needs it too.

@jamestrew
Copy link
Contributor Author

It includes it 😁

some pickers (buffers,

@przepompownia
Copy link

Maybe I need to verify it on minimal env first.

@przepompownia
Copy link

przepompownia commented Feb 25, 2024

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

@Conni2461 Conni2461 self-requested a review February 25, 2024 14:40
@Conni2461
Copy link
Member

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

@przepompownia
Copy link

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.

@jamestrew
Copy link
Contributor Author

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

@przepompownia
Copy link

przepompownia commented Feb 25, 2024

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
Copy link

Now it works!

@przepompownia
Copy link

@jamestrew could you update this branch, please?

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Buffer 'live' preview
3 participants