Wednesday, October 4, 2023

Re: syntax files: how to prevent embedded syntax from leaking out of a region?

On Wednesday, October 4, 2023 at 1:55:55 PM UTC-5 Christian Brabandt wrote:
> Either nobody knows the answer or nobody understood the problem.

Thank you Christian, I'm glad to know it got out there anyway.

So maybe it will help for me to restate the problem, maybe not.

This has to do with syntax highlighting when a file written in one language has embedded in it a code block written in a different language.
The Vim distribution includes two great practical examples of how to do this.
Both examples highlight the code inside the block using different syntax rules than the code outside the block.

One is in help document "syntax.txt" under the tag "sh-embed".
This shows how to extend the sh syntax so that embedded awk code is highlighted using awk syntax.

The other is syntax file "ant.vim", which has a powerful technique to support multiple languages embedded in Ant syntax and highlight each language using the correct syntax file.

The problem I'm having is that sometimes code past the end of the block gets highlighted using the syntax rules for the code inside the block.

Here is an excerpt from the syntax I developed.
The excerpt highlights most of the buffer using the preinstalled "tex.vim".
Then, within any embedded code block introduced by a <<blockname>>= code block introducer, and terminated by an @ or another <<blockname>>= code block introducer, the embedded code block is highlighted by the preinstalled "make.vim".

syntax include @SyntaxTeX syntax/tex.vim
unlet b:current_syntax
syntax region Normal start=/\v%^/ end=/\v%$/ contains=@SyntaxTeX
syntax match nowebCodeChunkDeclarationMake "\v^[<][<][mM]akefile(|\s.*)[>][>][=]\s*$"
\ skipnl
\ containedin=@SyntaxTeX
\ nextgroup=nowebCodeChunkBodyMake
highlight link nowebCodeChunkDeclarationMake PreProc
syntax include @SyntaxMake syntax/make.vim
unlet b:current_syntax
syntax region nowebCodeChunkBodyMake
\ keepend
\ start="\v.*"
\ end="\v^([@]($| ))|([<][<].*[>][>][=][ \t]*$)"me=s-1
\ contained
\ contains=@SyntaxMake

The key bit is the "keepend" keyword on the embedded code block.
It is there to force the block terminator to also terminate any contained item.
It should stop the embedded language from reading past the block terminator.
But the embedded language syntax can override this using "extend".
That can cause the embedded language syntax highlighting to continue past the end of the code block delimiter.
And the problem seems to be that some of the preinstalled syntaxes do use "extend".

The question is how to prevent the preinstalled syntaxes from ever highlighting past the end of the delimiter.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/7864bd47-6f44-42c5-8449-53e7740eae17n%40googlegroups.com.

No comments: