Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
istvans opened this issue Jul 24, 2017 · 15 comments
Open

Comments

@istvans
Copy link

istvans commented Jul 24, 2017

"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
Copy link
Collaborator

Jason3S commented Jul 24, 2017

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 Jason3S added the FAQ label Jul 24, 2017
@Jason3S
Copy link
Collaborator

Jason3S commented Jul 29, 2017

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

@bartosz-antosik
Copy link

@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
Copy link

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

@Jason3S
Copy link
Collaborator

Jason3S commented Oct 29, 2017

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
Copy link

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

@krasi-georgiev
Copy link

+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
Copy link

Zooce commented Jun 23, 2019

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
Copy link

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

@Gabriel-p
Copy link

Was this ever implemented?

@Jason3S
Copy link
Collaborator

Jason3S commented Sep 21, 2021

@Gabriel-p,

What do you want to achieve?

@Gabriel-p
Copy link

@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
Copy link
Collaborator

Jason3S commented Sep 21, 2021

Please try:

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

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

@Gabriel-p
Copy link

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

@konstabark
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants