Files
turbo-debug.nvim/lua/turbo-debug/breakpoints.lua
T
swaits 7a21d372ff fix: deferred dep loading + idempotent setup
Two related bugs surfaced when the user tried adding `setup({...})` to
their init.lua alongside our auto-setup:

1. **`inmemory.bps = nil` crash.** When a user's `init.lua` does
   `vim.pack.add(..., turbo-debug)` and then calls `setup({...})`
   synchronously right after, the nested `vim.pack.add` calls inside
   our own `plugin/turbo-debug.lua` (for dap/dapui/persistent-
   breakpoints) are DEFERRED by nvim's pack module — so the user's
   follow-up `require('persistent-breakpoints')` throws "module not
   found". On the previous `initialized = true` guard, the error set
   the flag but left setup half-complete; any later call short-
   circuited and `inmemory.bps` stayed `nil`, exploding the first
   breakpoint toggle (`api.lua:15: attempt to index field 'bps'`).

2. **Colorscheme override ignored.** Same root cause: user's
   `setup({ colorscheme = "nightfox" })` errored before reaching
   `config.merge`, so `config.opts.colorscheme` stayed `nil` and
   `M.enter` never switched themes.

Three changes that together fix both:

- **`init.lua` `setup()` self-defers** when `require('persistent-
  breakpoints')` fails, `vim.schedule`s itself once deps can load.
- **`plugin/turbo-debug.lua` auto-setup is now `vim.schedule`d** and
  skips entirely if the user's explicit `setup({...})` already ran
  (tracked via `M._setup_called`) — no more stomping user opts with
  a second `config.merge(nil)`.
- **`breakpoints.setup` is now fully idempotent**: dropped the
  `initialized` flag, since every call inside it
  (`persistent-breakpoints.setup`, `sign_define`, `nvim_set_hl`,
  `vim.keymap.set`) is safe to re-run.
2026-04-12 18:01:02 -06:00

101 lines
5.8 KiB
Lua

local config = require("turbo-debug.config")
local M = {}
-- This function is idempotent — every operation inside is safe to re-run.
-- That property matters because turbo-debug.setup() can get called twice in
-- practice:
-- (1) Deferred auto-setup from plugin/turbo-debug.lua, which runs after
-- vim.pack.add has finished registering all deps in runtimepath. The
-- defer is necessary — calling setup synchronously during
-- vim.pack.add would try to `require('persistent-breakpoints.api')`
-- before the plugin's lua/ directory is on the runtimepath and
-- crash with "module not found".
-- (2) User's explicit setup({...}) in their init.lua with config
-- overrides. It runs immediately (before the deferred auto-setup).
--
-- Previously a single `initialized` flag guarded this function so it only
-- ran once. That guard was fragile: if the first call errored partway
-- (case above), the flag was set but the setup was incomplete, and the
-- second call skipped the retry. We now let every call do its full work;
-- persistent-breakpoints.setup re-reads its bps file, sign_define overrides
-- a previous definition, nvim_set_hl overrides a previous link, and
-- vim.keymap.set overrides a previous binding.
function M.setup()
require("persistent-breakpoints").setup({
load_breakpoints_event = { "BufReadPost" },
})
-- link sign highlights to standard diagnostic groups so they pick up the
-- user's colorscheme instead of hardcoding hex colors.
vim.api.nvim_set_hl(0, "DapBreakpoint", { default = true, link = "DiagnosticError" })
vim.api.nvim_set_hl(0, "DapBreakpointCondition", { default = true, link = "DiagnosticWarn" })
vim.api.nvim_set_hl(0, "DapBreakpointRejected", { default = true, link = "DiagnosticHint" })
vim.api.nvim_set_hl(0, "DapStopped", { default = true, link = "DiagnosticOk" })
vim.api.nvim_set_hl(0, "DapStoppedLine", { default = true, link = "Search" })
vim.api.nvim_set_hl(0, "DapLogPoint", { default = true, link = "DiagnosticInfo" })
-- Gutter signs: colorful emoji for instant recognition. Modern terminals
-- (Ghostty, Kitty, WezTerm, iTerm) render these full-color.
-- 🔴 U+1F534 large red circle = breakpoint
-- 🟡 U+1F7E1 large yellow circle = conditional breakpoint
-- ⭕ U+2B55 heavy large circle = rejected breakpoint
-- 👉 U+1F449 backhand pointing right = current execution line (IP)
-- 📝 U+1F4DD memo = log point
vim.fn.sign_define("DapBreakpoint", { text = "\xf0\x9f\x94\xb4", texthl = "DapBreakpoint", numhl = "DapBreakpoint" })
vim.fn.sign_define("DapBreakpointCondition", { text = "\xf0\x9f\x9f\xa1", texthl = "DapBreakpointCondition", numhl = "DapBreakpointCondition" })
vim.fn.sign_define("DapBreakpointRejected", { text = "\xe2\xad\x95", texthl = "DapBreakpointRejected", numhl = "DapBreakpointRejected" })
vim.fn.sign_define("DapStopped", { text = "\xf0\x9f\x91\x89", texthl = "DapStopped", linehl = "DapStoppedLine", numhl = "DapStopped" })
vim.fn.sign_define("DapLogPoint", { text = "\xf0\x9f\x93\x9d", texthl = "DapLogPoint", numhl = "DapLogPoint" })
-- unobtrusive inline virtual text: italic, comment-colored
vim.api.nvim_set_hl(0, "NvimDapVirtualText", { default = true, link = "Comment", italic = true })
vim.api.nvim_set_hl(0, "NvimDapVirtualTextChanged", { default = true, link = "DiagnosticWarn", italic = true })
vim.api.nvim_set_hl(0, "NvimDapVirtualTextError", { default = true, link = "DiagnosticError", italic = true })
vim.api.nvim_set_hl(0, "NvimDapVirtualTextInfo", { default = true, link = "DiagnosticInfo", italic = true })
-- turbo-debug's own UI groups (help float, active-pane borders, winbar badge)
vim.api.nvim_set_hl(0, "TurboDebugHelpBrand", { default = true, link = "Title" })
vim.api.nvim_set_hl(0, "TurboDebugHelpTagline", { default = true, link = "Comment", italic = true })
vim.api.nvim_set_hl(0, "TurboDebugHelpSection", { default = true, link = "Function" })
vim.api.nvim_set_hl(0, "TurboDebugHelpSectionIcon", { default = true, link = "Constant" })
vim.api.nvim_set_hl(0, "TurboDebugHelpKey", { default = true, link = "Special" })
vim.api.nvim_set_hl(0, "TurboDebugHelpParen", { default = true, link = "Comment" })
vim.api.nvim_set_hl(0, "TurboDebugCtrlButton", { default = true, link = "Function" })
vim.api.nvim_set_hl(0, "TurboDebugCtrlMuted", { default = true, link = "Comment" })
-- derived highlight: the IP "reason" vtext (STEP / BREAKPOINT / EXCEPTION /
-- PAUSE / etc.) — bold + reverse + WarningMsg color so it renders as an
-- inverted-color badge that absolutely demands attention. nvim_set_hl
-- can't combine link + bold, so copy attrs explicitly and re-apply on
-- ColorScheme so theme switches don't blank it out.
local function apply_ip_reason_hl()
local ok, src = pcall(vim.api.nvim_get_hl, 0, { name = "WarningMsg", link = false })
if not ok or not src or not src.fg then return end
vim.api.nvim_set_hl(0, "TurboDebugIPReason", {
fg = src.fg, bg = src.bg, bold = true, reverse = true, default = true,
})
end
apply_ip_reason_hl()
vim.api.nvim_create_autocmd("ColorScheme", {
group = vim.api.nvim_create_augroup("TurboDebugColors", { clear = true }),
callback = apply_ip_reason_hl,
})
-- global mappings (work outside debug mode)
local gk = config.opts.global_keys
local pb = require("persistent-breakpoints.api")
local bindings = {
{ gk.breakpoint, pb.toggle_breakpoint, "Toggle breakpoint" },
{ gk.cond_breakpoint, pb.set_conditional_breakpoint, "Conditional breakpoint" },
{ gk.clear_breakpoints, pb.clear_all_breakpoints, "Clear all breakpoints" },
}
for _, b in ipairs(bindings) do
if b[1] then
vim.keymap.set("n", b[1], b[2], { silent = true, desc = b[3] })
end
end
end
return M