Elffuss BraidHub

kikocis/continue public

fix: clean up diff viewer
Changeseta70b3aacc1e399b98d9c7da3183060791b2d5baa642bded12f2780b1fb03478f
AuthorDallin Romney <dallinromney@gmail.com> · 2025-09-04 14:22 -07:00
CommitterDallin Romney <dallinromney@gmail.com> · 2025-09-04 14:22 -07:00
Parents9faa443da3b1

Browse the files at this changeset

1 file changed

modified gui/src/pages/gui/ToolCallDiv/FindAndReplace.tsx +77 −166
@@ -3,7 +3,7 @@
33 import { EditOperation } from "core/tools/definitions/multiEdit";
44 import { renderContextItems } from "core/util/messageContent";
55 import { getLastNPathParts, getUriPathBasename } from "core/util/uri";
6-import { ChangeObject, diffLines } from "diff";
6+import { diffLines } from "diff";
77 import { useContext, useMemo, useState } from "react";
88 import { ApplyActions } from "../../../components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions";
99 import { FileInfo } from "../../../components/StyledMarkdownPreview/StepContainerPreToolbar/FileInfo";
@@ -14,6 +14,7 @@
1414 selectToolCallById,
1515 } from "../../../redux/selectors/selectToolCalls";
1616 import { performFindAndReplace } from "../../../util/clientTools/findAndReplaceUtils";
17+import { cn } from "../../../util/cn";
1718 import { getStatusIcon } from "./utils";
1819
1920 interface FindAndReplaceDisplayProps {
@@ -25,6 +26,48 @@
2526 historyIndex: number;
2627 }
2728
29+const MAX_SAME_LINES = 2;
30+
31+function EllipsisLine() {
32+ return (
33+ <div className="text-description-muted px-3 py-1 text-center font-mono">
34+ ⋯
35+ </div>
36+ );
37+}
38+
39+function DiffLines({
40+ lines,
41+ className = "",
42+ diffChar = " ",
43+ diffCharClass = "text-description-muted",
44+}: {
45+ lines: string[];
46+ diffChar?: string;
47+ diffCharClass?: string;
48+ className?: string;
49+}) {
50+ return (
51+ <>
52+ {lines.map((line, lineIndex) => {
53+ const isLastPartLine = lineIndex === lines.length - 1;
54+ if (line === "" && isLastPartLine) return null;
55+ return (
56+ <div
57+ key={lineIndex}
58+ className={cn("text-foreground px-3 py-px font-mono", className)}
59+ >
60+ <span className={cn("mr-2 select-none", diffCharClass)}>
61+ {diffChar}
62+ </span>
63+ {line}
64+ </div>
65+ );
66+ })}
67+ </>
68+ );
69+}
70+
2871 export function FindAndReplaceDisplay({
2972 fileUri,
3073 relativeFilePath,
@@ -122,107 +165,6 @@
122165 }
123166 }, [toolCallState?.status, toolCallState?.output]);
124167
125- const processedDiff: (ChangeObject<string> | { ellipsis: true })[] =
126- useMemo(() => {
127- if (!diffResult?.diff) return [];
128-
129- const MAX_CONTEXT_LINES = 2;
130- const processed: (ChangeObject<string> | { ellipsis: true })[] = [];
131-
132- for (let i = 0; i < diffResult.diff.length; i++) {
133- const part = diffResult.diff[i];
134-
135- if (part.added || part.removed) {
136- processed.push(part);
137- } else {
138- // This is an unchanged part
139- const lines = part.value.split("\n");
140- // Remove empty line at the end if it exists
141- if (lines[lines.length - 1] === "") {
142- lines.pop();
143- }
144-
145- const isFirstPart = i === 0;
146- const isLastPart = i === diffResult.diff.length - 1;
147- const hasChangesAfter =
148- i < diffResult.diff.length - 1 &&
149- diffResult.diff.slice(i + 1).some((p) => p.added || p.removed);
150- const hasChangesBefore =
151- i > 0 &&
152- diffResult.diff.slice(0, i).some((p) => p.added || p.removed);
153-
154- let startIndex = 0;
155- let endIndex = lines.length;
156- let addEllipsisAtStart = false;
157- let addEllipsisAtEnd = false;
158-
159- if (isFirstPart && hasChangesAfter) {
160- // Show only last MAX_CONTEXT_LINES lines
161- const newStartIndex = Math.max(0, lines.length - MAX_CONTEXT_LINES);
162- if (newStartIndex > 0) {
163- addEllipsisAtStart = true;
164- }
165- startIndex = newStartIndex;
166- } else if (isLastPart && hasChangesBefore) {
167- // Show only first MAX_CONTEXT_LINES lines
168- const newEndIndex = Math.min(lines.length, MAX_CONTEXT_LINES);
169- if (newEndIndex < lines.length) {
170- addEllipsisAtEnd = true;
171- }
172- endIndex = newEndIndex;
173- } else if (
174- !isFirstPart &&
175- !isLastPart &&
176- (hasChangesBefore || hasChangesAfter)
177- ) {
178- // Show MAX_CONTEXT_LINES at start and end
179- if (lines.length > MAX_CONTEXT_LINES * 2) {
180- const contextLines = [
181- ...lines.slice(0, MAX_CONTEXT_LINES),
182- ...lines.slice(-MAX_CONTEXT_LINES),
183- ];
184- processed.push({
185- ...part,
186- value:
187- contextLines.slice(0, MAX_CONTEXT_LINES).join("\n") + "\n",
188- });
189-
190- // Add ellipsis indicator
191- processed.push({
192- ellipsis: true,
193- });
194-
195- processed.push({
196- ...part,
197- value: contextLines.slice(MAX_CONTEXT_LINES).join("\n") + "\n",
198- });
199- continue;
200- }
201- }
202-
203- if (startIndex < endIndex) {
204- const visibleLines = lines.slice(startIndex, endIndex);
205- if (visibleLines.length > 0) {
206- if (addEllipsisAtStart) {
207- processed.unshift({ ellipsis: true });
208- }
209-
210- processed.push({
211- ...part,
212- value: visibleLines.join("\n") + "\n",
213- });
214-
215- if (addEllipsisAtEnd) {
216- processed.push({ ellipsis: true });
217- }
218- }
219- }
220- }
221- }
222-
223- return processed;
224- }, [diffResult?.diff]);
225-
226168 // Unified container component that always renders the same structure
227169 const renderContainer = (content: React.ReactNode) => (
228170 <div className="outline-command-border -outline-offset-0.5 rounded-default bg-editor mx-2 my-1 flex min-w-0 flex-col outline outline-1">
@@ -301,81 +243,50 @@
301243 <pre
302244 className={`bg-editor m-0 w-full text-xs leading-tight ${config?.ui?.codeWrap ? "whitespace-pre-wrap" : "whitespace-pre"}`}
303245 >
304- {processedDiff.map((part, index) => {
305- if ("ellipsis" in part) {
306- return (
307- <div
308- key={index}
309- className="text-description-muted px-3 py-1 text-center font-mono"
310- >
311- ⋯
312- </div>
313- );
314- }
315-
246+ {diffResult.diff?.map((part, index) => {
316247 if (part.removed) {
317248 return (
318- <div
249+ <DiffLines
319250 key={index}
320- className="text-foreground border-l-4 border-red-900 bg-red-900/30"
321- >
322- {part.value.split("\n").map((line, lineIndex) => {
323- if (
324- line === "" &&
325- lineIndex === part.value.split("\n").length - 1
326- )
327- return null;
328- return (
329- <div key={lineIndex} className="px-3 py-px font-mono">
330- <span className="mr-2 select-none text-red-600">-</span>
331- {line}
332- </div>
333- );
334- })}
335- </div>
251+ lines={part.value.split("\n")}
252+ className="border-l-4 border-red-900 bg-red-900/30"
253+ diffCharClass="text-red-600"
254+ />
336255 );
337256 } else if (part.added) {
338257 return (
339- <div
258+ <DiffLines
340259 key={index}
341- className="text-foreground border-l-4 border-green-600 bg-green-600/20"
342- >
343- {part.value.split("\n").map((line, lineIndex) => {
344- if (
345- line === "" &&
346- lineIndex === part.value.split("\n").length - 1
347- )
348- return null;
349- return (
350- <div key={lineIndex} className="px-3 py-px font-mono">
351- <span className="mr-2 select-none text-green-600">+</span>
352- {line}
353- </div>
354- );
355- })}
356- </div>
260+ lines={part.value.split("\n")}
261+ diffCharClass="text-green-600"
262+ className="border-l-4 border-green-600 bg-green-600/20"
263+ />
357264 );
358265 } else {
266+ const isFirst = index === 0;
267+ const isLast = index === diffResult.diff.length - 1;
268+ const lines = part.value.split("\n");
269+ const showStartEllipsis = isFirst && lines.length > MAX_SAME_LINES;
270+ const showEndEllipsis = isLast && lines.length > MAX_SAME_LINES;
271+ const showMiddleEllipses =
272+ !isFirst && !isLast && lines.length > MAX_SAME_LINES * 2 + 1;
273+
274+ const startLines = showStartEllipsis
275+ ? lines.slice(-MAX_SAME_LINES)
276+ : showMiddleEllipses || showEndEllipsis
277+ ? lines.slice(0, MAX_SAME_LINES)
278+ : lines;
279+ const endLines = showMiddleEllipses
280+ ? lines.slice(-MAX_SAME_LINES)
281+ : [];
282+
359283 return (
360284 <div key={index}>
361- {part.value.split("\n").map((line, lineIndex) => {
362- if (
363- line === "" &&
364- lineIndex === part.value.split("\n").length - 1
365- )
366- return null;
367- return (
368- <div
369- key={lineIndex}
370- className="text-foreground px-3 py-px font-mono"
371- >
372- <span className="text-description-muted mr-2 select-none">
373- {" "}
374- </span>
375- {line}
376- </div>
377- );
378- })}
285+ {showStartEllipsis && <EllipsisLine />}
286+ <DiffLines lines={startLines} />
287+ {showMiddleEllipses && <EllipsisLine />}
288+ <DiffLines lines={endLines} />
289+ {showEndEllipsis && <EllipsisLine />}
379290 </div>
380291 );
381292 }