Add CodeEditor creation to Component/Tool (switching between JetBrains- and LS-based variants)

This commit is contained in:
Dominik Jain authored and Dominik Jain committed 2025-07-09 20:05:59 +02:00
1 parent e989d1f87a
commit 3873ca3506
4 files changed
+15 -8

No files matched your search

+1 -1
View File
@@ -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
+2 -3
View File
@@ -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
+3 -4
View File
@@ -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
+9
View File
@@ -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