【问题标题】:Autocompletion list RESET when typing a dot on SublimeText在 Sublime Text 上键入点时自动完成列表 RESET
【发布时间】:2017-06-26 13:31:17
【问题描述】:

我实现了一个 SublimeText 自动补全插件:

import sublime_plugin
import sublime

tensorflow_functions = ["tf.test","tf.AggregationMethod","tf.Assert","tf.AttrValue", (etc....)]

class TensorflowAutocomplete(sublime_plugin.EventListener):

    def __init__(self):

        self.tf_completions = [("%s \tTensorflow" % s, s) for s in tensorflow_functions]

    def on_query_completions(self, view, prefix, locations):

        if view.match_selector(locations[0], 'source.python'):
            return self.tf_completions
        else:
            return[]

它很好用,但问题是当我输入“。”时它会重置完成建议。

例如,我输入“tf”,它会提示我所有的自定义列表,然后我输入“tf”。它建议我一个列表,因为我之前没有输入“tf”。我希望我的脚本考虑在点之前输入的内容。

很难解释。你知道我需要做什么吗?

编辑:

它的作用如下:

您可以在此处看到“tf”未突出显示。

【问题讨论】:

  • 您可能想尝试从 word_separators 设置中删除 . 并查看您的位置。如果有帮助,您可能希望在特定语法设置中执行此操作,以免在其他地方造成意外后果,

标签: sublimetext3 sublimetext sublime-text-plugin package-control sublimetext-snippet


【解决方案1】:

通常,Sublime Text 会替换所有内容,直到最后一个单词分隔符(即点)并插入您的补全文本。

如果你想插入一个带有单词分隔符的补全,你只需要去掉内容,它不会被替换。因此,您只需查看之前的行,提取最后一个点之前的文本,过滤并删除您的补全。这是我这样做的一般模式:

import re

import sublime_plugin
import sublime

tensorflow_functions = ["tf.AggregationMethod()","tf.Assert()","tf.AttrValue()","tf.AttrValue.ListValue()"]

RE_TRIGGER_BEFORE = re.compile(
    r"\w*(\.[\w\.]+)"
)


class TensorflowAutocomplete(sublime_plugin.EventListener):

    def __init__(self):

        self.tf_completions = [("%s \tTensorflow" % s, s) for s in tensorflow_functions]

    def on_query_completions(self, view, prefix, locations):

        loc = locations[0]
        if not view.match_selector(loc, 'source.python'):
            return

        completions = self.tf_completions
        # get the inverted line before the location
        line_before_reg = sublime.Region(view.line(loc).a, loc)
        line_before = view.substr(line_before_reg)[::-1]

        # check if it matches the trigger
        m = RE_TRIGGER_BEFORE.match(line_before)
        if m:
            # get the text before the .
            trigger = m.group(1)[::-1]
            # filter and strip the completions
            completions = [
                (c[0], c[1][len(trigger):]) for c in completions
                if c[1].startswith(trigger)
            ]

        return completions

【讨论】:

  • 感谢您的回答。不幸的是它不起作用!由于我真的不明白发生了什么,我无法调试它。知道错误可能在哪里吗?
  • 如果我创建一个新插件并将整个代码复制到那里,它会为 python 文件添加自动完成功能,并且还可以使用 tf. 前缀完成。究竟什么不起作用? ST控制台ctrl+` 有输出吗?
  • 控制台输出:当前触发器名称:'python-calltip-call-signature' 当前触发器名称:'python-complete-local-symbols' 当前触发器名称:'python-complete-object-members'。没有错误。我编辑了我的问题
  • 是的,它没有突出显示,但过滤并正确插入?您无法突出显示它,只能将其从可见文本中剥离。
  • 最后一个.之前的视图中的文本被提取并存储在trigger中(例如tf.tf.contrib.)。之后,从完成中删除此触发器 (c[1][len(trigger):]),并过滤完成列表以从该触发器开始 (if c[1].startswith(trigger))
猜你喜欢
  • 2014-10-05
  • 2019-08-02
  • 2014-07-28
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多