From fcf061e4004b2eb053d1c9f4fdc6faa32b53cbee Mon Sep 17 00:00:00 2001 From: Romil Bijarnia Date: Wed, 11 Nov 2026 18:20:58 +1100 Subject: [PATCH] fix(web): preserve selections that start on a blank code line --- apps/web/src/composer-code-block.test.ts | 10 +++++++--- apps/web/src/composer-code-block.ts | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/apps/web/src/composer-code-block.test.ts b/apps/web/src/composer-code-block.test.ts index 567e6c0759532..0affbe76dcf88 100644 --- a/apps/web/src/composer-code-block.test.ts +++ b/apps/web/src/composer-code-block.test.ts @@ -144,11 +144,15 @@ describe("indentCodeBlock", () => { expect(textOf(editor)).toBe("one\ntwo"); }); - it("keeps the selection across the shifted lines", () => { - const editor = codeEditor("one\ntwo", { from: 0, to: 7 }); + it.each([ + ["one\ntwo", " one\n two", 5], + ["\none\ntwo", "\n one\n two", 1], + ])("keeps the selection across the shifted lines in %s", (code, expected, from) => { + const editor = codeEditor(code, { from: 0, to: code.length }); indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr)); indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr)); - expect(textOf(editor)).toBe(" one\n two"); + expect(textOf(editor)).toBe(expected); + expect(editor.state.selection.from).toBe(from); }); it("reports handled when outdenting can go no further, so Tab never escapes the editor", () => { diff --git a/apps/web/src/composer-code-block.ts b/apps/web/src/composer-code-block.ts index bf220d1715662..30c7630473160 100644 --- a/apps/web/src/composer-code-block.ts +++ b/apps/web/src/composer-code-block.ts @@ -137,7 +137,7 @@ export function indentCodeBlock( return true; } - const lineStart = block.text.lastIndexOf("\n", Math.max(0, startOffset - 1)) + 1; + const lineStart = startOffset === 0 ? 0 : block.text.lastIndexOf("\n", startOffset - 1) + 1; const newlineAfter = block.text.indexOf("\n", Math.max(startOffset, endOffset - 1)); const lineEnd = newlineAfter === -1 ? block.text.length : newlineAfter;