Vim syntax: Spell checking between certain regions

浪尽此生 提交于 2019-12-11 03:38:29

问题


I'm trying to create a syntax file for this language called Sugar Cube 2. You can find more about it here: http://www.motoslave.net/sugarcube/2/docs/macros.html

Generally, I don't want to spell check between the macros (e.g., <<if $myVariable>>). But, you can make your own macros, and I happen to have made one that works like this:

<<myDescription "This is a string that should be in english">>

As you can see, "english" should be capitalized, so it would be useful to have spell checking in that case.

I already know about vim's syntax. It's keyword, match, region, @NoSpell, etc. But I'm really struggling to put these concepts together to achieve what I want: Spell checking between a specific macro, but not all macros. Here's my idea, and it leverages the concepts described in syn-priority:

syn match macroDelimiter "\v(<<|>>)"
" there's much more keywords than this
syn keyword macroKeywords contained if elseif else myDescription
syn region mostMacros matchgroup=macroKeywords start="<<" end=">>" contains=@NoSpell
syn region myMacro matchgroup=macroKeywords start="<<myDescription" end=">>"

I mean... I tried that and it works. One thing I don't like is how myDescription is highlighted just like the angled brackets. I also don't like how the word myDescription itself is spell checked, but I can live with that one. Is there a way to fix these issues?


This will fix the above issues:

set spell spelllang=en_us
syn match macroDelimiter "\v(<<|>>)"
" there's much more keywords than this
syn keyword macroKeywords contained if elseif else myDescription
syn region macroString start=+"+ end=+"+ skip=+\\"+
syn region mostMacros matchgroup=macroDelimiter start="<<" end=">>" contains=@NoSpell,macroString
"Notice how this is commented out
"syn region myMacro matchgroup=macroDelimiter start="<<myDescription" end=">>"

hi link macroKeywords Keyword
hi link macroDelimiter Constant

But it adds a major problem: There are other macros with Strings. The <<link>> and <<goto>> macros have Strings, too. But I don't want to spell check the strings there.


回答1:


To get rid of the matchgroup highlighting for myDescription, but still enforce the match, end the start of the group via \ze:

syn region myMacro matchgroup=macroKeywords start="<<\zemyDescription" end=">>" contains=@Spell

This will enable spelling for myDescription itself. To avoid that, you have to turn the syntax for it from keyword to match, in order to add contains=@NoSpell to it:

syn match myDescription "myDescription" contains=@NoSpell
syn region myMacro matchgroup=macroKeywords start="<<\zemyDescription" end=">>" contains=@Spell,myDescription


来源:https://stackoverflow.com/questions/47296273/vim-syntax-spell-checking-between-certain-regions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!