mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-31 17:55:01 +00:00
cache the completions in AutoCompleter
This commit is contained in:
parent
60e838df9f
commit
4b0192ffd9
2 changed files with 28 additions and 22 deletions
|
@ -41,7 +41,7 @@ class Commands:
|
||||||
raise SwitchModel(model)
|
raise SwitchModel(model)
|
||||||
|
|
||||||
def completions_model(self):
|
def completions_model(self):
|
||||||
models = [] # litellm.model_cost.keys()
|
models = litellm.model_cost.keys()
|
||||||
return models
|
return models
|
||||||
|
|
||||||
def cmd_models(self, args):
|
def cmd_models(self, args):
|
||||||
|
@ -79,20 +79,22 @@ class Commands:
|
||||||
def is_command(self, inp):
|
def is_command(self, inp):
|
||||||
return inp[0] in "/!"
|
return inp[0] in "/!"
|
||||||
|
|
||||||
def get_commands(self, with_completions=False):
|
def get_completions(self, cmd):
|
||||||
commands = dict()
|
assert cmd.startswith("/")
|
||||||
|
cmd = cmd[1:]
|
||||||
|
|
||||||
|
fun = getattr(self, f"completions_{cmd}", None)
|
||||||
|
if not fun:
|
||||||
|
return []
|
||||||
|
return sorted(fun())
|
||||||
|
|
||||||
|
def get_commands(self):
|
||||||
|
commands = []
|
||||||
for attr in dir(self):
|
for attr in dir(self):
|
||||||
if not attr.startswith("cmd_"):
|
if not attr.startswith("cmd_"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
cmd = attr[4:]
|
cmd = attr[4:]
|
||||||
completions = getattr(self, f"completions_{cmd}", None)
|
commands.append("/" + cmd)
|
||||||
if completions and with_completions:
|
|
||||||
completions = sorted(completions())
|
|
||||||
else:
|
|
||||||
completions = []
|
|
||||||
|
|
||||||
commands["/" + cmd] = completions
|
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
|
|
||||||
|
@ -112,7 +114,7 @@ class Commands:
|
||||||
first_word = words[0]
|
first_word = words[0]
|
||||||
rest_inp = inp[len(words[0]) :]
|
rest_inp = inp[len(words[0]) :]
|
||||||
|
|
||||||
all_commands = self.get_commands().keys()
|
all_commands = self.get_commands()
|
||||||
matching_commands = [cmd for cmd in all_commands if cmd.startswith(first_word)]
|
matching_commands = [cmd for cmd in all_commands if cmd.startswith(first_word)]
|
||||||
return matching_commands, first_word, rest_inp
|
return matching_commands, first_word, rest_inp
|
||||||
|
|
||||||
|
@ -618,7 +620,7 @@ class Commands:
|
||||||
|
|
||||||
def cmd_help(self, args):
|
def cmd_help(self, args):
|
||||||
"Show help about all commands"
|
"Show help about all commands"
|
||||||
commands = sorted(self.get_commands().keys())
|
commands = sorted(self.get_commands())
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
cmd_method_name = f"cmd_{cmd[1:]}"
|
cmd_method_name = f"cmd_{cmd[1:]}"
|
||||||
cmd_method = getattr(self, cmd_method_name, None)
|
cmd_method = getattr(self, cmd_method_name, None)
|
||||||
|
@ -632,7 +634,7 @@ class Commands:
|
||||||
"Show help about all commands in markdown"
|
"Show help about all commands in markdown"
|
||||||
|
|
||||||
res = ""
|
res = ""
|
||||||
commands = sorted(self.get_commands().keys())
|
commands = sorted(self.get_commands())
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
cmd_method_name = f"cmd_{cmd[1:]}"
|
cmd_method_name = f"cmd_{cmd[1:]}"
|
||||||
cmd_method = getattr(self, cmd_method_name, None)
|
cmd_method = getattr(self, cmd_method_name, None)
|
||||||
|
|
20
aider/io.py
20
aider/io.py
|
@ -36,12 +36,9 @@ class AutoCompleter(Completer):
|
||||||
|
|
||||||
self.words = set()
|
self.words = set()
|
||||||
|
|
||||||
import time
|
self.commands = commands
|
||||||
|
self.command_names = self.commands.get_commands()
|
||||||
start = time.time()
|
self.command_completions = dict()
|
||||||
self.commands = commands.get_commands(with_completions=True)
|
|
||||||
dur = time.time() - start
|
|
||||||
dump(dur)
|
|
||||||
|
|
||||||
for rel_fname in addable_rel_fnames:
|
for rel_fname in addable_rel_fnames:
|
||||||
self.words.add(rel_fname)
|
self.words.add(rel_fname)
|
||||||
|
@ -71,7 +68,7 @@ class AutoCompleter(Completer):
|
||||||
if text[0] == "/":
|
if text[0] == "/":
|
||||||
if len(words) == 1 and not text[-1].isspace():
|
if len(words) == 1 and not text[-1].isspace():
|
||||||
partial = words[0]
|
partial = words[0]
|
||||||
candidates = self.commands.keys()
|
candidates = self.command_names
|
||||||
for cmd in candidates:
|
for cmd in candidates:
|
||||||
if cmd.startswith(partial):
|
if cmd.startswith(partial):
|
||||||
yield Completion(cmd, start_position=-len(partial))
|
yield Completion(cmd, start_position=-len(partial))
|
||||||
|
@ -79,7 +76,14 @@ class AutoCompleter(Completer):
|
||||||
cmd = words[0]
|
cmd = words[0]
|
||||||
partial = words[-1]
|
partial = words[-1]
|
||||||
|
|
||||||
candidates = self.commands.get(cmd, [])
|
if cmd not in self.command_names:
|
||||||
|
return
|
||||||
|
if cmd not in self.command_completions:
|
||||||
|
candidates = self.commands.get_completions(cmd)
|
||||||
|
self.command_completions[cmd] = candidates
|
||||||
|
else:
|
||||||
|
candidates = self.command_completions[cmd]
|
||||||
|
|
||||||
for word in candidates:
|
for word in candidates:
|
||||||
if partial in word:
|
if partial in word:
|
||||||
yield Completion(word, start_position=-len(partial))
|
yield Completion(word, start_position=-len(partial))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue