vscode-spell-checker icon indicating copy to clipboard operation
vscode-spell-checker copied to clipboard

Add general setting to restrict the scope to comments and strings only

Open istvans opened this issue 6 years ago • 15 comments

"The goal of this spell checker is to help with catching common spelling errors while keeping the number of false positives low." I think I'm not the only one who thinks a valid, compiling C++ code shouldn't have any underline in the editor, even if not all the method or variable names are from English. There should be an option in the VSCode user settings to restrict the scope to comments and string literals only. Just like how https://github.com/EWSoftware/VSSpellChecker does it perfectly for the big brother.

istvans avatar Jul 24 '17 18:07 istvans

It is possible to check only comments and strings:

Here is an example cspell.json file. It can be added into the .vscode directory within your project.

// cSpell Settings
{
    // Version of the setting file.  Always 0.1
    "version": "0.1",
    // language - current active spelling language
    "language": "en",
    // words - list of words to be always considered correct
    "words": [
        "deprioritize"
    ],
    // flagWords - list of words to be always considered incorrect
    "flagWords": [],
    "dictionaryDefinitions": [
        // re-define the cpp dictionary to prevent it from loading (this is up to you)
        {"name": "cpp", "file": ""}
    ],
    // Settings per language
    "languageSettings": [
        {
            // use with cpp or c files
            "languageId": "cpp,c",
            // turn off compound words, because it is only checking strings.
            "allowCompoundWords": false,
            // Only check comments and strings
            "includeRegExpList": [
                "CStyleComment",
                "string"
            ],
            // Exclude includes, because they are also strings.
            "ignoreRegExpList": [
                "/#include.*/"
            ]
        }
    ]
}

or you can add the following to your VS Code settings.json:

    "cSpell.dictionaryDefinitions": [
        // re-define the cpp dictionary to prevent it from loading (this is up to you)
        {"name": "cpp", "file": ""}
    ],
    "cSpell.languageSettings": [
        {
            // use with cpp or c files
            "languageId": "cpp,c",
            // turn off compound words, because it is only checking strings.
            "allowCompoundWords": false,
            // Only check comments and strings
            "includeRegExpList": [
                "CStyleComment",
                "string"
            ],
            // Exclude includes, because they are also strings.
            "ignoreRegExpList": [
                "/#include.*/"
            ]
        }
    ]

Jason3S avatar Jul 24 '17 19:07 Jason3S

I am working on adding some UI to make this a bit easier.

Jason3S avatar Jul 29 '17 21:07 Jason3S

@istvans: I hope @Jason3S wont mind if I mention here that there is another extension (developed by me) for VSCode which does exactly what you proposed: Spell Right. It does have a syntactic parsers for groups of similar documents (textual documents, programming languages, XML style documents etc.) which allows to spell only strings and comments of programming languages (e.g. C, C++, Python and few dozens of other). It can also be mitigated to spell any combination of body (in textual documents) and/or comments and/or strings (e.g. If someone prefers not to have comments in LaTeX or HTML spelled).

So, in principle, it follows your wish - a code is not spelled, only the comments/strings are.

bartosz-antosik avatar Sep 02 '17 10:09 bartosz-antosik

I think this behavior should be enabled by default - dont check variables - just comments and string literals.

cancerberoSgx avatar Oct 25 '17 11:10 cancerberoSgx

I can understand that there are situations where checking only strings and comments is desirable.

@cancerberoSgx which programming language are you using and what language (English, Spanish, Russian, French, etc) do you write your code in?

Jason3S avatar Oct 29 '17 14:10 Jason3S

I write code in English, but lots of times I write and read variables with abbreviations, numbers, internal names, etc

cancerberoSgx avatar Oct 29 '17 15:10 cancerberoSgx

+1 for making this enabled by default for all languages.

I am using golang and in English. Especially in golang there are a lot of abbreviations for var names so it makes the extention hard to use.

krasi-georgiev avatar Jun 21 '19 10:06 krasi-georgiev

Any progress on this? I've had to add so many words to the folder dictionaries that it's becoming kind of annoying.

Zooce avatar Jun 23 '19 18:06 Zooce

@Jason3S Is there any progress on this feature - it'll be really helpful?

jaladh-singhal avatar Jun 25 '20 11:06 jaladh-singhal

Was this ever implemented?

Gabriel-p avatar Sep 20 '21 17:09 Gabriel-p

@Gabriel-p,

What do you want to achieve?

Jason3S avatar Sep 21 '21 08:09 Jason3S

@Jason3S I wanted to restrict spelling to comments. I found a way using I believe a method you recommended in one of the issues here:

    "cSpell.languageSettings": [
        // This one works with python
        {
            "languageId": "python",
            "includeRegExpList": [
                "/#.*/",
                "/('''|\"\"\")[^\\1]+?\\1/g"
            ]
        }
    ],

Gabriel-p avatar Sep 21 '21 13:09 Gabriel-p

Please try:

    "cSpell.languageSettings": [
        // This one works with python
        {
            "languageId": "python",
            "includeRegExpList": [
                "strings",
                "comments"
            ]
        }
    ],

See: cspell-dicts/dictionaries/python/cspell-ext.json

Jason3S avatar Sep 21 '21 18:09 Jason3S

That block does not work. It still checks non-comment lines

Gabriel-p avatar Sep 21 '21 19:09 Gabriel-p

These are my settings: for python, C++ and C I have spell check only for comments and strings and for .json files only comments spell check (becaus e.g. for settings.json there are a lot of strings that raise plenty of messages in problems section.

        {
            "languageId": "python,cpp,c",
            "includeRegExpList": [
                // For Python
                "comments",
                "strings"
                // For C++ and C
                "CStyleComment",
                "string"
            ]
        },
        {
            "languageId": "jsonc", // (.json with comments) because most "commands" here are strings
            "includeRegExpList": [
                "CStyleComment",
            ]
        }
    ],

Notice how you can have just one block for all you languages (add more seperated by a comma) and you just add the language-specific way of telling it to check for comments ans strings (only the json case has different block because we don't want it to check for strings there).

konstabark avatar Mar 03 '24 00:03 konstabark