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

feat: refine results action #1115

Merged
merged 1 commit into from
Jul 1, 2022
Merged
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
56 changes: 56 additions & 0 deletions lua/telescope/actions/generate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
---@brief ]]

local actions = require "telescope.actions"
local config = require "telescope.config"
local action_state = require "telescope.actions.state"
local finders = require "telescope.finders"

local action_generate = {}

--- Display the keymaps of registered actions similar to which-key.nvim.<br>
Expand Down Expand Up @@ -54,4 +58,56 @@ action_generate.which_key = function(opts)
end
end

action_generate.refine = function(prompt_bufnr, opts)
opts = opts or {}
opts.prompt_to_prefix = vim.F.if_nil(opts.prompt_to_prefix, false)
opts.prefix_hl_group = vim.F.if_nil(opts.prompt_hl_group, "TelescopePromptPrefix")
opts.prompt_prefix = vim.F.if_nil(opts.promt_prefix, config.values.prompt_prefix)
opts.reset_multi_selection = vim.F.if_nil(opts.reset_multi_selection, false)
opts.reset_prompt = vim.F.if_nil(opts.reset_prompt, true)
opts.sorter = vim.F.if_nil(opts.sorter, config.values.generic_sorter {})

local current_picker = action_state.get_current_picker(prompt_bufnr)

-- title
if opts.prompt_title then
current_picker.prompt_border:change_title(opts.prompt_title)
end

if opts.results_title then
current_picker.results_border:change_title(opts.results_title)
end

local results = {}
for entry in current_picker.manager:iter() do
table.insert(results, entry)
end

-- if opts.sorter == false, keep older sorter
if opts.sorter then
current_picker.sorter:_destroy()
current_picker.sorter = opts.sorter
current_picker.sorter:_init()
end

local new_finder = finders.new_table {
results = results,
entry_maker = function(x)
return x
end,
}

if not opts.reset_multi_selection and action_state.get_current_line() ~= "" then
opts.multi = current_picker._multi
end

if opts.prompt_to_prefix then
local prompt = action_state.get_current_line()
local current_prefix = current_picker.prompt_prefix
local suffix = current_prefix ~= opts.prompt_prefix and current_prefix or ""
opts.new_prefix = suffix .. prompt .. " " .. opts.prompt_prefix
end
current_picker:refresh(new_finder, opts)
end

return action_generate
10 changes: 10 additions & 0 deletions lua/telescope/builtin/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ files.live_grep = function(opts)
-- TODO: It would be cool to use `--json` output for this
-- and then we could get the highlight positions directly.
sorter = sorters.highlighter_only(opts),
attach_mappings = function(_, map)
map("i", "<c-space>", function(prompt_bufnr)
local line = action_state.get_current_line()
require("telescope.actions.generate").refine(prompt_bufnr, {
prompt_title = "Find Word (" .. line .. ")",
sorter = conf.generic_sorter(opts),
})
end)
return true
end,
}):find()
end

Expand Down
15 changes: 13 additions & 2 deletions lua/telescope/builtin/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local channel = require("plenary.async.control").channel

local action_state = require "telescope.actions.state"
local sorters = require "telescope.sorters"
local conf = require("telescope.config").values
local finders = require "telescope.finders"
local make_entry = require "telescope.make_entry"
Expand Down Expand Up @@ -315,7 +316,17 @@ lsp.dynamic_workspace_symbols = function(opts)
fn = get_workspace_symbols_requester(opts.bufnr, opts),
},
previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter(opts),
sorter = sorters.highlighter_only(opts),
attach_mappings = function(_, map)
map("i", "<c-space>", function(prompt_bufnr)
local line = action_state.get_current_line()
require("telescope.actions.generate").refine(prompt_bufnr, {
prompt_title = "LSP Workspace Symbols (" .. line .. ")",
sorter = conf.generic_sorter(opts),
})
end)
return true
end,
}):find()
end

Expand Down
1 change: 1 addition & 0 deletions lua/telescope/finders/async_oneshot_finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ return function(opts)
end
end,
results = results,
entry_maker = entry_maker,
}, {
__call = function(_, prompt, process_result, process_complete)
if not job_started then
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/finders/async_static_finder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ return function(opts)

return setmetatable({
results = results,
entry_maker = entry_maker,
close = function() end,
}, {
__call = function(_, _, process_result, process_complete)
Expand Down
2 changes: 1 addition & 1 deletion lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ function Picker:refresh(finder, opts)
local handle = type(opts.new_prefix) == "table" and unpack or function(x)
return x
end
self:change_prompt_prefix(handle(opts.new_prefix))
self:change_prompt_prefix(handle(opts.new_prefix), opts.prefix_hl_group)
end

if finder then
Expand Down