Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 33e24d1

Browse files
author
Hinell
committed
feat(ui): apply mrjones2014.diff
1 parent a7d140f commit 33e24d1

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

lua/legendary/api/executor.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local Toolbox = require('legendary.toolbox')
22
local Log = require('legendary.log')
33
local Config = require('legendary.config')
4+
local State = require('legendary.data.state')
45
local util = require('legendary.util')
56

67
local function update_item_frecency_score(item)
@@ -83,6 +84,7 @@ end
8384
function M.exec_item(item, context)
8485
vim.schedule(function()
8586
M.restore_context(context, function()
87+
State.last_executed_item = item
8688
update_item_frecency_score(item)
8789
if Toolbox.is_function(item) then
8890
item.implementation()
@@ -122,12 +124,11 @@ end
122124
---still return true.
123125
---@param ignore_filters boolean|nil whether to ignore the filters used when selecting the item, default false
124126
function M.repeat_previous(ignore_filters)
125-
local State = require('legendary.data.state')
126-
if State.most_recent_item then
127+
if State.last_executed_item then
127128
if not ignore_filters and State.most_recent_filters then
128129
for _, filter in ipairs(State.most_recent_filters) do
129130
-- if any filter does not match, abort executions
130-
local err, matches = pcall(filter, State.most_recent_item)
131+
local err, matches = pcall(filter, State.last_executed_item)
131132
if not err and not matches then
132133
Log.warn(
133134
'Previously executed item no longer matches previously used filters, use `:LegendaryRepeat!`'
@@ -138,7 +139,7 @@ function M.repeat_previous(ignore_filters)
138139
end
139140
end
140141
local context = M.build_context()
141-
M.exec_item(State.most_recent_item, context)
142+
M.exec_item(State.last_executed_item, context)
142143
end
143144
end
144145

lua/legendary/data/itemlist.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ local Log = require('legendary.log')
1414
---@field private sorted boolean
1515
local ItemList = class('ItemList')
1616

17+
ItemList.TOPLEVEL_LIST_ID = 'toplevel'
18+
1719
---@private
1820
function ItemList:initialize()
1921
self.items = {}
@@ -124,7 +126,13 @@ function ItemList:sort_inplace(opts)
124126

125127
-- if no items have been added, and the most recent item has not changed,
126128
-- we're already sorted
127-
if self.sorted and (not opts.most_recent_first or (self.items[1] == State.most_recent_item)) then
129+
if
130+
self.sorted
131+
and (
132+
not opts.most_recent_first
133+
or (self.items[1] == State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID])
134+
)
135+
then
128136
return
129137
end
130138

@@ -188,10 +196,8 @@ function ItemList:sort_inplace(opts)
188196
end
189197

190198
if opts.most_recent_first then
191-
if opts.itemgroup then
192-
return item1 == State.itemgroup_history[opts.itemgroup]
193-
else
194-
return item1 == State.most_recent_item
199+
if item1 == State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID] then
200+
return true
195201
end
196202
end
197203

@@ -224,9 +230,11 @@ function ItemList:sort_inplace(opts)
224230
-- sort by most recent last, and after other sorts are done
225231
-- if most recent is already at top, nothing to do, and attempting to sort will cause
226232
-- an error since it doesn't need to be sorted
227-
if opts.most_recent_first and State.most_recent_item and State.most_recent_item ~= self.items[1] then
233+
if
234+
opts.most_recent_first and State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID] ~= self.items[1]
235+
then
228236
items = Sorter.mergesort(items, function(item)
229-
return item == State.most_recent_item
237+
return item == State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID]
230238
end)
231239
end
232240

lua/legendary/data/state.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ local ItemList = require('legendary.data.itemlist')
22

33
---@class LegendaryState
44
---@field items ItemList
5-
---@field most_recent_item LegendaryItem|nil
5+
---@field last_executed_item LegendaryItem|nil
66
---@field most_recent_filters LegendaryItemFilter[]|nil
7-
---@field itemgroup_history ItemList[]
7+
---@field itemgroup_history table<string, LegendaryItem>
88
local M = {}
99

1010
M.items = ItemList:create()
11-
M.most_recent_item = nil
11+
M.last_executed_item = nil
1212
M.most_recent_filters = nil
1313
M.itemgroup_history = {}
1414

lua/legendary/ui/init.lua

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ local Toolbox = require('legendary.toolbox')
66
local Format = require('legendary.ui.format')
77
local Executor = require('legendary.api.executor')
88
local Log = require('legendary.log')
9+
local ItemList = require('legendary.data.itemlist')
910

1011
---@class LegendaryUi
1112
---@field select fun(opts:LegendaryFindOpts)
1213
local M = {}
1314

14-
---@class LegendaryFindOpts
15+
---@class LegendaryFindOpts : ItemListSortInplaceOpts
1516
---@field itemgroup string Find items in this item group only
1617
---@field filters LegendaryItemFilter[]
1718
---@field select_prompt string|fun():string
@@ -82,11 +83,7 @@ local function select_inner(opts, context, itemlist)
8283
return
8384
end
8485

85-
if opts.itemgroup then
86-
State.itemgroup_history[opts.itemgroup] = selected
87-
else
88-
State.most_recent_item = selected
89-
end
86+
State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID] = selected
9087

9188
if Toolbox.is_itemgroup(selected) then
9289
local item_group_id = selected:id()

0 commit comments

Comments
 (0)