wip: Added support for command completion in getinput.

This commit is contained in:
Paul Gauthier 2023-05-10 11:01:11 -07:00
parent a76781b519
commit d24cbf9ffa

View file

@ -22,24 +22,33 @@ class Commands:
commands = [] commands = []
for attr in dir(self): for attr in dir(self):
if attr.startswith("cmd_"): if attr.startswith("cmd_"):
commands.append(attr[4:]) commands.append('/' + attr[4:])
return commands return commands
class FileContentCompleter(Completer): class FileContentCompleter(Completer):
def __init__(self, fnames): def __init__(self, fnames, commands):
self.commands = commands
self.words = set() self.words = set()
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
words = text.split() words = text.split()
if not words: if not words:
return return
if text[0] == '/' and len(words) == 1:
candidates = self.commands.get_commands()
else:
candidates = self.words
last_word = words[-1] last_word = words[-1]
for word in self.words: for word in candidates:
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))
@ -79,7 +88,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) completer_instance = FileContentCompleter(fnames, Commands())
if multiline_input: if multiline_input:
show = ". " show = ". "