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

Enable leap to work as a telescope item selector that triggers a pick #218

Closed
wants to merge 2 commits into from

Conversation

aaronlifton
Copy link
Contributor

I have the following telescope mapping, which enables me to pick a single row from a Telescope list via leap (s). Because the mapping picks a row in telescope, it triggers the closing of the telescope window which causes leap to not be able to find the win/buf. The error can be reproduced as follows, given the following mapping:

local function get_telescope_targets(prompt_bufnr)
  local pick = require("telescope.actions.state").get_current_picker(prompt_bufnr)
  local scroller = require("telescope.pickers.scroller")

  local wininfo = vim.fn.getwininfo(pick.results_win)

  local first =
    math.max(scroller.top(pick.sorting_strategy, pick.max_results, pick.manager:num_results()), wininfo[1].topline - 1)
  local last = wininfo[1].botline - 1

  local targets = {}
  for row = last, first, -1 do
    local target = {
      wininfo = wininfo[1],
      pos = { row + 1, 1 },
      row = row,
      pick = pick,
    }
    table.insert(targets, target)
  end
  return targets
end

-- under telescope mappings
{
  ["s"] = function(prompt_bufnr)
    local win = vim.api.nvim_get_current_win()

    require("leap").leap({
      targets = get_telescope_targets(prompt_bufnr),
      action = function(target)
        target.pick:set_selection(target.row)
        require("telescope.actions").select_default(prompt_bufnr)
      end,
    })
  end,
}

So in a telescope window, entering normal mode and pressing s will turn the list of telescope items into leap targets, and selecting one will pick that item.
However, since this closes the telescope window, there are 2 undefined win/buf errors inside of leap, since leap seems to not expect a leap target like a telescope item. So, I protected these statements with guard clauses, and now I'm able to leap to a telescope target without error. I felt this was appropriate because it enables further usages of leap.

Here is a video of this customization in my branch working without error:

leap.recording.mp4

@aaronlifton aaronlifton changed the title Enable leap to work as a telescope item selector Enable leap to work as a telescope item selector that triggers a pick Mar 30, 2024
@@ -21,14 +21,17 @@
(fn M.cleanup [self affected-windows]
; Clear beacons & cursor.
(each [_ [bufnr id] (ipairs self.extmarks)]
(api.nvim_buf_del_extmark bufnr self.ns id))
(let [is-valid-buf (api.nvim_buf_is_valid bufnr)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: use when if there is no else-branch; no need for extracting the result

    (when (api.nvim_buf_is_valid bufnr)
      (api.nvim_buf_del_extmark bufnr self.ns id)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this was the first time I used fennel, so I assumed there were some style mistakes! Working on these changes now

(set self.extmarks [])
; Clear backdrop.
(when (pcall api.nvim_get_hl_by_name self.group.backdrop false) ; group exists?
(each [_ winid (ipairs affected-windows)]
(local wininfo (. (vim.fn.getwininfo winid) 1))
(api.nvim_buf_clear_namespace
wininfo.bufnr self.ns (dec wininfo.topline) wininfo.botline))
(if (> (length wininfo) 0)
Copy link
Owner

@ggandor ggandor Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not simply:

    (each [_ winid (ipairs affected-windows)]
      (when (api.nvim_win_is_valid winid)
          ...)

Note: It just occurred to me that there is a potential problem with the backdrop cleanup. What if the window has become invalid, but the buffer is still there? We do not remove the backdrop then.
Anyway, this can be fixed in a followup commit, should be thought through.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, made the changes. That's a great point, perhaps there would be affected-bufs too.

[:w win name] (if (api.nvim_win_is_valid win)
(api.nvim_win_set_option win name val))
[:b buf name] (if (api.nvim_buf_is_valid buf)
(api.nvim_buf_set_option buf name val))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if -> when; indentation

@ggandor
Copy link
Owner

ggandor commented Apr 8, 2024

@aaronlifton :)

@ggandor ggandor closed this in 1ee1d30 May 29, 2024
ggandor added a commit that referenced this pull request May 29, 2024
Problem: The target window/buffer might be automatically closed right
after selecting the target (use case: e.g., results of a fuzzy finder),
and Leap trying to do cleanup there results in error.

Solution: Check if the window/buffer is still valid before cleanup.

Closes #218.

Co-authored-by: Aaron Lifton <aaronlifton@gmail.com>
@ggandor
Copy link
Owner

ggandor commented May 29, 2024

Sorry for the delay, just did some tweaks & added you as co-author, to speed things up. Thanks very much for the contribution!

The snippet above works fine, but there is a stack overflow error if triggering it before entering any input. Do you happen to have an idea how that could be fixed? (It might worth adding it to the "Extending Leap" section, but this edge case is a bit annoying.)

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.

None yet

2 participants