Sublime Forum

When to use snippets, completions?

#1

For most users, there’s probably little difference between snippets or completions, both let you insert blocks of code with tab-stops. There is, however, one difference that I find rather substantial.

Take the trigger pre-word for instance. When using completions, the trigger is working in fuzzy mode – I could type word and the pre-word would be among the suggestions in the completion popup. If it was a snippet, I would have to start with the letter p (or, more intuitively, use the entire pre prefix). I’m not aware of any other differences between snippets and completions, but to me this is rather substantial.

I have a package that comes with 6,000+ snippets. The reason I went with snippets is to avoid annoyed users, who would otherwise constantly get suggestions for completions as they type, and require them to take action to avoid this (e.g. pressing Escape.) This is especially true for documentation files, e.g. Markdown. On the other hand I’m worrying about maintainability of thousands of files (and, while possibly negligible, performance of reading and parsing thousands of files). While my mentioned package is quite mature at this point, I add new snippets every now and then, or change the behaviour of existing snippets with a search & replace in Sublime Text. The former would be easier when working with completions, since I could duplicate lines and make my changes. The latter is rather slow, since Sublime Text still performs poorly on thousands of files.

Anyway, to make a long story short, I was wondering Sublime Text offers a way to make use of the pros of completions (maintainability) and snippets (strict triggers).

1 Like

Minimum auto complete length please
Tinker tools
#2

I’ve started this feature comparison, did I miss anything?

Feature       | Snippets | Completions
--------------|----------|------------ 
Format        | Plist    | JSON
Description   | Yes      | Yes, hacky
Many per File | No       | Yes
Fuzzy         | No       | Yes

Any comments on the initial post?

0 Likes

#3

one potential option would be to create a keybinding that will override the default autocomplete (key)binding(s) when the caret is immediately after one of your strict triggers in the relevant scope/context. i.e.

  • create all “snippets” in a single JSON file (format specific to your plugin)
  • create a plugin that will read this file and expose a “query context” (see on_query_context in the EventListener class) that can be used to:
  • define a keybinding that will insert the “snippet” that relates to the trigger characters to the left of the caret

so technically it would neither be a completion nor a snippet, but it would kinda have the merged functionality you are looking for.

2 Likes