sublimetextsublime-text-plugin

Sublime Plugin - Show possible arguments for a selected function like intellisense


I'm trying to write a plugin and was wondering if there's a way to show the autocompletions based on what what specifically selected. In this language, functions don't have all the arguments needed so having it autocomplete every single argument is not a good idea.

For example, say the user selects a command called [&Command] from autocomplete, I'd want autocomplete to reappear after selecting it and show all the possible arguments the command can take. One possible command is [&Command func="Show"] but another could be [&Command demo="Create" type="Id"] (not actual commands, this is just an example)

In a nutshell, is there a way to get what the user selected from autocomplete and then use it to force autocomplete to appear and change what autocompletions are shown?


Solution

  • Incase anyone wants to know the solution I came up with so far (some minor issues still happen like requiring to press space twice to trigger the autocomplete):

    class ScriptListener(sublime_plugin.EventListener):
    
    
    commands = [
        '&Command1',
        '&Command2'
    ]
    
    Command1_completions = [
        'param1=',
        'param2='
    ]
    
    Command2_completions = [
        'param1=',
        'param2='
    ]
    
    last_word = ""
    current_row = ""
    
    def on_query_completions(self, view, prefix, locations):
        if not view.match_selector(locations[0], "source.script"):
            return None
    
        if self.last_word == self.commands[0] or self.commands[0] in self.current_row:
            return self._remove_known_arguments(self.Command1_completions)
        elif self.last_word == self.commands[1] or self.commands[1] in self.current_row:
            return self._remove_known_arguments(self.Command2_completions)
        else:
            return None
    
    def on_modified(self, view):
        for region in view.sel():
            if (' ' in view.substr(view.word(region))):
                self.current_row = view.substr(view.line(view.sel()[0]))
                if any(command in self.current_row for command in self.commands):
                    view.run_command("auto_complete")
            else:
                self.last_word = view.substr(view.word(region))
    
    def on_selection_modified(self, view):
        #Get current row
        self.current_row = view.substr(view.line(view.sel()[0]))
    
    def _remove_known_arguments(self, comp_list):
        for arg in comp_list:
            if (arg in self.current_row):
                comp_list.remove(arg)
        return comp_list