Skip to content

Commit d407f54

Browse files
saphidgithub-actions[bot]claudejuliusmarminge
authored
fix(web): renumber following items when continuing a plain-Markdown numbered list (pingdotgg#14332)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Sonnet 5.5 <[email protected]> Co-authored-by: SAPHID <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent b177a16 commit d407f54

3 files changed

Lines changed: 132 additions & 26 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4216,14 +4216,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
42164216
: listIndentForTab(snapshot.value, selection.start, selection.end);
42174217
if (
42184218
edit &&
4219-
applyPromptReplacement(
4220-
edit.start,
4221-
edit.end,
4222-
edit.replacement,
4223-
key === "Tab"
4224-
? { expandedCursorAfterReplace: selection.start + edit.replacement.length }
4225-
: undefined,
4226-
)
4219+
applyPromptReplacement(edit.start, edit.end, edit.replacement, {
4220+
expandedCursorAfterReplace: edit.cursorAfter,
4221+
})
42274222
) {
42284223
return true;
42294224
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,37 @@ describe("nextOrderedMarkerText", () => {
8383
expect(nextOrderedMarkerText(marker)).toBe(expected);
8484
});
8585
});
86+
87+
describe("composer list renumbering", () => {
88+
it("renumbers following items with their padding and leaves the caret after the inserted marker", () => {
89+
const value = "1. @README.md go\n2. two\n03. three\n10. four";
90+
const edit = listContinuationForEnter(value, value.indexOf("go"))!;
91+
expect(applyEdit(value, edit)).toBe("1. @README.md \n2. go\n3. two\n04. three\n5. four");
92+
expect(edit.cursorAfter).toBe("1. @README.md \n2. ".length);
93+
});
94+
95+
it("renumbers indented paren siblings across tab-indented children and stops at a parent", () => {
96+
const value = " 1) a\n\t1) child\n 2) b\n1) parent\n 3) untouched";
97+
const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
98+
expect(applyEdit(value, edit)).toBe(
99+
" 1) a\n 2) \n\t1) child\n 3) b\n1) parent\n 3) untouched",
100+
);
101+
expect(edit.cursorAfter).toBe(" 1) a\n 2) ".length);
102+
});
103+
104+
it.each([
105+
["a blank line", ""],
106+
["plain text", "text"],
107+
["a different delimiter", "1) other list"],
108+
])("stops renumbering at %s", (_label, boundary) => {
109+
const value = `1. a\n2. b\n${boundary}\n3. untouched`;
110+
const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
111+
expect(applyEdit(value, edit)).toBe(`1. a\n2. \n3. b\n${boundary}\n3. untouched`);
112+
});
113+
114+
it("preserves multiline chip contents while renumbering surrounding items", () => {
115+
const value = '1. a\n2. @"file\n2. name" \n3. b';
116+
const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
117+
expect(applyEdit(value, edit)).toBe('1. a\n2. \n3. @"file\n2. name" \n4. b');
118+
});
119+
});

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

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export interface ComposerListEdit {
44
start: number;
55
end: number;
66
replacement: string;
7+
/** Expanded caret offset after the edit is applied. */
8+
cursorAfter: number;
79
}
810

911
type ListMarker =
@@ -64,21 +66,80 @@ function nextMarkerText(marker: ListMarker): string {
6466
return `${marker.indent}${marker.bullet} `;
6567
}
6668

67-
function segmentSource(
68-
segment: ReturnType<typeof splitPromptIntoComposerSegments>[number],
69-
): string {
70-
if (segment.type === "text") return segment.text;
71-
return segment.source;
69+
// Markdown tabs advance to the next four-column stop.
70+
function indentWidth(line: string): number {
71+
let width = 0;
72+
for (const char of line) {
73+
if (char === " ") width += 1;
74+
else if (char === "\t") width += 4 - (width % 4);
75+
else break;
76+
}
77+
return width;
78+
}
79+
80+
function renumberFollowingItems(
81+
value: string,
82+
afterLineEnd: number,
83+
marker: Extract<ListMarker, { kind: "ordered" }>,
84+
tokens: ReturnType<typeof inlineTokenRanges>,
85+
): { end: number; text: string } | null {
86+
const baseWidth = indentWidth(marker.indent);
87+
const parts: string[] = [];
88+
let expected = Number.parseInt(marker.numberText, 10) + 2;
89+
let position = afterLineEnd;
90+
let copiedUntil = afterLineEnd;
91+
let tokenIndex = 0;
92+
while (position < value.length && Number.isSafeInteger(expected)) {
93+
const lineStart = position + 1;
94+
const nextBreak = value.indexOf("\n", lineStart);
95+
const lineEnd = nextBreak === -1 ? value.length : nextBreak;
96+
position = lineEnd;
97+
let token = tokens[tokenIndex];
98+
while (token && token.end <= lineStart) {
99+
tokenIndex += 1;
100+
token = tokens[tokenIndex];
101+
}
102+
// A newline inside a chip does not start a list item.
103+
if (token && token.start < lineStart) continue;
104+
105+
const line = value.slice(lineStart, lineEnd);
106+
const width = indentWidth(line);
107+
if (line.trim() === "" || width < baseWidth) break;
108+
if (width > baseWidth) continue;
109+
const parsed = parseListMarker(line);
110+
if (parsed?.marker.kind !== "ordered" || parsed.marker.delimiter !== marker.delimiter) break;
111+
112+
const current = parsed.marker;
113+
const numberStart = lineStart + current.indent.length;
114+
const numberEnd = numberStart + current.numberText.length;
115+
const replacement = current.numberText.startsWith("0")
116+
? String(expected).padStart(current.numberText.length, "0")
117+
: String(expected);
118+
if (replacement !== current.numberText) {
119+
parts.push(value.slice(copiedUntil, numberStart), replacement);
120+
copiedUntil = numberEnd;
121+
}
122+
expected += 1;
123+
}
124+
return parts.length === 0 ? null : { end: copiedUntil, text: parts.join("") };
72125
}
73126

74-
function isInsideInlineToken(value: string, cursor: number): boolean {
127+
function inlineTokenRanges(value: string): Array<{ start: number; end: number }> {
128+
const ranges: Array<{ start: number; end: number }> = [];
75129
let offset = 0;
76130
for (const segment of splitPromptIntoComposerSegments(value)) {
77-
const end = offset + segmentSource(segment).length;
78-
if (segment.type !== "text" && cursor > offset && cursor < end) return true;
131+
const end = offset + (segment.type === "text" ? segment.text : segment.source).length;
132+
if (segment.type !== "text") ranges.push({ start: offset, end });
79133
offset = end;
80134
}
81-
return false;
135+
return ranges;
136+
}
137+
138+
function isInsideInlineToken(
139+
tokens: ReturnType<typeof inlineTokenRanges>,
140+
cursor: number,
141+
): boolean {
142+
return tokens.some((range) => cursor > range.start && cursor < range.end);
82143
}
83144

84145
function currentLine(value: string, cursor: number): { start: number; end: number; text: string } {
@@ -95,15 +156,31 @@ export function listContinuationForEnter(value: string, cursor: number): Compose
95156
if (!parsed) return null;
96157
const markerEnd = line.start + parsed.markerEnd;
97158
if (cursor < markerEnd) return null;
98-
if (isInsideInlineToken(value, cursor)) return null;
159+
const tokens = inlineTokenRanges(value);
160+
if (isInsideInlineToken(tokens, cursor)) return null;
99161
if (value.slice(markerEnd, line.end).trim() === "") {
100-
return { start: line.start, end: Math.max(cursor, markerEnd), replacement: "" };
162+
return {
163+
start: line.start,
164+
end: Math.max(cursor, markerEnd),
165+
replacement: "",
166+
cursorAfter: line.start,
167+
};
168+
}
169+
const insertion = `\n${nextMarkerText(parsed.marker)}`;
170+
const cursorAfter = cursor + insertion.length;
171+
const { marker } = parsed;
172+
if (marker.kind === "ordered") {
173+
const renumbered = renumberFollowingItems(value, line.end, marker, tokens);
174+
if (renumbered) {
175+
return {
176+
start: cursor,
177+
end: renumbered.end,
178+
replacement: insertion + value.slice(cursor, line.end) + renumbered.text,
179+
cursorAfter,
180+
};
181+
}
101182
}
102-
return {
103-
start: cursor,
104-
end: cursor,
105-
replacement: `\n${nextMarkerText(parsed.marker)}`,
106-
};
183+
return { start: cursor, end: cursor, replacement: insertion, cursorAfter };
107184
}
108185

109186
export function listIndentForTab(
@@ -114,6 +191,6 @@ export function listIndentForTab(
114191
if (!Number.isInteger(start) || start !== end || start < 0 || start > value.length) return null;
115192
const line = currentLine(value, start);
116193
if (!parseListMarker(line.text)) return null;
117-
if (isInsideInlineToken(value, start)) return null;
118-
return { start: line.start, end: line.start, replacement: " " };
194+
if (isInsideInlineToken(inlineTokenRanges(value), start)) return null;
195+
return { start: line.start, end: line.start, replacement: " ", cursorAfter: start + 2 };
119196
}

0 commit comments

Comments
 (0)