whitespace ignored matching algorithm + tests
This commit is contained in:
parent
4868cdf885
commit
13d5527f2a
2 changed files with 509 additions and 220 deletions
|
|
@ -1,319 +1,548 @@
|
|||
import { findSearchMatch } from "./findSearchMatch";
|
||||
|
||||
describe("findSearchMatch", () => {
|
||||
describe("Exact matches", () => {
|
||||
it("should find exact match at the beginning of file", () => {
|
||||
const fileContent = `function hello() {
|
||||
describe("Exact matches", () => {
|
||||
it("should find exact match at the beginning of file", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `function hello() {
|
||||
const searchContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: fileContent.length,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: fileContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should find exact match in the middle of file", () => {
|
||||
const fileContent = `const a = 1;
|
||||
it("should find exact match in the middle of file", () => {
|
||||
const fileContent = `const a = 1;
|
||||
function hello() {
|
||||
console.log("world");
|
||||
}
|
||||
const b = 2;`;
|
||||
const searchContent = `function hello() {
|
||||
const searchContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 13, // After "const a = 1;\n"
|
||||
endIndex: 13 + searchContent.length,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: 13, // After "const a = 1;\n"
|
||||
endIndex: 13 + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should find exact match at the end of file", () => {
|
||||
const fileContent = `const a = 1;
|
||||
it("should find exact match at the end of file", () => {
|
||||
const fileContent = `const a = 1;
|
||||
function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `function hello() {
|
||||
const searchContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 13, // After "const a = 1;\n"
|
||||
endIndex: fileContent.length,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: 13, // After "const a = 1;\n"
|
||||
endIndex: fileContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should find exact match with single line", () => {
|
||||
const fileContent = `const a = 1;
|
||||
it("should find exact match with single line", () => {
|
||||
const fileContent = `const a = 1;
|
||||
const b = 2;
|
||||
const c = 3;`;
|
||||
const searchContent = `const b = 2;`;
|
||||
const searchContent = `const b = 2;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 13, // After "const a = 1;\n"
|
||||
endIndex: 13 + searchContent.length,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: 13, // After "const a = 1;\n"
|
||||
endIndex: 13 + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should find exact match with whitespace preserved", () => {
|
||||
const fileContent = `function test() {
|
||||
it("should find exact match with whitespace preserved", () => {
|
||||
const fileContent = `function test() {
|
||||
const x = 1;
|
||||
const y = 2;
|
||||
}`;
|
||||
const searchContent = ` const x = 1;
|
||||
const searchContent = ` const x = 1;
|
||||
const y = 2;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
// The actual indexOf result is 18, not 17
|
||||
expect(result).toEqual({
|
||||
startIndex: 18,
|
||||
endIndex: 18 + searchContent.length,
|
||||
});
|
||||
// The actual indexOf result is 18, not 17
|
||||
expect(result).toEqual({
|
||||
startIndex: 18,
|
||||
endIndex: 18 + searchContent.length,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Trimmed matches", () => {
|
||||
it("should find trimmed match when exact match fails due to leading newlines", () => {
|
||||
const fileContent = `function hello() {
|
||||
describe("Trimmed matches", () => {
|
||||
it("should find trimmed match when exact match fails due to leading newlines", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `\n\nfunction hello() {
|
||||
const searchContent = `\n\nfunction hello() {
|
||||
console.log("world");
|
||||
}\n\n`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: fileContent.length, // Uses trimmed length
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: fileContent.length, // Uses trimmed length
|
||||
});
|
||||
});
|
||||
|
||||
it("should find trimmed match when exact match fails due to trailing newlines", () => {
|
||||
const fileContent = `const a = 1;
|
||||
it("should find trimmed match when exact match fails due to trailing newlines", () => {
|
||||
const fileContent = `const a = 1;
|
||||
function hello() {
|
||||
return "world";
|
||||
}
|
||||
const b = 2;`;
|
||||
const searchContent = `\nfunction hello() {
|
||||
const searchContent = `\nfunction hello() {
|
||||
return "world";
|
||||
}\n`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
// The function finds the exact match first (at index 12), not the trimmed match
|
||||
expect(result).toEqual({
|
||||
startIndex: 12,
|
||||
endIndex: 12 + searchContent.length,
|
||||
});
|
||||
// The function finds the exact match first (at index 12), not the trimmed match
|
||||
expect(result).toEqual({
|
||||
startIndex: 12,
|
||||
endIndex: 12 + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should find trimmed match for single line with extra whitespace", () => {
|
||||
const fileContent = `const a = 1;
|
||||
it("should find trimmed match for single line with extra whitespace", () => {
|
||||
const fileContent = `const a = 1;
|
||||
const b = 2;
|
||||
const c = 3;`;
|
||||
const searchContent = `\n const b = 2; \n`;
|
||||
const searchContent = `\n const b = 2; \n`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
const trimmedSearchContent = searchContent.trim();
|
||||
const expectedStart = fileContent.indexOf(trimmedSearchContent);
|
||||
const trimmedSearchContent = searchContent.trim();
|
||||
const expectedStart = fileContent.indexOf(trimmedSearchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + trimmedSearchContent.length,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + trimmedSearchContent.length,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Empty search content", () => {
|
||||
it("should match at beginning for empty search content", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = "";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 0,
|
||||
});
|
||||
});
|
||||
|
||||
describe("Empty search content", () => {
|
||||
it("should match at beginning for empty search content", () => {
|
||||
const fileContent = `function hello() {
|
||||
it("should match at beginning for whitespace-only search content", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = "";
|
||||
const searchContent = "\n\n \t \n";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("should match at beginning for whitespace-only search content", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = "\n\n \t \n";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("should match at beginning for empty file and empty search", () => {
|
||||
const fileContent = "";
|
||||
const searchContent = "";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 0,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 0,
|
||||
});
|
||||
});
|
||||
|
||||
describe("No matches", () => {
|
||||
it("should return null when search content is not found", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `function goodbye() {
|
||||
console.log("world");
|
||||
}`;
|
||||
it("should match at beginning for empty file and empty search", () => {
|
||||
const fileContent = "";
|
||||
const searchContent = "";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("should return null when trimmed search content is not found", () => {
|
||||
const fileContent = `function hello() {
|
||||
describe("No matches", () => {
|
||||
it("should return null when search content is not found", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `\n\nfunction goodbye() {
|
||||
const searchContent = `function goodbye() {
|
||||
console.log("world");
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should return null when trimmed search content is not found", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `\n\nfunction goodbye() {
|
||||
console.log("world");
|
||||
}\n\n`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should return null for non-existent single line", () => {
|
||||
const fileContent = `const a = 1;
|
||||
it("should return null for non-existent single line", () => {
|
||||
const fileContent = `const a = 1;
|
||||
const b = 2;
|
||||
const c = 3;`;
|
||||
const searchContent = `const d = 4;`;
|
||||
const searchContent = `const d = 4;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should return null for partial match", () => {
|
||||
const fileContent = `function hello() {
|
||||
it("should return null for partial match", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `function hello() {
|
||||
const searchContent = `function hello() {
|
||||
console.log("universe");
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Edge cases", () => {
|
||||
it("should handle empty file with non-empty search", () => {
|
||||
const fileContent = "";
|
||||
const searchContent = "function hello() {}";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should handle very large content", () => {
|
||||
const repeatedLine = "const x = 1;\n";
|
||||
const fileContent =
|
||||
repeatedLine.repeat(1000) +
|
||||
"const target = 2;\n" +
|
||||
repeatedLine.repeat(1000);
|
||||
const searchContent = "const target = 2;";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
const expectedStart = repeatedLine.length * 1000;
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
describe("Edge cases", () => {
|
||||
it("should handle empty file with non-empty search", () => {
|
||||
const fileContent = "";
|
||||
const searchContent = "function hello() {}";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should handle very large content", () => {
|
||||
const repeatedLine = "const x = 1;\n";
|
||||
const fileContent =
|
||||
repeatedLine.repeat(1000) +
|
||||
"const target = 2;\n" +
|
||||
repeatedLine.repeat(1000);
|
||||
const searchContent = "const target = 2;";
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
const expectedStart = repeatedLine.length * 1000;
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle special characters and symbols", () => {
|
||||
const fileContent = `const regex = /[a-zA-Z]+/g;
|
||||
it("should handle special characters and symbols", () => {
|
||||
const fileContent = `const regex = /[a-zA-Z]+/g;
|
||||
const symbols = !@#$%^&*();
|
||||
const unicode = "🚀 Hello 世界";`;
|
||||
const searchContent = `const symbols = !@#$%^&*();`;
|
||||
const searchContent = `const symbols = !@#$%^&*();`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
const expectedStart = fileContent.indexOf(searchContent);
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
const expectedStart = fileContent.indexOf(searchContent);
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should prefer exact match over trimmed match", () => {
|
||||
const fileContent = `function test() {
|
||||
it("should prefer exact match over trimmed match", () => {
|
||||
const fileContent = `function test() {
|
||||
const x = 1;
|
||||
}
|
||||
|
||||
function test() {
|
||||
const x = 1;
|
||||
}`;
|
||||
const searchContent = `function test() {
|
||||
const searchContent = `function test() {
|
||||
const x = 1;
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
// Should find the first exact match, not a trimmed version
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle multiple occurrences and return first match", () => {
|
||||
const fileContent = `const a = 1;
|
||||
const b = 2;
|
||||
const a = 1;`;
|
||||
const searchContent = `const a = 1;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: searchContent.length,
|
||||
});
|
||||
// Should find the first exact match, not a trimmed version
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
describe("Real-world scenarios", () => {
|
||||
it("should match typical function replacement", () => {
|
||||
const fileContent = `class Calculator {
|
||||
it("should handle multiple occurrences and return first match", () => {
|
||||
const fileContent = `const a = 1;
|
||||
const b = 2;
|
||||
const a = 1;`;
|
||||
const searchContent = `const a = 1;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: searchContent.length,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Whitespace-ignored matches", () => {
|
||||
it("should match code with different indentation", () => {
|
||||
const fileContent = `function example() {
|
||||
const x = 1;
|
||||
const y = 2;
|
||||
return x + y;
|
||||
}`;
|
||||
const searchContent = `const x=1;
|
||||
const y=2;
|
||||
return x+y;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.startIndex).toBe(20); // After "function example() {\n"
|
||||
expect(result!.endIndex).toBe(72); // End of " return x + y;"
|
||||
|
||||
// Verify the matched content contains the original whitespace
|
||||
const matchedContent = fileContent.substring(
|
||||
result!.startIndex,
|
||||
result!.endIndex,
|
||||
);
|
||||
expect(matchedContent).toBe(`
|
||||
const x = 1;
|
||||
const y = 2;
|
||||
return x + y;`);
|
||||
});
|
||||
|
||||
it("should match single line with different spacing", () => {
|
||||
const fileContent = `const a = 1;
|
||||
const b = 2;
|
||||
const c = 3;`;
|
||||
const searchContent = `const b=2;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.startIndex).toBe(12); // After "const a = 1;"
|
||||
expect(result!.endIndex).toBe(32); // End of "const b = 2;"
|
||||
|
||||
// Verify the matched content preserves original spacing
|
||||
const matchedContent = fileContent.substring(
|
||||
result!.startIndex,
|
||||
result!.endIndex,
|
||||
);
|
||||
expect(matchedContent).toBe(`
|
||||
const b = 2;`);
|
||||
});
|
||||
|
||||
it("should match with tabs vs spaces", () => {
|
||||
const fileContent = `function test() {
|
||||
\tif (condition) {
|
||||
\t\treturn true;
|
||||
\t}
|
||||
}`;
|
||||
const searchContent = `if (condition) {
|
||||
return true;
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.startIndex).toBe(17); // After "function test() {"
|
||||
expect(result!.endIndex).toBe(53); // End of "\t}"
|
||||
|
||||
// Verify the matched content preserves original tabs
|
||||
const matchedContent = fileContent.substring(
|
||||
result!.startIndex,
|
||||
result!.endIndex,
|
||||
);
|
||||
expect(matchedContent).toBe(`
|
||||
\tif (condition) {
|
||||
\t\treturn true;
|
||||
\t}`);
|
||||
});
|
||||
|
||||
it("should match complex whitespace patterns", () => {
|
||||
const fileContent = `const obj = {
|
||||
key1 : 'value1',
|
||||
key2 : 'value2',
|
||||
key3 : 'value3'
|
||||
};`;
|
||||
const searchContent = `key1:'value1',
|
||||
key2:'value2',
|
||||
key3:'value3'`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.startIndex).toBe(13); // After "const obj = {"
|
||||
expect(result!.endIndex).toBe(83); // End of " key3 : 'value3'"
|
||||
|
||||
// Verify the matched content preserves original formatting
|
||||
const matchedContent = fileContent.substring(
|
||||
result!.startIndex,
|
||||
result!.endIndex,
|
||||
);
|
||||
expect(matchedContent).toBe(`
|
||||
key1 : 'value1',
|
||||
key2 : 'value2',
|
||||
key3 : 'value3'`);
|
||||
});
|
||||
|
||||
it("should handle newlines and mixed whitespace", () => {
|
||||
const fileContent = `function calc() {
|
||||
|
||||
|
||||
let result = 0;
|
||||
|
||||
result += 5;
|
||||
|
||||
|
||||
return result;
|
||||
}`;
|
||||
const searchContent = `let result=0;result+=5;return result;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.startIndex).toBe(17); // After "function calc() {\n\n"
|
||||
expect(result!.endIndex).toBe(78); // End of " return result;"
|
||||
|
||||
// Verify the matched content includes all original whitespace
|
||||
const matchedContent = fileContent.substring(
|
||||
result!.startIndex,
|
||||
result!.endIndex,
|
||||
);
|
||||
expect(matchedContent).toBe(`
|
||||
|
||||
|
||||
let result = 0;
|
||||
|
||||
result += 5;
|
||||
|
||||
|
||||
return result;`);
|
||||
});
|
||||
|
||||
it("should prefer exact match over whitespace-ignored match", () => {
|
||||
const fileContent = `const x=1;
|
||||
const x = 1;`;
|
||||
const searchContent = `const x=1;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
// Should find the exact match first (at index 0), not the whitespace-ignored match
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 10,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle empty content after whitespace removal", () => {
|
||||
const fileContent = `function test() {
|
||||
return 1;
|
||||
}`;
|
||||
const searchContent = `\n\n \t \n`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
// Should match at beginning for empty/whitespace-only content
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle complex code blocks with different formatting", () => {
|
||||
const fileContent = `class Example {
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
this.value = 0;
|
||||
}
|
||||
|
||||
getValue() {
|
||||
return this.value;
|
||||
}
|
||||
}`;
|
||||
const searchContent = `constructor(name){
|
||||
this.name=name;
|
||||
this.value=0;
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.startIndex).toBe(15); // After "class Example {"
|
||||
expect(result!.endIndex).toBe(95); // End of " }"
|
||||
|
||||
// Verify the matched content preserves original formatting
|
||||
const matchedContent = fileContent.substring(
|
||||
result!.startIndex,
|
||||
result!.endIndex,
|
||||
);
|
||||
expect(matchedContent).toBe(`
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
this.value = 0;
|
||||
}`);
|
||||
});
|
||||
|
||||
it("should return null when no whitespace-ignored match exists", () => {
|
||||
const fileContent = `function hello() {
|
||||
console.log("world");
|
||||
}`;
|
||||
const searchContent = `function goodbye() {
|
||||
console.log("universe");
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should handle multiple potential matches and return first", () => {
|
||||
const fileContent = `const a=1;
|
||||
const a = 1;
|
||||
const a = 1;`;
|
||||
const searchContent = `const a=1;`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
// Should find the first exact match
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: 10,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Real-world scenarios", () => {
|
||||
it("should match typical function replacement", () => {
|
||||
const fileContent = `class Calculator {
|
||||
constructor() {
|
||||
this.result = 0;
|
||||
}
|
||||
|
|
@ -328,41 +557,41 @@ const a = 1;`;
|
|||
return this;
|
||||
}
|
||||
}`;
|
||||
const searchContent = ` reset() {
|
||||
const searchContent = ` reset() {
|
||||
this.result = 0;
|
||||
return this;
|
||||
}`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
const expectedStart = fileContent.indexOf(searchContent);
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
const expectedStart = fileContent.indexOf(searchContent);
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should match import statement", () => {
|
||||
const fileContent = `import React from 'react';
|
||||
it("should match import statement", () => {
|
||||
const fileContent = `import React from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import './App.css';
|
||||
|
||||
function App() {
|
||||
return <div>Hello World</div>;
|
||||
}`;
|
||||
const searchContent = `import { useState, useEffect } from 'react';`;
|
||||
const searchContent = `import { useState, useEffect } from 'react';`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
const expectedStart = fileContent.indexOf(searchContent);
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
const expectedStart = fileContent.indexOf(searchContent);
|
||||
expect(result).toEqual({
|
||||
startIndex: expectedStart,
|
||||
endIndex: expectedStart + searchContent.length,
|
||||
});
|
||||
});
|
||||
|
||||
it("should match comment block", () => {
|
||||
const fileContent = `/**
|
||||
it("should match comment block", () => {
|
||||
const fileContent = `/**
|
||||
* This is a comment
|
||||
* that spans multiple lines
|
||||
*/
|
||||
|
|
@ -370,17 +599,16 @@ function test() {
|
|||
// Single line comment
|
||||
return true;
|
||||
}`;
|
||||
const searchContent = `/**
|
||||
const searchContent = `/**
|
||||
* This is a comment
|
||||
* that spans multiple lines
|
||||
*/`;
|
||||
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
const result = findSearchMatch(fileContent, searchContent);
|
||||
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: searchContent.length,
|
||||
});
|
||||
expect(result).toEqual({
|
||||
startIndex: 0,
|
||||
endIndex: searchContent.length,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -51,10 +51,71 @@ function trimmedMatch(
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whitespace-ignored matching strategy
|
||||
* Removes all whitespace from both content and search, then finds the match
|
||||
*/
|
||||
function whitespaceIgnoredMatch(
|
||||
fileContent: string,
|
||||
searchContent: string,
|
||||
): SearchMatchResult | null {
|
||||
// Remove all whitespace (spaces, tabs, newlines, etc.)
|
||||
const strippedFileContent = fileContent.replace(/\s/g, "");
|
||||
const strippedSearchContent = searchContent.replace(/\s/g, "");
|
||||
|
||||
if (strippedSearchContent === "") {
|
||||
return null; // Empty search after stripping whitespace
|
||||
}
|
||||
|
||||
const strippedIndex = strippedFileContent.indexOf(strippedSearchContent);
|
||||
if (strippedIndex === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Map the stripped position back to the original file content
|
||||
let originalStartIndex = 0;
|
||||
let strippedCharCount = 0;
|
||||
|
||||
// Find the original position by counting non-whitespace characters
|
||||
for (let i = 0; i < fileContent.length; i++) {
|
||||
if (strippedCharCount === strippedIndex) {
|
||||
originalStartIndex = i;
|
||||
break;
|
||||
}
|
||||
if (!/\s/.test(fileContent[i])) {
|
||||
strippedCharCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// Find the end position by counting the length of the search content
|
||||
let originalEndIndex = originalStartIndex;
|
||||
let matchedChars = 0;
|
||||
|
||||
for (
|
||||
let i = originalStartIndex;
|
||||
i < fileContent.length && matchedChars < strippedSearchContent.length;
|
||||
i++
|
||||
) {
|
||||
if (!/\s/.test(fileContent[i])) {
|
||||
matchedChars++;
|
||||
}
|
||||
originalEndIndex = i + 1;
|
||||
}
|
||||
|
||||
return {
|
||||
startIndex: originalStartIndex,
|
||||
endIndex: originalEndIndex,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordered list of matching strategies to try
|
||||
*/
|
||||
const matchingStrategies: MatchStrategy[] = [exactMatch, trimmedMatch];
|
||||
const matchingStrategies: MatchStrategy[] = [
|
||||
exactMatch,
|
||||
trimmedMatch,
|
||||
whitespaceIgnoredMatch,
|
||||
];
|
||||
|
||||
/**
|
||||
* Find the exact match position for search content in file content.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue