From 96a309216d001536178ac52cab85ae037d7af43c Mon Sep 17 00:00:00 2001 From: rootiest Date: Wed, 28 Aug 2024 06:35:28 -0400 Subject: [PATCH] feat: add profiling support with environment variable configuration --- init.lua | 16 ++++++++++++++ lua/config/profile.lua | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 lua/config/profile.lua diff --git a/init.lua b/init.lua index 389dd57..3f0a6e6 100644 --- a/init.lua +++ b/init.lua @@ -51,6 +51,9 @@ -- ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ -- The rootiest NeoVim configuration! +-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ OPTIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━ +-- Options are configured in the lua/config/options.lua file + -- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ROOTIEST ━━━━━━━━━━━━━━━━━━━━━━━━━━━ -- Rootiest Configuration require("config.rootiest").setup() @@ -62,3 +65,16 @@ require("config.lazy") -- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ROCKS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ -- Load LuaRocks package manager and plugins require("config.rocks") + +-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ PROFILING ━━━━━━━━━━━━━━━━━━━━━━━━━━ +-- Load profiling package +require("config.profile") + +-- Profiling options: Set by environment variable +-- ────────────────────────────────────────────────────────────────────── +-- NVIM_PROFILE=1 Start profiling at startup +-- NVIM_PROFILE_MODILE="lualine" Set profiling target to lualine +-- ────────────────────────────────────────────────────────────────────── + +-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ KEYMAPS ━━━━━━━━━━━━━━━━━━━━━━━━━━━ +-- Keymaps are configured in the lua/config/keymaps.lua file diff --git a/lua/config/profile.lua b/lua/config/profile.lua new file mode 100644 index 0000000..e38b748 --- /dev/null +++ b/lua/config/profile.lua @@ -0,0 +1,47 @@ +-- ╭─────────────────────────────────────────────────────────╮ +-- │ PROFILER │ +-- ╰─────────────────────────────────────────────────────────╯ + +-- Check if the environment variable is set +local should_profile = os.getenv("NVIM_PROFILE") +local profile_module = os.getenv("NVIM_PROFILE_MODULE") or "*" + +-- Start the profiler +if should_profile then + require("profile").instrument_autocmds() + if should_profile:lower():match("^start") then + require("profile").start(profile_module) + else + require("profile").instrument(profile_module) + end +end + +-- Function to toggle the profiler +local function toggle_profile() + local prof = require("profile") + if prof.is_recording() then + prof.stop() + vim.ui.input({ + prompt = "Save profile to:", + completion = "file", + default = "profile.json", + }, function(filename) + if filename then + prof.export(filename) + vim.notify(string.format("Wrote %s", filename)) + end + end) + else + prof.start(profile_module) + end +end + +-- Add the keybind to toggle the profiler +require("data").func.add_keymap("d", function() + local prof_name = profile_module + if profile_module == "*" then + prof_name = "all" + end + print("Profiling module: " .. prof_name) + toggle_profile() +end, "Toggle Profiler")