Sublime Forum

Preventing unwanted auto-completion in comments

#1

I’m using SublimeText 3 for Typescript editing. I’ll create a comment and complete a sentence with a period and a newline. ST3 will present me a dropdown with helpful auto-completion. When writing text this is unwelcome, causing “mistakes” where my text starts look like:
This is my sample text.CreateMapPolyFill()My next sentence

The forum’s suggested solution for comments is incomprehensible to me. I don’t want to become a ST3 developer, just use it for my paid work.

Can someone tell me a “open this config file, insert this exact text right HERE” instruction that works?

Thanks,
Jerome.

0 Likes

Auto complete, how to clear it and 'reload'
#2

Ok mate. Sublime Text’s just awesome, so you’re not allowed to waste it :smile: So, find a course, and start learning:

english: https://www.youtube.com/watch?v=SVkR1ZkNusI&list=PLpcSpRrAaOaqQMDlCzE_Y6IUUzaSfYocK
french: https://www.youtube.com/playlist?list=PLjDTyz7qfUnNnVHzGlnzzoNedFu1rB1sS

It’s maybe gonna take 1 hour, I promise that in what, 2 days, you will have caught up on this “wasted” time, and you’ll keep saving time. :wink:

So, just a quick explaination:

Your settings are just JSON files. To keep it simple, you have 2 settings file: The default one, there’s all the default settings in it, commented. do not edit it. Each time you’ll update ST, it’ll be overwritten. As you probably guessed, there’s a User one. You put your custom settings in this one. The user one overwrites the default one.

To open this, go to Preferences -> Settings. On the left, it’s the default one, on the right, the user one.

So, there is the default completion, which uses the words in your current file and your snippets (maybe you don’t know what that is, but don’t worry, it’s explained in both of the courses :wink: )

And it’s the settings auto_complete_selector that controls in which scope the auto completion triggers. If you look in your default setting file, you’ll find it.

"auto_complete_selector": "meta.tag - punctuation.definition.tag.begin, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc",

from stackoverflow

it’s possible to AND, OR, and subtract scope selectors, e.g.: (a | b) & c - d would select the scope which is not matched by d, and matched by both c, and a or b.

As you can see, the default auto completion doesn’t trigger in string, because of this:

- string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc

neither in comment:

- comment

So, do you have installed any packages? Apparently yes, the Typescript one. And it’s this one that doesn’t respect the settings auto_complete_selector, and propose you some completion when you don’t want.

In your case, the plugin has build properly: you’ve can change this setting:

"auto_complete_triggers" : [ {"selector": "source.ts", "characters": "."} ],

So, this is a settings for the packages, which means you have to set it in the Packages Settings, not in your global one.

For this, go to:

Preferences -> Packages Settings -> Type Script -> TypeScript Settings – User

And set this a content:

{
    "auto_complete_triggers" : [ {"selector": "source.ts - string", "characters": "."} ]
}

tl;dr: I took the time to write it, why wouldn’t you read it? :slightly_smiling:

Hope it helps. If you have any question, ask! :wink:

matt

1 Like

#3

Hmm, I use TypeScript in ST3 (one of my main use cases) and I’ve never had this happen to me. I use this TypeScript plugin, is that what you’re using?

This is sort of a rehash of the post that you referenced in your comment, but by default the settings make sure that this sort of thing can’t happen.

Off the top of my head the only things that occur are:

  1. You (or a plugin you installed) modified the default setting of the auto_complete_selector option that indicates where autocomplete should trigger. Part of the default settings indicate that it should not trigger inside of a comment (or a string), so if that part has been removed, that might cause the problem.

  2. The file is not set to the TypeScript syntax; the setting above relies on the syntax highlighting to tell what construct the cursor is on, so if the syntax is not set, it doesn’t know you’re in a comment.

  3. As the thread mentions, there may be some snippet or completion involved (from a package outside of the TypeScript package, if you’re using the one I referenced above) that is not properly constraining itself.

For the first case, you can check your own preferences to see if you’ve modified that value yourself (Preferences .> Settings from the menu).

You’d notice if the syntax is not set correctly because the syntax highlighting would be wrong or missing, To double check, the status bar tells you (on the far right) what the current syntax for the current file is, which should say TypeScript.

For the other cases, there is some other plugin that is not playing nice, so it would be hard to say how to resolve the problem without knowing which one it is. One thing you can do is disable all of your packages (except the TypeScript one) and see if that solves the problem, then slowly enable them one at a time until you see the problem come back.

0 Likes

#4

Replies to both Terence and Matt:

Terence:

This is my auto_complete_selector text:

"auto_complete_selector": "meta.tag - punctuation.definition.tag.begin, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc",

My Preferences.sublime-settings is:

{ "color_scheme": "Packages/Color Scheme - Default/Blackboard.tmTheme", "font_size": 11, "ignored_packages": [ "Vintage" ] }

When I’m editing a *.ts file the lower-right says “Typescript”.

Under Preferences -> Package Settings I seem to have both “Typescript” and “Arctic Typescript”. I don’t know whether “Typescript” is the same package as you referenced in the link. I did go to my PC’s directory:
C:\Users\<me>\AppData\Roaming\Sublime Text 3\Packages\Typescript and got a file named package-metadata.json. Here are the contents:

{"version": "2.1.0", "url": "http://typescriptlang.org", "description": "IO wrapper around TypeScript language services, allowing for easy consumption by editor plugins", "sublime_text": "*", "dependencies": [], "platforms": ["*"]}
If this file is what you have on your PC then perhaps I ought to remove Arctic Typescript.

Comments?

Matt:

I see your remark about checking auto_complete_selector – mine is the same as yours – and configuring something. Once I find if I’ve got packages fighting each other I can try reconfiguring.

In your TL;DR comment, you ask if I didn’t read that link about “solved”. As I said, I did read it but, without context, it was and is still largely opaque to me.

For example, you say add “- string”. But there are many places where I could do that, and there are already numerous “string.*” entries. I’m not sure if the position of parameters matters (e.g., does A - B work the same as B - A?).

0 Likes

#5

That TypeScript package is the one that I’m using, yes. I don’t use (and until now didn’t know about) Arctic Typescript. I took a peek and it appears to provide similar services (build system, snippets, syntax highlighting, etc). The author mentions that TypeScript is an optional dependency, so I assume that means he thinks they complement each other or work together OK.

You can fix your problem by following the instructions that @math2001 mentioned in his post above, although I would modify what he said you should modify the setting to; he said you should add this:

{
    "auto_complete_triggers" : [ {"selector": "source.ts - string", "characters": "."} ]
}

However that would stop it from popping up on a period in a string; you would probably want to set it to the following:

{
    "auto_complete_triggers" : [ {"selector": "source.ts - string - comment", "characters": "."} ]
}

That will stop it from happening in a string and a comment (which is what you originally asked for). You could also remove the - string part if you wanted to have this happen in a string, but probably you don’t.

With that said, does this by any chance happen only on a newly created file that you haven’t saved to disk yet? Take a look at this:

Here I create a new buffer, set the syntax to TypeScript, and enter a comment. The period after the text “That” and “Other” offer me suggestions of other words that are currently in the file, and outside of a comment, the this keyword is not recognized as a language element.

When I save the file as a TypeScript file, suddenly entering the same text does not cause the autocomplete popup.

This is without the change mentioned above; i.e. I can replicate your results in a new TypeScript file without changing anything, but once I save the file to disk (or load an existing file from disk) it starts working as expected. So (in my case at least) the fix is not actually needed.

I haven’t delved too deeply, but at first blust it looks like this is because the TypeScript plugin does it’s own autocompletion handling (it can use context to determine what members to show in the popup, for example) and until the file is saved it doesn’t know what to put there and defaults to everything or some such.

0 Likes

#6

In short, no it does not; order is important to a large degree.

Sublime Text borrows a system called scopes (sometimes referred to as scope selectors or just selectors) from TextMate. Scopes are applied to a file based on the currently assigned syntax definition (such as Plain Text or TypeScript), so every character in a file is assigned one or more scopes that define what it is.

The best way to get a handle on what’s going on with scopes is to move the cursor to various locations in a file and select Tools > Developer > Show Scope Name (or use the associated key binding, which you can see in the menu).

The scope system supports both hierarchy and specificity, and the matching system is fairly loose in order to allow you to dial in on what you want without having to compose ridiculously long selectors. For example, here is a short TypeScript file (since we’re talking about that here).

// A simple demo
class something
{
    private _value : string = "Value";

    // I am a comment

    /* I am a comment too  */
    
    /** I am also a comment */
}

let example : string = "Example";

First, specificity: The overall scope applied to everything in the entire file is source.ts; source because it’s a source file (as opposed to text for a plain text or HTML file, for example) with the suffix of .ts to indicate that this is not only source, it’s typescript source.

This also applies to the comments; all of them have the scope of comment to indicate that they’re comments, but the first two are comment.line while the last two are comment.block. Even further than that, the last one is comment.block.documentation because it’s a JSDoc comment.

Second, hierarchy: The scopes have descendents like CSS selectors do. So the entire file is source.ts, but everything between the braces has the scope source.ts meta.class.ts because everything inside is a part of a class. This again applies to the comments as well; the first one has a scope of source.ts comment.line but the second one has the scope source.ts meta.class.ts comment.line.

In use scope selectors work like CSS descendant selectors for matching, which also includes grouping, subtraction, AND and OR, and that’s why the order is important. The more specific you make your query, the closer you can dial in exactly what you want to match. This includes not only including a hierarchy, but also including the specificity of one or more suffixes.

Here are some (admittedly contrived) examples based on that sample TypeScript file I posted above.

Descendant examples

  • source and source.ts match everything in the file
  • comment matches all comments in the file
  • comment.line matches only the line comments in the file
  • meta.class comment matches only the comments inside of the class

Grouping examples

  • comment, string matches all of the comments and also the string constants “Value” and “Example”.
  • meta.class comment, string matches only the comments inside the class and also the string constants “Value” and “Example”.

Subtraction examples

  • source - meta.class matches everything in the file that’s not inside the class
  • source.ts comment - comment.block.documentation matches all comments in the file except for the documentation comment at the end of the class body

Boolean Examples

  • (comment.block | string) matches all block comments and also the string constants “Value” and “Example”.
  • (comment.block | string) & meta.class matches all of the block comments and strings that are also inside of the class (so not the “Example” string constant)
  • (comment.block | string) & meta.class - comment.block.documentation matches all of the block comments and strings that are also inside of the class, but not the documentation comment.

These scopes are used all over the place in Sublime text. Some examples are:

  • Syntax highlighting uses scopes to determine what colors from your color scheme to use for the different parts of the file.

  • Build systems have optional scope selectors (usually just the top level) to allow Sublime to determine what build system it should use based on what you’re doing.

  • Key bindings can be told to only apply when the cursor is at a certain location in a file

  • Snippets can use a scope to inhibit where they can be triggered (for example, you could make a ctor snippet that expands to a TypeScript constructor that only applies while you’re inside a class definition).

  • Autocomplete and the Autocompletion system use a scope to determine when and if their completions apply.

3 Likes

Autocomplete is not disabled for strings
Questions about the "scope stack" and related APIs
Colouring typedefs in c++
Syntax Highlighting for Plain Text [noobie asking]
#7

I did the change in the Typescript - User section:
{
“auto_complete_triggers” : [ { “selector” : “source.ts - string - comment”, “characters” : “.” } ]
}

to no good effect. I think I shall reinstall my Typescript package.

The issue occurs with new and existing .ts files. It doesn’t occur with .java files, haven’t tested yet with .js files.

I’ll read the manual as @math2001 suggested.

0 Likes

#8

In the process of doing the reinstall I see that I’m also using a package TypescriptCompletion (github.com/RonanDrouglazet/TypescriptCompletion). I think that this is contributing to my issues. Do you two use this package?

0 Likes

#9

I do not use that one either, but the name alone sounds suspicious. The TypeScript package itself seems to have pretty good completion support on it’s own so I never went looking for anything else.

0 Likes