Skip to content

fix(debugger): keep editor line numbers 1:1 with source for breakpoints#686

Open
andreahlert wants to merge 1 commit into
bigskysoftware:debuggerfrom
andreahlert:fix/debugger-breakpoint-line-mapping
Open

fix(debugger): keep editor line numbers 1:1 with source for breakpoints#686
andreahlert wants to merge 1 commit into
bigskysoftware:debuggerfrom
andreahlert:fix/debugger-breakpoint-line-mapping

Conversation

@andreahlert

Copy link
Copy Markdown
Contributor

Part of #685 (item 6).

The bug

normalizeScript reformats the script for display in the panel editor. It trimmed leading/trailing blank lines, which shifts every line up. But breakpoints and the paused-line highlight match against the tokenizer's original line numbers (token.line). So a script whose _ attribute starts with a newline (extremely common in HTML) displayed on click on Monaco line 1 while the tokenizer has it on line 2. A gutter click stored the Monaco line; the breakpoint check compared the source line; they never matched.

That's the "breakpoints sometimes work" report: it worked only when there was no leading blank line. Setting breakpoints from code worked because that path uses source lines directly.

The fix

Make the editor's line numbers stay 1:1 with the source so there is a single coordinate system everywhere (gutter clicks, the breakpoint check, and the paused-line highlight).

  • normalizeScript now de-indents horizontally only and never adds or removes lines. Leading/trailing blank lines are kept as empty lines, so Monaco display line N == token.line N. This fixes the gutter mismatch and the paused-line highlight at the root, without scattering offset math across every call site.
  • pauseAt now waits for editorReady before drawing the current-line decoration (mirroring showLivePause). selectElement mounts Monaco asynchronously, so the old synchronous highlight was applied to the not-yet-mounted editor and dropped, which is why the paused line frequently showed no highlight on the first break. It now reuses applyLineDecoration.

Verification

  • Algebraic check of normalizeScript on leading-newline, double-leading-newline, and no-leading-newline scripts: display line now equals source line in every case (previously off by the number of leading blank lines), and line count is preserved. No regression on the no-leading-newline case.
  • End-to-end in the panel against a _="\n on click ... breakpoint ..." script: execution pauses at the breakpoint, the Monaco model shows the command on the correct line, and the current line is highlighted (it was not highlighted at all before).

Scope

Source-only change to src/ext/debugger.js. dist/ is intentionally left out: the committed dist/ is already stale relative to committed src/ (e.g. the htmx-detection guard), so a rebuild here would pull in unrelated drift. Rebuild dist/ separately at release.

normalizeScript reformatted the displayed script by trimming leading and
trailing blank lines, but breakpoints and the paused-line highlight match
against the tokenizer's original line numbers (token.line). A leading blank
line shifted the display, so gutter clicks and the highlight landed on the
wrong line ("breakpoints sometimes work"). Setting them from code worked,
which matched the report.

- normalizeScript now de-indents horizontally only and never adds or removes
  lines, so Monaco display lines stay 1:1 with token.line.
- pauseAt waits for editorReady before highlighting (like showLivePause), so
  the paused line is highlighted on the first break instead of being applied
  to the not-yet-mounted editor.

Part of bigskysoftware#685
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant