diff --git a/src/serena/agent.py b/src/serena/agent.py index 8985b65c..59b98f13 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -1268,14 +1268,15 @@ class ReplaceSymbolBodyTool(Tool, ToolMarkerCanEdit): relative_path: str, body: str, ) -> str: - """ + r""" Replaces the body of the symbol with the given `name_path`. - IMPORTANT: + + Important: You don't need to provide an adjusted indentation, as the tool will automatically add the indentation of the original symbol to each line. For example, for replacing a method in python, you can just write (using the standard python indentation): body="def my_method_replacement(self, ...):\n first_line\n second_line...". So each line after the first line only has - an indentation of 4 (the indentation relative to the first characted), + an indentation of 4 (the indentation relative to the first characted), since the additional indentation will be added by the tool. Same for more deeply nested cases. You always only need to write the relative indentation to the first character of the first line, and that in turn should not have any indentation. @@ -1283,7 +1284,8 @@ class ReplaceSymbolBodyTool(Tool, ToolMarkerCanEdit): :param name_path: for finding the symbol to replace, same logic as in the `find_symbol` tool. :param relative_path: the relative path to the file containing the symbol - :param body: the new symbol body. + :param body: the new symbol body. + """ self.symbol_manager.replace_body( name_path, diff --git a/src/serena/symbol.py b/src/serena/symbol.py index e566f7f9..7f75098b 100644 --- a/src/serena/symbol.py +++ b/src/serena/symbol.py @@ -684,7 +684,8 @@ class SymbolManager: start_line, start_col = start_pos["line"], start_pos["character"] if use_same_indentation: indent = " " * start_col - body = "\n".join(indent + line for line in body.splitlines()) + body_lines = body.splitlines() + body = body_lines[0] + "\n" + "\n".join(indent + line for line in body_lines[1:]) # make sure body always ends with at least one newline if not body.endswith("\n"):