refactor Commands to a new file

This commit is contained in:
Paul Gauthier 2023-05-10 11:08:57 -07:00
parent 82ee7fe0a5
commit 2f85dc6733
2 changed files with 15 additions and 23 deletions

View file

@ -21,13 +21,14 @@ from aider.dump import dump
from aider.getinput import get_input from aider.getinput import get_input
from aider import utils from aider import utils
from aider import prompts from aider import prompts
from aider.commands import Commands
openai.api_key = os.getenv("OPENAI_API_KEY") openai.api_key = os.getenv("OPENAI_API_KEY")
class Coder: class Coder:
fnames = set() fnames = set()
relative_to_fname = dict() commands = Commands()
last_modified = 0 last_modified = 0
repo = None repo = None
@ -180,7 +181,11 @@ class Coder:
else: else:
print() print()
inp = get_input(self.history_file, self.fnames) inp = get_input(self.history_file, self.fnames, self.commands)
if inp.startswith("/"):
self.commands.run(inp, self.console)
return
self.num_control_c = 0 self.num_control_c = 0

View file

@ -12,19 +12,6 @@ import sys
import time import time
import random import random
class Commands:
def cmd_help(self):
print('help')
def cmd_ls(self):
print('ls')
def get_commands(self):
commands = []
for attr in dir(self):
if attr.startswith("cmd_"):
commands.append('/' + attr[4:])
return commands
class FileContentCompleter(Completer): class FileContentCompleter(Completer):
def __init__(self, fnames, commands): def __init__(self, fnames, commands):
@ -34,7 +21,7 @@ class FileContentCompleter(Completer):
for fname in fnames: for fname in fnames:
with open(fname, "r") as f: with open(fname, "r") as f:
content = f.read() content = f.read()
self.words.update(re.split(r'\W+', content)) self.words.update(re.split(r"\W+", content))
def get_completions(self, document, complete_event): def get_completions(self, document, complete_event):
text = document.text_before_cursor text = document.text_before_cursor
@ -42,7 +29,7 @@ class FileContentCompleter(Completer):
if not words: if not words:
return return
if text[0] == '/' and len(words) == 1: if text[0] == "/" and len(words) == 1:
candidates = self.commands.get_commands() candidates = self.commands.get_commands()
else: else:
candidates = self.words candidates = self.words
@ -52,6 +39,7 @@ class FileContentCompleter(Completer):
if word.lower().startswith(last_word.lower()): if word.lower().startswith(last_word.lower()):
yield Completion(word, start_position=-len(last_word)) yield Completion(word, start_position=-len(last_word))
def canned_input(show_prompt): def canned_input(show_prompt):
console = Console() console = Console()
@ -66,21 +54,20 @@ def canned_input(show_prompt):
return input_line return input_line
def get_input(history_file, fnames, commands):
def get_input(history_file, fnames):
fnames = list(fnames) fnames = list(fnames)
if len(fnames) > 1: if len(fnames) > 1:
common_prefix = os.path.commonprefix(fnames) common_prefix = os.path.commonprefix(fnames)
short_fnames = [fname.replace(common_prefix, '', 1) for fname in fnames] short_fnames = [fname.replace(common_prefix, "", 1) for fname in fnames]
else: else:
short_fnames = [os.path.basename(fnames[0])] short_fnames = [os.path.basename(fnames[0])]
show = ' '.join(short_fnames) show = " ".join(short_fnames)
if len(show) > 10: if len(show) > 10:
show += "\n" show += "\n"
show += "> " show += "> "
if not sys.stdin.isatty(): if not sys.stdin.isatty():
return canned_input(show_prompt) return canned_input(show)
inp = "" inp = ""
multiline_input = False multiline_input = False
@ -88,7 +75,7 @@ def get_input(history_file, fnames):
style = Style.from_dict({"": "green"}) style = Style.from_dict({"": "green"})
while True: while True:
completer_instance = FileContentCompleter(fnames, Commands()) completer_instance = FileContentCompleter(fnames, commands)
if multiline_input: if multiline_input:
show = ". " show = ". "