如何重用提示工具箱中的PathCompleter中的补全 [英] How to reuse completions from PathCompleter in prompt_toolkit
问题描述
我正在为我的项目创建一个REPL工具(为简化起见,该工具直接执行输入的命令,或者(如果输入了命令".x some/path/to/file")从文件中读取并执行它们.我的问题与自动完成用户输入有关(使用hint_toolkit).
我有类似的东西(最小的可执行示例):
导入提示工具包从prompt_toolkit.completion导入完成器,完成从hint_toolkit.document导入文档从hint_toolkit.contrib.completers导入PathCompleter类CommandCompleter(Completer):def __init __():self.path_completer = PathCompleter()self.commands = [".x","command1","command2"]def get_completions(self,document,complete_event):如果document.text.startswith(.x"):sub_doc =文档(document.text [3:])从(Completion(cmd.text,-document.cursor_position)产生#??????????????????????????????????对于cmd在self.path_completer.get_completions(sub_doc,complete_event)中#???????别的:从(Completion(cmd,-document.cursor_position)产生用于self.commands中的cmd如果cmd.startswith(document.text))如果__name__ =="__main__":而True:other_args = {}输入= hint_toolkit.prompt(>>>",completer = CommandCompleter(),** other_args)#使用输入做某事(略)
第二个if分支(用于命令)可以正常工作,但是我不知道如何正确调用 PathCompleter.get_completions()
方法并重构 Completion
对象从它的结果(在哪里是???)在第一个分支中.诀窍在于,我仅对部分输入使用补全,而各种子字符串,位置计算等(至今)仍未导致令人满意的行为(即提供路径并构造正确的输入线)./p>
我肯定会继续搜索,但是如果有人知道如何重写它,那将非常有用.
注意:如果整个输入仅是路径(并且可以正常工作),则将使用 self.path_completer.get_completions(document,complete_event)的收益
.
以下内容可能会解决该问题:
sub_doc =文档(document.text [3:])从(Completion(completion.text,complete.start_position,display = completion.display)的收益完成在self.path_completer.get_completions(sub_doc,complete_event)中
-
completion.text
包含将要插入的文本; -
completion.start_position
包含相对于光标位置要插入文本的位置(在此特定示例中,我们可以从嵌套的完成程序中获取值). -
completion.display
是弹出菜单中显示的值.(在这种情况下,整个文件名,而不仅仅是插入的字符串.
如果还有其他问题,请随时打开GitHub问题.
I am creating a REPL tool for my project that (simplified for clarity) either directly executes entered commands or (if a command ".x some/path/to/file" is entered) reads and executes them from file. My question is related to auto-completing the user input (using prompt_toolkit).
I have something like (minimum executable example):
import prompt_toolkit
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.document import Document
from prompt_toolkit.contrib.completers import PathCompleter
class CommandCompleter(Completer):
def __init__(self):
self.path_completer = PathCompleter()
self.commands = [".x", "command1", "command2"]
def get_completions(self, document, complete_event):
if document.text.startswith(".x "):
sub_doc = Document(document.text[3:])
yield from (Completion(cmd.text, -document.cursor_position)
# ???????? ?????????????????????????
for cmd
in self.path_completer.get_completions(sub_doc, complete_event))
# ???????
else:
yield from (Completion(cmd, -document.cursor_position)
for cmd in self.commands
if cmd.startswith(document.text))
if __name__ == "__main__":
while True:
other_args = {}
input = prompt_toolkit.prompt(">>> ", completer=CommandCompleter(), **other_args)
# Do something with input (omitted)
The second if-branch (for commands) works correctly but I don't know how to properly call the PathCompleter.get_completions()
method and reconstruct the Completion
objects from its result (where the ???'s are) in the first branch. The trick is that I am using the completion only for a part of the input and various sub-stringing, position calculations etc. did not (yet) lead to the satisfactory behaviour (i.e. offering the paths and constructing the correct input line).
I will definitely go on searching but if anyone knows how to rewrite this, it would be very useful.
Note: yield from self.path_completer.get_completions(document, complete_event)
would be used if the whole input would be just the path (and this works correctly).
Probably the following should fix it:
sub_doc = Document(document.text[3:])
yield from (Completion(completion.text, completion.start_position, display=completion.display)
for completion
in self.path_completer.get_completions(sub_doc, complete_event))
completion.text
contains the text that is going to be inserted;completion.start_position
contains the place where the text is going to be inserted, relative to the cursor position (in this particular example we can take the value from the nested completer).completion.display
is the value displayed in the pop-up menu. (In this case, the whole filename, rather than only the inserted string.
Feel free to open a GitHub issue if you have any more questions.
这篇关于如何重用提示工具箱中的PathCompleter中的补全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!