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
58function nextMarkerText(marker: ListMarker): string {
59 if (marker.kind === "ordered") {
60 const number = Number.parseInt(marker.numberText, 10);
61 const next = Number.isSafeInteger(number)
62 ? String(number + 1).padStart(marker.numberText.length, "0")
63 : marker.numberText;
64 return `${marker.indent}${next}${marker.delimiter} `;
65 }
66 if (marker.kind === "task") {
67 return `${marker.indent}- [ ] `;
68 }
69 return `${marker.indent}${marker.bullet} `;
70}
71
72function segmentSource(
73 segment: ReturnType<typeof splitPromptIntoComposerSegments>[number],
74): string {
75 if (segment.type === "text") return segment.text;
76 return segment.source;
77}
78
79/** True when splitting at the caret would cut an inline chip in two. */
80function isInsideInlineToken(value: string, cursor: number): boolean {
81 let offset = 0;
82 for (const segment of splitPromptIntoComposerSegments(value)) {
83 const end = offset + segmentSource(segment).length;
84 if (segment.type !== "text" && cursor > offset && cursor < end) return true;
85 offset = end;
86 }
87 return false;
88}
89
90function currentLine(value: string, cursor: number): { start: number; end: number; text: string } {
91 const start = value.lastIndexOf("\n", cursor - 1) + 1;
92 const endIndex = value.indexOf("\n", cursor);
93 const end = endIndex === -1 ? value.length : endIndex;
94 return { start, end, text: value.slice(start, end) };
95}
96
97/**
98 * Enter on a list item line: continue the list, or exit it when the item is
99 * empty. Returns null for non-list lines, carets inside the marker, and
100 * carets inside an inline chip — all fall through to a plain newline.
101 */
102export function listContinuationForEnter(value: string, cursor: number): ComposerListEdit | null {
103 if (!Number.isInteger(cursor) || cursor < 0 || cursor > value.length) return null;
104 const line = currentLine(value, cursor);
105 const parsed = parseListMarker(line.text);
106 if (!parsed) return null;
107 const markerEnd = line.start + parsed.markerEnd;
108 if (cursor < markerEnd) return null;
109 if (isInsideInlineToken(value, cursor)) return null;
110 if (value.slice(markerEnd, line.end).trim() === "") {
111 // Empty item: Enter exits the list by removing the marker.
112 return { start: line.start, end: Math.max(cursor, markerEnd), replacement: "" };
113 }
114 return {
115 start: cursor,
116 end: cursor,
117 replacement: `\n${nextMarkerText(parsed.marker)}`,
118 };
119}
120
121/**
122 * Tab on a list item line: indent by two spaces. Ranged selections, non-list
123 * lines, and carets inside an inline chip fall through (Shift+Tab stays the
124 * plan-mode toggle and is handled before this is consulted).
125 */
126export function listIndentForTab(
127 value: string,
128 start: number,
129 end: number,
130): ComposerListEdit | null {
131 if (!Number.isInteger(start) || start !== end || start < 0 || start > value.length) return null;
132 const line = currentLine(value, start);
133 if (!parseListMarker(line.text)) return null;
134 if (isInsideInlineToken(value, start)) return null;
135 return { start: line.start, end: line.start, replacement: " " };
136}