Skip to content
TypeScript

fix(web): Shift+Tab moves a nested composer list item back out (#27016)

tsmith1658003dd8c7bfa
diff --git a/apps/web/src/components/chat/composerCommandKey.ts b/apps/web/src/components/chat/composerCommandKey.ts
index 99dbbdea89c..33a17f1938f 100644
--- a/apps/web/src/components/chat/composerCommandKey.ts
+++ b/apps/web/src/components/chat/composerCommandKey.ts
@@ -20,7 +20,12 @@ import { type PendingUserInput } from "../../session-logic";
import { type PreviewAnnotationPayload } from "@t3tools/contracts";
import { type ReviewCommentContext } from "../../reviewCommentContext";
import { composerTargetKey } from "../../composerDraftStore";
-import { listContinuationForEnter, listIndentForTab } from "../../composer-list-continuation";
+import {
+ type ComposerListEdit,
+ listContinuationForEnter,
+ listIndentForTab,
+ listOutdentForTab,
+} from "../../composer-list-continuation";
import type { Dispatch, RefObject, SetStateAction } from "react";
/**
@@ -172,6 +177,25 @@ export function composerCommandKeyHandler(input: {
return true;
};
+ const applyListEdit = (
+ event: KeyboardEvent,
+ listEdit: (value: string, start: number, end: number) => ComposerListEdit | null,
+ ): boolean => {
+ if (isLiteralPendingAnswer || event.isComposing) return false;
+ const selection = composerEditorRef.current?.readSelectionRange();
+ const snapshot = readComposerSnapshot();
+ if (!selection || selection.start !== selection.end || snapshot.value !== promptRef.current) {
+ return false;
+ }
+ const edit = listEdit(snapshot.value, selection.start, selection.end);
+ return (
+ edit !== null &&
+ applyPromptReplacement(edit.start, edit.end, edit.replacement, {
+ expandedCursorAfterReplace: edit.cursorAfter,
+ })
+ );
+ };
+
const onComposerCommandKey = (key: string, event: KeyboardEvent, isTaskItem = false) => {
const submissionIntent = composerSubmissionIntentForKey({
event,
@@ -183,6 +207,7 @@ export function composerCommandKeyHandler(input: {
prompt: promptRef.current,
});
if (key === "Tab" && event.shiftKey && submissionIntent === null) {
+ if (applyListEdit(event, listOutdentForTab)) return true;
if (!planModeUiEnabled) return false;
toggleInteractionMode();
return true;
@@ -229,27 +254,8 @@ export function composerCommandKeyHandler(input: {
return true;
}
if (key === "Enter" && isTaskItem) return false;
- if (
- !isLiteralPendingAnswer &&
- !event.isComposing &&
- (key === "Enter" || (key === "Tab" && !event.shiftKey))
- ) {
- const selection = composerEditorRef.current?.readSelectionRange();
- const snapshot = readComposerSnapshot();
- if (selection && selection.start === selection.end && snapshot.value === promptRef.current) {
- const edit =
- key === "Enter"
- ? listContinuationForEnter(snapshot.value, selection.start)
- : listIndentForTab(snapshot.value, selection.start, selection.end);
- if (
- edit &&
- applyPromptReplacement(edit.start, edit.end, edit.replacement, {
- expandedCursorAfterReplace: edit.cursorAfter,
- })
- ) {
- return true;
- }
- }
+ if (key === "Enter" || (key === "Tab" && !event.shiftKey)) {
+ return applyListEdit(event, key === "Enter" ? listContinuationForEnter : listIndentForTab);
}
return false;
};
diff --git a/apps/web/src/composer-list-continuation.test.ts b/apps/web/src/composer-list-continuation.test.ts
index 12774bae205..92ff8b34434 100644
--- a/apps/web/src/composer-list-continuation.test.ts
+++ b/apps/web/src/composer-list-continuation.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
import {
listContinuationForEnter,
listIndentForTab,
+ listOutdentForTab,
nextOrderedMarkerText,
} from "./composer-list-continuation";
@@ -86,6 +87,30 @@ describe("composer list indentation", () => {
const edit = listIndentForTab(value, value.length, value.length)!;
expect(applyEdit(value, edit)).toBe("1. parent\n 1. first\n 2. second");
});
+
+ it.each(["- parent\n- child", "1. parent\n2. child", " 7. child"])(
+ "undoes Tab on %j with Shift+Tab and keeps the caret on its text",
+ (value) => {
+ const cursor = value.lastIndexOf("child") + 2;
+ const nest = listIndentForTab(value, cursor, cursor)!;
+ const nested = applyEdit(value, nest);
+ const edit = listOutdentForTab(nested, nest.cursorAfter, nest.cursorAfter)!;
+ expect(applyEdit(nested, edit)).toBe(value);
+ expect(edit.cursorAfter).toBe(cursor);
+ },
+ );
+
+ it.each([
+ ["1. a\n 1. b\n2. c", "1. a\n2. b\n3. c"],
+ ["- a\n 5. b", "- a\n5. b"],
+ ])("outdents %j to %j and renumbers only within the parent's list", (value, expected) => {
+ const edit = listOutdentForTab(value, value.indexOf("b"), value.indexOf("b"))!;
+ expect(applyEdit(value, edit)).toBe(expected);
+ });
+
+ it("leaves a top-level item alone so Shift+Tab can still switch Build/Plan", () => {
+ expect(listOutdentForTab("- item", 6, 6)).toBeNull();
+ });
});
describe("nextOrderedMarkerText", () => {
diff --git a/apps/web/src/composer-list-continuation.ts b/apps/web/src/composer-list-continuation.ts
index 583cf1bd04a..ca5c78daaa2 100644
--- a/apps/web/src/composer-list-continuation.ts
+++ b/apps/web/src/composer-list-continuation.ts
@@ -302,3 +302,41 @@ export function listIndentForTab(
}
return { start: line.start, end: line.start, replacement: " ", cursorAfter: start + 2 };
}
+
+export function listOutdentForTab(
+ value: string,
+ start: number,
+ end: number,
+): ComposerListEdit | null {
+ if (!Number.isInteger(start) || start !== end || start < 0 || start > value.length) return null;
+ const line = currentLine(value, start);
+ const parsed = parseListMarker(line.text);
+ if (!parsed?.marker.indent) return null;
+ const tokens = inlineTokenRanges(value);
+ if (isInsideInlineToken(tokens, start)) return null;
+ const { marker } = parsed;
+ const parent = parentMarker(value, line.start, marker, tokens);
+ // With no parent above, undo one step of Tab's two-space fallback.
+ const indent = parent?.indent ?? marker.indent.replace(/^(?:\t| {1,2})/, "");
+ const continues =
+ parent?.kind === "ordered" &&
+ marker.kind === "ordered" &&
+ parent.delimiter === marker.delimiter;
+ const number = continues ? nestedNumber(marker, parent) : { replacedLength: 0, text: "" };
+ const replacedEnd = line.start + marker.indent.length + number.replacedLength;
+ const replacement = indent + number.text;
+ const cursorAfter =
+ start < replacedEnd
+ ? line.start + indent.length
+ : start - replacedEnd + line.start + replacement.length;
+ const renumbered = continues ? renumberFollowingItems(value, line.end, parent, tokens) : null;
+ if (renumbered) {
+ return {
+ start: line.start,
+ end: renumbered.end,
+ replacement: replacement + value.slice(replacedEnd, line.end) + renumbered.text,
+ cursorAfter,
+ };
+ }
+ return { start: line.start, end: replacedEnd, replacement, cursorAfter };
+}
diff --git a/docs/user/composer.md b/docs/user/composer.md
index 714572dfd50..db41f6cc013 100644
--- a/docs/user/composer.md
+++ b/docs/user/composer.md
@@ -23,7 +23,7 @@ included, and `#1234` without a space still looks up a pull request.
Enter sends. In a list or quote, **Shift+Enter** continues it, and Shift+Enter
on an empty line moves out one level, then leaves it; **Tab** nests a list item
-under the one above. In a code block, Enter
+under the one above and **Shift+Tab** moves it back out. In a code block, Enter
starts a new line at the current indentation, **Tab** and **Shift+Tab** indent
the selected lines, and a closing ` ``` ` followed by Enter, or two blank lines
at the end, leave the block. **Backspace** at the start of a code block turns it