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

Display preview window whether there're items or not

Open kqvanity opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. I'm trying to develop a telescope extension. During development I faced couple of issues

  • I currently use the picker to display accompanying metadata/info that may/may not rely on the presence of items in the picker. Telescope's current behavior wouldn't display any data available at the previewer if the picker is devoid of items. I was wondering if there's a workaround
  • Sometimes I'd like to directly access the bufnr of the preview window right from the picker. My current workaround is declaring a top-level variable that gets updated at define_preview via self.state.bufnr then access from the picker, but I'm wondering if there's a direct way to access it!
local preview_bufnr = 0
local previewer = function(word, code)
    local words = {}
    return previewers.new_buffer_previewer {
        title = "Translation",
        setup = function(self)
           -- `self` at `setup` doesn't provide access to `self.state.bufnr` so can't prepopulate previewer with initial state
            words = grab_all(word, code)
            return {}
        end,
        define_preview = function(self, entry)
            preview_bufnr = self.state.bufnr
            vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, words )
        end,
    }
end
  • Can the preview render regular text as markdown?

  • setup function of previewers.new_buffer_previewer is stated -according to the docs- to return a table which should be access at self.state of define_preview but that's not the case. Only bufnr and win_id are available. I define an initial one-off value during preview initialization and would like to create it once & continually access later on at each define_preview invocation. My current workaround is a local variable at previewers.new_buffer_previewer wrapper function, which works but because I use define_preview to define initial state, the state keeps getting reset when scrolling up/down.

Current finder implementation

        finder = finders.new_table {
            results = words,
            entry_maker = function(entry)
                return {
                    value = entry,
                    display = entry.title,
                    ordinal = entry.title
                }
            end
        },

Additional context I'm using Telescope from master branch on Arch Linux. Checkhealth output


==============================================================================
telescope: require("telescope.health").check()

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

Checking external dependencies ~
- OK rg: found ripgrep 14.1.0
- WARNING fd: not found. Install [sharkdp/fd](https://github.com/sharkdp/fd) for extended capabilities

===== Installed extensions ===== ~

Telescope Extension: `ast_grep` ~
- OK ast-grep: found ast-grep 0.22.3

Telescope Extension: `helpgrep` ~
- No healthcheck provided

Telescope Extension: `notify` ~
- No healthcheck provided

Telescope Extension: `persisted` ~
- No healthcheck provided

Telescope Extension: `software-licenses` ~
- No healthcheck provided

kqvanity avatar Jul 28 '24 10:07 kqvanity

I currently use the picker to display accompanying metadata/info that may/may not rely on the presence of items in the picker. Telescope's current behavior wouldn't display any data available at the previewer if the picker is devoid of items. I was wondering if there's a workaround

I'm not aware of any great ways to do this. Maybe just always having a dummy entry for the metadata. But as the name suggests, the previewer is a thing for previewing results. It's not really designed at a blank window to rendering whatever information.

Sometimes I'd like to directly access the bufnr of the preview window right from the picker. My current workaround is declaring a top-level variable that gets updated at define_preview via self.state.bufnr then access from the picker, but I'm wondering if there's a direct way to access it!

You should be able to get the previewer bufnr from the picker, picker.preview_bufnr.

Can the preview render regular text as markdown?

Theoretically yes, but you'll have to do a lot your own leg work for the define_preview of new_buffer_previewer. You won't be able to leverage buffer_preview_maker since that either handles filetype detection or you can use the filetype_hook but that's mostly for blocking previewing certain filetypes.

setup function of previewers.new_buffer_previewer is stated -according to the docs- to return a table which should be access at self.state of define_preview but that's not the case. Only bufnr and win_id are available. I define an initial one-off value during preview initialization and would like to create it once & continually access later on at each define_preview invocation. My current workaround is a local variable at previewers.new_buffer_previewer wrapper function, which works but because I use define_preview to define initial state, the state keeps getting reset when scrolling up/down.

No you're right, that appears to be a bug. I will fix that immediately thanks.

jamestrew avatar Aug 14 '24 01:08 jamestrew

No you're right, that appears to be a bug. I will fix that immediately thanks.

https://github.com/nvim-telescope/telescope.nvim/pull/3253

jamestrew avatar Aug 14 '24 01:08 jamestrew

https://github.com/nvim-telescope/telescope.nvim/pull/3253

Thanks

You should be able to get the previewer bufnr from the picker, picker.preview_bufnr.

can you please provide more context. I'm trying to access the preview bufnr from within attach_mappings callback

kqvanity avatar Sep 14 '24 12:09 kqvanity

Something like this should work

require("telescope.builtin").find_files({
  attach_mappings = function(_, map)
    map("i", "<C-e>", function(prompt_bufnr)
      local state = require("telescope.state").get_status(prompt_bufnr)
      if state.picker.previewer then
        local winid = state.preview_win
        local bufnr = vim.api.nvim_win_get_buf(winid)
        print("preview bufnr:", bufnr)
      end
    end)
    return true
  end,
})

But also using an autocommand might be useful as well

vim.api.nvim_create_autocmd("User", {
  pattern = "TelescopePreviewerLoaded",
  callback = function(args)
    if args.data.filetype ~= "help" then
      vim.wo.number = true
    elseif args.data.bufname:match("*.csv") then
      vim.wo.wrap = false
    end
  end,
})

jamestrew avatar Sep 22 '24 18:09 jamestrew

Thank you!

I still don't what the autocommand excerpt for.

kqvanity avatar Sep 26 '24 14:09 kqvanity