From 3873ca350624d2d2c7756692a4976d463bdfa175 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Wed, 9 Jul 2025 18:02:03 +0200 Subject: [PATCH] Add CodeEditor creation to Component/Tool (switching between JetBrains- and LS-based variants) --- src/serena/code_editor.py | 2 +- src/serena/tools/file_tools.py | 5 ++--- src/serena/tools/ls_tools.py | 7 +++---- src/serena/tools/tools_base.py | 9 +++++++++ 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/serena/code_editor.py b/src/serena/code_editor.py index 16a108b2..b0d6de37 100644 --- a/src/serena/code_editor.py +++ b/src/serena/code_editor.py @@ -12,7 +12,7 @@ from solidlsp.ls import LSPFileBuffer from solidlsp.ls_utils import TextUtils from .config.serena_config import Project -from .tools import JetBrainsPluginClient +from .tools.jetbrains_plugin_client import JetBrainsPluginClient if TYPE_CHECKING: from .agent import SerenaAgent diff --git a/src/serena/tools/file_tools.py b/src/serena/tools/file_tools.py index 81c7faff..0bd091ee 100644 --- a/src/serena/tools/file_tools.py +++ b/src/serena/tools/file_tools.py @@ -13,7 +13,6 @@ from collections import defaultdict from fnmatch import fnmatch from pathlib import Path -from serena.code_editor import LanguageServerCodeEditor from serena.text_utils import search_files from serena.tools import SUCCESS_RESULT, TOOL_DEFAULT_MAX_ANSWER_LENGTH, EditedFileContext, Tool, ToolMarkerCanEdit from serena.util.file_system import scan_directory @@ -224,7 +223,7 @@ class DeleteLinesTool(Tool, ToolMarkerCanEdit): if not self.lines_read.were_lines_read(relative_path, (start_line, end_line)): read_lines_tool = self.agent.get_tool(ReadFileTool) return f"Error: Must call `{read_lines_tool.get_name_from_cls()}` first to read exactly the affected lines." - code_editor = LanguageServerCodeEditor(self.symbol_manager) + code_editor = self.create_code_editor() code_editor.delete_lines(relative_path, start_line, end_line) return SUCCESS_RESULT @@ -283,7 +282,7 @@ class InsertAtLineTool(Tool, ToolMarkerCanEdit): """ if not content.endswith("\n"): content += "\n" - code_editor = LanguageServerCodeEditor(self.symbol_manager) + code_editor = self.create_code_editor() code_editor.insert_at_line(relative_path, line, content) return SUCCESS_RESULT diff --git a/src/serena/tools/ls_tools.py b/src/serena/tools/ls_tools.py index 859cd4f1..19109280 100644 --- a/src/serena/tools/ls_tools.py +++ b/src/serena/tools/ls_tools.py @@ -7,7 +7,6 @@ from collections.abc import Sequence from copy import copy from typing import Any -from serena.code_editor import LanguageServerCodeEditor from serena.tools import SUCCESS_RESULT, TOOL_DEFAULT_MAX_ANSWER_LENGTH, Tool, ToolMarkerCanEdit from solidlsp.ls_types import SymbolKind @@ -223,7 +222,7 @@ class ReplaceSymbolBodyTool(Tool, ToolMarkerCanEdit): :param body: the new symbol body. Important: Begin directly with the symbol definition and provide no leading indentation for the first line (but do indent the rest of the body according to the context). """ - code_editor = LanguageServerCodeEditor(self.symbol_manager) + code_editor = self.create_code_editor() code_editor.replace_body( name_path, relative_file_path=relative_path, @@ -252,7 +251,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. """ - code_editor = LanguageServerCodeEditor(self.symbol_manager) + code_editor = self.create_code_editor() code_editor.insert_after_symbol(name_path, relative_file_path=relative_path, body=body) return SUCCESS_RESULT @@ -277,6 +276,6 @@ 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 """ - code_editor = LanguageServerCodeEditor(self.symbol_manager) + code_editor = self.create_code_editor() code_editor.insert_before_symbol(name_path, relative_file_path=relative_path, body=body) return SUCCESS_RESULT diff --git a/src/serena/tools/tools_base.py b/src/serena/tools/tools_base.py index 8cf53ca6..67f43f97 100644 --- a/src/serena/tools/tools_base.py +++ b/src/serena/tools/tools_base.py @@ -19,6 +19,7 @@ from solidlsp import SolidLanguageServer if TYPE_CHECKING: from serena.agent import LinesRead, MemoriesManager, SerenaAgent + from serena.code_editor import CodeEditor log = logging.getLogger(__name__) T = TypeVar("T") @@ -54,6 +55,14 @@ class Component(ABC): assert self.agent.symbol_manager is not None return self.agent.symbol_manager + def create_code_editor(self) -> "CodeEditor": + from ..code_editor import JetBrainsCodeEditor, LanguageServerCodeEditor + + if self.agent.serena_config.jetbrains: + return JetBrainsCodeEditor(project=self.agent.get_active_project(), agent=self.agent) + else: + return LanguageServerCodeEditor(self.symbol_manager, agent=self.agent) + @property def lines_read(self) -> "LinesRead": assert self.agent.lines_read is not None