Skip to content

Commit

Permalink
Merge pull request #36 from desdic/dev
Browse files Browse the repository at this point in the history
feature: Add a jump only function
  • Loading branch information
desdic committed Jan 19, 2024
2 parents 40febb2 + a1373e8 commit 7fd005b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 20 deletions.
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ require("telescope").extensions = {

| Parameter | Value(s) | Description |
| :-- | :--| :---------- |
| aliases | empty | Create aliases for longer lists of queries |
| query | functions,callings,comments,labels | A comma seperated list with queries you want to run |
| buffers | all | Run queries on all buffers |
| disable_indentation | true/false(default) | Strips spaces from line when showing in telescope |
| include_hidden_buffers | true/false(default) | when all buffers are selected only the visible are shown unless `includehiddenbuffers` is true |
| match | name and string | Matches a variable (minus the agrolens namespace) from the query with a string. If no string is provided its the word where the cursor is |
| same_type | true(default)/false | default we only match on same filetype across buffers but you can run queries on all if you like |
| aliases | empty | Create aliases for longer lists of queries. |
| query | functions,callings,comments,labels | A comma seperated list with queries you want to run. |
| buffers | all | Run queries on all buffers. |
| disable_indentation | true/false(default) | Strips spaces from line when showing in telescope. |
| include_hidden_buffers | true/false(default) | when all buffers are selected only the visible are shown unless `includehiddenbuffers` is true. |
| match | name and string | Matches a variable (minus the agrolens namespace) from the query with a string. If no string is provided its the word where the cursor is. |
| same_type | true(default)/false | default we only match on same filetype across buffers but you can run queries on all if you like. |
| jump | next/prev | Jump to the next or previous match of `agrolens.scope` based in query input. Only works on current buffer. |


Examples
Expand Down Expand Up @@ -103,6 +104,28 @@ Same query as above but `agrolens.name` must by either main or myfunc
:Telescope agrolens query=functions buffers=all match=name=main,name=myfunc
```

Jump to next match in query `work`

```
:Telescope agrolens query=work jump=next
```

## Aliases

Using aliases its possible to create a new query name where its a list of other queries like

```
agrolens = {
...
aliases = {
yamllist = "docker-compose,github-workflow-steps",
work = "cheflxchost,github-workflow-steps,pytest",
},
},
```

Using the query `yamllist` makes a combination of docker-compose, github-workflow-steps queries.

## Json/Yaml/etc

Some file formats just doesn't fit into the category labels so custom ones have been made for different formats for json/yaml. See [SUPPORTED](SUPPORTED.md)
Expand Down
103 changes: 90 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,71 @@ agrolens._add_entries = function(
return entries
end

agrolens.jump_next = function(curline, jumplist)
table.sort(jumplist)
for _, line in pairs(jumplist) do
if line > curline then
vim.api.nvim_win_set_cursor(0, { line, 0 })
break
end
end
end

agrolens.jump_prev = function(curline, jumplist)
table.sort(jumplist, function(a, b)
return a > b
end)
for _, line in pairs(jumplist) do
if line < curline then
vim.api.nvim_win_set_cursor(0, { line, 0 })
break
end
end
end

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

agrolens._generate_jump_list = function(opts)
local entries = {}
local ts = vim.treesitter
local capture_names = opts.queries
local bufnr = 0
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

return hash_keys_to_list(entries)
end

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

Expand Down Expand Up @@ -320,18 +386,29 @@ 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
local jumplist = agrolens._generate_jump_list(opts)
local curline = vim.api.nvim_win_get_cursor(0)[1]

if opts.jump == "next" then
agrolens.jump_next(curline, jumplist)
elseif opts.jump == "prev" then
agrolens.jump_prev(curline, jumplist)
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 7fd005b

Please sign in to comment.