fix error in search and replace, update rule to utilize multiple operation

This commit is contained in:
sam hoang
2025-01-28 21:04:36 +07:00
parent 9b175a736e
commit 411182a5d9
2 changed files with 22 additions and 16 deletions

View File

@@ -1707,35 +1707,41 @@ export class Cline {
// Read the original file content // Read the original file content
const fileContent = await fs.readFile(absolutePath, "utf-8") const fileContent = await fs.readFile(absolutePath, "utf-8")
const lines = fileContent.split("\n") let lines = fileContent.split("\n")
let newContent = fileContent
// Apply each search/replace operation
for (const op of parsedOperations) { for (const op of parsedOperations) {
const flags = op.regex_flags ?? (op.ignore_case ? "gi" : "g")
const multilineFlags = flags.includes("m") ? flags : flags + "m"
const searchPattern = op.use_regex const searchPattern = op.use_regex
? new RegExp(op.search, op.regex_flags || (op.ignore_case ? "gi" : "g")) ? new RegExp(op.search, multilineFlags)
: new RegExp(escapeRegExp(op.search), op.ignore_case ? "gi" : "g") : new RegExp(escapeRegExp(op.search), multilineFlags)
if (op.start_line || op.end_line) { if (op.start_line || op.end_line) {
// Line-restricted replacement const startLine = Math.max((op.start_line ?? 1) - 1, 0)
const startLine = (op.start_line || 1) - 1 const endLine = Math.min((op.end_line ?? lines.length) - 1, lines.length - 1)
const endLine = (op.end_line || lines.length) - 1
// Get the content before and after the target section
const beforeLines = lines.slice(0, startLine) const beforeLines = lines.slice(0, startLine)
const targetLines = lines.slice(startLine, endLine + 1)
const afterLines = lines.slice(endLine + 1) const afterLines = lines.slice(endLine + 1)
const modifiedLines = targetLines.map((line) => // Get the target section and perform replacement
line.replace(searchPattern, op.replace), const targetContent = lines.slice(startLine, endLine + 1).join("\n")
) const modifiedContent = targetContent.replace(searchPattern, op.replace)
const modifiedLines = modifiedContent.split("\n")
newContent = [...beforeLines, ...modifiedLines, ...afterLines].join("\n") // Reconstruct the full content with the modified section
lines = [...beforeLines, ...modifiedLines, ...afterLines]
} else { } else {
// Global replacement // Global replacement
newContent = newContent.replace(searchPattern, op.replace) const fullContent = lines.join("\n")
const modifiedContent = fullContent.replace(searchPattern, op.replace)
lines = modifiedContent.split("\n")
} }
} }
const newContent = lines.join("\n")
this.consecutiveMistakeCount = 0 this.consecutiveMistakeCount = 0
// Show diff preview // Show diff preview

View File

@@ -27,8 +27,8 @@ ${
? "- 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." ? "- 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."
: "- 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." : "- 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."
} }
${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." : ""} ${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" : ""}
${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." : ""} ${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" : ""}
- 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. - 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.
- 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. - 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.
* For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$"