From 0c11ebebcc63bca1afbb9263b2230438a32274ce Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Mon, 23 Jun 2025 14:11:41 +0200 Subject: [PATCH] Remove obsolete flag at_new_line --- src/serena/agent.py | 14 +----- src/serena/symbol.py | 102 +++++++++++++++++-------------------------- 2 files changed, 41 insertions(+), 75 deletions(-) diff --git a/src/serena/agent.py b/src/serena/agent.py index b51285e..b8cde47 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -1738,12 +1738,7 @@ class InsertAfterSymbolTool(Tool, ToolMarkerCanEdit): :param body: the body/content to be inserted. The inserted code shall begin with the next line after the symbol. """ - self.symbol_manager.insert_after_symbol( - name_path, - relative_file_path=relative_path, - body=body, - use_same_indentation=False, - ) + self.symbol_manager.insert_after_symbol(name_path, relative_file_path=relative_path, body=body, use_same_indentation=False) return SUCCESS_RESULT @@ -1767,12 +1762,7 @@ class InsertBeforeSymbolTool(Tool, ToolMarkerCanEdit): :param relative_path: the relative path to the file containing the symbol :param body: the body/content to be inserted before the line in which the referenced symbol is defined """ - self.symbol_manager.insert_before_symbol( - name_path, - relative_file_path=relative_path, - body=body, - use_same_indentation=False, - ) + self.symbol_manager.insert_before_symbol(name_path, relative_file_path=relative_path, body=body, use_same_indentation=False) return SUCCESS_RESULT diff --git a/src/serena/symbol.py b/src/serena/symbol.py index 4466322..e92ffdf 100644 --- a/src/serena/symbol.py +++ b/src/serena/symbol.py @@ -725,15 +725,7 @@ class SymbolManager: def _count_trailing_newlines(cls, text: Reversible) -> int: return cls._count_leading_newlines(reversed(text)) - def insert_after_symbol( - self, - name_path: str, - relative_file_path: str, - body: str, - *, - use_same_indentation: bool = True, - at_new_line: bool = True, - ) -> None: + def insert_after_symbol(self, name_path: str, relative_file_path: str, body: str, *, use_same_indentation: bool = True) -> None: """ Inserts content after the symbol with the given name in the given file. """ @@ -747,13 +739,9 @@ class SymbolManager: f"Found symbols at locations: \n" + json.dumps([s.location.to_dict() for s in symbol_candidates], indent=2) ) symbol = symbol_candidates[-1] - return self.insert_after_symbol_at_location( - symbol.location, body, at_new_line=at_new_line, use_same_indentation=use_same_indentation - ) + return self.insert_after_symbol_at_location(symbol.location, body, use_same_indentation=use_same_indentation) - def insert_after_symbol_at_location( - self, location: SymbolLocation, body: str, *, at_new_line: bool = True, use_same_indentation: bool = True - ) -> None: + def insert_after_symbol_at_location(self, location: SymbolLocation, body: str, *, use_same_indentation: bool = True) -> None: """ Appends content after the given symbol @@ -775,25 +763,22 @@ class SymbolManager: if pos is None: raise ValueError(f"Symbol at {location} does not have a defined end position.") - line, col = pos["line"], pos["character"] - - if at_new_line: - # start at beginning of next line - col = 0 - line += 1 - # make sure a suitable number of leading empty lines is used (at least 0/1 depending on the symbol type, - # otherweise as many as the caller wanted to insert) - original_leading_newlines = self._count_leading_newlines(body) - body = body.lstrip("\r\n") - min_empty_lines = 0 - if symbol.is_neighbouring_definition_separated_by_empty_line(): - min_empty_lines = 1 - num_leading_empty_lines = max(min_empty_lines, original_leading_newlines) - if num_leading_empty_lines: - body = ("\n" * num_leading_empty_lines) + body - # make sure the one line break succeeding the original symbol, which we repurposed as prefix via - # `line += 1`, is replaced - body = body.rstrip("\r\n") + "\n" + # start at the beginning of the next line + col = 0 + line = pos["line"] + 1 + # make sure a suitable number of leading empty lines is used (at least 0/1 depending on the symbol type, + # otherweise as many as the caller wanted to insert) + original_leading_newlines = self._count_leading_newlines(body) + body = body.lstrip("\r\n") + min_empty_lines = 0 + if symbol.is_neighbouring_definition_separated_by_empty_line(): + min_empty_lines = 1 + num_leading_empty_lines = max(min_empty_lines, original_leading_newlines) + if num_leading_empty_lines: + body = ("\n" * num_leading_empty_lines) + body + # make sure the one line break succeeding the original symbol, which we repurposed as prefix via + # `line += 1`, is replaced + body = body.rstrip("\r\n") + "\n" if use_same_indentation: symbol_start_pos = symbol.body_start_position @@ -817,20 +802,11 @@ class SymbolManager: # > test test # > second line # > dataclass_instance.status = "active" # Reassign dataclass field - col = 0 with self._edited_symbol_location(location): self._lang_server.insert_text_at_position(location.relative_path, line=line, column=col, text_to_be_inserted=body) - def insert_before_symbol( - self, - name_path: str, - relative_file_path: str, - body: str, - *, - at_new_line: bool = True, - use_same_indentation: bool = True, - ) -> None: + def insert_before_symbol(self, name_path: str, relative_file_path: str, body: str, *, use_same_indentation: bool = True) -> None: """ Inserts content before the symbol with the given name in the given file. """ @@ -844,11 +820,9 @@ class SymbolManager: f"Found symbols at locations: \n" + json.dumps([s.location.to_dict() for s in symbol_candidates], indent=2) ) symbol = symbol_candidates[0] - self.insert_before_symbol_at_location(symbol.location, body, at_new_line=at_new_line, use_same_indentation=use_same_indentation) + self.insert_before_symbol_at_location(symbol.location, body, use_same_indentation=use_same_indentation) - def insert_before_symbol_at_location( - self, location: SymbolLocation, body: str, *, at_new_line: bool = True, use_same_indentation: bool = True - ) -> None: + def insert_before_symbol_at_location(self, location: SymbolLocation, body: str, *, use_same_indentation: bool = True) -> None: """ Inserts content before the given symbol @@ -859,25 +833,27 @@ class SymbolManager: symbol_start_pos = symbol.body_start_position if symbol_start_pos is None: raise ValueError(f"Symbol at {location} does not have a defined start position.") - line = symbol_start_pos["line"] - col = symbol_start_pos["character"] if use_same_indentation: - indent = " " * (col) + indent = " " * (symbol_start_pos["character"]) body = "\n".join(indent + line for line in body.splitlines()) - if at_new_line: - col = 0 - original_trailing_empty_lines = self._count_trailing_newlines(body) - 1 - # ensure eol is present at end - body = body.rstrip() + "\n" - # add suitable number of trailing empty lines after the body (at least 0/1 depending on the symbol type, - # otherwise as many as the caller wanted to insert) - min_trailing_empty_lines = 0 - if symbol.is_neighbouring_definition_separated_by_empty_line(): - min_trailing_empty_lines = 1 - num_trailing_newlines = max(min_trailing_empty_lines, original_trailing_empty_lines) - body += "\n" * num_trailing_newlines + # insert position is the start of line where the symbol is defined + line = symbol_start_pos["line"] + col = 0 + + original_trailing_empty_lines = self._count_trailing_newlines(body) - 1 + + # ensure eol is present at end + body = body.rstrip() + "\n" + + # add suitable number of trailing empty lines after the body (at least 0/1 depending on the symbol type, + # otherwise as many as the caller wanted to insert) + min_trailing_empty_lines = 0 + if symbol.is_neighbouring_definition_separated_by_empty_line(): + min_trailing_empty_lines = 1 + num_trailing_newlines = max(min_trailing_empty_lines, original_trailing_empty_lines) + body += "\n" * num_trailing_newlines assert location.relative_path is not None