feat(external-editor): Add TextEditorConfig and ExternalEditorConfig models
- Add TextEditorConfig and ExternalEditorConfig dataclasses to models.py - Create src/external_editor.py with ExternalEditorLauncher class - Add tests for configuration and launcher functionality - Support for config.toml [tools.text_editors] and manual_slop.toml default_editor
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""External Editor Launcher - Opens files in external text editors for diff viewing."""
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional, List
|
||||
|
||||
from src.models import ExternalEditorConfig, TextEditorConfig
|
||||
|
||||
|
||||
class ExternalEditorLauncher:
|
||||
def __init__(self, config: ExternalEditorConfig):
|
||||
self.config = config
|
||||
|
||||
def get_editor(self, editor_name: Optional[str] = None) -> Optional[TextEditorConfig]:
|
||||
if editor_name:
|
||||
return self.config.editors.get(editor_name)
|
||||
return self.config.get_default()
|
||||
|
||||
def build_diff_command(
|
||||
self, editor: TextEditorConfig, original_path: str, modified_path: str
|
||||
) -> List[str]:
|
||||
cmd = [editor.path] + editor.diff_args + [original_path, modified_path]
|
||||
return cmd
|
||||
|
||||
def launch_diff(
|
||||
self, editor_name: Optional[str], original_path: str, modified_path: str
|
||||
) -> Optional[subprocess.Popen]:
|
||||
editor = self.get_editor(editor_name)
|
||||
if not editor:
|
||||
return None
|
||||
cmd = self.build_diff_command(editor, original_path, modified_path)
|
||||
try:
|
||||
return subprocess.Popen(cmd)
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
|
||||
def launch_editor(self, editor_name: Optional[str], file_path: str) -> Optional[subprocess.Popen]:
|
||||
editor = self.get_editor(editor_name)
|
||||
if not editor:
|
||||
return None
|
||||
try:
|
||||
return subprocess.Popen([editor.path, file_path])
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
|
||||
|
||||
def get_default_launcher() -> ExternalEditorLauncher:
|
||||
from src import models
|
||||
config = models.load_config()
|
||||
editors_config = config.get("tools", {}).get("text_editors", {})
|
||||
default_editor = config.get("tools", {}).get("default_editor")
|
||||
ext_config = ExternalEditorConfig.from_dict({
|
||||
"editors": editors_config,
|
||||
"default_editor": default_editor,
|
||||
})
|
||||
return ExternalEditorLauncher(ext_config)
|
||||
|
||||
|
||||
def resolve_project_editor_override(project_path: Optional[str]) -> Optional[str]:
|
||||
if not project_path:
|
||||
return None
|
||||
from src import models
|
||||
try:
|
||||
proj = models.load_project(project_path)
|
||||
return proj.get("default_editor")
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def create_temp_modified_file(content: str) -> str:
|
||||
with tempfile.NamedTemporaryFile(mode="w", suffix="_modified", delete=False, encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
return f.name
|
||||
pass
|
||||
+110
@@ -284,6 +284,8 @@ class WorkerContext:
|
||||
persona_id: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass
|
||||
@dataclass
|
||||
class Metadata:
|
||||
id: str
|
||||
@@ -324,6 +326,114 @@ class Metadata:
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TextEditorConfig:
|
||||
name: str
|
||||
path: str
|
||||
diff_args: List[str] = field(default_factory=list)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"path": self.path,
|
||||
"diff_args": self.diff_args,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "TextEditorConfig":
|
||||
return cls(
|
||||
name=data["name"],
|
||||
path=data["path"],
|
||||
diff_args=data.get("diff_args", []),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExternalEditorConfig:
|
||||
editors: Dict[str, TextEditorConfig] = field(default_factory=dict)
|
||||
default_editor: Optional[str] = None
|
||||
|
||||
def get_default(self) -> Optional[TextEditorConfig]:
|
||||
if self.default_editor and self.default_editor in self.editors:
|
||||
return self.editors[self.default_editor]
|
||||
if self.editors:
|
||||
return next(iter(self.editors.values()))
|
||||
return None
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"editors": {k: v.to_dict() for k, v in self.editors.items()},
|
||||
"default_editor": self.default_editor,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "ExternalEditorConfig":
|
||||
editors = {}
|
||||
for name, ed_data in data.get("editors", {}).items():
|
||||
if isinstance(ed_data, dict):
|
||||
editors[name] = TextEditorConfig.from_dict(ed_data)
|
||||
elif isinstance(ed_data, str):
|
||||
editors[name] = TextEditorConfig(name=name, path=ed_data)
|
||||
return cls(
|
||||
editors=editors,
|
||||
default_editor=data.get("default_editor"),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TextEditorConfig:
|
||||
name: str
|
||||
path: str
|
||||
diff_args: List[str] = field(default_factory=list)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"path": self.path,
|
||||
"diff_args": self.diff_args,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "TextEditorConfig":
|
||||
return cls(
|
||||
name=data["name"],
|
||||
path=data["path"],
|
||||
diff_args=data.get("diff_args", []),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExternalEditorConfig:
|
||||
editors: Dict[str, TextEditorConfig] = field(default_factory=dict)
|
||||
default_editor: Optional[str] = None
|
||||
|
||||
def get_default(self) -> Optional[TextEditorConfig]:
|
||||
if self.default_editor and self.default_editor in self.editors:
|
||||
return self.editors[self.default_editor]
|
||||
if self.editors:
|
||||
return next(iter(self.editors.values()))
|
||||
return None
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"editors": {k: v.to_dict() for k, v in self.editors.items()},
|
||||
"default_editor": self.default_editor,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> "ExternalEditorConfig":
|
||||
editors = {}
|
||||
for name, ed_data in data.get("editors", {}).items():
|
||||
if isinstance(ed_data, dict):
|
||||
editors[name] = TextEditorConfig.from_dict(ed_data)
|
||||
elif isinstance(ed_data, str):
|
||||
editors[name] = TextEditorConfig(name=name, path=ed_data)
|
||||
return cls(
|
||||
editors=editors,
|
||||
default_editor=data.get("default_editor"),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TrackState:
|
||||
metadata: Metadata
|
||||
|
||||
Reference in New Issue
Block a user