Skip to content

Commit 7dfb9c1

Browse files
committed
feat(ui): added options to sort/filter profiling data
1 parent fde5fee commit 7dfb9c1

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

lua/lazy/view/init.lua

+35-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ local Config = require("lazy.core.config")
55
---@class LazyViewState
66
---@field mode string
77
---@field plugin? string
8+
local default_state = {
9+
mode = "home",
10+
profile = {
11+
threshold = 0,
12+
sort_time_taken = true,
13+
},
14+
}
815

916
---@class LazyView
1017
---@field buf number
@@ -69,7 +76,7 @@ function M.create(opts)
6976
opts = opts or {}
7077
local self = setmetatable({}, { __index = M })
7178

72-
self.state = { mode = "home" }
79+
self.state = vim.deepcopy(default_state)
7380

7481
self:mount()
7582

@@ -96,6 +103,33 @@ function M.create(opts)
96103
end
97104
end)
98105

106+
self:on_key("<C-s>", function()
107+
if self.state.mode == "profile" then
108+
self.state.profile.sort_time_taken = not self.state.profile.sort_time_taken
109+
self:update()
110+
end
111+
end)
112+
113+
self:on_key("<C-f>", function()
114+
if self.state.mode == "profile" then
115+
vim.ui.input({
116+
prompt = "Enter time threshold in ms, like 0.5",
117+
default = tostring(self.state.profile.threshold),
118+
}, function(input)
119+
if not input then
120+
return
121+
end
122+
local num = input == "" and 0 or tonumber(input)
123+
if not num then
124+
Util.error("Please input a number")
125+
else
126+
self.state.profile.threshold = num
127+
self:update()
128+
end
129+
end)
130+
end
131+
end)
132+
99133
self:setup_hover()
100134
self:setup_modes()
101135
return self

lua/lazy/view/render.lua

+40-4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ end
144144
function M:help()
145145
self:append("Help", "LazyH2"):nl():nl()
146146

147+
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl()
147148
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl()
148149

149150
self:append("Most properties can be hovered with ")
@@ -459,28 +460,63 @@ end
459460

460461
function M:profile()
461462
self:append("Profile", "LazyH2"):nl():nl()
463+
self
464+
:append("You can press ")
465+
:append("<C-s>", "LazySpecial")
466+
:append(" to change sorting between chronological order & time taken.")
467+
:nl()
468+
self
469+
:append("Press ")
470+
:append("<C-f>", "LazySpecial")
471+
:append(" to filter profiling entries that took more time than a given threshold")
472+
:nl()
473+
474+
self:nl()
462475
local symbols = {
463476
"",
464477
"",
465478
"",
466479
"",
467480
}
468481

482+
---@param a LazyProfile
483+
---@param b LazyProfile
484+
local function sort(a, b)
485+
return a.time > b.time
486+
end
487+
488+
---@param entry LazyProfile
489+
local function get_children(entry)
490+
---@type LazyProfile[]
491+
local children = entry
492+
493+
if self.view.state.profile.sort_time_taken then
494+
children = {}
495+
for _, child in ipairs(entry) do
496+
children[#children + 1] = child
497+
end
498+
table.sort(children, sort)
499+
end
500+
return children
501+
end
502+
469503
---@param entry LazyProfile
470504
local function _profile(entry, depth)
505+
if entry.time / 1e6 < self.view.state.profile.threshold then
506+
return
507+
end
471508
local data = type(entry.data) == "string" and { source = entry.data } or entry.data
472509
data.time = entry.time
473510
local symbol = symbols[depth] or symbols[#symbols]
474-
self:append((" "):rep(depth)):append(" " .. symbol, "LazySpecial"):append(" ")
511+
self:append((" "):rep(depth)):append(symbol, "LazySpecial"):append(" ")
475512
self:reason(data, { time_right = true })
476513
self:nl()
477-
478-
for _, child in ipairs(entry) do
514+
for _, child in ipairs(get_children(entry)) do
479515
_profile(child, depth + 1)
480516
end
481517
end
482518

483-
for _, entry in ipairs(Util._profiles[1]) do
519+
for _, entry in ipairs(get_children(Util._profiles[1])) do
484520
_profile(entry, 1)
485521
end
486522
end

0 commit comments

Comments
 (0)