简体   繁体   中英

sublime text 3 auto-complete plugin not working

I try to write a plugin to get all the classes in current folder to do an auto-complete injection.

the following code is in my python file:

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

but somehow when I work in a folder dir with a class named "LandingController.java" and I try to get the result, the auto complete is not working at all.

However, as you may noticed I did a error_message output of all the contents I got, there are actual a list of class name found.

Can anyone help me solve this? thank you!

It turn outs that the actual format which sublime text accept is: [(word,word),...]

but thanks to MattDMo who point out the documentation since the official documentation says nothing about the auto complete part.

For better understanding of the auto complete injection api, you could follow Zinggi's plugin DictionaryAutoComplete and this is the github link

So for a standard solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM