Skip to content

Commit 411182a

Browse files
committed
fix error in search and replace, update rule to utilize multiple operation
1 parent 9b175a7 commit 411182a

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/core/Cline.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,35 +1707,41 @@ export class Cline {
17071707

17081708
// Read the original file content
17091709
const fileContent = await fs.readFile(absolutePath, "utf-8")
1710-
const lines = fileContent.split("\n")
1711-
let newContent = fileContent
1710+
let lines = fileContent.split("\n")
17121711

1713-
// Apply each search/replace operation
17141712
for (const op of parsedOperations) {
1713+
const flags = op.regex_flags ?? (op.ignore_case ? "gi" : "g")
1714+
const multilineFlags = flags.includes("m") ? flags : flags + "m"
1715+
17151716
const searchPattern = op.use_regex
1716-
? new RegExp(op.search, op.regex_flags || (op.ignore_case ? "gi" : "g"))
1717-
: new RegExp(escapeRegExp(op.search), op.ignore_case ? "gi" : "g")
1717+
? new RegExp(op.search, multilineFlags)
1718+
: new RegExp(escapeRegExp(op.search), multilineFlags)
17181719

17191720
if (op.start_line || op.end_line) {
1720-
// Line-restricted replacement
1721-
const startLine = (op.start_line || 1) - 1
1722-
const endLine = (op.end_line || lines.length) - 1
1721+
const startLine = Math.max((op.start_line ?? 1) - 1, 0)
1722+
const endLine = Math.min((op.end_line ?? lines.length) - 1, lines.length - 1)
17231723

1724+
// Get the content before and after the target section
17241725
const beforeLines = lines.slice(0, startLine)
1725-
const targetLines = lines.slice(startLine, endLine + 1)
17261726
const afterLines = lines.slice(endLine + 1)
17271727

1728-
const modifiedLines = targetLines.map((line) =>
1729-
line.replace(searchPattern, op.replace),
1730-
)
1728+
// Get the target section and perform replacement
1729+
const targetContent = lines.slice(startLine, endLine + 1).join("\n")
1730+
const modifiedContent = targetContent.replace(searchPattern, op.replace)
1731+
const modifiedLines = modifiedContent.split("\n")
17311732

1732-
newContent = [...beforeLines, ...modifiedLines, ...afterLines].join("\n")
1733+
// Reconstruct the full content with the modified section
1734+
lines = [...beforeLines, ...modifiedLines, ...afterLines]
17331735
} else {
17341736
// Global replacement
1735-
newContent = newContent.replace(searchPattern, op.replace)
1737+
const fullContent = lines.join("\n")
1738+
const modifiedContent = fullContent.replace(searchPattern, op.replace)
1739+
lines = modifiedContent.split("\n")
17361740
}
17371741
}
17381742

1743+
const newContent = lines.join("\n")
1744+
17391745
this.consecutiveMistakeCount = 0
17401746

17411747
// Show diff preview

src/core/prompts/sections/rules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ ${
2727
? "- You should use apply_diff instead of write_to_file when making changes to existing files since it is much faster and easier to apply a diff than to write the entire file again. Only use write_to_file to edit files when apply_diff has failed repeatedly to apply the diff."
2828
: "- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool."
2929
}
30-
${experiments?.["insert_code_block"] === true ? "- Use the insert_code_block tool to add code snippets or content block to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location." : ""}
31-
${experiments?.["search_and_replace"] === true ? "- Use the search_and_replace tool to find and replace text or regex in files. This tool allows you to search for a specific regex pattern or text and replace it with another value. Be cautious when using this tool to ensure you are replacing the correct text." : ""}
30+
${experiments?.["insert_code_block"] === true ? "- Use the insert_code_block tool to add code snippets or content block to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location. It's can support multiple operations" : ""}
31+
${experiments?.["search_and_replace"] === true ? "- Use the search_and_replace tool to find and replace text or regex in files. This tool allows you to search for a specific regex pattern or text and replace it with another value. Be cautious when using this tool to ensure you are replacing the correct text. It's can support multiple operations" : ""}
3232
- Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode.
3333
- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write.
3434
* For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$"

0 commit comments

Comments
 (0)