jlelong / vscode-latex-basics

LaTeX grammars for Visual Studio Code
MIT License
19 stars 10 forks source link

Incorrect syntax highlight of \iffalse...\fi: brace tricks and more #97

Closed muzimuzhi closed 1 month ago

muzimuzhi commented 1 month ago

A clear and concise description of what the bug is.

754e79d (Highlight iffalse blocks as comments (#78), 2024-03-08) and 8624d0b (Only catch \iffalse at the beginning of a line, 2024-04-20) made the \iffalse ... \fi blocks highlighted as comments, when \iffalse is at the beginning of a line. But as the complexity of TeX, there are still false positive cases.

One kind of such cases are brace tricks like \iffalse{\fi} which are usually encountered in tabular-related implementation. See this Q&A on TeX-SX for more info.

I listed some rarer cases in https://github.com/texstudio-org/texstudio/issues/618#issuecomment-499325441.

Catching corner cases like these may not worth the effort. Maybe there can be a new setting to turn off \iffalse ... \fi highlight?

A minimal example to reproduce the issue

% array.sty v2.6d (2024/06/14), line 229-232
\protected\def\@arraycr {
  \tbl_count_missing_cells:n {@arraycr}
  \iffalse{\fi\ifnum 0=`}\fi
  \@ifstar \@xarraycr \@xarraycr}

image

jlelong commented 1 month ago

Thanks for your report. Unfortunately, VS Code syntax engine does not support variables to activate or deactivate some rules.

I have had a quick look at the corner cases you listed in https://github.com/texstudio-org/texstudio/issues/618#issuecomment-499325441, I do not think these can be robustly fixed. It should be possible to restrict \iffalse...\fi highlighting to LaTeX language and to drop it in TeX language. I am sure if this could really help.

jlelong commented 1 month ago

@muzimuzhi Could you try #98? This PR should make all the corner cases listed in https://github.com/texstudio-org/texstudio/issues/618#issuecomment-499325441 work well.