From 7dfb9c1f5cb8dcad4133a93da68cbdb5c8001035 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 23 Dec 2022 10:43:22 +0100 Subject: [PATCH] feat(ui): added options to sort/filter profiling data --- lua/lazy/view/init.lua | 36 +++++++++++++++++++++++++++++++- lua/lazy/view/render.lua | 44 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lua/lazy/view/init.lua b/lua/lazy/view/init.lua index 3d40df9..5954361 100644 --- a/lua/lazy/view/init.lua +++ b/lua/lazy/view/init.lua @@ -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 @@ -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() @@ -96,6 +103,33 @@ function M.create(opts) end end) + self:on_key("", 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("", 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 diff --git a/lua/lazy/view/render.lua b/lua/lazy/view/render.lua index ca69c42..88c8a8e 100644 --- a/lua/lazy/view/render.lua +++ b/lua/lazy/view/render.lua @@ -144,6 +144,7 @@ end function M:help() self:append("Help", "LazyH2"):nl():nl() + self:append("You can press "):append("", "LazySpecial"):append(" on a plugin to show its details."):nl() self:append("You can press "):append("", "LazySpecial"):append(" on a plugin to show its details."):nl() self:append("Most properties can be hovered with ") @@ -459,6 +460,18 @@ end function M:profile() self:append("Profile", "LazyH2"):nl():nl() + self + :append("You can press ") + :append("", "LazySpecial") + :append(" to change sorting between chronological order & time taken.") + :nl() + self + :append("Press ") + :append("", "LazySpecial") + :append(" to filter profiling entries that took more time than a given threshold") + :nl() + + self:nl() local symbols = { "●", "➜", @@ -466,21 +479,44 @@ function M:profile() "‒", } + ---@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