Skip to content

Commit

Permalink
feat(ui): added options to sort/filter profiling data
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 23, 2022
1 parent fde5fee commit 7dfb9c1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
36 changes: 35 additions & 1 deletion lua/lazy/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ local Config = require("lazy.core.config")
---@class LazyViewState
---@field mode string
---@field plugin? string
local default_state = {
mode = "home",
profile = {
threshold = 0,
sort_time_taken = true,
},
}

---@class LazyView
---@field buf number
Expand Down Expand Up @@ -69,7 +76,7 @@ function M.create(opts)
opts = opts or {}
local self = setmetatable({}, { __index = M })

self.state = { mode = "home" }
self.state = vim.deepcopy(default_state)

self:mount()

Expand All @@ -96,6 +103,33 @@ function M.create(opts)
end
end)

self:on_key("<C-s>", function()
if self.state.mode == "profile" then
self.state.profile.sort_time_taken = not self.state.profile.sort_time_taken
self:update()
end
end)

self:on_key("<C-f>", function()
if self.state.mode == "profile" then
vim.ui.input({
prompt = "Enter time threshold in ms, like 0.5",
default = tostring(self.state.profile.threshold),
}, function(input)
if not input then
return
end
local num = input == "" and 0 or tonumber(input)
if not num then
Util.error("Please input a number")
else
self.state.profile.threshold = num
self:update()
end
end)
end
end)

self:setup_hover()
self:setup_modes()
return self
Expand Down
44 changes: 40 additions & 4 deletions lua/lazy/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ end
function M:help()
self:append("Help", "LazyH2"):nl():nl()

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

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

function M:profile()
self:append("Profile", "LazyH2"):nl():nl()
self
:append("You can press ")
:append("<C-s>", "LazySpecial")
:append(" to change sorting between chronological order & time taken.")
:nl()
self
:append("Press ")
:append("<C-f>", "LazySpecial")
:append(" to filter profiling entries that took more time than a given threshold")
:nl()

self:nl()
local symbols = {
"",
"",
"",
"",
}

---@param a LazyProfile
---@param b LazyProfile
local function sort(a, b)
return a.time > b.time
end

---@param entry LazyProfile
local function get_children(entry)
---@type LazyProfile[]
local children = entry

if self.view.state.profile.sort_time_taken then
children = {}
for _, child in ipairs(entry) do
children[#children + 1] = child
end
table.sort(children, sort)
end
return children
end

---@param entry LazyProfile
local function _profile(entry, depth)
if entry.time / 1e6 < self.view.state.profile.threshold then
return
end
local data = type(entry.data) == "string" and { source = entry.data } or entry.data
data.time = entry.time
local symbol = symbols[depth] or symbols[#symbols]
self:append((" "):rep(depth)):append(" " .. symbol, "LazySpecial"):append(" ")
self:append((" "):rep(depth)):append(symbol, "LazySpecial"):append(" ")
self:reason(data, { time_right = true })
self:nl()

for _, child in ipairs(entry) do
for _, child in ipairs(get_children(entry)) do
_profile(child, depth + 1)
end
end

for _, entry in ipairs(Util._profiles[1]) do
for _, entry in ipairs(get_children(Util._profiles[1])) do
_profile(entry, 1)
end
end
Expand Down

0 comments on commit 7dfb9c1

Please sign in to comment.