Open History to see the last commit
1import { splitPromptIntoComposerSegments } from "./composer-editor-mentions";
2
3/**
4 * List continuation and indentation for the composer.
5 *
6 * Implemented once at the ChatComposer level (store replacement), so both
7 * 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.
11 */
12
13export interface ComposerListEdit {
14 /** Expanded (plain string) offsets into the prompt. */
15 start: number;
16 end: number;
17 replacement: string;
18}
19
20type ListMarker =
21 | { kind: "ordered"; indent: string; numberText: string; delimiter: "." | ")"; space: string }
22 | { kind: "task"; indent: string; space: string }
23 | { kind: "bullet"; indent: string; bullet: string; space: string };
24
25function parseListMarker(line: string): { marker: ListMarker; markerEnd: number } | null {
26 const indent = line.match(/^[ \t]*/)?.[0] ?? "";
27 const rest = line.slice(indent.length);
28 const ordered = rest.match(/^(\d+)([.)])((?:[ \t]+)|\s*$)/);
29 if (ordered && ordered[1] !== undefined && ordered[2] !== undefined) {
30 return {
31 marker: {
32 kind: "ordered",
33 indent,
34 numberText: ordered[1],
35 delimiter: ordered[2] === ")" ? ")" : ".",
36 space: ordered[3] ?? "",
37 },
38 markerEnd: indent.length + ordered[0].length,
39 };
40 }
41 const task = rest.match(/^-\s\[[ xX]\]((?:[ \t]+)|\s*$)/);
42 if (task) {
43 return {
44 marker: { kind: "task", indent, space: task[1] ?? "" },
45 markerEnd: indent.length + task[0].length,
46 };
47 }
48 const bullet = rest.match(/^([-*+])((?:[ \t]+)|\s*$)/);
49 if (bullet && bullet[1] !== undefined) {
50 return {
51 marker: { kind: "bullet", indent, bullet: bullet[1], space: bullet[2] ?? "" },
52 markerEnd: indent.length + bullet[0].length,
53 };
54 }
55 return null;
56}
57
58/**
59 * The ordered marker that follows `3.` or `09)`: one higher, same delimiter,
60 * zero padding kept, and a number too large to count left as typed. Shared
61 * with the rich editor's native split so both modes count the same way.
62 */
63export function nextOrderedMarkerText(marker: string): string {
64 const numberText = marker.slice(0, -1);
65 const delimiter = marker.slice(-1);
66 const number = Number.parseInt(numberText, 10);
67 const next = Number.isSafeInteger(number)
68 ? String(number + 1).padStart(numberText.length, "0")
69 : numberText;
70 return `${next}${delimiter}`;
71}
72
73function nextMarkerText(marker: ListMarker): string {
74 if (marker.kind === "ordered") {
75 return `${marker.indent}${nextOrderedMarkerText(`${marker.numberText}${marker.delimiter}`)} `;
76 }
77 if (marker.kind === "task") {
78 return `${marker.indent}- [ ] `;
79 }
80 return `${marker.indent}${marker.bullet} `;
81}
82
83function segmentSource(
84 segment: ReturnType<typeof splitPromptIntoComposerSegments>[number],
85): string {
86 if (segment.type === "text") return segment.text;
87 return segment.source;
88}
89
90/** True when splitting at the caret would cut an inline chip in two. */
91function isInsideInlineToken(value: string, cursor: number): boolean {
92 let offset = 0;
93 for (const segment of splitPromptIntoComposerSegments(value)) {
94 const end = offset + segmentSource(segment).length;
95 if (segment.type !== "text" && cursor > offset && cursor < end) return true;
96 offset = end;
97 }
98 return false;
99}
100
101function currentLine(value: string, cursor: number): { start: number; end: number; text: string } {
102 const start = value.lastIndexOf("\n", cursor - 1) + 1;
103 const endIndex = value.indexOf("\n", cursor);
104 const end = endIndex === -1 ? value.length : endIndex;
105 return { start, end, text: value.slice(start, end) };
106}
107
108/**
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.
112 */
113export function listContinuationForEnter(value: string, cursor: number): ComposerListEdit | null {
114 if (!Number.isInteger(cursor) || cursor < 0 || cursor > value.length) return null;
115 const line = currentLine(value, cursor);
116 const parsed = parseListMarker(line.text);
117 if (!parsed) return null;
118 const markerEnd = line.start + parsed.markerEnd;
119 if (cursor < markerEnd) return null;
120 if (isInsideInlineToken(value, cursor)) return null;
121 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: "" };
124 }
125 return {
126 start: cursor,
127 end: cursor,
128 replacement: `\n${nextMarkerText(parsed.marker)}`,
129 };
130}
131
132/**
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).
136 */
137export function listIndentForTab(
138 value: string,
139 start: number,
140 end: number,
141): 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: " " };
147}