【问题标题】:sublime text 3 auto-complete plugin not workingsublime text 3 自动完成插件不起作用
【发布时间】:2015-02-23 13:01:22
【问题描述】:

我尝试编写一个插件来让当前文件夹中的所有类进行自动完成注入。

以下代码在我的 python 文件中:

class FolderPathAutoComplete(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        folders = view.window().folders()
        results = get_path_classes(folders)
        all_text = ""
        for result in results:
            all_text += result + "\n"
        #sublime.error_message(all_text)
        return results


def get_path_classes(folders):
    classesList = []
    for folder in folders:
        for root, dirs, files in os.walk(folder):
            for filename in files:
                filepath = root +"/"+filename
                if filepath.endswith(".java"):
                    filepath = filepath.replace(".java","")
                    filepath = filepath[filepath.rfind("/"):]
                    filepath = filepath[1:]
                    classesList.append(filepath)
    return classesList

但不知何故,当我使用名为“LandingController.java”的类在文件夹目录中工作并尝试获取结果时,自动完成根本不起作用。

但是,正如您可能注意到的那样,我对获得的所有内容进行了 error_message 输出,实际上找到了一个类名列表。

谁能帮我解决这个问题?谢谢!

【问题讨论】:

  • 你不想返回all_text而不是results吗?
  • on_query_completions 接受字符串而不是list 作为注入结果吗?
  • 是的,它会的。有关如何构建完成的更多信息,请参阅the docs

标签: python sublimetext3 sublime-text-plugin


【解决方案1】:

原来sublime text接受的实际格式是:[(word,word),...]

但感谢 MattDMo 指出文档,因为官方文档没有提及自动完成部分。

为了更好地理解自动完成注入 api,您可以关注 Zinggi 的插件 DictionaryAutoComplete,这是github link

所以对于标准解决方案:

class FolderPathAutoComplete(sublime_plugin.EventListener):

    def on_query_completions(self, view, prefix, locations):
        suggestlist = self.get_autocomplete_list(prefix)
        return suggestlist


    def get_autocomplete_list(self, word):
        global classesList

        autocomplete_list = []
        uniqueautocomplete = set()
        # filter relevant items:
        for w in classesList:
            try:
                if word.lower() in w.lower():
                    actual_class = parse_class_path_only_name(w)
                    if actual_class not in uniqueautocomplete:
                        uniqueautocomplete.add(actual_class)
                        autocomplete_list.append((actual_class, actual_class))
            except UnicodeDecodeError:
                print(actual_class)
                # autocomplete_list.append((w, w))
                continue

        return autocomplete_list

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2013-08-31
    • 2017-10-13
    • 1970-01-01
    • 2019-08-02
    相关资源
    最近更新 更多