0
votes

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!

1
Don't you want to return all_text instead of results? - MattDMo
is the on_query_completions accept string instead of list as injection results? - Tim Raynor
Yes, it will. See the docs for more info on how to structure completions. - MattDMo

1 Answers

0
votes

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