Vscode-spell-checker: Spellcheck only comments

Created on 17 Jul 2017  路  12Comments  路  Source: streetsidesoftware/vscode-spell-checker

Would be nice to have an option to spell check only comments, independent of the language.

This allows documentation writers to get spell checking without drowning in warnings from badly written code.

FAQ enhancement

Most helpful comment

There is a way to do it for each language. But it has to be done for each language.
What languages are you using? I'll see if I can give you an example.

The idea to only include text for matching via a regex.

Here is an example that can be added to your user or workspace settings.json file.

    "cSpell.languageSettings": [
        // This one works with python
        {
            "languageId": "python",
            "includeRegExpList": [
                "/#.*/",
                "/('''|\"\"\")[^\\1]+?\\1/g"
            ]
        },
        // this one works with javascript, C, typescript, etc, 
        // but you need to copy it and change the language id.
        {
            "languageId": "javascript",
            "includeRegExpList": [
               "CStyleComment"
            ]
        }
    ]

This is a version that works in a cSpell file:

    "languageSettings": [
        // This one works with python
        {
            "languageId": "python",
            "includeRegExpList": [
                "/#.*/",
                "/('''|\"\"\")[^\\1]+?\\1/g"
            ]
        },
        // this one works with javascript, C, typescript, etc, 
        // but you need to copy it and change the language id.
        {
            "languageId": "javascript",
            "includeRegExpList": [
               "CStyleComment"
            ]
        }
    ]

All 12 comments

There is a way to do it for each language. But it has to be done for each language.
What languages are you using? I'll see if I can give you an example.

The idea to only include text for matching via a regex.

Here is an example that can be added to your user or workspace settings.json file.

    "cSpell.languageSettings": [
        // This one works with python
        {
            "languageId": "python",
            "includeRegExpList": [
                "/#.*/",
                "/('''|\"\"\")[^\\1]+?\\1/g"
            ]
        },
        // this one works with javascript, C, typescript, etc, 
        // but you need to copy it and change the language id.
        {
            "languageId": "javascript",
            "includeRegExpList": [
               "CStyleComment"
            ]
        }
    ]

This is a version that works in a cSpell file:

    "languageSettings": [
        // This one works with python
        {
            "languageId": "python",
            "includeRegExpList": [
                "/#.*/",
                "/('''|\"\"\")[^\\1]+?\\1/g"
            ]
        },
        // this one works with javascript, C, typescript, etc, 
        // but you need to copy it and change the language id.
        {
            "languageId": "javascript",
            "includeRegExpList": [
               "CStyleComment"
            ]
        }
    ]

thanks Jason for the quick reply, I'm well aware that I can maintain such a file myself for the languages that I'm using, I just wonder whether it would be smarter to support such a feature out of the box for the convenience of documentation authors. If it's too complex, feel free to close this bug report.

I agree, it would be useful.

I don't have an easy way to detect comments in each language. At the moment, the only way is to add include expressions.

I have been playing with the idea of reading TextMate colorizer files and trying to glean the meaning from those. But I don't have a lot of time to work on this.

@Jason3S can you provide language-specific expression for golang? thanks
Also +1 for this feature.

It might be worth noting that combining the above with the strings expression suggested in #116 and in the documentation worked well. Also, it seems language ids can be listed as a comma-separated list rather than copying the whole block (not in the docs, but seen in #116 and seems to work when testing). All together, this worked well enough to keep me using Code Spell Checker 馃槃

"cSpell.languageSettings": [
    // This one works with Python
    {
        "languageId": "python",
        "includeRegExpList": [
            "/#.*/",
           "/('''|\"\"\")[^\\1]+?\\1/g",
            "strings"
        ]
    },
    // This one works with JavaScript, Typescript, etc
    {
        "languageId": "javascript,typescript",
        "includeRegExpList": [
            "CStyleComment",
            "strings"
        ]
    },
    // 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.*/"
        ]
    }
]

@jmcker great example.

@Jason3S @jmcker thanks for these snippet. I'm really bad at regex and have been trying to remove spell checking in inline code (between backticks) in comments. Any idea on how to do that ? thanks :)

Isn't it possible to use the syntax highlighting category determined by grammar for the current language (see https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide) in order to activate/deactivate spell checking rules in a more general way? This seems more convenient than repeating regular expressions for each language, although it's cool to have the regex rules for corner cases.

Isn't it possible to use the syntax highlighting category determined by grammar for the current language (see https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide) in order to activate/deactivate spell checking rules in a more general way? This seems more convenient than repeating regular expressions for each language, although it's cool to have the regex rules for corner cases.

It isn't possible for an extension to get access to the syntax-highlighting. I do have a long term plan on how to do this. It is more of a time constraint.

@Jason3S @jmcker thanks for these snippet. I'm really bad at regex and have been trying to remove spell checking in inline code (between backticks) in comments. Any idea on how to do that ? thanks :)

Please open a new issue with your exact challenge. Include some examples and refer to this issue. I will see if I can help you out.

@YannDubs See comment above.

golang

@raffaelespazzoli Here is my cSpell settings for golang (add to User settings. json):

"cSpell.languageSettings": [
        // GoLang
        // Set what strings to check (see https://github.com/streetsidesoftware/vscode-spell-checker/issues/107)
        {
            "languageId": "go",
            // Turn off compound words, because it is only checking strings.
            "allowCompoundWords": false,
            // Only check comments and strings
            "includeRegExpList": [
                "CStyleComment",
                "string"
            ],
            // Exclude imports, because they are also strings.
            "ignoreRegExpList": [
                // ignore mulltiline imports
                "import\\s*\\((.|[\r\n])*?\\)",
                // ignore single line imports
                "import\\s*.*\".*?\""
            ],
        }
    ]

Also you can fork it here https://gist.github.com/r3code/21d1e9a3f862ad865808f07225b59068

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alallier picture alallier  路  3Comments

ghost picture ghost  路  6Comments

lucamartinetti picture lucamartinetti  路  3Comments

xwang233 picture xwang233  路  4Comments

honghui-qiao picture honghui-qiao  路  3Comments