streetsidesoftware / vscode-spell-checker

A simple source code spell checker for code
https://streetsidesoftware.github.io/vscode-spell-checker/
Other
1.43k stars 127 forks source link

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

Open istvans opened 7 years ago

istvans commented 7 years ago

"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.

Jason3S commented 7 years ago

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 commented 7 years ago

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

bartosz-antosik commented 7 years ago

@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.

cancerberoSgx commented 7 years ago

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

Jason3S commented 7 years ago

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?

cancerberoSgx commented 7 years ago

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

krasi-georgiev commented 5 years ago

+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.

Zooce commented 5 years ago

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

jaladh-singhal commented 4 years ago

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

Gabriel-p commented 3 years ago

Was this ever implemented?

Jason3S commented 3 years ago

@Gabriel-p,

What do you want to achieve?

Gabriel-p commented 3 years ago

@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"
            ]
        }
    ],
Jason3S commented 3 years ago

Please try:

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

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

Gabriel-p commented 3 years ago

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

konstabark commented 8 months ago

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).