fix(external_editor): rewrite corrupted file, proper function ordering

This commit is contained in:
2026-05-07 21:04:55 -04:00
parent ca4719687a
commit fa0026371d
+8 -21
View File
@@ -1,6 +1,7 @@
"""External Editor Launcher - Opens files in external text editors for diff viewing.""" """External Editor Launcher - Opens files in external text editors for diff viewing."""
from __future__ import annotations from __future__ import annotations
import os
import subprocess import subprocess
import tempfile import tempfile
from pathlib import Path from pathlib import Path
@@ -49,22 +50,7 @@ class ExternalEditorLauncher:
_cached_vscode_config: Optional[TextEditorConfig] = None _cached_vscode_config: Optional[TextEditorConfig] = None
def auto_detect_vscode() -> Optional[TextEditorConfig]: def _find_vscode_in_registry() -> Optional[str]:
global _cached_vscode_config
if _cached_vscode_config is not None:
return _cached_vscode_config
vscode_path = _find_vscode_in_registry() or _find_vscode_common_paths()
if vscode_path:
_cached_vscode_config = TextEditorConfig(
name="vscode",
path=vscode_path,
diff_args=["--new-window", "--diff"]
)
return _cached_vscode_config
def get_default_launcher() -> ExternalEditorLauncher:
import subprocess
paths = [] paths = []
reg_keys = [ reg_keys = [
r"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", r"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
@@ -81,7 +67,7 @@ def get_default_launcher() -> ExternalEditorLauncher:
line = line.strip() line = line.strip()
if line and line != "": if line and line != "":
exe_path = line.strip() + "\\Code.exe" exe_path = line.strip() + "\\Code.exe"
if subprocess.run(["test", "-f", exe_path], check=False).returncode == 0: if os.path.exists(exe_path):
paths.append(exe_path) paths.append(exe_path)
except Exception: except Exception:
pass pass
@@ -91,7 +77,6 @@ def get_default_launcher() -> ExternalEditorLauncher:
def _find_vscode_common_paths() -> Optional[str]: def _find_vscode_common_paths() -> Optional[str]:
import os
candidates = [ candidates = [
r"C:\apps\Microsoft VS Code\Code.exe", r"C:\apps\Microsoft VS Code\Code.exe",
r"C:\Program Files\Microsoft VS Code\Code.exe", r"C:\Program Files\Microsoft VS Code\Code.exe",
@@ -105,14 +90,17 @@ def _find_vscode_common_paths() -> Optional[str]:
def auto_detect_vscode() -> Optional[TextEditorConfig]: def auto_detect_vscode() -> Optional[TextEditorConfig]:
global _cached_vscode_config
if _cached_vscode_config is not None:
return _cached_vscode_config
vscode_path = _find_vscode_in_registry() or _find_vscode_common_paths() vscode_path = _find_vscode_in_registry() or _find_vscode_common_paths()
if vscode_path: if vscode_path:
return TextEditorConfig( _cached_vscode_config = TextEditorConfig(
name="vscode", name="vscode",
path=vscode_path, path=vscode_path,
diff_args=["--new-window", "--diff"] diff_args=["--new-window", "--diff"]
) )
return None return _cached_vscode_config
def get_default_launcher() -> ExternalEditorLauncher: def get_default_launcher() -> ExternalEditorLauncher:
@@ -152,4 +140,3 @@ def create_temp_modified_file(content: str) -> str:
with tempfile.NamedTemporaryFile(mode="w", suffix="_modified", delete=False, encoding="utf-8") as f: with tempfile.NamedTemporaryFile(mode="w", suffix="_modified", delete=False, encoding="utf-8") as f:
f.write(content) f.write(content)
return f.name return f.name
pass