allow /commands to provide their own completions

This commit is contained in:
Paul Gauthier 2023-05-10 16:00:04 -07:00
parent 623d36d831
commit bc43d9ac31
2 changed files with 11 additions and 12 deletions

View file

@ -36,12 +36,11 @@ class Commands:
self.console.print(f"Error: Command {cmd_name} not found.") self.console.print(f"Error: Command {cmd_name} not found.")
def get_command_completions(self, cmd_name): 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) cmd_completions_method = getattr(self, cmd_completions_method_name, None)
if cmd_completions_method: if cmd_completions_method:
return cmd_completions_method() return set(cmd_completions_method())
else:
return None
def run(self, inp): def run(self, inp):
words = inp.strip().split() words = inp.strip().split()
if not words: if not words:
@ -144,7 +143,7 @@ class Commands:
else: else:
self.console.print(f"[red]{matched_file} is already in the chat") 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() return self.coder.get_active_files()
def cmd_drop(self, args): def cmd_drop(self, args):

View file

@ -1,5 +1,4 @@
import os import os
from pygments import highlight
from pygments.lexers import guess_lexer_for_filename from pygments.lexers import guess_lexer_for_filename
from pygments.token import Token from pygments.token import Token
from prompt_toolkit.styles import Style from prompt_toolkit.styles import Style
@ -24,17 +23,19 @@ class FileContentCompleter(Completer):
content = f.read() content = f.read()
lexer = guess_lexer_for_filename(fname, content) lexer = guess_lexer_for_filename(fname, content)
tokens = list(lexer.get_tokens(content)) tokens = list(lexer.get_tokens(content))
self.words.update( self.words.update(token[1] for token in tokens if token[0] in Token.Name)
token[1] for token in tokens if token[0] in Token.Name
)
def get_completions(self, document, complete_event): def get_completions(self, document, complete_event):
text = document.text_before_cursor text = document.text_before_cursor
words = text.split() words = text.split()
if not words: if not words:
return return
if text[0] == "/" and len(words) == 1: if text[0] == "/":
candidates = self.commands.get_commands() 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: else:
candidates = self.words candidates = self.words
@ -59,7 +60,6 @@ def canned_input(show_prompt):
def get_input(history_file, fnames, commands): def get_input(history_file, fnames, commands):
fnames = list(fnames) fnames = list(fnames)
if len(fnames) > 1: if len(fnames) > 1:
common_prefix = os.path.commonpath(fnames) common_prefix = os.path.commonpath(fnames)