Changesets · Branches · Tags · Compare
| Changeset | 6c0ea8c4e384afb12a7ed9765f7501d2e5d5a667f7831a7692dd5571aabbc775 |
|---|---|
| Author | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-26 12:20 -07:00 |
| Committer | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-26 12:20 -07:00 |
| Parents | e7644ab0b173 |
12 files changed
deleted core/core_temp.ts +0 −0
added core/edit/searchAndReplace/findSearchMatch.test.ts +386 −0
| @@ -0,0 +1,386 @@ | ||
| 1 | +import { findSearchMatch } from "./findSearchMatch"; | |
| 2 | + | |
| 3 | +describe("findSearchMatch", () => { | |
| 4 | + describe("Exact matches", () => { | |
| 5 | + it("should find exact match at the beginning of file", () => { | |
| 6 | + const fileContent = `function hello() { | |
| 7 | + console.log("world"); | |
| 8 | +}`; | |
| 9 | + const searchContent = `function hello() { | |
| 10 | + console.log("world"); | |
| 11 | +}`; | |
| 12 | + | |
| 13 | + const result = findSearchMatch(fileContent, searchContent); | |
| 14 | + | |
| 15 | + expect(result).toEqual({ | |
| 16 | + startIndex: 0, | |
| 17 | + endIndex: fileContent.length, | |
| 18 | + }); | |
| 19 | + }); | |
| 20 | + | |
| 21 | + it("should find exact match in the middle of file", () => { | |
| 22 | + const fileContent = `const a = 1; | |
| 23 | +function hello() { | |
| 24 | + console.log("world"); | |
| 25 | +} | |
| 26 | +const b = 2;`; | |
| 27 | + const searchContent = `function hello() { | |
| 28 | + console.log("world"); | |
| 29 | +}`; | |
| 30 | + | |
| 31 | + const result = findSearchMatch(fileContent, searchContent); | |
| 32 | + | |
| 33 | + expect(result).toEqual({ | |
| 34 | + startIndex: 13, // After "const a = 1;\n" | |
| 35 | + endIndex: 13 + searchContent.length, | |
| 36 | + }); | |
| 37 | + }); | |
| 38 | + | |
| 39 | + it("should find exact match at the end of file", () => { | |
| 40 | + const fileContent = `const a = 1; | |
| 41 | +function hello() { | |
| 42 | + console.log("world"); | |
| 43 | +}`; | |
| 44 | + const searchContent = `function hello() { | |
| 45 | + console.log("world"); | |
| 46 | +}`; | |
| 47 | + | |
| 48 | + const result = findSearchMatch(fileContent, searchContent); | |
| 49 | + | |
| 50 | + expect(result).toEqual({ | |
| 51 | + startIndex: 13, // After "const a = 1;\n" | |
| 52 | + endIndex: fileContent.length, | |
| 53 | + }); | |
| 54 | + }); | |
| 55 | + | |
| 56 | + it("should find exact match with single line", () => { | |
| 57 | + const fileContent = `const a = 1; | |
| 58 | +const b = 2; | |
| 59 | +const c = 3;`; | |
| 60 | + const searchContent = `const b = 2;`; | |
| 61 | + | |
| 62 | + const result = findSearchMatch(fileContent, searchContent); | |
| 63 | + | |
| 64 | + expect(result).toEqual({ | |
| 65 | + startIndex: 13, // After "const a = 1;\n" | |
| 66 | + endIndex: 13 + searchContent.length, | |
| 67 | + }); | |
| 68 | + }); | |
| 69 | + | |
| 70 | + it("should find exact match with whitespace preserved", () => { | |
| 71 | + const fileContent = `function test() { | |
| 72 | + const x = 1; | |
| 73 | + const y = 2; | |
| 74 | +}`; | |
| 75 | + const searchContent = ` const x = 1; | |
| 76 | + const y = 2;`; | |
| 77 | + | |
| 78 | + const result = findSearchMatch(fileContent, searchContent); | |
| 79 | + | |
| 80 | + // The actual indexOf result is 18, not 17 | |
| 81 | + expect(result).toEqual({ | |
| 82 | + startIndex: 18, | |
| 83 | + endIndex: 18 + searchContent.length, | |
| 84 | + }); | |
| 85 | + }); | |
| 86 | + }); | |
| 87 | + | |
| 88 | + describe("Trimmed matches", () => { | |
| 89 | + it("should find trimmed match when exact match fails due to leading newlines", () => { | |
| 90 | + const fileContent = `function hello() { | |
| 91 | + console.log("world"); | |
| 92 | +}`; | |
| 93 | + const searchContent = `\n\nfunction hello() { | |
| 94 | + console.log("world"); | |
| 95 | +}\n\n`; | |
| 96 | + | |
| 97 | + const result = findSearchMatch(fileContent, searchContent); | |
| 98 | + | |
| 99 | + expect(result).toEqual({ | |
| 100 | + startIndex: 0, | |
| 101 | + endIndex: fileContent.length, // Uses trimmed length | |
| 102 | + }); | |
| 103 | + }); | |
| 104 | + | |
| 105 | + it("should find trimmed match when exact match fails due to trailing newlines", () => { | |
| 106 | + const fileContent = `const a = 1; | |
| 107 | +function hello() { | |
| 108 | + return "world"; | |
| 109 | +} | |
| 110 | +const b = 2;`; | |
| 111 | + const searchContent = `\nfunction hello() { | |
| 112 | + return "world"; | |
| 113 | +}\n`; | |
| 114 | + | |
| 115 | + const result = findSearchMatch(fileContent, searchContent); | |
| 116 | + | |
| 117 | + // The function finds the exact match first (at index 12), not the trimmed match | |
| 118 | + expect(result).toEqual({ | |
| 119 | + startIndex: 12, | |
| 120 | + endIndex: 12 + searchContent.length, | |
| 121 | + }); | |
| 122 | + }); | |
| 123 | + | |
| 124 | + it("should find trimmed match for single line with extra whitespace", () => { | |
| 125 | + const fileContent = `const a = 1; | |
| 126 | +const b = 2; | |
| 127 | +const c = 3;`; | |
| 128 | + const searchContent = `\n const b = 2; \n`; | |
| 129 | + | |
| 130 | + const result = findSearchMatch(fileContent, searchContent); | |
| 131 | + | |
| 132 | + const trimmedSearchContent = searchContent.trim(); | |
| 133 | + const expectedStart = fileContent.indexOf(trimmedSearchContent); | |
| 134 | + | |
| 135 | + expect(result).toEqual({ | |
| 136 | + startIndex: expectedStart, | |
| 137 | + endIndex: expectedStart + trimmedSearchContent.length, | |
| 138 | + }); | |
| 139 | + }); | |
| 140 | + }); | |
| 141 | + | |
| 142 | + describe("Empty search content", () => { | |
| 143 | + it("should match at beginning for empty search content", () => { | |
| 144 | + const fileContent = `function hello() { | |
| 145 | + console.log("world"); | |
| 146 | +}`; | |
| 147 | + const searchContent = ""; | |
| 148 | + | |
| 149 | + const result = findSearchMatch(fileContent, searchContent); | |
| 150 | + | |
| 151 | + expect(result).toEqual({ | |
| 152 | + startIndex: 0, | |
| 153 | + endIndex: 0, | |
| 154 | + }); | |
| 155 | + }); | |
| 156 | + | |
| 157 | + it("should match at beginning for whitespace-only search content", () => { | |
| 158 | + const fileContent = `function hello() { | |
| 159 | + console.log("world"); | |
| 160 | +}`; | |
| 161 | + const searchContent = "\n\n \t \n"; | |
| 162 | + | |
| 163 | + const result = findSearchMatch(fileContent, searchContent); | |
| 164 | + | |
| 165 | + expect(result).toEqual({ | |
| 166 | + startIndex: 0, | |
| 167 | + endIndex: 0, | |
| 168 | + }); | |
| 169 | + }); | |
| 170 | + | |
| 171 | + it("should match at beginning for empty file and empty search", () => { | |
| 172 | + const fileContent = ""; | |
| 173 | + const searchContent = ""; | |
| 174 | + | |
| 175 | + const result = findSearchMatch(fileContent, searchContent); | |
| 176 | + | |
| 177 | + expect(result).toEqual({ | |
| 178 | + startIndex: 0, | |
| 179 | + endIndex: 0, | |
| 180 | + }); | |
| 181 | + }); | |
| 182 | + }); | |
| 183 | + | |
| 184 | + describe("No matches", () => { | |
| 185 | + it("should return null when search content is not found", () => { | |
| 186 | + const fileContent = `function hello() { | |
| 187 | + console.log("world"); | |
| 188 | +}`; | |
| 189 | + const searchContent = `function goodbye() { | |
| 190 | + console.log("world"); | |
| 191 | +}`; | |
| 192 | + | |
| 193 | + const result = findSearchMatch(fileContent, searchContent); | |
| 194 | + | |
| 195 | + expect(result).toBeNull(); | |
| 196 | + }); | |
| 197 | + | |
| 198 | + it("should return null when trimmed search content is not found", () => { | |
| 199 | + const fileContent = `function hello() { | |
| 200 | + console.log("world"); | |
| 201 | +}`; | |
| 202 | + const searchContent = `\n\nfunction goodbye() { | |
| 203 | + console.log("world"); | |
| 204 | +}\n\n`; | |
| 205 | + | |
| 206 | + const result = findSearchMatch(fileContent, searchContent); | |
| 207 | + | |
| 208 | + expect(result).toBeNull(); | |
| 209 | + }); | |
| 210 | + | |
| 211 | + it("should return null for non-existent single line", () => { | |
| 212 | + const fileContent = `const a = 1; | |
| 213 | +const b = 2; | |
| 214 | +const c = 3;`; | |
| 215 | + const searchContent = `const d = 4;`; | |
| 216 | + | |
| 217 | + const result = findSearchMatch(fileContent, searchContent); | |
| 218 | + | |
| 219 | + expect(result).toBeNull(); | |
| 220 | + }); | |
| 221 | + | |
| 222 | + it("should return null for partial match", () => { | |
| 223 | + const fileContent = `function hello() { | |
| 224 | + console.log("world"); | |
| 225 | +}`; | |
| 226 | + const searchContent = `function hello() { | |
| 227 | + console.log("universe"); | |
| 228 | +}`; | |
| 229 | + | |
| 230 | + const result = findSearchMatch(fileContent, searchContent); | |
| 231 | + | |
| 232 | + expect(result).toBeNull(); | |
| 233 | + }); | |
| 234 | + }); | |
| 235 | + | |
| 236 | + describe("Edge cases", () => { | |
| 237 | + it("should handle empty file with non-empty search", () => { | |
| 238 | + const fileContent = ""; | |
| 239 | + const searchContent = "function hello() {}"; | |
| 240 | + | |
| 241 | + const result = findSearchMatch(fileContent, searchContent); | |
| 242 | + | |
| 243 | + expect(result).toBeNull(); | |
| 244 | + }); | |
| 245 | + | |
| 246 | + it("should handle very large content", () => { | |
| 247 | + const repeatedLine = "const x = 1;\n"; | |
| 248 | + const fileContent = | |
| 249 | + repeatedLine.repeat(1000) + | |
| 250 | + "const target = 2;\n" + | |
| 251 | + repeatedLine.repeat(1000); | |
| 252 | + const searchContent = "const target = 2;"; | |
| 253 | + | |
| 254 | + const result = findSearchMatch(fileContent, searchContent); | |
| 255 | + | |
| 256 | + const expectedStart = repeatedLine.length * 1000; | |
| 257 | + expect(result).toEqual({ | |
| 258 | + startIndex: expectedStart, | |
| 259 | + endIndex: expectedStart + searchContent.length, | |
| 260 | + }); | |
| 261 | + }); | |
| 262 | + | |
| 263 | + it("should handle special characters and symbols", () => { | |
| 264 | + const fileContent = `const regex = /[a-zA-Z]+/g; | |
| 265 | +const symbols = !@#$%^&*(); | |
| 266 | +const unicode = "🚀 Hello 世界";`; | |
| 267 | + const searchContent = `const symbols = !@#$%^&*();`; | |
| 268 | + | |
| 269 | + const result = findSearchMatch(fileContent, searchContent); | |
| 270 | + | |
| 271 | + const expectedStart = fileContent.indexOf(searchContent); | |
| 272 | + expect(result).toEqual({ | |
| 273 | + startIndex: expectedStart, | |
| 274 | + endIndex: expectedStart + searchContent.length, | |
| 275 | + }); | |
| 276 | + }); | |
| 277 | + | |
| 278 | + it("should prefer exact match over trimmed match", () => { | |
| 279 | + const fileContent = `function test() { | |
| 280 | + const x = 1; | |
| 281 | +} | |
| 282 | + | |
| 283 | +function test() { | |
| 284 | + const x = 1; | |
| 285 | +}`; | |
| 286 | + const searchContent = `function test() { | |
| 287 | + const x = 1; | |
| 288 | +}`; | |
| 289 | + | |
| 290 | + const result = findSearchMatch(fileContent, searchContent); | |
| 291 | + | |
| 292 | + // Should find the first exact match, not a trimmed version | |
| 293 | + expect(result).toEqual({ | |
| 294 | + startIndex: 0, | |
| 295 | + endIndex: searchContent.length, | |
| 296 | + }); | |
| 297 | + }); | |
| 298 | + | |
| 299 | + it("should handle multiple occurrences and return first match", () => { | |
| 300 | + const fileContent = `const a = 1; | |
| 301 | +const b = 2; | |
| 302 | +const a = 1;`; | |
| 303 | + const searchContent = `const a = 1;`; | |
| 304 | + | |
| 305 | + const result = findSearchMatch(fileContent, searchContent); | |
| 306 | + | |
| 307 | + expect(result).toEqual({ | |
| 308 | + startIndex: 0, | |
| 309 | + endIndex: searchContent.length, | |
| 310 | + }); | |
| 311 | + }); | |
| 312 | + }); | |
| 313 | + | |
| 314 | + describe("Real-world scenarios", () => { | |
| 315 | + it("should match typical function replacement", () => { | |
| 316 | + const fileContent = `class Calculator { | |
| 317 | + constructor() { | |
| 318 | + this.result = 0; | |
| 319 | + } | |
| 320 | + | |
| 321 | + reset() { | |
| 322 | + this.result = 0; | |
| 323 | + return this; | |
| 324 | + } | |
| 325 | + | |
| 326 | + add(value) { | |
| 327 | + this.result += value; | |
| 328 | + return this; | |
| 329 | + } | |
| 330 | +}`; | |
| 331 | + const searchContent = ` reset() { | |
| 332 | + this.result = 0; | |
| 333 | + return this; | |
| 334 | + }`; | |
| 335 | + | |
| 336 | + const result = findSearchMatch(fileContent, searchContent); | |
| 337 | + | |
| 338 | + const expectedStart = fileContent.indexOf(searchContent); | |
| 339 | + expect(result).toEqual({ | |
| 340 | + startIndex: expectedStart, | |
| 341 | + endIndex: expectedStart + searchContent.length, | |
| 342 | + }); | |
| 343 | + }); | |
| 344 | + | |
| 345 | + it("should match import statement", () => { | |
| 346 | + const fileContent = `import React from 'react'; | |
| 347 | +import { useState, useEffect } from 'react'; | |
| 348 | +import './App.css'; | |
| 349 | + | |
| 350 | +function App() { | |
| 351 | + return <div>Hello World</div>; | |
| 352 | +}`; | |
| 353 | + const searchContent = `import { useState, useEffect } from 'react';`; | |
| 354 | + | |
| 355 | + const result = findSearchMatch(fileContent, searchContent); | |
| 356 | + | |
| 357 | + const expectedStart = fileContent.indexOf(searchContent); | |
| 358 | + expect(result).toEqual({ | |
| 359 | + startIndex: expectedStart, | |
| 360 | + endIndex: expectedStart + searchContent.length, | |
| 361 | + }); | |
| 362 | + }); | |
| 363 | + | |
| 364 | + it("should match comment block", () => { | |
| 365 | + const fileContent = `/** | |
| 366 | + * This is a comment | |
| 367 | + * that spans multiple lines | |
| 368 | + */ | |
| 369 | +function test() { | |
| 370 | + // Single line comment | |
| 371 | + return true; | |
| 372 | +}`; | |
| 373 | + const searchContent = `/** | |
| 374 | + * This is a comment | |
| 375 | + * that spans multiple lines | |
| 376 | + */`; | |
| 377 | + | |
| 378 | + const result = findSearchMatch(fileContent, searchContent); | |
| 379 | + | |
| 380 | + expect(result).toEqual({ | |
| 381 | + startIndex: 0, | |
| 382 | + endIndex: searchContent.length, | |
| 383 | + }); | |
| 384 | + }); | |
| 385 | + }); | |
| 386 | +}); |
added core/edit/searchAndReplace/findSearchMatch.ts +55 −0
| @@ -0,0 +1,55 @@ | ||
| 1 | +/** | |
| 2 | + * Represents a match result with start and end character positions | |
| 3 | + */ | |
| 4 | +export interface SearchMatchResult { | |
| 5 | + /** The starting character index of the match in the file content */ | |
| 6 | + startIndex: number; | |
| 7 | + /** The ending character index of the match in the file content */ | |
| 8 | + endIndex: number; | |
| 9 | +} | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Find the exact match position for search content in file content. | |
| 13 | + * Uses simple exact matching with trimmed newlines for MVP. | |
| 14 | + * | |
| 15 | + * Matching Strategy: | |
| 16 | + * 1. If search content is empty, matches at the beginning of file (position 0) | |
| 17 | + * 2. Try exact string match first | |
| 18 | + * 3. Fallback to trimmed content match if exact match fails | |
| 19 | + * | |
| 20 | + * @param fileContent - The complete content of the file to search in | |
| 21 | + * @param searchContent - The content to search for | |
| 22 | + * @returns Match result with character positions, or null if no match found | |
| 23 | + */ | |
| 24 | +export function findSearchMatch( | |
| 25 | + fileContent: string, | |
| 26 | + searchContent: string | |
| 27 | +): SearchMatchResult | null { | |
| 28 | + // Strip leading/trailing newlines for basic matching strategy | |
| 29 | + const trimmedSearchContent = searchContent.trim(); | |
| 30 | + | |
| 31 | + if (trimmedSearchContent === '') { | |
| 32 | + // Empty search content matches the beginning of the file | |
| 33 | + return { startIndex: 0, endIndex: 0 }; | |
| 34 | + } | |
| 35 | + | |
| 36 | + // Try exact match first | |
| 37 | + const exactIndex = fileContent.indexOf(searchContent); | |
| 38 | + if (exactIndex !== -1) { | |
| 39 | + return { | |
| 40 | + startIndex: exactIndex, | |
| 41 | + endIndex: exactIndex + searchContent.length | |
| 42 | + }; | |
| 43 | + } | |
| 44 | + | |
| 45 | + // Fallback: try trimmed content match | |
| 46 | + const trimmedIndex = fileContent.indexOf(trimmedSearchContent); | |
| 47 | + if (trimmedIndex !== -1) { | |
| 48 | + return { | |
| 49 | + startIndex: trimmedIndex, | |
| 50 | + endIndex: trimmedIndex + trimmedSearchContent.length | |
| 51 | + }; | |
| 52 | + } | |
| 53 | + | |
| 54 | + return null; | |
| 55 | +} | |
| No newline at the end of the file |
added core/edit/searchAndReplace/parseSearchReplaceBlock.test.ts +197 −0
| @@ -0,0 +1,197 @@ | ||
| 1 | +import { parseSearchReplaceBlock } from "./parseSearchReplaceBlock"; | |
| 2 | +describe("parseSearchReplaceBlock", () => { | |
| 3 | + describe("Complete blocks", () => { | |
| 4 | + it("should parse a complete basic search/replace block", () => { | |
| 5 | + const content = `------- SEARCH | |
| 6 | +reset() { | |
| 7 | +this.result = 0; | |
| 8 | +return this; | |
| 9 | +} | |
| 10 | +======= | |
| 11 | +// Resets the calculator result to 0 and returns the instance for method chaining | |
| 12 | +reset() { | |
| 13 | +this.result = 0; | |
| 14 | +return this; | |
| 15 | +} | |
| 16 | ++++++++ REPLACE`; | |
| 17 | + | |
| 18 | + const result = parseSearchReplaceBlock(content); | |
| 19 | + | |
| 20 | + expect(result.isComplete).toBe(true); | |
| 21 | + expect(result.searchContent).toBe(`reset() { | |
| 22 | +this.result = 0; | |
| 23 | +return this; | |
| 24 | +}`); | |
| 25 | + expect(result.replaceContent) | |
| 26 | + .toBe(`// Resets the calculator result to 0 and returns the instance for method chaining | |
| 27 | +reset() { | |
| 28 | +this.result = 0; | |
| 29 | +return this; | |
| 30 | +}`); | |
| 31 | + expect(result.error).toBeUndefined(); | |
| 32 | + }); | |
| 33 | + | |
| 34 | + it("should handle different marker lengths", () => { | |
| 35 | + const content = `------- SEARCH | |
| 36 | +old content | |
| 37 | +========= | |
| 38 | +new content | |
| 39 | ++++++++++++ REPLACE`; | |
| 40 | + | |
| 41 | + const result = parseSearchReplaceBlock(content); | |
| 42 | + | |
| 43 | + expect(result.isComplete).toBe(true); | |
| 44 | + expect(result.searchContent).toBe("old content"); | |
| 45 | + expect(result.replaceContent).toBe("new content"); | |
| 46 | + }); | |
| 47 | + | |
| 48 | + it("should handle empty search content", () => { | |
| 49 | + const content = `------- SEARCH | |
| 50 | +======= | |
| 51 | +new content | |
| 52 | ++++++++ REPLACE`; | |
| 53 | + | |
| 54 | + const result = parseSearchReplaceBlock(content); | |
| 55 | + | |
| 56 | + expect(result.isComplete).toBe(true); | |
| 57 | + expect(result.searchContent).toBe(""); | |
| 58 | + expect(result.replaceContent).toBe("new content"); | |
| 59 | + }); | |
| 60 | + | |
| 61 | + it("should handle empty replace content", () => { | |
| 62 | + const content = `------- SEARCH | |
| 63 | +old content | |
| 64 | +======= | |
| 65 | ++++++++ REPLACE`; | |
| 66 | + | |
| 67 | + const result = parseSearchReplaceBlock(content); | |
| 68 | + | |
| 69 | + expect(result.isComplete).toBe(true); | |
| 70 | + expect(result.searchContent).toBe("old content"); | |
| 71 | + expect(result.replaceContent).toBe(""); | |
| 72 | + }); | |
| 73 | + }); | |
| 74 | + | |
| 75 | + describe("Incomplete blocks", () => { | |
| 76 | + it("should handle incomplete search block", () => { | |
| 77 | + const content = `------- SEARCH | |
| 78 | +some content`; | |
| 79 | + | |
| 80 | + const result = parseSearchReplaceBlock(content); | |
| 81 | + | |
| 82 | + expect(result.isComplete).toBe(false); | |
| 83 | + expect(result.searchContent).toBe("some content"); | |
| 84 | + expect(result.replaceContent).toBe(""); | |
| 85 | + expect(result.error).toBeUndefined(); | |
| 86 | + }); | |
| 87 | + | |
| 88 | + it("should handle incomplete replace block", () => { | |
| 89 | + const content = `------- SEARCH | |
| 90 | +old content | |
| 91 | +======= | |
| 92 | +new content`; | |
| 93 | + | |
| 94 | + const result = parseSearchReplaceBlock(content); | |
| 95 | + | |
| 96 | + expect(result.isComplete).toBe(false); | |
| 97 | + expect(result.searchContent).toBe("old content"); | |
| 98 | + expect(result.replaceContent).toBe("new content"); | |
| 99 | + expect(result.error).toBeUndefined(); | |
| 100 | + }); | |
| 101 | + }); | |
| 102 | + | |
| 103 | + describe("Error cases", () => { | |
| 104 | + it("should error on search end without start", () => { | |
| 105 | + const content = `======= | |
| 106 | +some content | |
| 107 | ++++++++ REPLACE`; | |
| 108 | + | |
| 109 | + const result = parseSearchReplaceBlock(content); | |
| 110 | + | |
| 111 | + expect(result.isComplete).toBe(false); | |
| 112 | + expect(result.error).toBe( | |
| 113 | + "Found search block end marker without matching start marker", | |
| 114 | + ); | |
| 115 | + }); | |
| 116 | + | |
| 117 | + it("should error on replace end without replace start", () => { | |
| 118 | + const content = `+++++++ REPLACE`; | |
| 119 | + | |
| 120 | + const result = parseSearchReplaceBlock(content); | |
| 121 | + | |
| 122 | + expect(result.isComplete).toBe(false); | |
| 123 | + expect(result.error).toBe( | |
| 124 | + "Found replace block end marker without matching replace start marker", | |
| 125 | + ); | |
| 126 | + }); | |
| 127 | + }); | |
| 128 | + | |
| 129 | + describe("Regex pattern validation", () => { | |
| 130 | + it("should match various search start patterns", () => { | |
| 131 | + const patterns = ["--- SEARCH", "------- SEARCH", "------------- SEARCH"]; | |
| 132 | + | |
| 133 | + patterns.forEach((pattern) => { | |
| 134 | + const content = `${pattern} | |
| 135 | +content | |
| 136 | +======= | |
| 137 | +replacement | |
| 138 | ++++++++ REPLACE`; | |
| 139 | + const result = parseSearchReplaceBlock(content); | |
| 140 | + expect(result.isComplete).toBe(true); | |
| 141 | + }); | |
| 142 | + }); | |
| 143 | + | |
| 144 | + it("should match various search end patterns", () => { | |
| 145 | + const patterns = ["===", "=======", "=============="]; | |
| 146 | + | |
| 147 | + patterns.forEach((pattern) => { | |
| 148 | + const content = `------- SEARCH | |
| 149 | +content | |
| 150 | +${pattern} | |
| 151 | +replacement | |
| 152 | ++++++++ REPLACE`; | |
| 153 | + | |
| 154 | + const result = parseSearchReplaceBlock(content); | |
| 155 | + expect(result.isComplete).toBe(true); | |
| 156 | + }); | |
| 157 | + }); | |
| 158 | + | |
| 159 | + it("should match various replace end patterns", () => { | |
| 160 | + const patterns = [ | |
| 161 | + "+++ REPLACE", | |
| 162 | + "+++++++ REPLACE", | |
| 163 | + "+++++++++++ REPLACE", | |
| 164 | + ]; | |
| 165 | + | |
| 166 | + patterns.forEach((pattern) => { | |
| 167 | + const content = `------- SEARCH | |
| 168 | +content | |
| 169 | +======= | |
| 170 | +replacement | |
| 171 | +${pattern}`; | |
| 172 | + | |
| 173 | + const result = parseSearchReplaceBlock(content); | |
| 174 | + expect(result.isComplete).toBe(true); | |
| 175 | + }); | |
| 176 | + }); | |
| 177 | + | |
| 178 | + it("should not match invalid patterns", () => { | |
| 179 | + const invalidPatterns = [ | |
| 180 | + "-- SEARCH", // Too few dashes | |
| 181 | + "SEARCH", // No markers | |
| 182 | + "== SEARCH", // Wrong markers | |
| 183 | + "=== REPLACE", // Wrong end marker | |
| 184 | + "++++ SEARCH", // Wrong markers for search | |
| 185 | + ]; | |
| 186 | + | |
| 187 | + invalidPatterns.forEach((pattern) => { | |
| 188 | + const content = `${pattern} | |
| 189 | +content`; | |
| 190 | + | |
| 191 | + const result = parseSearchReplaceBlock(content); | |
| 192 | + expect(result.isComplete).toBe(false); | |
| 193 | + expect(result.searchContent).toBe(""); // Should not enter search mode | |
| 194 | + }); | |
| 195 | + }); | |
| 196 | + }); | |
| 197 | +}); |
added core/edit/searchAndReplace/parseSearchReplaceBlock.ts +90 −0
| @@ -0,0 +1,90 @@ | ||
| 1 | +const SEARCH_BLOCK_START_REGEX = /^[-]{3,} SEARCH$/; | |
| 2 | +const SEARCH_BLOCK_END_REGEX = /^[=]{3,}$/; | |
| 3 | +const REPLACE_BLOCK_END_REGEX = /^[+]{3,} REPLACE$/; | |
| 4 | + | |
| 5 | +export interface SearchReplaceBlockResult { | |
| 6 | + /** Whether a complete search/replace block has been parsed */ | |
| 7 | + isComplete: boolean; | |
| 8 | + /** The content to search for in the file */ | |
| 9 | + searchContent?: string; | |
| 10 | + /** The content to replace the search content with */ | |
| 11 | + replaceContent?: string; | |
| 12 | + /** Error message if parsing failed */ | |
| 13 | + error?: string; | |
| 14 | +} | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Parses a search and replace block from content. | |
| 18 | + * | |
| 19 | + * Expected format: | |
| 20 | + * ------- SEARCH | |
| 21 | + * [content to find] | |
| 22 | + * ======= | |
| 23 | + * [content to replace with] | |
| 24 | + * +++++++ REPLACE | |
| 25 | + * | |
| 26 | + * @param content - The content to parse | |
| 27 | + * @returns ParseResult with completion status and extracted content | |
| 28 | + */ | |
| 29 | +export function parseSearchReplaceBlock( | |
| 30 | + content: string, | |
| 31 | +): SearchReplaceBlockResult { | |
| 32 | + const lines = content.split("\n"); | |
| 33 | + let currentSearchContent = ""; | |
| 34 | + let currentReplaceContent = ""; | |
| 35 | + let inSearch = false; | |
| 36 | + let inReplace = false; | |
| 37 | + | |
| 38 | + for (const line of lines) { | |
| 39 | + if (SEARCH_BLOCK_START_REGEX.test(line)) { | |
| 40 | + inSearch = true; | |
| 41 | + inReplace = false; | |
| 42 | + currentSearchContent = ""; | |
| 43 | + currentReplaceContent = ""; | |
| 44 | + continue; | |
| 45 | + } | |
| 46 | + | |
| 47 | + if (SEARCH_BLOCK_END_REGEX.test(line)) { | |
| 48 | + if (!inSearch) { | |
| 49 | + return { | |
| 50 | + isComplete: false, | |
| 51 | + error: "Found search block end marker without matching start marker", | |
| 52 | + }; | |
| 53 | + } | |
| 54 | + inSearch = false; | |
| 55 | + inReplace = true; | |
| 56 | + continue; | |
| 57 | + } | |
| 58 | + | |
| 59 | + if (REPLACE_BLOCK_END_REGEX.test(line)) { | |
| 60 | + if (!inReplace) { | |
| 61 | + return { | |
| 62 | + isComplete: false, | |
| 63 | + error: | |
| 64 | + "Found replace block end marker without matching replace start marker", | |
| 65 | + }; | |
| 66 | + } | |
| 67 | + | |
| 68 | + // Complete block found | |
| 69 | + return { | |
| 70 | + isComplete: true, | |
| 71 | + searchContent: currentSearchContent.trim(), | |
| 72 | + replaceContent: currentReplaceContent.trim(), | |
| 73 | + }; | |
| 74 | + } | |
| 75 | + | |
| 76 | + // Accumulate content | |
| 77 | + if (inSearch) { | |
| 78 | + currentSearchContent += line + "\n"; | |
| 79 | + } else if (inReplace) { | |
| 80 | + currentReplaceContent += line + "\n"; | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + // Block is not yet complete | |
| 85 | + return { | |
| 86 | + isComplete: false, | |
| 87 | + searchContent: currentSearchContent.trim(), | |
| 88 | + replaceContent: currentReplaceContent.trim(), | |
| 89 | + }; | |
| 90 | +} |
modified core/tools/index.ts +3 −4
| @@ -13,6 +13,8 @@ | ||
| 13 | 13 | import { searchAndReplaceInFileTool } from "./definitions/searchAndReplaceInFile"; |
| 14 | 14 | import { searchWebTool } from "./definitions/searchWeb"; |
| 15 | 15 | import { viewDiffTool } from "./definitions/viewDiff"; |
| 16 | +import { viewRepoMapTool } from "./definitions/viewRepoMap"; | |
| 17 | +import { viewSubdirectoryTool } from "./definitions/viewSubdirectory"; | |
| 16 | 18 | |
| 17 | 19 | export const baseToolDefinitions = [ |
| 18 | 20 | readFileTool, |
| @@ -26,9 +28,6 @@ | ||
| 26 | 28 | lsTool, |
| 27 | 29 | createRuleBlock, |
| 28 | 30 | fetchUrlContentTool, |
| 29 | - // replacing with ls tool for now | |
| 30 | - // viewSubdirectoryTool, | |
| 31 | - // viewRepoMapTool, | |
| 32 | 31 | ]; |
| 33 | 32 | |
| 34 | 33 | export const getConfigDependentToolDefinitions = ( |
| @@ -36,6 +35,6 @@ | ||
| 36 | 35 | ): Tool[] => [ |
| 37 | 36 | requestRuleTool(params), |
| 38 | 37 | ...(params.enableExperimentalTools |
| 39 | - ? [searchAndReplaceInFileTool] | |
| 38 | + ? [searchAndReplaceInFileTool, viewRepoMapTool, viewSubdirectoryTool] | |
| 40 | 39 | : [editFileTool]), |
| 41 | 40 | ]; |
modified gui/src/pages/gui/Chat.tsx +1 −1
| @@ -319,7 +319,7 @@ | ||
| 319 | 319 | inputId={item.message.id} |
| 320 | 320 | /> |
| 321 | 321 | </> |
| 322 | - ) : item.message.role === "tool" ? null : item.message.role === // /> // toolCallId={item.message.toolCallId} // contextItems={item.contextItems} // <ToolOutput | |
| 322 | + ) : item.message.role === "tool" ? null : item.message.role === | |
| 323 | 323 | "assistant" && |
| 324 | 324 | item.message.toolCalls && |
| 325 | 325 | item.toolCallState ? ( |
modified gui/src/pages/gui/ToolCallDiv/EditFile.tsx +1 −1
| @@ -42,11 +42,11 @@ | ||
| 42 | 42 | |
| 43 | 43 | return ( |
| 44 | 44 | <StyledMarkdownPreview |
| 45 | + expandCodeblocks // TODO: Conditional based on edit tool or search & replace | |
| 45 | 46 | isRenderingInStepContainer |
| 46 | 47 | disableManualApply |
| 47 | 48 | source={src} |
| 48 | 49 | forceStreamId={applyState?.streamId} |
| 49 | - expandCodeblocks={false} | |
| 50 | 50 | itemIndex={props.historyIndex} |
| 51 | 51 | /> |
| 52 | 52 | ); |
modified gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +9 −0
| @@ -33,6 +33,15 @@ | ||
| 33 | 33 | historyIndex={historyIndex} |
| 34 | 34 | /> |
| 35 | 35 | ); |
| 36 | + case BuiltInToolNames.SearchAndReplaceInFile: | |
| 37 | + return ( | |
| 38 | + <EditFile | |
| 39 | + relativeFilePath={args.filepath ?? ""} | |
| 40 | + changes={args.diff ?? ""} | |
| 41 | + toolCallId={toolCall.id} | |
| 42 | + historyIndex={historyIndex} | |
| 43 | + /> | |
| 44 | + ); | |
| 36 | 45 | case BuiltInToolNames.RunTerminalCommand: |
| 37 | 46 | return ( |
| 38 | 47 | <RunTerminalCommand |
modified gui/src/pages/gui/ToolCallDiv/ToolCallArgs.tsx +1 −3
| @@ -55,9 +55,7 @@ | ||
| 55 | 55 | {args.map(([key, value]) => ( |
| 56 | 56 | <div key={key} className="flex flex-row items-center gap-2 py-0.5"> |
| 57 | 57 | <span className="text-lightgray">{key}:</span> |
| 58 | - <code className="line-clamp-1 break-all"> | |
| 59 | - {JSON.stringify(value)} | |
| 60 | - </code> | |
| 58 | + <code className="break-all">{JSON.stringify(value)}</code> | |
| 61 | 59 | </div> |
| 62 | 60 | ))} |
| 63 | 61 | </div> |
modified gui/src/util/clientTools/callClientTool.ts +4 −0
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | import { IIdeMessenger } from "../../context/IdeMessenger"; |
| 4 | 4 | import { AppThunkDispatch, RootState } from "../../redux/store"; |
| 5 | 5 | import { editToolImpl } from "./editImpl"; |
| 6 | +import { searchReplaceToolImpl } from "./searchReplaceImpl"; | |
| 6 | 7 | |
| 7 | 8 | export interface ClientToolExtras { |
| 8 | 9 | getState: () => RootState; |
| @@ -37,6 +38,9 @@ | ||
| 37 | 38 | case BuiltInToolNames.EditExistingFile: |
| 38 | 39 | output = await editToolImpl(parsedArgs, toolCall.id, extras); |
| 39 | 40 | break; |
| 41 | + case BuiltInToolNames.SearchAndReplaceInFile: | |
| 42 | + output = await searchReplaceToolImpl(parsedArgs, toolCall.id, extras); | |
| 43 | + break; | |
| 40 | 44 | default: |
| 41 | 45 | throw new Error(`Invalid client tool name ${toolCall.function.name}`); |
| 42 | 46 | } |
added gui/src/util/clientTools/searchReplaceImpl.ts +89 −0
| @@ -0,0 +1,89 @@ | ||
| 1 | +import { ContextItem } from "core"; | |
| 2 | +import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch"; | |
| 3 | +import { parseSearchReplaceBlock } from "core/edit/searchAndReplace/parseSearchReplaceBlock"; | |
| 4 | +import { resolveRelativePathInDir } from "core/util/ideUtils"; | |
| 5 | +import { ClientToolImpl } from "./callClientTool"; | |
| 6 | + | |
| 7 | +export const searchReplaceToolImpl: ClientToolImpl = async ( | |
| 8 | + args, | |
| 9 | + toolCallId, | |
| 10 | + extras, | |
| 11 | +) => { | |
| 12 | + const { filepath, diff } = args; | |
| 13 | + | |
| 14 | + // Resolve the file path | |
| 15 | + const resolvedFilepath = await resolveRelativePathInDir( | |
| 16 | + filepath, | |
| 17 | + extras.ideMessenger.ide, | |
| 18 | + ); | |
| 19 | + if (!resolvedFilepath) { | |
| 20 | + throw new Error(`File ${filepath} does not exist`); | |
| 21 | + } | |
| 22 | + | |
| 23 | + // Parse the search/replace block from the diff content | |
| 24 | + const parseResult = parseSearchReplaceBlock(diff); | |
| 25 | + | |
| 26 | + if (parseResult.error) { | |
| 27 | + throw new Error(`Parse error: ${parseResult.error}`); | |
| 28 | + } | |
| 29 | + | |
| 30 | + if (!parseResult.isComplete) { | |
| 31 | + throw new Error("Incomplete search/replace block"); | |
| 32 | + } | |
| 33 | + | |
| 34 | + const { searchContent, replaceContent } = parseResult; | |
| 35 | + | |
| 36 | + try { | |
| 37 | + // Read the current file content | |
| 38 | + const fileContent = | |
| 39 | + await extras.ideMessenger.ide.readFile(resolvedFilepath); | |
| 40 | + | |
| 41 | + // Find the search match | |
| 42 | + const match = findSearchMatch(fileContent, searchContent || ""); | |
| 43 | + | |
| 44 | + if (!match) { | |
| 45 | + throw new Error(`Search content not found in file:\n${searchContent}`); | |
| 46 | + } | |
| 47 | + | |
| 48 | + // Apply the replacement | |
| 49 | + const newContent = | |
| 50 | + fileContent.substring(0, match.startIndex) + | |
| 51 | + (replaceContent || "") + | |
| 52 | + fileContent.substring(match.endIndex); | |
| 53 | + | |
| 54 | + // Write the updated file | |
| 55 | + await extras.ideMessenger.request("writeFile", { | |
| 56 | + path: resolvedFilepath, | |
| 57 | + contents: newContent, | |
| 58 | + }); | |
| 59 | + | |
| 60 | + // Create context item showing the change | |
| 61 | + const contextItem: ContextItem = { | |
| 62 | + name: `Modified ${filepath}`, | |
| 63 | + description: `Applied search and replace to ${filepath}`, | |
| 64 | + content: `Search and replace operation completed successfully. | |
| 65 | + | |
| 66 | +Search content: | |
| 67 | +\`\`\` | |
| 68 | +${searchContent} | |
| 69 | +\`\`\` | |
| 70 | + | |
| 71 | +Replace content: | |
| 72 | +\`\`\` | |
| 73 | +${replaceContent} | |
| 74 | +\`\`\` | |
| 75 | + | |
| 76 | +File: ${filepath}`, | |
| 77 | + editing: true, | |
| 78 | + }; | |
| 79 | + | |
| 80 | + return { | |
| 81 | + respondImmediately: true, | |
| 82 | + output: [contextItem], | |
| 83 | + }; | |
| 84 | + } catch (error) { | |
| 85 | + throw new Error( | |
| 86 | + `Failed to apply search and replace: ${error instanceof Error ? error.message : String(error)}`, | |
| 87 | + ); | |
| 88 | + } | |
| 89 | +}; |