Skip to content

Commit 0b4e119

Browse files
fix(web): Tab nests numbered list items in the composer
Tab on a list item only prepended two spaces and kept the marker, so `1. a` / Shift+Enter / Tab produced ` 2. ` instead of a nested `1.`. Two spaces under `1. ` is also not nesting in CommonMark (the child must reach the parent's content column), so the agent received a flat list. - Tab nests the item at the content column of the item above and numbers a nested ordered list from 1, or continues an existing sublist. - Shift+Enter on an empty nested item moves it up one level and continues the parent's numbering (the rich-text composer already did this; the plain composer left the whole list). Fixes #17124
1 parent 30cc788 commit 0b4e119

4 files changed

Lines changed: 191 additions & 23 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4489,9 +4489,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
44894489
edit.start,
44904490
edit.end,
44914491
edit.replacement,
4492-
key === "Tab"
4493-
? { expandedCursorAfterReplace: selection.start + edit.replacement.length }
4494-
: undefined,
4492+
edit.cursor === undefined ? undefined : { expandedCursorAfterReplace: edit.cursor },
44954493
)
44964494
) {
44974495
return true;

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,57 @@ describe("composer list continuation", () => {
6464
expect(listContinuationForEnter(value, cursor)).toBeNull();
6565
});
6666

67-
it("indents list items on Tab", () => {
68-
expect(applyEdit("- foo", listIndentForTab("- foo", 2, 2)!)).toBe(" - foo");
67+
it.each([
68+
["1. a\n 1. ", "1. a\n2. "],
69+
["1) a\n 1) x\n 2) ", "1) a\n 1) x\n2) "],
70+
["- a\n 1. ", "- a\n- "],
71+
["1. a\n - [ ] ", "1. a\n2. "],
72+
["1. a\n 1. x\n 1. ", "1. a\n 1. x\n 2. "],
73+
])("leaves one level of nesting on an empty nested item in %j", (value, expected) => {
74+
expect(applyEdit(value, listContinuationForEnter(value, value.length)!)).toBe(expected);
75+
});
76+
77+
it("exits the list on an empty item indented with no parent", () => {
78+
expect(applyEdit(" - ", listContinuationForEnter(" - ", 4)!)).toBe("");
79+
});
80+
});
81+
82+
describe("listIndentForTab", () => {
83+
function tab(value: string, cursor = value.length) {
84+
const edit = listIndentForTab(value, cursor, cursor);
85+
return edit && { value: applyEdit(value, edit), cursor: edit.cursor };
86+
}
87+
88+
it.each([
89+
// Children nest at the item's content column, so Markdown reads them as nested.
90+
["1. a\n2. ", "1. a\n 1. "],
91+
["1. a\n2. b", "1. a\n 1. b"],
92+
["10) a\n11) ", "10) a\n 1) "],
93+
["1. a\n 1. x\n2. ", "1. a\n 1. x\n 2. "],
94+
["1. a\n 1. x\n 1. y\n2. ", "1. a\n 1. x\n 1. y\n 2. "],
95+
["1. a\n - x\n2. ", "1. a\n - x\n 1. "],
96+
["1. a\n 1. x\n2. ", "1. a\n 1. x\n 2. "],
97+
["- a\n- ", "- a\n - "],
98+
["- [ ] a\n- [ ] ", "- [ ] a\n - [ ] "],
99+
["- a\n1. ", "- a\n 1. "],
100+
])("nests the last item of %j", (value, expected) => {
101+
const result = tab(value);
102+
expect(result?.value).toBe(expected);
103+
expect(result?.cursor).toBe(expected.length);
104+
});
105+
106+
it("keeps the caret on the item text", () => {
107+
const value = "1. a\n2. bc";
108+
expect(tab(value, value.length - 1)).toEqual({ value: "1. a\n 1. bc", cursor: 12 });
109+
});
110+
111+
it("indents by two spaces with no item to nest under", () => {
112+
expect(tab("- foo", 2)).toEqual({ value: " - foo", cursor: 4 });
113+
expect(tab("text\n1. ")).toEqual({ value: "text\n 1. ", cursor: 10 });
114+
expect(tab("1. a\n 1. ")).toEqual({ value: "1. a\n 1. ", cursor: 13 });
115+
});
116+
117+
it("ignores non-list lines and ranged selections", () => {
69118
expect(listIndentForTab("plain", 2, 2)).toBeNull();
70119
expect(listIndentForTab("- foo", 1, 3)).toBeNull();
71120
});

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

Lines changed: 137 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
55
*
66
* Implemented once at the ChatComposer level (store replacement), so both
77
* composer modes behave identically and serialize identically:
8-
* Shift+Enter on a list item continues it, Enter on an empty item exits the
9-
* list, and Tab indents the item. Plain Markdown markers only — no real list
10-
* nodes anywhere.
8+
* Shift+Enter on a list item continues it, Enter on an empty item leaves one
9+
* level of nesting (or the list), and Tab nests the item under the one above.
10+
* Plain Markdown markers only — no real list nodes anywhere.
1111
*/
1212

1313
export interface ComposerListEdit {
1414
/** Expanded (plain string) offsets into the prompt. */
1515
start: number;
1616
end: number;
1717
replacement: string;
18+
/** Expanded caret after the edit; defaults to the end of the replacement. */
19+
cursor?: number;
1820
}
1921

2022
type ListMarker =
@@ -98,17 +100,63 @@ function isInsideInlineToken(value: string, cursor: number): boolean {
98100
return false;
99101
}
100102

101-
function currentLine(value: string, cursor: number): { start: number; end: number; text: string } {
103+
interface Line {
104+
start: number;
105+
end: number;
106+
text: string;
107+
}
108+
109+
function currentLine(value: string, cursor: number): Line {
102110
const start = value.lastIndexOf("\n", cursor - 1) + 1;
103111
const endIndex = value.indexOf("\n", cursor);
104112
const end = endIndex === -1 ? value.length : endIndex;
105113
return { start, end, text: value.slice(start, end) };
106114
}
107115

116+
/** The list lines directly above `line`, nearest first, up to the first non-list line. */
117+
function* listLinesAbove(value: string, line: Line): Generator<ListMarker> {
118+
let start = line.start;
119+
while (start > 0) {
120+
const previous = currentLine(value, start - 1);
121+
const parsed = parseListMarker(previous.text);
122+
if (!parsed) return;
123+
yield parsed.marker;
124+
start = previous.start;
125+
}
126+
}
127+
128+
function isDeeper(indent: string, than: string): boolean {
129+
return indent.length > than.length && indent.startsWith(than);
130+
}
131+
132+
/**
133+
* How far a child must be indented to nest under this item: its content
134+
* column, as CommonMark counts it. Two spaces under `1. ` would read as a
135+
* sibling, so the agent would get a flat list.
136+
*/
137+
function childIndentWidth(marker: ListMarker): number {
138+
// A task's box is item content, so its children nest under the dash.
139+
if (marker.kind === "task") return 2;
140+
const markerText =
141+
marker.kind === "ordered" ? `${marker.numberText}${marker.delimiter}` : marker.bullet;
142+
const space = marker.space.length;
143+
return markerText.length + (space >= 1 && space <= 4 ? space : 1);
144+
}
145+
146+
/** The nearest item above at a shallower indent: the one `indent` nests under. */
147+
function parentMarker(value: string, line: Line, indent: string): ListMarker | null {
148+
if (indent === "") return null;
149+
for (const marker of listLinesAbove(value, line)) {
150+
if (isDeeper(indent, marker.indent)) return marker;
151+
}
152+
return null;
153+
}
154+
108155
/**
109-
* Enter on a list item line: continue the list, or exit it when the item is
110-
* empty. Returns null for non-list lines, carets inside the marker, and
111-
* carets inside an inline chip — all fall through to a plain newline.
156+
* Enter on a list item line: continue the list, or, on an empty item, leave
157+
* one level of nesting or the list itself. Returns null for non-list lines,
158+
* carets inside the marker, and carets inside an inline chip — all fall
159+
* through to a plain newline.
112160
*/
113161
export function listContinuationForEnter(value: string, cursor: number): ComposerListEdit | null {
114162
if (!Number.isInteger(cursor) || cursor < 0 || cursor > value.length) return null;
@@ -119,8 +167,14 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
119167
if (cursor < markerEnd) return null;
120168
if (isInsideInlineToken(value, cursor)) return null;
121169
if (value.slice(markerEnd, line.end).trim() === "") {
122-
// Empty item: Enter exits the list by removing the marker.
123-
return { start: line.start, end: Math.max(cursor, markerEnd), replacement: "" };
170+
// Empty item: a nested one moves up a level and continues its parent's
171+
// list; a top-level one exits the list by removing the marker.
172+
const parent = parentMarker(value, line, parsed.marker.indent);
173+
return {
174+
start: line.start,
175+
end: Math.max(cursor, markerEnd),
176+
replacement: parent ? nextMarkerText(parent) : "",
177+
};
124178
}
125179
return {
126180
start: cursor,
@@ -129,19 +183,85 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
129183
};
130184
}
131185

186+
interface ListPrefix {
187+
indent: string;
188+
numberText?: string;
189+
}
190+
191+
/** The list item line at a collapsed caret, unless the caret splits an inline chip. */
192+
function listLineAt(
193+
value: string,
194+
start: number,
195+
end: number,
196+
): { line: Line; marker: ListMarker } | null {
197+
if (!Number.isInteger(start) || start !== end || start < 0 || start > value.length) return null;
198+
const line = currentLine(value, start);
199+
const parsed = parseListMarker(line.text);
200+
if (!parsed || isInsideInlineToken(value, start)) return null;
201+
return { line, marker: parsed.marker };
202+
}
203+
204+
/** Rewrites the item's indent and, for ordered items, its number, keeping the caret on the text. */
205+
function rewritePrefix(
206+
line: Line,
207+
marker: ListMarker,
208+
prefix: ListPrefix,
209+
cursor: number,
210+
): ComposerListEdit {
211+
const editEnd =
212+
line.start + marker.indent.length + (marker.kind === "ordered" ? marker.numberText.length : 0);
213+
const replacement = `${prefix.indent}${marker.kind === "ordered" ? (prefix.numberText ?? "1") : ""}`;
214+
const shift = replacement.length - (editEnd - line.start);
215+
return {
216+
start: line.start,
217+
end: editEnd,
218+
replacement,
219+
cursor: cursor >= editEnd ? cursor + shift : line.start + replacement.length,
220+
};
221+
}
222+
132223
/**
133-
* Tab on a list item line: indent by two spaces. Ranged selections, non-list
134-
* lines, and carets inside an inline chip fall through (Shift+Tab stays the
135-
* plan-mode toggle and is handled before this is consulted).
224+
* Tab on a list item line: nest it under the item above at the same indent,
225+
* written at that item's content column. A nested ordered item continues the
226+
* numbering of the sublist it joins, or starts it at 1. With no item to nest
227+
* under it indents by two spaces. Ranged selections, non-list lines, and
228+
* carets inside an inline chip fall through (Shift+Tab stays the plan-mode
229+
* toggle and is handled before this is consulted).
136230
*/
137231
export function listIndentForTab(
138232
value: string,
139233
start: number,
140234
end: number,
141235
): ComposerListEdit | null {
142-
if (!Number.isInteger(start) || start !== end || start < 0 || start > value.length) return null;
143-
const line = currentLine(value, start);
144-
if (!parseListMarker(line.text)) return null;
145-
if (isInsideInlineToken(value, start)) return null;
146-
return { start: line.start, end: line.start, replacement: " " };
236+
const at = listLineAt(value, start, end);
237+
if (!at) return null;
238+
const nested = nestedPrefix(value, at.line, at.marker);
239+
if (!nested) {
240+
return { start: at.line.start, end: at.line.start, replacement: " ", cursor: start + 2 };
241+
}
242+
return rewritePrefix(at.line, at.marker, nested, start);
243+
}
244+
245+
function nestedPrefix(value: string, line: Line, marker: ListMarker): ListPrefix | null {
246+
// Items already nested under the new parent, nearest first.
247+
const children: ListMarker[] = [];
248+
for (const above of listLinesAbove(value, line)) {
249+
if (isDeeper(above.indent, marker.indent)) {
250+
children.push(above);
251+
continue;
252+
}
253+
if (above.indent !== marker.indent) return null;
254+
// Join the sublist the item above already has, or start one at its content column.
255+
const sublistDepth = Math.min(...children.map((child) => child.indent.length));
256+
const lastChild = children.find((child) => child.indent.length === sublistDepth);
257+
const indent = lastChild?.indent ?? `${marker.indent}${" ".repeat(childIndentWidth(above))}`;
258+
if (marker.kind !== "ordered") return { indent };
259+
const sibling =
260+
lastChild?.kind === "ordered" && lastChild.delimiter === marker.delimiter ? lastChild : null;
261+
const numberText = sibling
262+
? nextOrderedMarkerText(`${sibling.numberText}${sibling.delimiter}`).slice(0, -1)
263+
: "1";
264+
return { indent, numberText };
265+
}
266+
return null;
147267
}

‎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 leaves it, one level of nesting at a time; **Tab** nests a
26+
list item, numbering a nested ordered list from 1. 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)