mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-12 20:15:56 +00:00
Permit rename of dirs/files in JB world, WIP
This commit is contained in:
1 parent
94b118dcdc
commit
10307f045f
3 files changed
+20
-16
No files matched your search
+13
-10
@@ -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,
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user