mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-25 23:05:00 +00:00
allow /commands to provide their own completions
This commit is contained in:
parent
623d36d831
commit
bc43d9ac31
2 changed files with 11 additions and 12 deletions
|
@ -36,12 +36,11 @@ class Commands:
|
|||
self.console.print(f"Error: Command {cmd_name} not found.")
|
||||
|
||||
def get_command_completions(self, cmd_name):
|
||||
cmd_completions_method_name = f"cmd_{cmd_name}_completions"
|
||||
cmd_completions_method_name = f"completions_{cmd_name}"
|
||||
cmd_completions_method = getattr(self, cmd_completions_method_name, None)
|
||||
if cmd_completions_method:
|
||||
return cmd_completions_method()
|
||||
else:
|
||||
return None
|
||||
return set(cmd_completions_method())
|
||||
|
||||
def run(self, inp):
|
||||
words = inp.strip().split()
|
||||
if not words:
|
||||
|
@ -144,7 +143,7 @@ class Commands:
|
|||
else:
|
||||
self.console.print(f"[red]{matched_file} is already in the chat")
|
||||
|
||||
def cmd_add_completions(self):
|
||||
def completions_add(self):
|
||||
return self.coder.get_active_files()
|
||||
|
||||
def cmd_drop(self, args):
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import os
|
||||
from pygments import highlight
|
||||
from pygments.lexers import guess_lexer_for_filename
|
||||
from pygments.token import Token
|
||||
from prompt_toolkit.styles import Style
|
||||
|
@ -24,17 +23,19 @@ class FileContentCompleter(Completer):
|
|||
content = f.read()
|
||||
lexer = guess_lexer_for_filename(fname, content)
|
||||
tokens = list(lexer.get_tokens(content))
|
||||
self.words.update(
|
||||
token[1] for token in tokens if token[0] in Token.Name
|
||||
)
|
||||
self.words.update(token[1] for token in tokens if token[0] in Token.Name)
|
||||
|
||||
def get_completions(self, document, complete_event):
|
||||
text = document.text_before_cursor
|
||||
words = text.split()
|
||||
if not words:
|
||||
return
|
||||
|
||||
if text[0] == "/" and len(words) == 1:
|
||||
candidates = self.commands.get_commands()
|
||||
if text[0] == "/":
|
||||
if len(words) == 1 and not text[-1].isspace():
|
||||
candidates = self.commands.get_commands()
|
||||
else:
|
||||
candidates = self.commands.get_command_completions(words[0][1:])
|
||||
else:
|
||||
candidates = self.words
|
||||
|
||||
|
@ -59,7 +60,6 @@ def canned_input(show_prompt):
|
|||
|
||||
|
||||
def get_input(history_file, fnames, commands):
|
||||
|
||||
fnames = list(fnames)
|
||||
if len(fnames) > 1:
|
||||
common_prefix = os.path.commonpath(fnames)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue