Changesets · Branches · Tags · Compare
| Changeset | a70b3aacc1e399b98d9c7da3183060791b2d5baa642bded12f2780b1fb03478f |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-09-04 14:22 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-09-04 14:22 -07:00 |
| Parents | 9faa443da3b1 |
1 file changed
modified gui/src/pages/gui/ToolCallDiv/FindAndReplace.tsx +77 −166
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | import { EditOperation } from "core/tools/definitions/multiEdit"; |
| 4 | 4 | import { renderContextItems } from "core/util/messageContent"; |
| 5 | 5 | import { getLastNPathParts, getUriPathBasename } from "core/util/uri"; |
| 6 | -import { ChangeObject, diffLines } from "diff"; | |
| 6 | +import { diffLines } from "diff"; | |
| 7 | 7 | import { useContext, useMemo, useState } from "react"; |
| 8 | 8 | import { ApplyActions } from "../../../components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions"; |
| 9 | 9 | import { FileInfo } from "../../../components/StyledMarkdownPreview/StepContainerPreToolbar/FileInfo"; |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | selectToolCallById, |
| 15 | 15 | } from "../../../redux/selectors/selectToolCalls"; |
| 16 | 16 | import { performFindAndReplace } from "../../../util/clientTools/findAndReplaceUtils"; |
| 17 | +import { cn } from "../../../util/cn"; | |
| 17 | 18 | import { getStatusIcon } from "./utils"; |
| 18 | 19 | |
| 19 | 20 | interface FindAndReplaceDisplayProps { |
| @@ -25,6 +26,48 @@ | ||
| 25 | 26 | historyIndex: number; |
| 26 | 27 | } |
| 27 | 28 | |
| 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 | + | |
| 28 | 71 | export function FindAndReplaceDisplay({ |
| 29 | 72 | fileUri, |
| 30 | 73 | relativeFilePath, |
| @@ -122,107 +165,6 @@ | ||
| 122 | 165 | } |
| 123 | 166 | }, [toolCallState?.status, toolCallState?.output]); |
| 124 | 167 | |
| 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 | - | |
| 226 | 168 | // Unified container component that always renders the same structure |
| 227 | 169 | const renderContainer = (content: React.ReactNode) => ( |
| 228 | 170 | <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 @@ | ||
| 301 | 243 | <pre |
| 302 | 244 | className={`bg-editor m-0 w-full text-xs leading-tight ${config?.ui?.codeWrap ? "whitespace-pre-wrap" : "whitespace-pre"}`} |
| 303 | 245 | > |
| 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) => { | |
| 316 | 247 | if (part.removed) { |
| 317 | 248 | return ( |
| 318 | - <div | |
| 249 | + <DiffLines | |
| 319 | 250 | 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 | + /> | |
| 336 | 255 | ); |
| 337 | 256 | } else if (part.added) { |
| 338 | 257 | return ( |
| 339 | - <div | |
| 258 | + <DiffLines | |
| 340 | 259 | 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 | + /> | |
| 357 | 264 | ); |
| 358 | 265 | } 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 | + | |
| 359 | 283 | return ( |
| 360 | 284 | <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 />} | |
| 379 | 290 | </div> |
| 380 | 291 | ); |
| 381 | 292 | } |