Changesets · Branches · Tags · Compare
| Changeset | 9faa443da3b13c4e0096b98a2c7c2f88ec4e35538475fd112726b628cc5dc191 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-09-03 17:17 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-09-03 17:17 -07:00 |
| Parents | 6c51ee08f10d |
1 file changed
modified gui/src/pages/gui/ToolCallDiv/FindAndReplace.tsx +114 −2
| @@ -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 { diffLines } from "diff"; | |
| 6 | +import { ChangeObject, 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"; |
| @@ -122,6 +122,107 @@ | ||
| 122 | 122 | } |
| 123 | 123 | }, [toolCallState?.status, toolCallState?.output]); |
| 124 | 124 | |
| 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 | + | |
| 125 | 226 | // Unified container component that always renders the same structure |
| 126 | 227 | const renderContainer = (content: React.ReactNode) => ( |
| 127 | 228 | <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"> |
| @@ -200,7 +301,18 @@ | ||
| 200 | 301 | <pre |
| 201 | 302 | className={`bg-editor m-0 w-full text-xs leading-tight ${config?.ui?.codeWrap ? "whitespace-pre-wrap" : "whitespace-pre"}`} |
| 202 | 303 | > |
| 203 | - {diffResult.diff.map((part, index) => { | |
| 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 | + | |
| 204 | 316 | if (part.removed) { |
| 205 | 317 | return ( |
| 206 | 318 | <div |