From bc43d9ac31b451b3f2da71e2465635569a1abc52 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 May 2023 16:00:04 -0700 Subject: [PATCH] allow /commands to provide their own completions --- aider/commands.py | 9 ++++----- aider/getinput.py | 14 +++++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 91dfa0129..e430f3ba9 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -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): diff --git a/aider/getinput.py b/aider/getinput.py index d2794c23f..e0970fc00 100644 --- a/aider/getinput.py +++ b/aider/getinput.py @@ -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)