Sublime Forum

Add snippets programatically?

#1

Hi, I have an idea for a plugin and I’m trying to figure out if it’s possible to add new snippets programatically. I did find new_snippet as command in the sublime unofficial documentation but I have no clue on how to use this.

The idea for the plugin is to write snippets inside source files as comments. When you open a file, the plugin read the included source files and adds the relevant snippets to the user’s snippet list. This is why I want to programatically add a snippet from a string. How to do this?

0 Likes

#2

I think that what you really want to do here is write a custom completions plugin.

1 Like

#3

Thanks, this may be what I’m looking for.

0 Likes

#4

Thanks again @ThomSmith, I made good progress.

However, I’m a bit stumped right now with on_query_completions. It works, but it seems to be suppressing all other word completions. I didn’t pass any kind of flag. Anybody know what’s going on here? I’m still on build 3126 if that matters.

0 Likes

#5

Ok after some experimenting, it seems the completions start bugging out if I return a trigger string that contains a dot.

For example

class CompletionListener(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        return [["obj.me1", "obj.method1()"]]

The completion itself works but kills all normal word completions. Seems like a bug?

0 Likes

#6
1 Like

#7

Ok good, it’s a known bug. I’ll simply have to avoid non-accepted characters for now.

0 Likes