diff --git a/src/features/debugger/editor/runToCursor.test.ts b/src/features/debugger/editor/runToCursor.test.ts index 34be5a2991..5eb94cc255 100644 --- a/src/features/debugger/editor/runToCursor.test.ts +++ b/src/features/debugger/editor/runToCursor.test.ts @@ -7,9 +7,17 @@ describe("inlay Run to Cursor", () => { expect(hoveredLine(100, 8, 100, line)).toBe(2); expect(hoveredLine(100, 8, 132, line)).toBe(2); }); - it("ignores the pointer further right, left of the text, and blank lines", () => { + it("counts the gutter left of the text, as the button can sit over it", () => { + expect(hoveredLine(100, 8, 99, line)).toBe(2); + expect(hoveredLine(100, 8, 40, line)).toBe(2); + }); + it("ignores the pointer further right and blank lines", () => { expect(hoveredLine(100, 8, 133, line)).toBeNull(); - expect(hoveredLine(100, 8, 99, line)).toBeNull(); expect(hoveredLine(100, 8, 104, { number: 4, text: " " })).toBeNull(); }); + it("keeps the line on the way to a button drawn past the first characters (a deep indent)", () => { + const deep = { number: 7, text: " return x" }; + expect(hoveredLine(100, 8, 200, deep, 226)).toBe(6); + expect(hoveredLine(100, 8, 227, deep, 226)).toBeNull(); + }); }); diff --git a/src/features/debugger/editor/runToCursor.ts b/src/features/debugger/editor/runToCursor.ts index 94a9f784c3..6e0a80adf3 100644 --- a/src/features/debugger/editor/runToCursor.ts +++ b/src/features/debugger/editor/runToCursor.ts @@ -1,5 +1,5 @@ /** - * Inlay Run to Cursor: while the debugger is paused, hovering the start of a + * Inlay Run to Cursor: while the debugger is paused, hovering the gutter or the start of a * line shows a small button left of its text that runs to that line (Resume on * the execution line itself). Settings › Debugger › "Show inlay Run to Cursor popup". */ @@ -19,14 +19,19 @@ import styles from "./editor.module.css"; const HOVER_CHARS = 4; const BUTTON = 20; -/** The 0-based line whose start the pointer is over, or null. */ +/** + * The 0-based line whose start the pointer is over, or null: the gutter and the first + * characters of the line. `reach` extends the area to the right edge of the button shown on + * this line, so moving to a button drawn further right (a deep indent) keeps it. + */ export function hoveredLine( textLeft: number, charWidth: number, x: number, line: { number: number; text: string }, + reach = -Infinity, ): number | null { - if (x < textLeft || x - textLeft > charWidth * HOVER_CHARS) return null; + if (x > Math.max(textLeft + charWidth * HOVER_CHARS, reach)) return null; return line.text.trim() ? line.number - 1 : null; } @@ -60,7 +65,11 @@ export function runToCursorInlay(ctx: EditorDocContext): Extension { const line = view.state.doc.lineAt(block.from); const textLeft = view.coordsAtPos(line.from, 1)?.left; if (textLeft == null) return this.hide(); - const target = hoveredLine(textLeft, view.defaultCharacterWidth, e.clientX, line); + // The button sits left of the first non-space character, over the indent or the gutter: + // the way to it stays inside the area. + const shown = + this.button && this.line === line.number - 1 ? this.button.getBoundingClientRect().right : undefined; + const target = hoveredLine(textLeft, view.defaultCharacterWidth, e.clientX, line, shown); if (target == null) return this.hide(); if (target !== this.line) this.show(target, block.top + view.documentTop, line.from); };