RishabhRD / popfix

Neovim lua API for highly extensible popup window
83 stars 3 forks source link

Question about buffer preview mode #12

Closed madskjeldgaard closed 3 years ago

madskjeldgaard commented 3 years ago

Hi ! Thanks for making all this cool work! I was wondering how to use the buffer preview mode in the select callback. The readme is a bit sparse on this subject. Do you have any examples of this? Specifically I am confused by what the buffer number returned is and how it's supposed to be used. Is the selected item supposed to be shown in the bufnr variable here ? The readme also mentions that it is a preexisting buffer which confuses me even more. Would be very thankful for some clarification. Thanks a lot!

RishabhRD commented 3 years ago

Hi @madskjeldgaard . Sad the readme was not good on this topic. In the plugin, users need to register a select callback that is automatically called when list selection changes. As the list selection changes, a new preview should be displayed in the preview window. Now, the responsibility of select callback is to do appropriate action(based on plugin) and also return the value to preview. Plugin makers can decide new preview on basis of parameters to select callback(selected index and the selected line). Now, for the buffer preview, the select callback should return a table containing 2 fields bufnr and line. Now, bufnr is the buffer number that should be displayed on the preview window. Popfix doesn't take the responsibility of creating buffers, because vim buffers are not only limited to files and thus can be used for many things like terminal buffer preview. Thus, it's the responsibility of the plugin utilizing popfix to create a buffer (or use existing buffers) and provide bufnr to select callback. Another parameter is the line that represents the line number that should be highlighted in the preview window. If the line parameter for the returned table is nil, no line is highlighted.

RishabhRD commented 3 years ago

For a code snippet sample, please take a look at my nvim-finder plugin. In the plugin file previewer.lua, there is a M.new_buffer_preview() function that basically returns a select callback for another plugin function so that it can be registered with popfix.

    return function(_, line)
    if line == nil then return {} end
    local buf = getBufferFromLine(line)
    return {
        bufnr = buf,
        line = tonumber(getLineNumber(line))
    }
    end

This is the code snippet from that function only. It is basically used to preview buffers that are currently opened in vim (for fuzzy finding all buffers). On the basis of the currently selected line, it provides the bufnr to preview.

Any selected line looks like this:

[bufnr] buffer-name

In this example, buffer pre-existed and was easy to obtain. Plugins can create buffers dynamically if needed and then return it to select callback.

RishabhRD commented 3 years ago

@madskjeldgaard please don't hesitate to ask any question if my current explanation is not clear. My native language is not English , so I may not be much expressive in my statements. Your questions are definitely helping me to improve README.md :smile:

madskjeldgaard commented 3 years ago

For a code snippet sample, please take a look at my nvim-finder plugin. In the plugin file previewer.lua, there is a M.new_buffer_preview() function that basically returns a select callback for another plugin function so that it can be registered with popfix.

    return function(_, line)
  if line == nil then return {} end
  local buf = getBufferFromLine(line)
  return {
      bufnr = buf,
      line = tonumber(getLineNumber(line))
  }
    end

This is the code snippet from that function only. It is basically used to preview buffers that are currently opened in vim (for fuzzy finding all buffers). On the basis of the currently selected line, it provides the bufnr to preview.

Any selected line looks like this:

[bufnr] buffer-name

In this example, buffer pre-existed and was easy to obtain. Plugins can create buffers dynamically if needed and then return it to select callback.

Ah. This is nice. Thanks!

madskjeldgaard commented 3 years ago

This solves my confusion (which was mainly my own!). In my usecase I think the text preview mode is a better match. Thanks!