Files
turbo-debug.nvim/lua/turbo-debug/init.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

44 lines
1.4 KiB
Lua

local config = require("turbo-debug.config")
local mode = require("turbo-debug.mode")
local M = {}
function M.setup(opts)
-- Self-defer if dependencies aren't yet on the runtimepath. This happens
-- when the user calls `setup({...})` from their init.lua right after
-- `vim.pack.add(..., turbo-debug)`: our plugin/turbo-debug.lua fires
-- nested `vim.pack.add` calls for dap/dapui/persistent-breakpoints, but
-- those nested adds only register the paths on the NEXT event loop tick
-- — so a synchronous follow-up `require('persistent-breakpoints')`
-- crashes with "module not found". `vim.schedule` lets the pack module
-- finalize before we retry. `_setup_called` still gets set on the retry,
-- so plugin/turbo-debug.lua's own deferred auto-setup correctly skips.
if not pcall(require, "persistent-breakpoints") then
vim.schedule(function() M.setup(opts) end)
return
end
M._setup_called = true
config.merge(opts)
require("turbo-debug.breakpoints").setup()
require("turbo-debug.adapters").setup()
-- global toggle keybinding
local toggle_key = config.opts.global_keys.toggle
if toggle_key then
vim.keymap.set("n", toggle_key, function() mode.toggle() end, {
silent = true, desc = "Toggle debug mode",
})
end
end
function M.toggle()
mode.toggle()
end
function M.active()
return mode.is_active()
end
return M