Skip to content

Commit

Permalink
feat(ui): apply mrjones2014.diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinell committed Oct 19, 2023
1 parent a7d140f commit 45411f6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
8 changes: 5 additions & 3 deletions lua/legendary/api/executor.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local Toolbox = require('legendary.toolbox')
local Log = require('legendary.log')
local Config = require('legendary.config')
local State = require('legendary.data.state')
local util = require('legendary.util')

local function update_item_frecency_score(item)
Expand Down Expand Up @@ -83,6 +84,7 @@ end
function M.exec_item(item, context)
vim.schedule(function()
M.restore_context(context, function()
State.last_executed_item = item
update_item_frecency_score(item)
if Toolbox.is_function(item) then
item.implementation()
Expand Down Expand Up @@ -123,11 +125,11 @@ end
---@param ignore_filters boolean|nil whether to ignore the filters used when selecting the item, default false
function M.repeat_previous(ignore_filters)
local State = require('legendary.data.state')
if State.most_recent_item then
if State.last_executed_item then
if not ignore_filters and State.most_recent_filters then
for _, filter in ipairs(State.most_recent_filters) do
-- if any filter does not match, abort executions
local err, matches = pcall(filter, State.most_recent_item)
local err, matches = pcall(filter, State.last_executed_item)
if not err and not matches then
Log.warn(
'Previously executed item no longer matches previously used filters, use `:LegendaryRepeat!`'
Expand All @@ -138,7 +140,7 @@ function M.repeat_previous(ignore_filters)
end
end
local context = M.build_context()
M.exec_item(State.most_recent_item, context)
M.exec_item(State.last_executed_item, context)
end
end

Expand Down
22 changes: 15 additions & 7 deletions lua/legendary/data/itemlist.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ local Log = require('legendary.log')
---@field private sorted boolean
local ItemList = class('ItemList')

ItemList.TOPLEVEL_LIST_ID = 'toplevel'

---@private
function ItemList:initialize()
self.items = {}
Expand Down Expand Up @@ -124,7 +126,13 @@ function ItemList:sort_inplace(opts)

-- if no items have been added, and the most recent item has not changed,
-- we're already sorted
if self.sorted and (not opts.most_recent_first or (self.items[1] == State.most_recent_item)) then
if
self.sorted
and (
not opts.most_recent_first
or (self.items[1] == State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID])
)
then
return
end

Expand Down Expand Up @@ -188,10 +196,8 @@ function ItemList:sort_inplace(opts)
end

if opts.most_recent_first then
if opts.itemgroup then
return item1 == State.itemgroup_history[opts.itemgroup]
else
return item1 == State.most_recent_item
if item1 == State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID] then
return true
end
end

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

Expand Down
6 changes: 3 additions & 3 deletions lua/legendary/data/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ local ItemList = require('legendary.data.itemlist')

---@class LegendaryState
---@field items ItemList
---@field most_recent_item LegendaryItem|nil
---@field last_executed_item LegendaryItem|nil
---@field most_recent_filters LegendaryItemFilter[]|nil
---@field itemgroup_history ItemList[]
---@field itemgroup_history table<string, LegendaryItem>
local M = {}

M.items = ItemList:create()
M.most_recent_item = nil
M.last_executed_item = nil
M.most_recent_filters = nil
M.itemgroup_history = {}

Expand Down
9 changes: 3 additions & 6 deletions lua/legendary/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ local Toolbox = require('legendary.toolbox')
local Format = require('legendary.ui.format')
local Executor = require('legendary.api.executor')
local Log = require('legendary.log')
local ItemList = require('legendary.data.itemlist')

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

---@class LegendaryFindOpts
---@class LegendaryFindOpts : ItemListSortInplaceOpts
---@field itemgroup string Find items in this item group only
---@field filters LegendaryItemFilter[]
---@field select_prompt string|fun():string
Expand Down Expand Up @@ -82,11 +83,7 @@ local function select_inner(opts, context, itemlist)
return
end

if opts.itemgroup then
State.itemgroup_history[opts.itemgroup] = selected
else
State.most_recent_item = selected
end
State.itemgroup_history[opts.itemgroup or ItemList.TOPLEVEL_LIST_ID] = selected

if Toolbox.is_itemgroup(selected) then
local item_group_id = selected:id()
Expand Down

0 comments on commit 45411f6

Please sign in to comment.