Search code examples
autocompletesublimetext3sublime-text-plugin

Enable completions-popup in Sublime Text 3


Unlike most other packages, PHP suppresses the completions/snippets-popup, forcing you to type the whole trigger before hitting tab and making it impossible to choose between different snippets with the same trigger.

I'm looking for a way to enable the completions-popup in PHP (or for all languages, overriding their default).

What I've tried:

The PHP Package includes the file "Completion Rules.tmPreferences" with

<key>cancelCompletion</key>
<string>^\s*(\}?\s*(else|do|try)|(class|function)\s*[a-zA-Z_0-9]+)$</string>

Afaik, this is what suppresses the popup. I changed the regex to an unmatchable selector

<key>cancelCompletion</key>
<string>/(?!)/</string>

and saved it with PackageResourceViewer so that it should override the default settings file, however, even after restarting in case the file is only loaded once, the popup still doesn't appear.

Edit:

As Enteleform pointed out, the popup works inside <?php ?>, the problem occurs when in a PHP file, but outside the php. The scope is (embedding.php, text.html.basic).


Solution

  • Auto completion is not automatically shown as you type HTML by default, as you have experienced. This is controlled by the auto_complete_selector preference, which defaults to the following selector: "meta.tag - punctuation.definition.tag.begin, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc". As you can see, it is enabled for the source scope (except when inside comments and strings), which is why it works in PHP, and for the meta.tag scope.

    Now, the HTML syntax definition only assigns the meta.tag scope when there is at least one character following the <. i.e. typing <d will show the auto completion list automatically.

    To change it to always show HTML completions, you can change your preference to "text.html, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc" - i.e. change the meta.tag - punctuation.definition.tag.begin to text.html.

    Alternatively, to always show completions as you type for any language, you can change your preference to the empty string "". However, I would recommend keeping the functionality of not showing auto completions inside strings and comments, and using "- comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc" instead. It is also worth noting that typing word separator characters does not trigger autocompletion automatically - you have to type the first letter of something that can be autocompleted.

    This preference should be added to your Preferences -> Settings - User file.

    Note: as you are overriding a default preference, it is possible that the default value could change in future ST3 builds. It may therefore be worth checking the default (from Preferences -> Settings - Default) occasionally, in case having a different value will negatively affect your experience.