Skip to content

Commit fec83a1

Browse files
michal-billtechjuliusmarmingeclaude
authored
fix(web): numbered composer lists nest and lift correctly (#17138)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 30eb7e7 commit fec83a1

3 files changed

Lines changed: 174 additions & 21 deletions

File tree

‎apps/web/src/composer-list-continuation.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { listContinuationForEnter, nextOrderedMarkerText } from "./composer-list-continuation";
3+
import {
4+
listContinuationForEnter,
5+
listIndentForTab,
6+
nextOrderedMarkerText,
7+
} from "./composer-list-continuation";
48

59
function applyEdit(value: string, edit: { start: number; end: number; replacement: string }) {
610
return value.slice(0, edit.start) + edit.replacement + value.slice(edit.end);
@@ -31,6 +35,25 @@ describe("composer list continuation", () => {
3135
expect(applyEdit("- [ ] ", listContinuationForEnter("- [ ] ", 6)!)).toBe("");
3236
});
3337

38+
it("lifts an empty nested item one level per press before leaving the list", () => {
39+
let value = "1. parent\n 1. child\n 1. ";
40+
for (const expected of [
41+
"1. parent\n 1. child\n 2. ",
42+
"1. parent\n 1. child\n2. ",
43+
"1. parent\n 1. child\n",
44+
]) {
45+
value = applyEdit(value, listContinuationForEnter(value, value.length)!);
46+
expect(value).toBe(expected);
47+
}
48+
});
49+
50+
it("renumbers the parent list after lifting an empty nested item", () => {
51+
const value = "1. a\n 1. \n2. b";
52+
const edit = listContinuationForEnter(value, value.indexOf("\n2."))!;
53+
expect(applyEdit(value, edit)).toBe("1. a\n2. \n3. b");
54+
expect(edit.cursorAfter).toBe("1. a\n2. ".length);
55+
});
56+
3457
it("refuses to split a supplementary currency skill chip", () => {
3558
const value = "1. 𑿝review go";
3659
const cursor = value.indexOf(" go") - 1;
@@ -44,6 +67,27 @@ describe("composer list continuation", () => {
4467
});
4568
});
4669

70+
describe("composer list indentation", () => {
71+
// regression: #17124 Tab must nest a numbered item at its parent's content column
72+
it.each([
73+
["1. parent\n2. child", "1. parent\n 1. child"],
74+
["10. parent\n11. child", "10. parent\n 1. child"],
75+
["1.\tparent\n2. child", "1.\tparent\n 1. child"],
76+
['1. @"file\n 9. name"\n2. child', '1. @"file\n 9. name"\n 1. child'],
77+
])("nests %j as a first child and keeps the caret on its text", (value, expected) => {
78+
const cursor = value.lastIndexOf("child") + 2;
79+
const edit = listIndentForTab(value, cursor, cursor)!;
80+
expect(applyEdit(value, edit)).toBe(expected);
81+
expect(edit.cursorAfter).toBe(expected.lastIndexOf("child") + 2);
82+
});
83+
84+
it("continues the numbering of an existing nested sibling", () => {
85+
const value = "1. parent\n 1. first\n2. second";
86+
const edit = listIndentForTab(value, value.length, value.length)!;
87+
expect(applyEdit(value, edit)).toBe("1. parent\n 1. first\n 2. second");
88+
});
89+
});
90+
4791
describe("nextOrderedMarkerText", () => {
4892
it.each([
4993
["1.", "2."],

‎apps/web/src/composer-list-continuation.ts‎

Lines changed: 127 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,60 @@ type ListMarker =
1313
| { kind: "task"; indent: string; space: string }
1414
| { kind: "bullet"; indent: string; bullet: string; space: string };
1515

16-
function parseListMarker(line: string): { marker: ListMarker; markerEnd: number } | null {
16+
interface ParsedListMarker {
17+
marker: ListMarker;
18+
markerEnd: number;
19+
/** Column a nested item must reach to belong to this item. */
20+
contentColumn: number;
21+
}
22+
23+
function parseListMarker(line: string): ParsedListMarker | null {
1724
const indent = line.match(/^[ \t]*/)?.[0] ?? "";
1825
const rest = line.slice(indent.length);
1926
const ordered = rest.match(/^(\d+)([.)])((?:[ \t]+)|\s*$)/);
2027
if (ordered && ordered[1] !== undefined && ordered[2] !== undefined) {
28+
const space = ordered[3] ?? "";
2129
return {
2230
marker: {
2331
kind: "ordered",
2432
indent,
2533
numberText: ordered[1],
2634
delimiter: ordered[2] === ")" ? ")" : ".",
27-
space: ordered[3] ?? "",
35+
space,
2836
},
2937
markerEnd: indent.length + ordered[0].length,
38+
contentColumn: contentColumn(indent, ordered[1].length + 1, space),
3039
};
3140
}
32-
const task = rest.match(/^-\s\[[ xX]\]((?:[ \t]+)|\s*$)/);
33-
if (task) {
41+
// The checkbox is item content, so a task's content starts after "- ".
42+
const task = rest.match(/^-(\s)\[[ xX]\]((?:[ \t]+)|\s*$)/);
43+
if (task && task[1] !== undefined) {
3444
return {
35-
marker: { kind: "task", indent, space: task[1] ?? "" },
45+
marker: { kind: "task", indent, space: task[2] ?? "" },
3646
markerEnd: indent.length + task[0].length,
47+
contentColumn: contentColumn(indent, 1, task[1]),
3748
};
3849
}
3950
const bullet = rest.match(/^([-*+])((?:[ \t]+)|\s*$)/);
4051
if (bullet && bullet[1] !== undefined) {
52+
const space = bullet[2] ?? "";
4153
return {
42-
marker: { kind: "bullet", indent, bullet: bullet[1], space: bullet[2] ?? "" },
54+
marker: { kind: "bullet", indent, bullet: bullet[1], space },
4355
markerEnd: indent.length + bullet[0].length,
56+
contentColumn: contentColumn(indent, 1, space),
4457
};
4558
}
4659
return null;
4760
}
4861

62+
// CommonMark content follows 1-4 columns of padding; wider padding is code, so the content
63+
// column sits one column past the marker.
64+
function contentColumn(indent: string, markerWidth: number, padding: string): number {
65+
const markerEnd = indentWidth(indent) + markerWidth;
66+
const space = indentWidth(padding, markerEnd);
67+
return markerEnd + (space >= 1 && space <= 4 ? space : 1);
68+
}
69+
4970
export function nextOrderedMarkerText(marker: string): string {
5071
const numberText = marker.slice(0, -1);
5172
const delimiter = marker.slice(-1);
@@ -67,14 +88,14 @@ function nextMarkerText(marker: ListMarker): string {
6788
}
6889

6990
// Markdown tabs advance to the next four-column stop.
70-
function indentWidth(line: string): number {
71-
let width = 0;
91+
function indentWidth(line: string, fromColumn = 0): number {
92+
let column = fromColumn;
7293
for (const char of line) {
73-
if (char === " ") width += 1;
74-
else if (char === "\t") width += 4 - (width % 4);
94+
if (char === " ") column += 1;
95+
else if (char === "\t") column += 4 - (column % 4);
7596
else break;
7697
}
77-
return width;
98+
return column - fromColumn;
7899
}
79100

80101
function renumberFollowingItems(
@@ -149,6 +170,55 @@ function currentLine(value: string, cursor: number): { start: number; end: numbe
149170
return { start, end, text: value.slice(start, end) };
150171
}
151172

173+
function* listMarkersAbove(
174+
value: string,
175+
lineStart: number,
176+
tokens: ReturnType<typeof inlineTokenRanges>,
177+
): Generator<ParsedListMarker> {
178+
let tokenIndex = tokens.length - 1;
179+
while (lineStart > 0) {
180+
const lineEnd = lineStart - 1;
181+
lineStart = value.lastIndexOf("\n", lineEnd - 1) + 1;
182+
// A newline inside a chip does not start a list item.
183+
for (let token = tokens[tokenIndex]; token; token = tokens[tokenIndex]) {
184+
if (token.start >= lineStart) tokenIndex -= 1;
185+
else if (token.end > lineStart) lineStart = value.lastIndexOf("\n", token.start - 1) + 1;
186+
else break;
187+
}
188+
const parsed = parseListMarker(value.slice(lineStart, lineEnd));
189+
if (!parsed) return;
190+
yield parsed;
191+
}
192+
}
193+
194+
function parentMarker(
195+
value: string,
196+
lineStart: number,
197+
marker: ListMarker,
198+
tokens: ReturnType<typeof inlineTokenRanges>,
199+
): ListMarker | null {
200+
const width = indentWidth(marker.indent);
201+
for (const above of listMarkersAbove(value, lineStart, tokens)) {
202+
if (indentWidth(above.marker.indent) < width) return above.marker;
203+
}
204+
return null;
205+
}
206+
207+
function nestedNumber(
208+
marker: ListMarker,
209+
previousChild: ListMarker | undefined,
210+
): { replacedLength: number; text: string } {
211+
if (marker.kind !== "ordered") return { replacedLength: 0, text: "" };
212+
const continues =
213+
previousChild?.kind === "ordered" && previousChild.delimiter === marker.delimiter;
214+
return {
215+
replacedLength: marker.numberText.length,
216+
text: continues
217+
? nextOrderedMarkerText(`${previousChild.numberText}${marker.delimiter}`).slice(0, -1)
218+
: "1",
219+
};
220+
}
221+
152222
export function listContinuationForEnter(value: string, cursor: number): ComposerListEdit | null {
153223
if (!Number.isInteger(cursor) || cursor < 0 || cursor > value.length) return null;
154224
const line = currentLine(value, cursor);
@@ -159,12 +229,21 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
159229
const tokens = inlineTokenRanges(value);
160230
if (isInsideInlineToken(tokens, cursor)) return null;
161231
if (value.slice(markerEnd, line.end).trim() === "") {
162-
return {
163-
start: line.start,
164-
end: Math.max(cursor, markerEnd),
165-
replacement: "",
166-
cursorAfter: line.start,
167-
};
232+
const parent = parentMarker(value, line.start, parsed.marker, tokens);
233+
const replacement = parent ? nextMarkerText(parent) : "";
234+
const cursorAfter = line.start + replacement.length;
235+
const end = Math.max(cursor, markerEnd);
236+
const renumbered =
237+
parent?.kind === "ordered" ? renumberFollowingItems(value, line.end, parent, tokens) : null;
238+
if (renumbered) {
239+
return {
240+
start: line.start,
241+
end: renumbered.end,
242+
replacement: replacement + value.slice(end, line.end) + renumbered.text,
243+
cursorAfter,
244+
};
245+
}
246+
return { start: line.start, end, replacement, cursorAfter };
168247
}
169248
const insertion = `\n${nextMarkerText(parsed.marker)}`;
170249
const cursorAfter = cursor + insertion.length;
@@ -190,7 +269,36 @@ export function listIndentForTab(
190269
): ComposerListEdit | null {
191270
if (!Number.isInteger(start) || start !== end || start < 0 || start > value.length) return null;
192271
const line = currentLine(value, start);
193-
if (!parseListMarker(line.text)) return null;
194-
if (isInsideInlineToken(inlineTokenRanges(value), start)) return null;
272+
const parsed = parseListMarker(line.text);
273+
if (!parsed) return null;
274+
const tokens = inlineTokenRanges(value);
275+
if (isInsideInlineToken(tokens, start)) return null;
276+
const { marker } = parsed;
277+
const width = indentWidth(marker.indent);
278+
let previousChild: ListMarker | undefined;
279+
for (const above of listMarkersAbove(value, line.start, tokens)) {
280+
const aboveWidth = indentWidth(above.marker.indent);
281+
if (aboveWidth < width) break;
282+
if (aboveWidth > width) {
283+
if (!previousChild || aboveWidth < indentWidth(previousChild.indent)) {
284+
previousChild = above.marker;
285+
}
286+
continue;
287+
}
288+
const indent =
289+
previousChild?.indent ?? above.marker.indent + " ".repeat(above.contentColumn - aboveWidth);
290+
const number = nestedNumber(marker, previousChild);
291+
const replacedEnd = line.start + marker.indent.length + number.replacedLength;
292+
const replacement = indent + number.text;
293+
return {
294+
start: line.start,
295+
end: replacedEnd,
296+
replacement,
297+
cursorAfter:
298+
start < replacedEnd
299+
? line.start + indent.length
300+
: start - replacedEnd + line.start + replacement.length,
301+
};
302+
}
195303
return { start: line.start, end: line.start, replacement: " ", cursorAfter: start + 2 };
196304
}

‎docs/user/composer.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ block. What you typed is what the agent receives, markers and numbering
2222
included, and `#1234` without a space still looks up a pull request.
2323

2424
Enter sends. In a list or quote, **Shift+Enter** continues it, and Shift+Enter
25-
on an empty line leaves it; **Tab** nests a list item. In a code block, Enter
25+
on an empty line moves out one level, then leaves it; **Tab** nests a list item
26+
under the one above. In a code block, Enter
2627
starts a new line at the current indentation, **Tab** and **Shift+Tab** indent
2728
the selected lines, and a closing ` ``` ` followed by Enter, or two blank lines
2829
at the end, leave the block. **Backspace** at the start of a code block turns it

0 commit comments

Comments
 (0)