Bug 378487

Summary: [PATCH] Incorrect termination of \iffalse comment for latex highlight
Product: [Applications] kate Reporter: Andrea <andrea.turrini>
Component: syntaxAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version First Reported In: Git   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed/Implemented In: KDE Frameworks 5.34
Sentry Crash Report:

Description Andrea 2017-04-06 04:58:52 UTC
The latex.xml syntax highlight file contains the following snippets of code for identifying a multiline comment obtained by means of \iffalse ... \fi:

<StringDetect String="\iffalse" attribute="Comment" context="Multiline Comment"/>

<context name="Multiline Comment" attribute="Comment" lineEndContext="#stay">
	<StringDetect String="\fi" attribute="Comment" context="#pop"/>
	<StringDetect String="\else" attribute="Comment" context="#pop"/>
</context>

It induces the correct highlighting in several cases, except when the part of text between \iffalse and \fi contains a command starting with "\fi", like \fill.
In such a case, the comment highlighting stops at \fi of \fill and this may cause a completely wrong highlighting of the remaining part of the text, in particular when this happens inside a math mode.

A possible fix for this situation is to replace the code

<context name="Multiline Comment" attribute="Comment" lineEndContext="#stay">
	<StringDetect String="\fi" attribute="Comment" context="#pop"/>
	<StringDetect String="\else" attribute="Comment" context="#pop"/>
</context>

with 

<context name="Multiline Comment" attribute="Comment" lineEndContext="#stay">
	<RegExpr String="\\fi([^a-zA-Z]|$)" attribute="Comment" context="#pop"/>
	<RegExpr String="\\else([^a-zA-Z]|$)" attribute="Comment" context="#pop"/>
</context>

which prevents to match \fi and \else as termination of the multiline comment when they occur as prefix of another command (like \fi in \fill).

The fix has been checked with kile 2.1.3 and kate 3.14.9 in openSUSE 13.2 as well as with kate 16.08.2 in openSUSE Leap 42.2; the shipped syntax file is the same as the one in the git repository, so it should apply there as well.
Comment 1 Dominik Haumann 2017-04-06 05:59:46 UTC
Could you try WordDetect instead of a regexp? This is faster and should do exactly what you want, see: 
https://docs.kde.org/stable5/en/applications/katepart/highlight.html
Comment 2 Andrea 2017-04-06 06:28:00 UTC
WordDetect indeed produces the expected highlighting, with the code:

<context name="Multiline Comment" attribute="Comment" lineEndContext="#stay">
	<WordDetect String="\fi" attribute="Comment" context="#pop"/>
	<WordDetect String="\else" attribute="Comment" context="#pop"/>
</context>
Comment 3 Christoph Feck 2017-04-21 15:18:36 UTC
Dominik, do you need a review request, or can we commit the change without further review?
Comment 4 Dominik Haumann 2017-04-22 00:20:30 UTC
As is, but the unit test needs to be adapted. Will do asap, sorry...
Comment 5 Dominik Haumann 2017-04-22 14:02:38 UTC
Git commit 4a3572b899a0796118f610252e0951b36d278e07 by Dominik Haumann.
Committed on 22/04/2017 at 14:00.
Pushed by dhaumann into branch 'master'.

LaTeX: Fix Incorrect termination of \iffalse comments

FIXED-IN: KDE Frameworks 5.34

M  +5    -1    autotests/html/highlight.tex.html
M  +5    -1    autotests/input/highlight.tex
M  +5    -1    autotests/reference/highlight.tex.ref
M  +8    -8    data/syntax/latex.xml

https://commits.kde.org/syntax-highlighting/4a3572b899a0796118f610252e0951b36d278e07