Skip to content

Commit fcf061e

Browse files
fix(web): preserve selections that start on a blank code line
1 parent d835e43 commit fcf061e

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎apps/web/src/composer-code-block.test.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,15 @@ describe("indentCodeBlock", () => {
144144
expect(textOf(editor)).toBe("one\ntwo");
145145
});
146146

147-
it("keeps the selection across the shifted lines", () => {
148-
const editor = codeEditor("one\ntwo", { from: 0, to: 7 });
147+
it.each([
148+
["one\ntwo", " one\n two", 5],
149+
["\none\ntwo", "\n one\n two", 1],
150+
])("keeps the selection across the shifted lines in %s", (code, expected, from) => {
151+
const editor = codeEditor(code, { from: 0, to: code.length });
149152
indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr));
150153
indentCodeBlock(editor.state, "in", (tr) => editor.view.dispatch(tr));
151-
expect(textOf(editor)).toBe(" one\n two");
154+
expect(textOf(editor)).toBe(expected);
155+
expect(editor.state.selection.from).toBe(from);
152156
});
153157

154158
it("reports handled when outdenting can go no further, so Tab never escapes the editor", () => {

‎apps/web/src/composer-code-block.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function indentCodeBlock(
137137
return true;
138138
}
139139

140-
const lineStart = block.text.lastIndexOf("\n", Math.max(0, startOffset - 1)) + 1;
140+
const lineStart = startOffset === 0 ? 0 : block.text.lastIndexOf("\n", startOffset - 1) + 1;
141141
const newlineAfter = block.text.indexOf("\n", Math.max(startOffset, endOffset - 1));
142142
const lineEnd = newlineAfter === -1 ? block.text.length : newlineAfter;
143143

0 commit comments

Comments
 (0)