nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.43k stars 824 forks source link

Display preview window whether there're items or not #3228

Closed kqvanity closed 6 days ago

kqvanity commented 1 month ago

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

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

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
jamestrew commented 1 month ago

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 commented 1 month ago

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

kqvanity commented 2 days ago

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