【问题标题】:How to implement auto closing of double quotes, parentheses, brackets with prompt_toolkit used in iPython 7.x?如何使用 iPython 7.x 中使用的 prompt_toolkit 实现双引号、括号、括号的自动关闭?
【发布时间】:2019-07-12 17:34:15
【问题描述】:

IPython 从第 5 版开始使用 prompt_toolkit 而不是 readline,我正在尝试使用它的这个包的实现来启用双引号、括号和方括号的自动关闭。我已经得到了这段代码:

ip = get_ipython()
kb = ip.pt_app.key_bindings
@kb.add('"')
def _(event):
    buffer = event.current_buffer
    buffer.insert_text('"')
    buffer.insert_text('"')

这不能正常工作,因为它只输入两个引号,光标位于它们后面。 buffer 对象似乎没有将光标移回的方法。不过,document 包含一个 document 对象,它包含在光标位置之前或之后插入文本的方法。于是修改代码:

ip = get_ipython()
kb = ip.pt_app.key_bindings
@kb.add('"') 
def _(event): 
    buffer = event.current_buffer 
    doc = buffer.document 
    doc.insert_before('"') 
    doc.insert_after('"')

当在键盘上按下 '"' 时,这不会产生任何输出。我从 prompt_toolkit 文档中收集到,该文档现在应该呈现到屏幕上,但我不知道如何完成此操作。所有帮助感激不尽!

【问题讨论】:

    标签: python ipython auto-close prompt-toolkit


    【解决方案1】:

    我找到了直接问题的解决方案 - 我的代码缺少参数:

    def _(event):
        buffer = event.current_buffer
        buffer.insert_text('"')
        buffer.insert_text('"', move_cursor=False)
    

    对于document 对象的呈现的更完整的答案仍将不胜感激,因为其他相关操作需要这样做,例如删除匹配的引号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-28
      • 2014-05-15
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多