luochen1990 / rainbow

Rainbow Parentheses Improved, shorter code, no level limit, smooth and fast, powerful configuration.
Apache License 2.0
1.78k stars 95 forks source link

Solution to the spell checking issue? #98

Closed luochen1990 closed 5 years ago

luochen1990 commented 6 years ago

As you know, the spell checking issue of this plugin have been there for years. Following is the change history about spell checking:

According to :h :syn-spell, if you add @Spell syntax rule, then for some filetypes which has no @Spell in the default syntax files, this will stop the default spell checking behavior.

SPELL CHECKING                      *:syn-spell*

:sy[ntax] spell [toplevel | notoplevel | default]
    This defines where spell checking is to be done for text that is not
    in a syntax item:

    toplevel:   Text is spell checked.
    notoplevel: Text is not spell checked.
    default:    When there is a @Spell cluster no spell checking.

    For text in syntax items use the @Spell and @NoSpell clusters
    |spell-syntax|.  When there is no @Spell and no @NoSpell cluster then
    spell checking is done for "default" and "toplevel".

    To activate spell checking the 'spell' option must be set.

And if you check the default syntax files, you can find out that only a few filetypes' specified syn spell explicitly.

$ cd /path/to/vim/vim80/syntax
$ grep -E "synt?a?x? spell" ./*
./context.vim:syn spell   toplevel
./gitcommit.vim:  syn spell toplevel
./groovy.vim:  syntax spell default  " added by Bram
./html.vim:syntax spell toplevel
./java.vim:  syntax spell default
./mallard.vim:syn spell toplevel
./papp.vim:  syntax spell default  " added by Bram
./rng.vim:syn spell toplevel
./scala.vim:" match scalaBlock, even with `syn spell notoplevel`.
./spyce.vim:syntax spell default  " added by Bram
./svn.vim:syn spell toplevel

And there is only 171 filetypes among 590 which you can add syntax rules with contains=@Spell safely (without change syn spell default's behavior from toplevel to notoplevel):

$ find ./* | wc -l
590

$ grep -l -E "synt?a?x? spell\>|@Spell" ./* | wc -l
171

And according to :h spell-syntax:

SYNTAX HIGHLIGHTING                 *spell-syntax*

Files that use syntax highlighting can specify where spell checking should be
done:

1.  everywhere             default
2.  in specific items          use "contains=@Spell"
3.  everywhere but specific items  use "contains=@NoSpell"

For the second method adding the @NoSpell cluster will disable spell checking
again.  This can be used, for example, to add @Spell to the comments of a
program, and add @NoSpell for items that shouldn't be checked.
Also see |:syn-spell| for text that is not in a syntax item.

I tidied the following table:

syn spell toplevel syn spell notoplevel
raw text Spell Check No Spell Check
default region ? ?
@Spell region Spell Check Spell Check
@NoSpell region No Spell Check No Spell Check

The spell checking rule for default region (non-toplevel text which is matched by some syntax rules) is not much clear, literally, it seem to be "Spell Check" on both situation.

An ideal design for rainbow (this plugin) is to make the parentheses region works the same as the toplevel text, so that it will keep the same behavior when people toggle rainbow on/off (this is still not true if the original syntax files defined some parentheses, in this situation, we need to keep the @Spell flag same with the original parentheses syntax rule). But it seems that there is no easy way to make it works for every case (every filetypes and 3-party-syntax- plugins).

If we want to solve the spell checking problem without some magic, we need to deal with each file types one by one, and all the 3-party-syntax-plugins have to be compatible with rainbow by themselves.

I'd appreciate it if you can provide any better idea, feel free to comment :)

alok commented 6 years ago

I think a stopgap solution that's at least easily changeable is to do one of 2 things:

  1. Add a new global option to implement #79. It could be named something like g:rainbow_experimental_spellcheck to emphasize that it doesn't always work.
  2. Like the first option, but instead of a global option it could be filetype/buffer specific and users can enable it themselves.

Neither of these fixes the problem totally, but for most common use cases (developing in the most popular languages), this should be ok since I imagine the more popular languages have better syntax files.

luochen1990 commented 6 years ago

@alok Actually you can config this via defining your own parentheses, we don't need to add an option to make it configurable.

shuwens commented 5 years ago

I just recently find out this issue and it is actually a bit annoying. For my case I only want to turn on spell checking for comment which is actually working by default in neovim. However, using rainbow will somehow turn on spell checking everywhere.

I also don't think people want spell checking for there code in general. Is this task a rather simple one so that we can wrap it up?

luochen1990 commented 5 years ago

@jethrosun the problem is, there are so many languages, and we need to offer a configurable option for this plugin's users to make them have a consistent experience, this is very hard and I cannot find a good solution yet.

luochen1990 commented 5 years ago

I pushed a new PR #119 and it supports a new option named parentheses_options, and you can add contains=@NoSpell or contains=Spell by yourself for specified filetypes now.

following is an example for .vim files:

let g:rainbow_conf = {
\   'separately': {
\       'vim': {
\           'parentheses_options': 'containedin=vimFuncBody contains=@NoSpell',
\           'parentheses': ['start=/(/ end=/)/', 'start=/\[/ end=/\]/', 'start=/{/ end=/}/ fold'],
\       },
\   }
\}

I will merge this develop branch into master if it is confirmed works well.

shuwens commented 5 years ago

Thank you for this new feature.

I assume this let us enable or disable spell checking by hand so the default code-aware spell checking feature is not supported any more, right?

luochen1990 commented 5 years ago

@jethrosun I think providing an option for users maybe the most practical solution at the moment. Maybe we can do better when we find a new solution.

luochen1990 commented 5 years ago

Now people can use 'parentheses_options': 'contains=@Spell' or 'parentheses_options': 'contains=@NoSpell' to solve this problem themself. This issue is closed for now.

boris-petrov commented 5 years ago

@luochen1990 - I'm not sure what I'm doing wrong, but when I add:

let g:rainbow_conf = {
\   'parentheses_options': 'contains=@NoSpell',
\}

This removes most syntax highlighting in parentheses in most file types. The same happens with Spell instead of NoSpell. What am I missing?

luochen1990 commented 5 years ago

My fault, parentheses_options is supposed to be used separately for each filetype (the global one may be covered by the default config of each filetype), and the default value of contains is TOP, so if you want to add an item into it, you should set it to TOP,@NoSpell.

let g:rainbow_conf = {
\   'separately': {
\       '*': {},
\       'markdown': {
\           'parentheses_options': 'contains=TOP,@NoSpell',
\       },
\   },
\}
boris-petrov commented 5 years ago

@luochen1990 - great, thanks! That fixed it.

luochen1990 commented 5 years ago

@boris-petrov I think update contains via append mode instead of write mode is more natural. I will fix it so you can config it the way you want.

boris-petrov commented 5 years ago

@luochen1990 - works like a charm, thanks!