Changesets · Branches · Tags · Compare
| Changeset | 778571452873fbeb807253b3bd2732ded6463fe0760f93cb286a954abd235a8e |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-07-04 20:04 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-07-04 20:04 -07:00 |
| Parents | 02a83b8d1340 |
4 files changed
modified core/edit/searchAndReplace/findSearchMatch.test.ts +27 −0
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | expect(result).toEqual({ |
| 15 | 15 | startIndex: 0, |
| 16 | 16 | endIndex: fileContent.length, |
| 17 | + strategyName: "exactMatch", | |
| 17 | 18 | }); |
| 18 | 19 | }); |
| 19 | 20 | |
| @@ -32,6 +33,7 @@ | ||
| 32 | 33 | expect(result).toEqual({ |
| 33 | 34 | startIndex: 13, // After "const a = 1;\n" |
| 34 | 35 | endIndex: 13 + searchContent.length, |
| 36 | + strategyName: "exactMatch", | |
| 35 | 37 | }); |
| 36 | 38 | }); |
| 37 | 39 | |
| @@ -49,6 +51,7 @@ | ||
| 49 | 51 | expect(result).toEqual({ |
| 50 | 52 | startIndex: 13, // After "const a = 1;\n" |
| 51 | 53 | endIndex: fileContent.length, |
| 54 | + strategyName: "exactMatch", | |
| 52 | 55 | }); |
| 53 | 56 | }); |
| 54 | 57 | |
| @@ -63,6 +66,7 @@ | ||
| 63 | 66 | expect(result).toEqual({ |
| 64 | 67 | startIndex: 13, // After "const a = 1;\n" |
| 65 | 68 | endIndex: 13 + searchContent.length, |
| 69 | + strategyName: "exactMatch", | |
| 66 | 70 | }); |
| 67 | 71 | }); |
| 68 | 72 | |
| @@ -80,6 +84,7 @@ | ||
| 80 | 84 | expect(result).toEqual({ |
| 81 | 85 | startIndex: 18, |
| 82 | 86 | endIndex: 18 + searchContent.length, |
| 87 | + strategyName: "exactMatch", | |
| 83 | 88 | }); |
| 84 | 89 | }); |
| 85 | 90 | }); |
| @@ -98,6 +103,7 @@ | ||
| 98 | 103 | expect(result).toEqual({ |
| 99 | 104 | startIndex: 0, |
| 100 | 105 | endIndex: fileContent.length, // Uses trimmed length |
| 106 | + strategyName: "trimmedMatch", | |
| 101 | 107 | }); |
| 102 | 108 | }); |
| 103 | 109 | |
| @@ -117,6 +123,7 @@ | ||
| 117 | 123 | expect(result).toEqual({ |
| 118 | 124 | startIndex: 12, |
| 119 | 125 | endIndex: 12 + searchContent.length, |
| 126 | + strategyName: "exactMatch", | |
| 120 | 127 | }); |
| 121 | 128 | }); |
| 122 | 129 | |
| @@ -134,6 +141,7 @@ | ||
| 134 | 141 | expect(result).toEqual({ |
| 135 | 142 | startIndex: expectedStart, |
| 136 | 143 | endIndex: expectedStart + trimmedSearchContent.length, |
| 144 | + strategyName: "trimmedMatch", | |
| 137 | 145 | }); |
| 138 | 146 | }); |
| 139 | 147 | }); |
| @@ -150,6 +158,7 @@ | ||
| 150 | 158 | expect(result).toEqual({ |
| 151 | 159 | startIndex: 0, |
| 152 | 160 | endIndex: 0, |
| 161 | + strategyName: "emptySearch", | |
| 153 | 162 | }); |
| 154 | 163 | }); |
| 155 | 164 | |
| @@ -164,6 +173,7 @@ | ||
| 164 | 173 | expect(result).toEqual({ |
| 165 | 174 | startIndex: 0, |
| 166 | 175 | endIndex: 0, |
| 176 | + strategyName: "emptySearch", | |
| 167 | 177 | }); |
| 168 | 178 | }); |
| 169 | 179 | |
| @@ -176,6 +186,7 @@ | ||
| 176 | 186 | expect(result).toEqual({ |
| 177 | 187 | startIndex: 0, |
| 178 | 188 | endIndex: 0, |
| 189 | + strategyName: "emptySearch", | |
| 179 | 190 | }); |
| 180 | 191 | }); |
| 181 | 192 | }); |
| @@ -256,6 +267,7 @@ | ||
| 256 | 267 | expect(result).toEqual({ |
| 257 | 268 | startIndex: expectedStart, |
| 258 | 269 | endIndex: expectedStart + searchContent.length, |
| 270 | + strategyName: "exactMatch", | |
| 259 | 271 | }); |
| 260 | 272 | }); |
| 261 | 273 | |
| @@ -271,6 +283,7 @@ | ||
| 271 | 283 | expect(result).toEqual({ |
| 272 | 284 | startIndex: expectedStart, |
| 273 | 285 | endIndex: expectedStart + searchContent.length, |
| 286 | + strategyName: "exactMatch", | |
| 274 | 287 | }); |
| 275 | 288 | }); |
| 276 | 289 | |
| @@ -292,6 +305,7 @@ | ||
| 292 | 305 | expect(result).toEqual({ |
| 293 | 306 | startIndex: 0, |
| 294 | 307 | endIndex: searchContent.length, |
| 308 | + strategyName: "exactMatch", | |
| 295 | 309 | }); |
| 296 | 310 | }); |
| 297 | 311 | |
| @@ -306,6 +320,7 @@ | ||
| 306 | 320 | expect(result).toEqual({ |
| 307 | 321 | startIndex: 0, |
| 308 | 322 | endIndex: searchContent.length, |
| 323 | + strategyName: "exactMatch", | |
| 309 | 324 | }); |
| 310 | 325 | }); |
| 311 | 326 | }); |
| @@ -326,6 +341,7 @@ | ||
| 326 | 341 | expect(result).not.toBeNull(); |
| 327 | 342 | expect(result!.startIndex).toBe(20); // After "function example() {\n" |
| 328 | 343 | expect(result!.endIndex).toBe(72); // End of " return x + y;" |
| 344 | + expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); | |
| 329 | 345 | |
| 330 | 346 | // Verify the matched content contains the original whitespace |
| 331 | 347 | const matchedContent = fileContent.substring( |
| @@ -349,6 +365,7 @@ | ||
| 349 | 365 | expect(result).not.toBeNull(); |
| 350 | 366 | expect(result!.startIndex).toBe(12); // After "const a = 1;" |
| 351 | 367 | expect(result!.endIndex).toBe(32); // End of "const b = 2;" |
| 368 | + expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); | |
| 352 | 369 | |
| 353 | 370 | // Verify the matched content preserves original spacing |
| 354 | 371 | const matchedContent = fileContent.substring( |
| @@ -374,6 +391,7 @@ | ||
| 374 | 391 | expect(result).not.toBeNull(); |
| 375 | 392 | expect(result!.startIndex).toBe(17); // After "function test() {" |
| 376 | 393 | expect(result!.endIndex).toBe(53); // End of "\t}" |
| 394 | + expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); | |
| 377 | 395 | |
| 378 | 396 | // Verify the matched content preserves original tabs |
| 379 | 397 | const matchedContent = fileContent.substring( |
| @@ -401,6 +419,7 @@ | ||
| 401 | 419 | expect(result).not.toBeNull(); |
| 402 | 420 | expect(result!.startIndex).toBe(13); // After "const obj = {" |
| 403 | 421 | expect(result!.endIndex).toBe(83); // End of " key3 : 'value3'" |
| 422 | + expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); | |
| 404 | 423 | |
| 405 | 424 | // Verify the matched content preserves original formatting |
| 406 | 425 | const matchedContent = fileContent.substring( |
| @@ -431,6 +450,7 @@ | ||
| 431 | 450 | expect(result).not.toBeNull(); |
| 432 | 451 | expect(result!.startIndex).toBe(17); // After "function calc() {\n\n" |
| 433 | 452 | expect(result!.endIndex).toBe(78); // End of " return result;" |
| 453 | + expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); | |
| 434 | 454 | |
| 435 | 455 | // Verify the matched content includes all original whitespace |
| 436 | 456 | const matchedContent = fileContent.substring( |
| @@ -459,6 +479,7 @@ | ||
| 459 | 479 | expect(result).toEqual({ |
| 460 | 480 | startIndex: 0, |
| 461 | 481 | endIndex: 10, |
| 482 | + strategyName: "exactMatch", | |
| 462 | 483 | }); |
| 463 | 484 | }); |
| 464 | 485 | |
| @@ -474,6 +495,7 @@ | ||
| 474 | 495 | expect(result).toEqual({ |
| 475 | 496 | startIndex: 0, |
| 476 | 497 | endIndex: 0, |
| 498 | + strategyName: "emptySearch", | |
| 477 | 499 | }); |
| 478 | 500 | }); |
| 479 | 501 | |
| @@ -498,6 +520,7 @@ | ||
| 498 | 520 | expect(result).not.toBeNull(); |
| 499 | 521 | expect(result!.startIndex).toBe(15); // After "class Example {" |
| 500 | 522 | expect(result!.endIndex).toBe(95); // End of " }" |
| 523 | + expect(result!.strategyName).toBe("whitespaceIgnoredMatch"); | |
| 501 | 524 | |
| 502 | 525 | // Verify the matched content preserves original formatting |
| 503 | 526 | const matchedContent = fileContent.substring( |
| @@ -536,6 +559,7 @@ | ||
| 536 | 559 | expect(result).toEqual({ |
| 537 | 560 | startIndex: 0, |
| 538 | 561 | endIndex: 10, |
| 562 | + strategyName: "exactMatch", | |
| 539 | 563 | }); |
| 540 | 564 | }); |
| 541 | 565 | }); |
| @@ -568,6 +592,7 @@ | ||
| 568 | 592 | expect(result).toEqual({ |
| 569 | 593 | startIndex: expectedStart, |
| 570 | 594 | endIndex: expectedStart + searchContent.length, |
| 595 | + strategyName: "exactMatch", | |
| 571 | 596 | }); |
| 572 | 597 | }); |
| 573 | 598 | |
| @@ -587,6 +612,7 @@ | ||
| 587 | 612 | expect(result).toEqual({ |
| 588 | 613 | startIndex: expectedStart, |
| 589 | 614 | endIndex: expectedStart + searchContent.length, |
| 615 | + strategyName: "exactMatch", | |
| 590 | 616 | }); |
| 591 | 617 | }); |
| 592 | 618 | |
| @@ -609,6 +635,7 @@ | ||
| 609 | 635 | expect(result).toEqual({ |
| 610 | 636 | startIndex: 0, |
| 611 | 637 | endIndex: searchContent.length, |
| 638 | + strategyName: "exactMatch", | |
| 612 | 639 | }); |
| 613 | 640 | }); |
| 614 | 641 | }); |
modified core/edit/searchAndReplace/findSearchMatch.ts +22 −14
| @@ -1,20 +1,28 @@ | ||
| 1 | 1 | /** |
| 2 | - * Represents a match result with start and end character positions | |
| 2 | + * Represents a basic match result with start and end character positions | |
| 3 | 3 | */ |
| 4 | -export interface SearchMatchResult { | |
| 4 | +interface BasicMatchResult { | |
| 5 | 5 | /** The starting character index of the match in the file content */ |
| 6 | 6 | startIndex: number; |
| 7 | 7 | /** The ending character index of the match in the file content */ |
| 8 | 8 | endIndex: number; |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | +/** | |
| 12 | + * Represents a match result with start and end character positions | |
| 13 | + */ | |
| 14 | +export interface SearchMatchResult extends BasicMatchResult { | |
| 15 | + /** The name of the strategy that successfully matched */ | |
| 16 | + strategyName: string; | |
| 17 | +} | |
| 18 | + | |
| 11 | 19 | /** |
| 12 | 20 | * Strategy function type for finding matches |
| 13 | 21 | */ |
| 14 | 22 | type MatchStrategy = ( |
| 15 | 23 | fileContent: string, |
| 16 | 24 | searchContent: string, |
| 17 | -) => SearchMatchResult | null; | |
| 25 | +) => BasicMatchResult | null; | |
| 18 | 26 | |
| 19 | 27 | /** |
| 20 | 28 | * Exact string matching strategy |
| @@ -22,7 +30,7 @@ | ||
| 22 | 30 | function exactMatch( |
| 23 | 31 | fileContent: string, |
| 24 | 32 | searchContent: string, |
| 25 | -): SearchMatchResult | null { | |
| 33 | +): BasicMatchResult | null { | |
| 26 | 34 | const exactIndex = fileContent.indexOf(searchContent); |
| 27 | 35 | if (exactIndex !== -1) { |
| 28 | 36 | return { |
| @@ -39,7 +47,7 @@ | ||
| 39 | 47 | function trimmedMatch( |
| 40 | 48 | fileContent: string, |
| 41 | 49 | searchContent: string, |
| 42 | -): SearchMatchResult | null { | |
| 50 | +): BasicMatchResult | null { | |
| 43 | 51 | const trimmedSearchContent = searchContent.trim(); |
| 44 | 52 | const trimmedIndex = fileContent.indexOf(trimmedSearchContent); |
| 45 | 53 | if (trimmedIndex !== -1) { |
| @@ -58,7 +66,7 @@ | ||
| 58 | 66 | function whitespaceIgnoredMatch( |
| 59 | 67 | fileContent: string, |
| 60 | 68 | searchContent: string, |
| 61 | -): SearchMatchResult | null { | |
| 69 | +): BasicMatchResult | null { | |
| 62 | 70 | // Remove all whitespace (spaces, tabs, newlines, etc.) |
| 63 | 71 | const strippedFileContent = fileContent.replace(/\s/g, ""); |
| 64 | 72 | const strippedSearchContent = searchContent.replace(/\s/g, ""); |
| @@ -109,12 +117,12 @@ | ||
| 109 | 117 | } |
| 110 | 118 | |
| 111 | 119 | /** |
| 112 | - * Ordered list of matching strategies to try | |
| 120 | + * Ordered list of matching strategies to try with their names | |
| 113 | 121 | */ |
| 114 | -const matchingStrategies: MatchStrategy[] = [ | |
| 115 | - exactMatch, | |
| 116 | - trimmedMatch, | |
| 117 | - whitespaceIgnoredMatch, | |
| 122 | +const matchingStrategies: Array<{ strategy: MatchStrategy; name: string }> = [ | |
| 123 | + { strategy: exactMatch, name: "exactMatch" }, | |
| 124 | + { strategy: trimmedMatch, name: "trimmedMatch" }, | |
| 125 | + { strategy: whitespaceIgnoredMatch, name: "whitespaceIgnoredMatch" }, | |
| 118 | 126 | ]; |
| 119 | 127 | |
| 120 | 128 | /** |
| @@ -137,14 +145,14 @@ | ||
| 137 | 145 | |
| 138 | 146 | if (trimmedSearchContent === "") { |
| 139 | 147 | // Empty search content matches the beginning of the file |
| 140 | - return { startIndex: 0, endIndex: 0 }; | |
| 148 | + return { startIndex: 0, endIndex: 0, strategyName: "emptySearch" }; | |
| 141 | 149 | } |
| 142 | 150 | |
| 143 | 151 | // Try each matching strategy in order |
| 144 | - for (const strategy of matchingStrategies) { | |
| 152 | + for (const { strategy, name } of matchingStrategies) { | |
| 145 | 153 | const result = strategy(fileContent, searchContent); |
| 146 | 154 | if (result !== null) { |
| 147 | - return result; | |
| 155 | + return { ...result, strategyName: name }; | |
| 148 | 156 | } |
| 149 | 157 | } |
| 150 | 158 |
modified gui/src/util/clientTools/searchReplaceImpl.test.ts +10 −0
| @@ -99,6 +99,7 @@ | ||
| 99 | 99 | mockFindSearchMatch.mockReturnValue({ |
| 100 | 100 | startIndex, |
| 101 | 101 | endIndex, |
| 102 | + strategyName: "exactMatch", | |
| 102 | 103 | }); |
| 103 | 104 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 104 | 105 | |
| @@ -173,10 +174,12 @@ | ||
| 173 | 174 | .mockReturnValueOnce({ |
| 174 | 175 | startIndex: firstStartIndex, |
| 175 | 176 | endIndex: firstEndIndex, |
| 177 | + strategyName: "exactMatch", | |
| 176 | 178 | }) |
| 177 | 179 | .mockReturnValueOnce({ |
| 178 | 180 | startIndex: secondStartIndex, |
| 179 | 181 | endIndex: secondEndIndex, |
| 182 | + strategyName: "exactMatch", | |
| 180 | 183 | }); |
| 181 | 184 | |
| 182 | 185 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| @@ -239,6 +242,7 @@ | ||
| 239 | 242 | mockFindSearchMatch.mockReturnValue({ |
| 240 | 243 | startIndex: 10, // Position of "remove this line" |
| 241 | 244 | endIndex: 26, // End of "remove this line" |
| 245 | + strategyName: "exactMatch", | |
| 242 | 246 | }); |
| 243 | 247 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 244 | 248 | |
| @@ -278,6 +282,7 @@ | ||
| 278 | 282 | mockFindSearchMatch.mockReturnValue({ |
| 279 | 283 | startIndex: 6, |
| 280 | 284 | endIndex: 11, |
| 285 | + strategyName: "exactMatch", | |
| 281 | 286 | }); |
| 282 | 287 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 283 | 288 | |
| @@ -344,6 +349,7 @@ | ||
| 344 | 349 | .mockReturnValueOnce({ |
| 345 | 350 | startIndex: 0, |
| 346 | 351 | endIndex: 13, |
| 352 | + strategyName: "exactMatch", | |
| 347 | 353 | }) |
| 348 | 354 | .mockReturnValueOnce(null); // Second search fails |
| 349 | 355 | |
| @@ -393,6 +399,7 @@ | ||
| 393 | 399 | mockFindSearchMatch.mockReturnValue({ |
| 394 | 400 | startIndex: 0, |
| 395 | 401 | endIndex: 7, |
| 402 | + strategyName: "exactMatch", | |
| 396 | 403 | }); |
| 397 | 404 | mockIdeMessenger.request.mockRejectedValue(new Error("Apply failed")); |
| 398 | 405 | |
| @@ -423,6 +430,7 @@ | ||
| 423 | 430 | mockFindSearchMatch.mockReturnValue({ |
| 424 | 431 | startIndex: 0, // Empty search matches at beginning |
| 425 | 432 | endIndex: 0, |
| 433 | + strategyName: "exactMatch", | |
| 426 | 434 | }); |
| 427 | 435 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 428 | 436 | |
| @@ -457,6 +465,7 @@ | ||
| 457 | 465 | mockFindSearchMatch.mockReturnValue({ |
| 458 | 466 | startIndex: 0, |
| 459 | 467 | endIndex: originalContent.length, |
| 468 | + strategyName: "exactMatch", | |
| 460 | 469 | }); |
| 461 | 470 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 462 | 471 | |
| @@ -492,6 +501,7 @@ | ||
| 492 | 501 | mockFindSearchMatch.mockReturnValue({ |
| 493 | 502 | startIndex: 0, |
| 494 | 503 | endIndex: 4, |
| 504 | + strategyName: "exactMatch", | |
| 495 | 505 | }); |
| 496 | 506 | mockIdeMessenger.request.mockResolvedValue({ status: "success" }); |
| 497 | 507 |
modified gui/src/util/clientTools/searchReplaceImpl.ts +14 −0
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch"; |
| 2 | 2 | import { parseAllSearchReplaceBlocks } from "core/edit/searchAndReplace/parseSearchReplaceBlock"; |
| 3 | 3 | import { resolveRelativePathInDir } from "core/util/ideUtils"; |
| 4 | +import posthog from "posthog-js"; | |
| 4 | 5 | import { v4 as uuid } from "uuid"; |
| 5 | 6 | import { ClientToolImpl } from "./callClientTool"; |
| 6 | 7 | |
| @@ -11,6 +12,9 @@ | ||
| 11 | 12 | ) => { |
| 12 | 13 | const { filepath, diff } = args; |
| 13 | 14 | |
| 15 | + const state = extras.getState(); | |
| 16 | + const allowAnonymousTelemetry = state.config.config.allowAnonymousTelemetry; | |
| 17 | + | |
| 14 | 18 | const streamId = uuid(); |
| 15 | 19 | |
| 16 | 20 | // Resolve the file path |
| @@ -42,6 +46,16 @@ | ||
| 42 | 46 | |
| 43 | 47 | // Find the search content in the current state of the file |
| 44 | 48 | const match = findSearchMatch(currentContent, searchContent || ""); |
| 49 | + | |
| 50 | + // Because we don't have access to use hooks, we check `allowAnonymousTelemetry` | |
| 51 | + // directly rather than using `CustomPostHogProvider` | |
| 52 | + if (allowAnonymousTelemetry) { | |
| 53 | + // Capture telemetry for tool calls | |
| 54 | + posthog.capture("find_replace_match_result", { | |
| 55 | + matchStrategy: match?.strategyName ?? "noMatch", | |
| 56 | + }); | |
| 57 | + } | |
| 58 | + | |
| 45 | 59 | if (!match) { |
| 46 | 60 | throw new Error( |
| 47 | 61 | `Search content not found in block ${i + 1}:\n${searchContent}`, |