@@ -6,34 +6,46 @@ local default_opts = {
66 trim = true ,
77}
88
9-
109return function (opts )
1110 opts = opts or {}
1211 opts = vim .tbl_extend (" force" , default_opts , opts )
1312
1413 return function (prompt_bufnr )
15-
1614 local wrap , yield = coroutine.wrap , coroutine.yield
1715
1816 local function permgen (a , n )
19- n = n or # a
20- if n <= 1 then
21- yield (a )
22- else
23- for i = 1 , n do
24- -- put i-th element as the last one
25- a [n ], a [i ] = a [i ], a [n ]
26- -- generate all permutations of the other elements
27- permgen (a , n - 1 )
28- -- restore i-th element
29- a [n ], a [i ] = a [i ], a [n ]
30- end
17+ n = n or # a
18+ if n <= 1 then
19+ yield (a )
20+ else
21+ for i = 1 , n do
22+ -- put i-th element as the last one
23+ a [n ], a [i ] = a [i ], a [n ]
24+ -- generate all permutations of the other elements
25+ permgen (a , n - 1 )
26+ -- restore i-th element
27+ a [n ], a [i ] = a [i ], a [n ]
3128 end
29+ end
3230 end
3331
3432 function permutations (a )
35- -- call with coroutine.wrap()
36- return wrap (function () permgen (a ) end )
33+ -- call with coroutine.wrap()
34+ return wrap (function ()
35+ permgen (a )
36+ end )
37+ end
38+
39+ function removeDuplicates (tbl )
40+ local newTbl = {}
41+ local seen = {}
42+ for _ , value in ipairs (tbl ) do
43+ if not seen [value ] then
44+ table.insert (newTbl , value )
45+ seen [value ] = true
46+ end
47+ end
48+ return newTbl
3749 end
3850
3951 local picker = action_state .get_current_picker (prompt_bufnr )
@@ -48,11 +60,8 @@ return function(opts)
4860 end
4961
5062 prompt = " "
51- -- TODO Remove duplicate permutations
52- -- a a b → a.*b.*a|b.*a.*a|b.*a.*a|a.*b.*a|a.*a.*b|a.*b.*a it should be:
53- -- a a b → a.*b.*a|b.*a.*a|a.*a.*b
54- for combo in permutations (tokens ) do
55- prompt = prompt .. " |" .. table.concat (combo , ' .*' )
63+ for combo in permutations (removeDuplicates (tokens )) do
64+ prompt = prompt .. " |" .. table.concat (combo , " .*" )
5665 end
5766 prompt = prompt :sub (2 )
5867
0 commit comments