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

add action to tokenize input #47

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ telescope.setup {
i = {
["<C-k>"] = lga_actions.quote_prompt(),
["<C-i>"] = lga_actions.quote_prompt({ postfix = " --iglob " }),
["<C-a>"] = lga_actions.tokenize(),
-- freeze the current list and start a fuzzy search in the frozen list
["<C-space>"] = actions.to_fuzzy_refine,
},
Expand Down Expand Up @@ -192,6 +193,7 @@ This table provides some mapping ideas:
| `actions.quote_prompt()` | Quote prompt | `foo` → `"foo"` |
| `actions.quote_prompt({ postfix = ' --iglob ' })` | Quote prompt and add `--iglob` | `foo` → `"foo" --iglob ` |
| `actions.quote_prompt({ postfix = ' -t' })` | Quote prompt and add `-t` | `foo` → `"foo" -t` |
| `actions.tokenize()` | "Fuzzy" tokenized search split by single space | `two words` → `words.*two|two.*words` |


### Shortcut functions
Expand Down
3 changes: 2 additions & 1 deletion lua/telescope-live-grep-args/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
-- SPDX-License-Identifier: MIT

return {
quote_prompt = require("telescope-live-grep-args.actions.quote_prompt"),
quote_prompt = require("telescope-live-grep-args.actions.quote_prompt"),
tokenize = require("telescope-live-grep-args.actions.tokenize"),
}
74 changes: 74 additions & 0 deletions lua/telescope-live-grep-args/actions/tokenize.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
local action_state = require("telescope.actions.state")

local default_opts = {
quote_char = '"',
postfix = " ",
trim = true,
}

return function(opts)
opts = opts or {}
opts = vim.tbl_extend("force", default_opts, opts)

return function(prompt_bufnr)
local wrap, yield = coroutine.wrap, coroutine.yield

local function permgen(a, n)
n = n or #a
if n <= 1 then
yield(a)
else
for i = 1, n do
-- put i-th element as the last one
a[n], a[i] = a[i], a[n]
-- generate all permutations of the other elements
permgen(a, n - 1)
-- restore i-th element
a[n], a[i] = a[i], a[n]
end
end
end

local function permutations(a)
-- call with coroutine.wrap()
return wrap(function()
permgen(a)
end)
end

local function removeDuplicates(tbl)
local newTbl = {}
local seen = {}
for _, value in ipairs(tbl) do
if not seen[value] then
table.insert(newTbl, value)
seen[value] = true
end
end
return newTbl
end

local picker = action_state.get_current_picker(prompt_bufnr)
local prompt = picker:_get_prompt()
if opts.trim then
prompt = vim.trim(prompt)
end
local tokens = {}

-- Pretty sure nobody will use this as part of the prompt!
local unique_string = "<!token_separator!>"
prompt = prompt:gsub("%s%s+", unique_string)
for token in prompt:gmatch("%S+") do
token = token:gsub(unique_string, " ")
table.insert(tokens, token)
end

prompt = ""
for combo in permutations(removeDuplicates(tokens)) do
prompt = prompt .. "|" .. table.concat(combo, ".*")
end
prompt = prompt:sub(2)

picker:set_prompt(prompt)
end
end