Skip to content

Commit 9eb8758

Browse files
committed
Allow to de-selected selected items in pick_many prompt
1 parent 71a7193 commit 9eb8758

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

lua/jdtls/ui.lua

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ function M.pick_one(items, prompt, label_fn)
2626
end
2727

2828

29+
local function index_of(xs, term)
30+
for i, x in pairs(xs) do
31+
if x == term then
32+
return i
33+
end
34+
end
35+
return -1
36+
end
37+
38+
2939
function M.pick_many(items, prompt, label_f)
3040
if not items or #items == 0 then
3141
return {}
@@ -36,11 +46,11 @@ function M.pick_many(items, prompt, label_f)
3646
end
3747

3848
local choices = {}
49+
local selected = {}
3950
for i, item in pairs(items) do
4051
table.insert(choices, string.format("%d. %s", i, label_f(item)))
4152
end
4253

43-
local selected = {}
4454
while true do
4555
local answer = vim.fn.input(string.format("\n%s\n%s (Esc to finish): ", table.concat(choices, "\n"), prompt))
4656
if answer == "" then
@@ -49,9 +59,15 @@ function M.pick_many(items, prompt, label_f)
4959

5060
local index = tonumber(answer)
5161
if index ~= nil then
52-
if string.find(choices[index], "*") == nil then
53-
table.insert(selected, items[index])
54-
choices[index] = choices[index] .. " *"
62+
local choice = choices[index]
63+
local item = items[index]
64+
if string.find(choice, "*") == nil then
65+
table.insert(selected, item)
66+
choices[index] = choice .. " *"
67+
else
68+
choices[index] = string.gsub(choice, " %*$", "")
69+
local idx = index_of(selected, item)
70+
table.remove(selected, idx)
5571
end
5672
end
5773
end

0 commit comments

Comments
 (0)