feat: multi selection. Only integrates with send_selected_to_qflist (#551)
This will not yet work with select actions. More work is needed in that case. Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
local a = vim.api
|
||||
local log = require('telescope.log')
|
||||
|
||||
local highlights = {}
|
||||
|
||||
@@ -76,25 +77,30 @@ function Highlighter:hi_selection(row, caret)
|
||||
)
|
||||
end
|
||||
|
||||
function Highlighter:hi_multiselect(row, entry)
|
||||
function Highlighter:hi_multiselect(row, is_selected)
|
||||
local results_bufnr = assert(self.picker.results_bufnr, "Must have a results bufnr")
|
||||
|
||||
if self.picker.multi_select[entry] then
|
||||
if is_selected then
|
||||
vim.api.nvim_buf_add_highlight(
|
||||
results_bufnr,
|
||||
ns_telescope_multiselection,
|
||||
"TelescopeMultiSelection",
|
||||
row,
|
||||
0,
|
||||
-1
|
||||
results_bufnr, ns_telescope_multiselection, "TelescopeMultiSelection", row, 0, -1
|
||||
)
|
||||
else
|
||||
vim.api.nvim_buf_clear_namespace(
|
||||
results_bufnr,
|
||||
ns_telescope_multiselection,
|
||||
row,
|
||||
row + 1
|
||||
local existing_marks = vim.api.nvim_buf_get_extmarks(
|
||||
results_bufnr, ns_telescope_multiselection, {row, 0}, {row, -1}, {}
|
||||
)
|
||||
|
||||
-- This is still kind of weird to me, since it seems like I'm erasing stuff
|
||||
-- when i shouldn't... perhaps it's a bout the gravity of the extmark?
|
||||
if #existing_marks > 0 then
|
||||
log.trace("Clearning row: ", row)
|
||||
|
||||
vim.api.nvim_buf_clear_namespace(
|
||||
results_bufnr,
|
||||
ns_telescope_multiselection,
|
||||
row,
|
||||
row + 1
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
51
lua/telescope/pickers/multi.lua
Normal file
51
lua/telescope/pickers/multi.lua
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
local MultiSelect = {}
|
||||
MultiSelect.__index = MultiSelect
|
||||
|
||||
function MultiSelect:new()
|
||||
return setmetatable({
|
||||
_entries = {}
|
||||
}, MultiSelect)
|
||||
end
|
||||
|
||||
function MultiSelect:get()
|
||||
local marked_entries = {}
|
||||
for entry, count in pairs(self._entries) do
|
||||
table.insert(marked_entries, {count, entry})
|
||||
end
|
||||
|
||||
table.sort(marked_entries, function(left, right)
|
||||
return left[1] < right[1]
|
||||
end)
|
||||
|
||||
local selections = {}
|
||||
for _, entry in ipairs(marked_entries) do
|
||||
table.insert(selections, entry[2])
|
||||
end
|
||||
|
||||
return selections
|
||||
end
|
||||
|
||||
function MultiSelect:is_selected(entry)
|
||||
return self._entries[entry]
|
||||
end
|
||||
|
||||
local multi_select_count = 0
|
||||
function MultiSelect:add(entry)
|
||||
multi_select_count = multi_select_count + 1
|
||||
self._entries[entry] = multi_select_count
|
||||
end
|
||||
|
||||
function MultiSelect:drop(entry)
|
||||
self._entries[entry] = nil
|
||||
end
|
||||
|
||||
function MultiSelect:toggle(entry)
|
||||
if self:is_selected(entry) then
|
||||
self:drop(entry)
|
||||
else
|
||||
self:add(entry)
|
||||
end
|
||||
end
|
||||
|
||||
return MultiSelect
|
||||
@@ -99,4 +99,16 @@ scroller.bottom = function(sorting_strategy, max_results, num_results)
|
||||
return max_results - 1
|
||||
end
|
||||
|
||||
scroller.better = function(sorting_strategy)
|
||||
if sorting_strategy == 'ascending' then
|
||||
return -1
|
||||
else
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
scroller.worse = function(sorting_strategy)
|
||||
return -(scroller.better(sorting_strategy))
|
||||
end
|
||||
|
||||
return scroller
|
||||
|
||||
Reference in New Issue
Block a user