refactor: Update editor discovery to support custom editor and remove AIDER_EDITOR

This commit is contained in:
Paul Gauthier (aider) 2024-11-21 09:55:15 -08:00
parent 2a387707ef
commit 2a1d2ef294

View file

@ -73,7 +73,6 @@ def get_environment_editor(default=None):
This function checks the following environment variables in order to This function checks the following environment variables in order to
determine the user's preferred editor: determine the user's preferred editor:
- AIDER_EDITOR
- VISUAL - VISUAL
- EDITOR - EDITOR
@ -82,13 +81,11 @@ def get_environment_editor(default=None):
:return: The preferred editor as specified by environment variables or the default value. :return: The preferred editor as specified by environment variables or the default value.
:rtype: str or None :rtype: str or None
""" """
editor = os.environ.get( editor = os.environ.get("VISUAL", os.environ.get("EDITOR", default))
"AIDER_EDITOR", os.environ.get("VISUAL", os.environ.get("EDITOR", default))
)
return editor return editor
def discover_editor(): def discover_editor(editor_override=None):
""" """
Discovers and returns the appropriate editor command as a list of arguments. Discovers and returns the appropriate editor command as a list of arguments.
@ -105,7 +102,10 @@ def discover_editor():
default_editor = DEFAULT_EDITOR_OS_X default_editor = DEFAULT_EDITOR_OS_X
else: else:
default_editor = DEFAULT_EDITOR_NIX default_editor = DEFAULT_EDITOR_NIX
editor = get_environment_editor(default_editor) if editor_override:
editor = editor_override
else:
editor = get_environment_editor(default_editor)
try: try:
return shlex.split(editor) return shlex.split(editor)
except ValueError as e: except ValueError as e:
@ -127,7 +127,7 @@ def file_editor(filepath):
subprocess.call(command_parts) subprocess.call(command_parts)
def pipe_editor(input_data="", suffix=None): def pipe_editor(input_data="", suffix=None, editor=None):
""" """
Opens the system editor with optional input data and returns the edited content. Opens the system editor with optional input data and returns the edited content.
@ -143,7 +143,9 @@ def pipe_editor(input_data="", suffix=None):
:rtype: str :rtype: str
""" """
filepath = write_temp_file(input_data, suffix) filepath = write_temp_file(input_data, suffix)
file_editor(filepath) command_parts = discover_editor(editor)
command_parts.append(filepath)
subprocess.call(command_parts)
with open(filepath, "r") as f: with open(filepath, "r") as f:
output_data = f.read() output_data = f.read()
try: try: