Add inline review comment selection to mobile diffs
- render comment rows and line selection state in the native diff view - wire long-press/tap gestures to start and complete review comments - tighten shared diff utilities and add coverage
This commit is contained in:
1 parent
a49c2d1301
commit
eba28035d2
15 files changed
+640
-37
No files matched your search
@@ -11,7 +11,7 @@ interface ExpoGlobalWithViewConfig {
|
||||
}
|
||||
|
||||
export interface NativeReviewDiffRow {
|
||||
readonly kind: "file" | "hunk" | "line" | "notice";
|
||||
readonly kind: "file" | "hunk" | "line" | "notice" | "comment";
|
||||
readonly id: string;
|
||||
readonly fileId?: string;
|
||||
readonly filePath?: string;
|
||||
@@ -31,6 +31,9 @@ export interface NativeReviewDiffRow {
|
||||
readonly oldLineNumber?: number | null;
|
||||
readonly newLineNumber?: number | null;
|
||||
readonly wordDiffRanges?: ReadonlyArray<NativeReviewDiffWordDiffRange>;
|
||||
readonly commentText?: string;
|
||||
readonly commentRangeLabel?: string;
|
||||
readonly commentSectionTitle?: string;
|
||||
}
|
||||
|
||||
export interface NativeReviewDiffWordDiffRange {
|
||||
@@ -100,6 +103,8 @@ export interface NativeReviewDiffViewProps extends ViewProps {
|
||||
readonly tokensResetKey?: string;
|
||||
readonly collapsedFileIdsJson?: string;
|
||||
readonly viewedFileIdsJson?: string;
|
||||
readonly selectedRowIdsJson?: string;
|
||||
readonly collapsedCommentIdsJson?: string;
|
||||
readonly appearanceScheme: "light" | "dark";
|
||||
readonly themeJson: string;
|
||||
readonly styleJson?: string;
|
||||
@@ -108,6 +113,17 @@ export interface NativeReviewDiffViewProps extends ViewProps {
|
||||
readonly onDebug?: (event: NativeSyntheticEvent<Record<string, unknown>>) => void;
|
||||
readonly onToggleFile?: (event: NativeSyntheticEvent<{ readonly fileId?: string }>) => void;
|
||||
readonly onToggleViewedFile?: (event: NativeSyntheticEvent<{ readonly fileId?: string }>) => void;
|
||||
readonly onPressLine?: (
|
||||
event: NativeSyntheticEvent<{
|
||||
readonly rowId?: string;
|
||||
readonly fileId?: string;
|
||||
readonly gesture?: "tap" | "longPress";
|
||||
readonly oldLineNumber?: number;
|
||||
readonly newLineNumber?: number;
|
||||
readonly change?: "context" | "add" | "delete";
|
||||
}>,
|
||||
) => void;
|
||||
readonly onToggleComment?: (event: NativeSyntheticEvent<{ readonly commentId?: string }>) => void;
|
||||
}
|
||||
|
||||
let cachedNativeReviewDiffView: ComponentType<NativeReviewDiffViewProps> | undefined;
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
countReviewCommentContexts,
|
||||
formatReviewCommentContext,
|
||||
parseReviewInlineComments,
|
||||
type ReviewCommentTarget,
|
||||
} from "./reviewCommentSelection";
|
||||
|
||||
function makeTarget(): ReviewCommentTarget {
|
||||
return {
|
||||
sectionId: "section-1",
|
||||
sectionTitle: "Working tree",
|
||||
filePath: "apps/demo/src/main.ts",
|
||||
startIndex: 0,
|
||||
endIndex: 1,
|
||||
lines: [
|
||||
{
|
||||
kind: "line",
|
||||
id: "line-1",
|
||||
change: "delete",
|
||||
oldLineNumber: 7,
|
||||
newLineNumber: null,
|
||||
content: "const retryLimit = 2;",
|
||||
additionTokenIndex: null,
|
||||
deletionTokenIndex: 0,
|
||||
comparison: null,
|
||||
},
|
||||
{
|
||||
kind: "line",
|
||||
id: "line-2",
|
||||
change: "add",
|
||||
oldLineNumber: null,
|
||||
newLineNumber: 7,
|
||||
content: "const retryLimit = 4;",
|
||||
additionTokenIndex: 0,
|
||||
deletionTokenIndex: null,
|
||||
comparison: null,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
describe("review comment serialization", () => {
|
||||
it("preserves enough metadata for inline diff rendering", () => {
|
||||
const serialized = formatReviewCommentContext(makeTarget(), "Please keep this configurable.");
|
||||
|
||||
expect(countReviewCommentContexts(serialized)).toBe(1);
|
||||
expect(parseReviewInlineComments(serialized)).toEqual([
|
||||
expect.objectContaining({
|
||||
sectionId: "section-1",
|
||||
sectionTitle: "Working tree",
|
||||
filePath: "apps/demo/src/main.ts",
|
||||
startIndex: 0,
|
||||
endIndex: 1,
|
||||
text: "Please keep this configurable.",
|
||||
}),
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import { useSyncExternalStore } from "react";
|
||||
import type { ReviewRenderableLineRow } from "./reviewModel";
|
||||
|
||||
export interface ReviewCommentTarget {
|
||||
readonly sectionId: string;
|
||||
readonly sectionTitle: string;
|
||||
readonly filePath: string;
|
||||
readonly lines: ReadonlyArray<ReviewRenderableLineRow>;
|
||||
@@ -10,8 +11,21 @@ export interface ReviewCommentTarget {
|
||||
readonly endIndex: number;
|
||||
}
|
||||
|
||||
export interface ReviewInlineComment {
|
||||
readonly id: string;
|
||||
readonly sectionId: string;
|
||||
readonly sectionTitle: string;
|
||||
readonly filePath: string;
|
||||
readonly startIndex: number;
|
||||
readonly endIndex: number;
|
||||
readonly rangeLabel: string;
|
||||
readonly text: string;
|
||||
}
|
||||
|
||||
let currentTarget: ReviewCommentTarget | null = null;
|
||||
const listeners = new Set<() => void>();
|
||||
const REVIEW_COMMENT_BLOCK_PATTERN = /<review_comment\b([^>]*)>\s*([\s\S]*?)<\/review_comment>/g;
|
||||
const REVIEW_COMMENT_ATTRIBUTE_PATTERN = /([a-zA-Z][a-zA-Z0-9_-]*)="([^"]*)"/g;
|
||||
|
||||
function emitChange() {
|
||||
listeners.forEach((listener) => listener());
|
||||
@@ -73,11 +87,12 @@ export function getReviewChangeMarker(change: ReviewRenderableLineRow["change"])
|
||||
}
|
||||
|
||||
export function buildReviewCommentTarget(
|
||||
target: Pick<ReviewCommentTarget, "sectionTitle" | "filePath" | "lines">,
|
||||
target: Pick<ReviewCommentTarget, "sectionId" | "sectionTitle" | "filePath" | "lines">,
|
||||
anchorIndex: number,
|
||||
lineIndex: number,
|
||||
): ReviewCommentTarget {
|
||||
return {
|
||||
sectionId: target.sectionId,
|
||||
sectionTitle: target.sectionTitle,
|
||||
filePath: target.filePath,
|
||||
lines: target.lines,
|
||||
@@ -142,9 +157,51 @@ function formatReviewSelectedDiff(target: ReviewCommentTarget): string {
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function escapeReviewCommentAttribute(value: string): string {
|
||||
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
||||
}
|
||||
|
||||
function unescapeReviewCommentAttribute(value: string): string {
|
||||
return value
|
||||
.replace(/</g, "<")
|
||||
.replace(/"/g, '"')
|
||||
.replace(/&/g, "&");
|
||||
}
|
||||
|
||||
function readReviewCommentAttributes(rawAttributes: string): Record<string, string> {
|
||||
const attributes: Record<string, string> = {};
|
||||
for (const match of rawAttributes.matchAll(REVIEW_COMMENT_ATTRIBUTE_PATTERN)) {
|
||||
attributes[match[1]!] = unescapeReviewCommentAttribute(match[2] ?? "");
|
||||
}
|
||||
return attributes;
|
||||
}
|
||||
|
||||
function readNonNegativeInteger(value: string | undefined): number | null {
|
||||
if (value === undefined || !/^\d+$/.test(value)) {
|
||||
return null;
|
||||
}
|
||||
return Number(value);
|
||||
}
|
||||
|
||||
function extractReviewCommentText(rawBody: string): string {
|
||||
const fenceIndex = rawBody.indexOf("```diff");
|
||||
const commentBody = fenceIndex >= 0 ? rawBody.slice(0, fenceIndex) : rawBody;
|
||||
return commentBody.trim();
|
||||
}
|
||||
|
||||
export function formatReviewCommentContext(target: ReviewCommentTarget, comment: string): string {
|
||||
const rangeLabel = formatReviewSelectedRangeLabel(target);
|
||||
return [
|
||||
"<review_comment>",
|
||||
[
|
||||
"<review_comment",
|
||||
` sectionId="${escapeReviewCommentAttribute(target.sectionId)}"`,
|
||||
` sectionTitle="${escapeReviewCommentAttribute(target.sectionTitle)}"`,
|
||||
` filePath="${escapeReviewCommentAttribute(target.filePath)}"`,
|
||||
` startIndex="${target.startIndex}"`,
|
||||
` endIndex="${target.endIndex}"`,
|
||||
` rangeLabel="${escapeReviewCommentAttribute(rangeLabel)}"`,
|
||||
">",
|
||||
].join(""),
|
||||
comment.trim(),
|
||||
"```diff",
|
||||
formatReviewSelectedDiff(target),
|
||||
@@ -154,5 +211,31 @@ export function formatReviewCommentContext(target: ReviewCommentTarget, comment:
|
||||
}
|
||||
|
||||
export function countReviewCommentContexts(value: string): number {
|
||||
return Array.from(value.matchAll(/<review_comment>/g)).length;
|
||||
return Array.from(value.matchAll(/<review_comment\b/g)).length;
|
||||
}
|
||||
|
||||
export function parseReviewInlineComments(value: string): ReadonlyArray<ReviewInlineComment> {
|
||||
const comments: ReviewInlineComment[] = [];
|
||||
for (const [index, match] of Array.from(value.matchAll(REVIEW_COMMENT_BLOCK_PATTERN)).entries()) {
|
||||
const attributes = readReviewCommentAttributes(match[1] ?? "");
|
||||
const startIndex = readNonNegativeInteger(attributes.startIndex);
|
||||
const endIndex = readNonNegativeInteger(attributes.endIndex);
|
||||
const filePath = attributes.filePath?.trim();
|
||||
const sectionId = attributes.sectionId?.trim();
|
||||
if (!filePath || !sectionId || startIndex === null || endIndex === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
comments.push({
|
||||
id: `review-comment:${index}:${sectionId}:${filePath}:${startIndex}:${endIndex}`,
|
||||
sectionId,
|
||||
sectionTitle: attributes.sectionTitle?.trim() || "Review",
|
||||
filePath,
|
||||
startIndex: Math.min(startIndex, endIndex),
|
||||
endIndex: Math.max(startIndex, endIndex),
|
||||
rangeLabel: attributes.rangeLabel?.trim() || "line",
|
||||
text: extractReviewCommentText(match[2] ?? ""),
|
||||
});
|
||||
}
|
||||
return comments;
|
||||
}
|
||||
@@ -110,6 +110,7 @@ function useMarkdownStyles(): MarkdownStyleSets {
|
||||
const codeBg = useThemeColor("--color-md-code-bg");
|
||||
const codeText = useThemeColor("--color-md-code-text");
|
||||
const hrColor = useThemeColor("--color-md-hr");
|
||||
const userBodyColor = useThemeColor("--color-user-bubble-foreground");
|
||||
const userCodeBg = useThemeColor("--color-md-user-code-bg");
|
||||
const userCodeText = useThemeColor("--color-md-user-code-text");
|
||||
const userFenceBg = useThemeColor("--color-md-user-fence-bg");
|
||||
@@ -124,6 +125,7 @@ function useMarkdownStyles(): MarkdownStyleSets {
|
||||
const markdownCodeBg = toMarkdownThemeColor(codeBg);
|
||||
const markdownCodeText = toMarkdownThemeColor(codeText);
|
||||
const markdownHrColor = toMarkdownThemeColor(hrColor);
|
||||
const markdownUserBodyColor = toMarkdownThemeColor(userBodyColor);
|
||||
const markdownUserCodeBg = toMarkdownThemeColor(userCodeBg);
|
||||
const markdownUserCodeText = toMarkdownThemeColor(userCodeText);
|
||||
const markdownUserFenceBg = toMarkdownThemeColor(userFenceBg);
|
||||
@@ -181,9 +183,17 @@ function useMarkdownStyles(): MarkdownStyleSets {
|
||||
list: { marginTop: 4, marginBottom: 4 },
|
||||
list_item: { marginTop: 0, marginBottom: 4 },
|
||||
task_list_item: { marginTop: 0, marginBottom: 4 },
|
||||
bold: { fontWeight: "700", color: markdownStrongColor, fontFamily: "DMSans_700Bold" },
|
||||
text: { lineHeight: 22 },
|
||||
bold: {
|
||||
fontWeight: "700",
|
||||
color: markdownStrongColor,
|
||||
fontFamily: "DMSans_700Bold",
|
||||
},
|
||||
italic: { fontStyle: "italic" },
|
||||
link: { color: markdownLinkColor, textDecorationLine: "underline" as const },
|
||||
link: {
|
||||
color: markdownLinkColor,
|
||||
textDecorationLine: "underline" as const,
|
||||
},
|
||||
blockquote: {
|
||||
borderLeftWidth: 3,
|
||||
borderLeftColor: markdownBlockquoteBorder,
|
||||
@@ -258,6 +268,9 @@ function useMarkdownStyles(): MarkdownStyleSets {
|
||||
...baseTheme,
|
||||
colors: {
|
||||
...baseTheme.colors,
|
||||
text: markdownUserBodyColor,
|
||||
heading: markdownUserBodyColor,
|
||||
link: markdownUserBodyColor,
|
||||
code: markdownUserCodeText,
|
||||
codeBackground: markdownUserCodeBg,
|
||||
border: markdownUserFenceBg,
|
||||
@@ -266,6 +279,19 @@ function useMarkdownStyles(): MarkdownStyleSets {
|
||||
const userStyles: NodeStyleOverrides = {
|
||||
...baseStyles,
|
||||
paragraph: { marginTop: 0, marginBottom: 0 },
|
||||
bold: {
|
||||
fontWeight: "700",
|
||||
color: markdownUserBodyColor,
|
||||
fontFamily: "DMSans_700Bold",
|
||||
},
|
||||
heading: {
|
||||
...baseStyles.heading,
|
||||
color: markdownUserBodyColor,
|
||||
},
|
||||
link: {
|
||||
color: markdownUserBodyColor,
|
||||
textDecorationLine: "underline" as const,
|
||||
},
|
||||
};
|
||||
|
||||
const assistantTheme: PartialMarkdownTheme = {
|
||||
@@ -312,6 +338,7 @@ function useMarkdownStyles(): MarkdownStyleSets {
|
||||
hrColor,
|
||||
linkColor,
|
||||
strongColor,
|
||||
userBodyColor,
|
||||
userCodeBg,
|
||||
userCodeText,
|
||||
userFenceBg,
|
||||
@@ -328,11 +355,12 @@ function renderFeedEntry(
|
||||
readonly onToggleWorkGroup: (groupId: string) => void;
|
||||
readonly onPressImage: (uri: string, headers?: Record<string, string>) => void;
|
||||
readonly iconSubtleColor: string | import("react-native").ColorValue;
|
||||
readonly userBubbleColor: string | import("react-native").ColorValue;
|
||||
readonly markdownStyles: MarkdownStyleSets;
|
||||
},
|
||||
) {
|
||||
const entry = info.item;
|
||||
const { markdownStyles, iconSubtleColor } = props;
|
||||
const { markdownStyles, iconSubtleColor, userBubbleColor } = props;
|
||||
|
||||
if (entry.type === "message") {
|
||||
const { message } = entry;
|
||||
@@ -344,7 +372,10 @@ function renderFeedEntry(
|
||||
if (isUser) {
|
||||
return (
|
||||
<View className="mb-3.5 items-end gap-1.5">
|
||||
<View className="max-w-[85%] gap-2 rounded-[22px] rounded-br-[10px] border border-blue-300/50 bg-blue-50/80 px-4 py-4 dark:border-blue-400/20 dark:bg-blue-500/12">
|
||||
<View
|
||||
className="max-w-[85%] gap-2 rounded-[22px] rounded-br-[6px] px-3.5 py-2.5"
|
||||
style={{ backgroundColor: userBubbleColor }}
|
||||
>
|
||||
{message.text.trim().length > 0 ? (
|
||||
<Markdown
|
||||
options={{ gfm: true }}
|
||||
@@ -372,14 +403,14 @@ function renderFeedEntry(
|
||||
>
|
||||
<Image
|
||||
source={{ uri, ...(headers ? { headers } : {}) }}
|
||||
className="aspect-[1.3] w-full rounded-[18px] bg-neutral-200 dark:bg-neutral-800"
|
||||
className="aspect-[1.3] w-full rounded-[14px] bg-white/15"
|
||||
resizeMode="cover"
|
||||
/>
|
||||
</TouchableOpacity>
|
||||
);
|
||||
})}
|
||||
</View>
|
||||
<Text className="px-1 text-right font-t3-medium text-xs text-neutral-500 dark:text-neutral-500">
|
||||
<Text className="px-1 text-right font-t3-medium text-xs text-neutral-600 dark:text-neutral-400">
|
||||
{timestampLabel}
|
||||
</Text>
|
||||
</View>
|
||||
@@ -427,7 +458,7 @@ function renderFeedEntry(
|
||||
</TouchableOpacity>
|
||||
);
|
||||
})}
|
||||
<Text className="font-t3-medium text-xs text-neutral-500 dark:text-neutral-500">
|
||||
<Text className="font-t3-medium text-xs text-neutral-600 dark:text-neutral-400">
|
||||
{timestampLabel}
|
||||
</Text>
|
||||
</View>
|
||||
@@ -437,18 +468,21 @@ function renderFeedEntry(
|
||||
if (entry.type === "queued-message") {
|
||||
return (
|
||||
<View className="mb-3.5 gap-1.5 items-end">
|
||||
<View className="max-w-[85%] gap-2 rounded-[22px] rounded-br-[10px] border border-sky-300/60 bg-sky-100/75 px-4 py-4 dark:border-sky-300/20 dark:bg-sky-400/10">
|
||||
<Text className="font-sans text-[15px] leading-[22px] text-neutral-950 dark:text-neutral-50">
|
||||
<View
|
||||
className="max-w-[85%] gap-2 rounded-[22px] rounded-br-[6px] px-3.5 py-2.5 opacity-60"
|
||||
style={{ backgroundColor: userBubbleColor }}
|
||||
>
|
||||
<Text className="font-sans text-[15px] leading-[22px] text-white">
|
||||
{entry.queuedMessage.text}
|
||||
</Text>
|
||||
{entry.queuedMessage.attachments.length > 0 ? (
|
||||
<Text className="font-t3-medium text-xs text-neutral-500 dark:text-neutral-500">
|
||||
<Text className="font-t3-medium text-xs text-white/75">
|
||||
{entry.queuedMessage.attachments.length} image
|
||||
{entry.queuedMessage.attachments.length === 1 ? "" : "s"} attached
|
||||
</Text>
|
||||
) : null}
|
||||
</View>
|
||||
<Text className="px-1 text-right font-t3-medium text-xs text-neutral-500 dark:text-neutral-500">
|
||||
<Text className="px-1 text-right font-t3-medium text-xs text-neutral-600 dark:text-neutral-400">
|
||||
{entry.sending ? "dispatching" : `${relativeTime(entry.createdAt)} • pending`}
|
||||
</Text>
|
||||
</View>
|
||||
@@ -537,6 +571,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
|
||||
const topContentInset = insets.top + IOS_NAV_BAR_HEIGHT;
|
||||
|
||||
const iconSubtleColor = useThemeColor("--color-icon-subtle");
|
||||
const userBubbleColor = useThemeColor("--color-user-bubble");
|
||||
const markdownStyles = useMarkdownStyles();
|
||||
|
||||
useEffect(() => {
|
||||
@@ -599,12 +634,14 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
|
||||
onToggleWorkGroup,
|
||||
onPressImage,
|
||||
iconSubtleColor,
|
||||
userBubbleColor,
|
||||
markdownStyles,
|
||||
}),
|
||||
[
|
||||
copiedRowId,
|
||||
expandedWorkGroups,
|
||||
iconSubtleColor,
|
||||
userBubbleColor,
|
||||
markdownStyles,
|
||||
onCopyWorkRow,
|
||||
onPressImage,
|
||||
@@ -652,7 +689,9 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
|
||||
keyboardShouldPersistTaps="handled"
|
||||
estimatedItemSize={80}
|
||||
initialScrollAtEnd
|
||||
maintainScrollAtEnd={{ on: { layout: true, itemLayout: true, dataChange: true } }}
|
||||
maintainScrollAtEnd={{
|
||||
on: { layout: true, itemLayout: true, dataChange: true },
|
||||
}}
|
||||
maintainScrollAtEndThreshold={0.1}
|
||||
refreshing={props.refreshing ?? false}
|
||||
onRefresh={props.onRefresh}
|
||||
|
||||
Reference in new issue
Block a user