Sublime Forum

Tab completion for snippets broken

#1

Hello,

I’m having trouble getting snippets to activate because the auto-completion functionality takes preference above snippets.

I have the settings:

{
  "tab_completion": false,
  "auto_complete_preserve_order": "strict"
}

The documentation in the settings editor for tab_completion says:

When enabled, pressing tab will insert the best matching completion.
When disabled, tab will only trigger snippets or insert a tab.

The documented behavior is the behavior that I want. I want tab to insert snippets and not autocomplete.

However when I type in the tab trigger for a snippet (println) and I press the tab key <tab> then the snippet does not activate, but the auto-completion activates.

Question: How do I get snippets to complete with their tab trigger?

Searching through past topics, I see there is a bot called @OdatNurd that automatically asks if the scenario was tried in Safe Mode, so I tried that out too.

Here are the reproduction steps:

  1. Open Submlime text 4143 in safe mode
  2. Open up user preferences
  3. Set the preferences to the preferences listed above, specifically "tab_completion": false.
  4. Type in a tab trigger for a snippet
  5. Press tab

Expected behavior: Snippet triggers

Actual behavior: Auto-completion triggers

0 Likes

#2

All actors on this forum are humans. That said, SAFE MODE is the only way to make sure a described behavior is not caused by a plugin. That’s fundamentally important to know when giving support.

I assume the syntax is Rust?

Completions and snippets have always been displayed next to each other in the completions panel. If both are present they are treated equally. I see the same behavior in ST 3210.

ST would select println as soon as it was fully typed by default. This is what is meant by “best matching completion”. You’ve however disabled that functionality by "auto_complete_preserve_order": "strict". Changing it to "some" re-enables it.

tab_completion

tab_completion is of interest in case auto_complete_selector doesn’t match, only. That’s the case in text documents. It is of no technical meaning in source code such as Rust when auto completion is always triggered while typing.

In text documents - if no completion panel is displayed automatically while typing - tab_completion controls whether a snippet is expanded when hitting tab key even if its trigger was not yet fully typed. If false a fully typed trigger is required to expand the snippet via tab key.

Example:

  • tab_completion: true => type “pri” => hit tab => snippet expanded
  • tab_completion: false => type “pri” => hit tab => \t is inserted

Notes

What is indeed a bit inconsistent is tab key expanding snippets and committing normal completions if both are present. The setting "auto_complete_commit_on_tab": false therefore seems not to have a practical meaning in those situations. But that’s a rather minor thing for me as I have that setting set true anyway.

2 Likes

#3

Thank you for all of the information! :slight_smile:

This behavior is surprising to me. I think it would be worthwhile to update the documentation in the settings screen to inform users of this fact. This in combination with the auto_complete_commit_on_tab documentation makes me think that I can turn off autocomplete when pressing the [tab] key.

By default, auto complete will commit the current completion on enter.
This setting can be used to make it complete on tab instead.

Maybe I’m confused :disappointed: Without the auto_complete_preserve_order setting disabled, I find that completions are seemingly random.

When I type a piece of text like in your example

  • Type pri
  • Press [tab]

different things happen. Sometimes it will trigger Snippet A, sometimes it will trigger Snippet B, sometimes it will autocomplete some other text.

This is quite confusing for my muscle memory. When I type the same initial keys and press tab I get different results, and I’m not sure why. I attempted to make snippet triggering more deterministic by configuring "auto_complete_preserve_order": "strict"

Question: How do I get deterministic behavior when attempting to trigger a snippet?

That is, how do I get the same thing to happen every time I type pri and then press the [tab]key?

0 Likes

#4

The best option to make snippets work deterministic is to learn their exact tabTrigger as this is the most reliable way to make sure they are selected first.

As long as those are partly entered, ST treats them as normal completion candidates and tries to find the best match based on several heuristics. AFAIK one part of it is distance between current caret and locations of completion candidates appear in a document.

I don’t however know what “some”, “none” values mean in detail. Maybe a “snippets” value could be added to only re-order fully matching snippets - if “some” doesn’t already mean that. Would be a feature request then (https://github.com/sublimehq/sublime_text/issues), I guess.

I’ve seen various discussions about this topic with the same kind of question. Some wonder why snippets are prefered over normal completions in some situations, others are confused about perfect matching completions being removed from the list (… to ensure snippets or other longer candidates take precedence).

I guess the major issue is ST missing an active link to writer’s mind - hence can’t know what’s the really desired item in a certain situation. :slight_smile:

A deterministic behavior depends on expectations and is therefore hard to create. As completions and snippets can be provided by several sources - even plugins - which may return different results in different locations, things become even more complicated.

I agree about the tab behavior and its descriptions. It’s not very clear under which circumstances those settings have certain effects. I still struggle with it everytime I try to reproduce things to answer questions like yours - just got used to always use tab to commit them and hence don’t touch those settings in my every day work. I am a bit uncertain whether it is caused by dropping ST3’s “insert_best_completion” mechanism, which enabled you to cycle through completions/snippets via tab key without displaying a completion panel.

0 Likes