Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/lua/bugabinga/profile.lua

Raw
--- Starts `profile.nvim`, if special `NVIM_PROFILE` environment variable is set.
--- Any value will instrument all modules. The 'start' value will also start the
--- profiler.
--- This file intentionally does not import any other Lua files, in order to not
--- skew the later profiling results with cached modules.

local should_profile = os.getenv 'NVIM_PROFILE'

if should_profile then
  vim.cmd.packadd 'profile'
  require 'profile'.instrument_autocmds()
  if should_profile:lower():match '^start' then
    require 'profile'.start '*'
  else
    require 'profile'.instrument '*'
  end
end

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
    vim.notify("Starting profiler")
    prof.start '*'
  end
end

vim.keymap.set('n', '<F12>', toggle_profile, { desc = 'Toggle profiler' })