From 10307f045fa5e6ce2c5aff191411d4eec65a3990 Mon Sep 17 00:00:00 2001 From: Michael Panchenko Date: Tue, 31 Mar 2026 15:54:11 +0200 Subject: [PATCH] Permit rename of dirs/files in JB world, WIP --- src/serena/code_editor.py | 23 +++++++++++-------- .../jetbrains/jetbrains_plugin_client.py | 2 +- src/serena/tools/symbol_tools.py | 11 +++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/serena/code_editor.py b/src/serena/code_editor.py index dfe4fe47..202efe0f 100644 --- a/src/serena/code_editor.py +++ b/src/serena/code_editor.py @@ -230,13 +230,14 @@ class CodeEditor(Generic[TSymbol], ABC): edited_file.delete_text_between_positions(start_pos, end_pos) @abstractmethod - def rename_symbol(self, name_path: str, relative_file_path: str, new_name: str) -> str: + def rename_symbol(self, name_path: str | None, relative_path: str, new_name: str) -> str: """ - Renames the symbol with the given name throughout the codebase. + Renames a symbol, file, or directory throughout the codebase. :param name_path: the name path of the symbol to rename - :param relative_file_path: the relative path of the file containing the symbol - :param new_name: the new name for the symbol + :param relative_path: if `name_path` is passed, the relative path of the file containing the symbol. + Otherwise, the path to the directory or file to rename. + :param new_name: the new name :return: a status message """ @@ -347,8 +348,10 @@ class LanguageServerCodeEditor(CodeEditor[LanguageServerSymbol]): operation.apply() return len(operations) - def rename_symbol(self, name_path: str, relative_file_path: str, new_name: str) -> str: - symbol = self._find_unique_symbol(name_path, relative_file_path) + def rename_symbol(self, name_path: str | None, relative_path: str, new_name: str) -> str: + if name_path is None: + raise NotImplementedError("Renaming files/directories is not yet implemented for in the language server backend.") + symbol = self._find_unique_symbol(name_path, relative_path) if not symbol.location.has_position_in_file(): raise ValueError(f"Symbol '{name_path}' does not have a valid position in file for renaming") @@ -356,9 +359,9 @@ class LanguageServerCodeEditor(CodeEditor[LanguageServerSymbol]): assert symbol.location.line is not None assert symbol.location.column is not None - lang_server = self._get_language_server(relative_file_path) + lang_server = self._get_language_server(relative_path) rename_result = lang_server.request_rename_symbol_edit( - relative_file_path=relative_file_path, line=symbol.location.line, column=symbol.location.column, new_name=new_name + relative_file_path=relative_path, line=symbol.location.line, column=symbol.location.column, new_name=new_name ) if rename_result is None: raise ValueError( @@ -425,11 +428,11 @@ class JetBrainsCodeEditor(CodeEditor[JetBrainsSymbol]): ) return JetBrainsSymbol(symbols[0], self._project) - def rename_symbol(self, name_path: str, relative_file_path: str, new_name: str) -> str: + def rename_symbol(self, name_path: str | None, relative_path: str, new_name: str) -> str: with JetBrainsPluginClient.from_project(self._project) as client: client.rename_symbol( name_path=name_path, - relative_path=relative_file_path, + relative_path=relative_path, new_name=new_name, rename_in_comments=False, rename_in_text_occurrences=False, diff --git a/src/serena/jetbrains/jetbrains_plugin_client.py b/src/serena/jetbrains/jetbrains_plugin_client.py index cd6b3b8b..f3ed6dc9 100644 --- a/src/serena/jetbrains/jetbrains_plugin_client.py +++ b/src/serena/jetbrains/jetbrains_plugin_client.py @@ -556,7 +556,7 @@ class JetBrainsPluginClient(ToStringMixin): return self._make_request("POST", "/inlineSymbol", request_data) def rename_symbol( - self, name_path: str, relative_path: str, new_name: str, rename_in_comments: bool, rename_in_text_occurrences: bool + self, name_path: str | None, relative_path: str, new_name: str, rename_in_comments: bool, rename_in_text_occurrences: bool ) -> None: """ Renames a symbol. diff --git a/src/serena/tools/symbol_tools.py b/src/serena/tools/symbol_tools.py index 2cf5c178..e17b74a0 100644 --- a/src/serena/tools/symbol_tools.py +++ b/src/serena/tools/symbol_tools.py @@ -408,27 +408,28 @@ class InsertBeforeSymbolTool(Tool, ToolMarkerSymbolicEdit): class RenameSymbolTool(Tool, ToolMarkerSymbolicEdit): """ - Renames a symbol throughout the codebase using language server refactoring capabilities. + Renames a symbol, file, or directory throughout the codebase using language server refactoring capabilities. """ def apply( self, - name_path: str, relative_path: str, new_name: str, + name_path: str | None = None, ) -> str: """ - Renames the symbol with the given `name_path` to `new_name` throughout the entire codebase. + Renames the symbol with the given `name_path` or a file/directory (if no `name_path` is passed) + to `new_name` throughout the entire codebase. Note: for languages with method overloading, like Java, name_path may have to include a method's signature to uniquely identify a method. - :param name_path: name path of the symbol to rename (definitions in the `find_symbol` tool apply) :param relative_path: the relative path to the file containing the symbol to rename :param new_name: the new name for the symbol + :param name_path: name path of the symbol to rename (definitions in the `find_symbol` tool apply) :return: result summary indicating success or failure """ code_editor = self.create_code_editor() - status_message = code_editor.rename_symbol(name_path, relative_file_path=relative_path, new_name=new_name) + status_message = code_editor.rename_symbol(name_path, relative_path=relative_path, new_name=new_name) return status_message