Skip to content

docs/fix: Telescope integration #149

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,76 @@ require('leap').leap {
```
</details>

Leap can also be used by external plugins, for example
in `telescope.nvim` to allow for quick selection between
search results. The below config maps (normal-mode) `s` within a Telescope picker
to use Leap to select an entry and perform the default action (i.e. `<CR>`)
on it, and (normal-mode) `S` to only select the entry (so you can perform a different action ot it).

<details>
<summary>Example: select a Telescope entry</summary>

```lua
local telescope = require "telescope"
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"

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

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

-- restrict targets to visible range of entries
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

telescope.setup {
defaults = {
-- ....
mappings = {
n = {
-- set the current selected entry using Leap
["S"] = function (prompt_bufnr)
require('leap').leap {
targets = get_telescope_targets(prompt_bufnr),
action = function (target)
target.pick:set_selection(target.row)
end
}
end,
-- perform the default action (i.e. <CR>) on the entry selected using Leap
["s"] = function (prompt_bufnr)
require('leap').leap {
targets = get_telescope_targets(prompt_bufnr),
action = function (target)
target.pick:set_selection(target.row)
actions.select_default(prompt_bufnr)
end
}
end
}
}
},
}
```
</details>

(see also [telescope-hop.nvim](https://github.com/nvim-telescope/telescope-hop.nvim)).

### Accessing the arguments passed to `leap`

The arguments of the current call are always available at runtime, in the
Expand Down
2 changes: 1 addition & 1 deletion fnl/leap/highlight.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
(fn M.cleanup [self affected-windows]
; Clear beacons & cursor.
(each [_ [bufnr id] (ipairs self.extmarks)]
(api.nvim_buf_del_extmark bufnr self.ns id))
(when (api.nvim_buf_is_valid bufnr) (api.nvim_buf_del_extmark bufnr self.ns id)))
(set self.extmarks [])
; Clear backdrop.
(when (pcall api.nvim_get_hl_by_name self.group.backdrop false) ; group exists?
Expand Down
4 changes: 2 additions & 2 deletions fnl/leap/main.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,8 @@ is either labeled (C) or not (B).
(fn restore-editor-opts []
(each [key val (pairs state.saved_editor_opts)]
(case key
[:w w name] (api.nvim_win_set_option w name val)
[:b b name] (api.nvim_buf_set_option b name val)
[:w w name] (when (api.nvim_win_is_valid w) (api.nvim_win_set_option w name val))
[:b b name] (when (api.nvim_buf_is_valid b) (api.nvim_buf_set_option b name val))
name (api.nvim_set_option name val))))


Expand Down
77 changes: 40 additions & 37 deletions lua/leap/highlight.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions lua/leap/main.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.