@devries-cameron20 / t3code Mirror · read-only / commits / fec83a18e3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 13:15 UTC

fix(web): numbered composer lists nest and lift correctly (#17138)

Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

michal-billtech committed Oct 28, 2026 at 19:08 UTC fec83a18e363cd0817064baeff181c3dd7e37f7d
3 files changed +174 -21
apps/web/src/composer-list-continuation.test.ts
+45 -1
@@ -1,6 +1,10 @@
1 import { describe, expect, it } from "vite-plus/test";
2
3 -import { listContinuationForEnter, nextOrderedMarkerText } from "./composer-list-continuation";
3 +import {
4 + listContinuationForEnter,
5 + listIndentForTab,
6 + nextOrderedMarkerText,
7 +} from "./composer-list-continuation";
8
9 function applyEdit(value: string, edit: { start: number; end: number; replacement: string }) {
10 return value.slice(0, edit.start) + edit.replacement + value.slice(edit.end);
@@ -31,6 +35,25 @@ describe("composer list continuation", () => {
35 expect(applyEdit("- [ ] ", listContinuationForEnter("- [ ] ", 6)!)).toBe("");
36 });
37
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 +
57 it("refuses to split a supplementary currency skill chip", () => {
58 const value = "1. 𑿝review go";
59 const cursor = value.indexOf(" go") - 1;
@@ -44,6 +67,27 @@ describe("composer list continuation", () => {
67 });
68 });
69
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 +
91 describe("nextOrderedMarkerText", () => {
92 it.each([
93 ["1.", "2."],
apps/web/src/composer-list-continuation.ts
+127 -19
@@ -13,39 +13,60 @@ type ListMarker =
13 | { kind: "task"; indent: string; space: string }
14 | { kind: "bullet"; indent: string; bullet: string; space: string };
15
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 {
24 const indent = line.match(/^[ \t]*/)?.[0] ?? "";
25 const rest = line.slice(indent.length);
26 const ordered = rest.match(/^(\d+)([.)])((?:[ \t]+)|\s*$)/);
27 if (ordered && ordered[1] !== undefined && ordered[2] !== undefined) {
28 + const space = ordered[3] ?? "";
29 return {
30 marker: {
31 kind: "ordered",
32 indent,
33 numberText: ordered[1],
34 delimiter: ordered[2] === ")" ? ")" : ".",
27 - space: ordered[3] ?? "",
35 + space,
36 },
37 markerEnd: indent.length + ordered[0].length,
38 + contentColumn: contentColumn(indent, ordered[1].length + 1, space),
39 };
40 }
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) {
44 return {
35 - marker: { kind: "task", indent, space: task[1] ?? "" },
45 + marker: { kind: "task", indent, space: task[2] ?? "" },
46 markerEnd: indent.length + task[0].length,
47 + contentColumn: contentColumn(indent, 1, task[1]),
48 };
49 }
50 const bullet = rest.match(/^([-*+])((?:[ \t]+)|\s*$)/);
51 if (bullet && bullet[1] !== undefined) {
52 + const space = bullet[2] ?? "";
53 return {
42 - marker: { kind: "bullet", indent, bullet: bullet[1], space: bullet[2] ?? "" },
54 + marker: { kind: "bullet", indent, bullet: bullet[1], space },
55 markerEnd: indent.length + bullet[0].length,
56 + contentColumn: contentColumn(indent, 1, space),
57 };
58 }
59 return null;
60 }
61
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 +
70 export function nextOrderedMarkerText(marker: string): string {
71 const numberText = marker.slice(0, -1);
72 const delimiter = marker.slice(-1);
@@ -67,14 +88,14 @@ function nextMarkerText(marker: ListMarker): string {
88 }
89
90 // 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;
93 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);
96 else break;
97 }
77 - return width;
98 + return column - fromColumn;
99 }
100
101 function renumberFollowingItems(
@@ -149,6 +170,55 @@ function currentLine(value: string, cursor: number): { start: number; end: numbe
170 return { start, end, text: value.slice(start, end) };
171 }
172
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 +
222 export function listContinuationForEnter(value: string, cursor: number): ComposerListEdit | null {
223 if (!Number.isInteger(cursor) || cursor < 0 || cursor > value.length) return null;
224 const line = currentLine(value, cursor);
@@ -159,12 +229,21 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
229 const tokens = inlineTokenRanges(value);
230 if (isInsideInlineToken(tokens, cursor)) return null;
231 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 };
247 }
248 const insertion = `\n${nextMarkerText(parsed.marker)}`;
249 const cursorAfter = cursor + insertion.length;
@@ -190,7 +269,36 @@ export function listIndentForTab(
269 ): ComposerListEdit | null {
270 if (!Number.isInteger(start) || start !== end || start < 0 || start > value.length) return null;
271 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 + }
303 return { start: line.start, end: line.start, replacement: " ", cursorAfter: start + 2 };
304 }
docs/user/composer.md
+2 -1
@@ -22,7 +22,8 @@ block. What you typed is what the agent receives, markers and numbering
22 included, and `#1234` without a space still looks up a pull request.
23
24 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
27 starts a new line at the current indentation, **Tab** and **Shift+Tab** indent
28 the selected lines, and a closing ` ``` ` followed by Enter, or two blank lines
29 at the end, leave the block. **Backspace** at the start of a code block turns it