Skip to content

Commit

Permalink
Add a jump only function
Browse files Browse the repository at this point in the history
  • Loading branch information
desdic committed Jan 18, 2024
1 parent 40febb2 commit 9be4116
Showing 1 changed file with 83 additions and 13 deletions.
96 changes: 83 additions & 13 deletions lua/telescope/_extensions/agrolenslib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ agrolens._add_entries = function(
local root = trees[1]:root()

for _, capture_name in ipairs(capture_names) do
local iter_query = vim.treesitter.query.get(filetype, "agrolens." .. capture_name)
local iter_query =
vim.treesitter.query.get(filetype, "agrolens." .. capture_name)
if iter_query then
for _, matches, _ in iter_query:iter_matches(root, bufnr) do
local entry = agrolens._create_entry(
Expand Down Expand Up @@ -166,6 +167,48 @@ agrolens._add_entries = function(
return entries
end

agrolens._jump = function(opts)
local entries = {}
local ts = vim.treesitter
local capture_names = opts.queries
for _, bufnr in ipairs(opts.bufids) do
local filetype = vim.filetype.match({ buf = bufnr })
if filetype and filetype ~= "" then
local ok, tsparser = pcall(ts.get_parser, bufnr, filetype)
if ok and tsparser and type(tsparser) ~= "string" then
local trees = tsparser:parse()
local root = trees[1]:root()

for _, capture_name in ipairs(capture_names) do
local iter_query = vim.treesitter.query.get(
filetype,
"agrolens." .. capture_name
)
if iter_query then
for _, matches, _ in
iter_query:iter_matches(root, bufnr)
do
for i, _ in pairs(matches) do
local curr_capture_name = iter_query.captures[i]
local lnum, _, _, _ = matches[i]:range()
if curr_capture_name == "agrolens.scope" then
entries[lnum + 1] = true
end
end
end
end
end
end
end
end

local linenumbers = {}
for k, _ in pairs(entries) do
table.insert(linenumbers, k)
end
return linenumbers
end

agrolens._get_captures = function(opts)
local entries = {}

Expand Down Expand Up @@ -320,18 +363,45 @@ agrolens.run = function(opts)
opts.sorter = opts.sorter or conf.generic_sorter(opts)
opts = agrolens._get_buffers(opts)

pickers
.new(opts, {
prompt_title = "Search",
finder = agrolens.generate_new_finder(opts),
previewer = opts.previewer,
sorter = opts.sorter,
attach_mappings = function(_, map)
map("i", "<c-space>", actions.to_fuzzy_refine)
return true
end,
})
:find()
if opts.jump then
opts.buffers = ""
local finder = agrolens._jump(opts)

local curLine = vim.api.nvim_win_get_cursor(0)[1]

if opts.jump == "next" then
table.sort(finder)
for _, line in pairs(finder) do
if line > curLine then
vim.api.nvim_win_set_cursor(0, { line, 0 })
break
end
end
else
table.sort(finder, function(a, b)
return a > b
end)
for _, line in pairs(finder) do
if line < curLine then
vim.api.nvim_win_set_cursor(0, { line, 0 })
break
end
end
end
else
pickers
.new(opts, {
prompt_title = "Search",
finder = agrolens.generate_new_finder(opts),
previewer = opts.previewer,
sorter = opts.sorter,
attach_mappings = function(_, map)
map("i", "<c-space>", actions.to_fuzzy_refine)
return true
end,
})
:find()
end
end
end

Expand Down

0 comments on commit 9be4116

Please sign in to comment.