texstudio-org / texstudio

TeXstudio is a fully featured LaTeX editor. Our goal is to make writing LaTeX documents as easy and comfortable as possible.
http://www.texstudio.org/
GNU General Public License v3.0
2.74k stars 342 forks source link

Syntax highlighting with lstlisting causes all following text to be green #3762

Open jeffspieg opened 4 weeks ago

jeffspieg commented 4 weeks ago

Environment

Expected behavior

Syntax highlighting would work for lstlisting

Actual behavior

image

How to reproduce

It seems to just happen with lstlisting as shown in the picture. If i select and hit Ctrl-T twice it fixes the problem, but it reoccurs frequently.

muzimuzhi commented 4 weeks ago

Tried with a minimal example in TeXstudio 4.8.2alpha1, not reproducible. Maybe that's already fixed between 4.8.0 and 4.8.2alpha1, or my example is too minimal.

\documentclass{article}
\usepackage{listings}

\begin{document}
content

\begin{lstlisting}[]
code code
\end{lstlisting}

abc abc
\end{document}
octaeder commented 4 weeks ago

TeXstudio 4.8.0 (git 4.8.0) Using Qt Version 6.7.0, compiled with Qt 6.7.0 R

image

mbertucci47 commented 4 weeks ago

I can't reproduce at the moment, but I will add that this happens to me frequently as well with verbatim environments (#V classifier in cwl). Deleting some text like a character of the env name and re-typing it usually fixes it.

sunderme commented 3 weeks ago

I would need some minimal example with confirmed behavior.

jeffspieg commented 3 weeks ago

Here is a minimal example from Texstudio green lstlisting bug.tex. I have minimized it more.

\input{../Autotestcon/preamble} \RCRtrue

\input{../Autotestcon/documentclass}

\printtrue

\input{../Autotestcon/pkg}

\begin{document} \bibliographystyle{IEEEtran} % % paper title % can use linebreaks \ within to get better formatting as desired % make the title area \title{Texstudio green bug} \maketitle

\subsubsection{Introduction}

Dynamic counters are important to prevent end row or column counting.

\begin{lstlisting}[
    caption={Dynamic counter of SQL pulled data column}, label=Dynamic counter of SQL data column, 
    linewidth=\columnwidth, breaklines=true, 
    language=Excel]
    =UNIQUE(
    FILTER(
    INDIRECT("DR!" & CHAR(64 + MATCH(D3, DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH(D3, DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A, 'Par1'!$N$1))),
    (INDIRECT("DR!" & CHAR(64 + MATCH("D Component", DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH("D Component", DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A, 'Par1'!$N$1))) <>
    INDIRECT("DR!" & CHAR(64 + MATCH("Material #", DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH("Material #", DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A,'Par1'!$N$1))))
    )
    )
\end{lstlisting}

Looking at Listing~\ref{Dynamic counter of SQL data column} we see a complex combination of functions that aims to filter and extract unique values from a dynamically defined range in the \texttt{DR} sheet, based on multiple conditions. Here’s a detailed breakdown of the formula.

% that's all folks

\end{document} % \fi

sunderme commented 3 weeks ago

what about all the \input files ? Can you upload the files ? The example above is skewed by github markdown interpretation.

jeffspieg commented 3 weeks ago

If the problem is in the tex editor you shouldn't need to compile, Why do you need the input files? I tested the upload . Even without the \end{document}, the problems shows up after the first lstlisting. I made my own lstlisting Language Excel, but that is allowed in the syntax so i am not sure why Texstudio is confused. So I don't see a github markdown problem.

muzimuzhi commented 3 weeks ago

@jeffspieg It's always recommended to mark code snippets/examples as Markdown Fenced code blocks.

Unfortunately, still not reproducible with your snippet, using TeXstudio 4.8.2alpha4. Can you check with 4.8.1 and 4.8.2alpha4? Does the wrong highlighting appears right after you opened the tex file without taking any other actions? It seems the steps to reproduce what you reported is still uncovered.

Example using lstlistings, v2

```tex \documentclass{IEEEtran} \usepackage{listings} \input{../Autotestcon/preamble} \RCRtrue \input{../Autotestcon/documentclass} \printtrue \input{../Autotestcon/pkg} \begin{document} \bibliographystyle{IEEEtran} % % paper title % can use linebreaks \ within to get better formatting as desired % make the title area \title{Texstudio green bug} \maketitle \subsubsection{Introduction} Dynamic counters are important to prevent end row or column counting. \begin{lstlisting}[ caption={Dynamic counter of SQL pulled data column}, label=Dynamic counter of SQL data column, linewidth=\columnwidth, breaklines=true, language=Excel] =UNIQUE( FILTER( INDIRECT("DR!" & CHAR(64 + MATCH(D3, DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH(D3, DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A, 'Par1'!$N$1))), (INDIRECT("DR!" & CHAR(64 + MATCH("D Component", DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH("D Component", DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A, 'Par1'!$N$1))) <> INDIRECT("DR!" & CHAR(64 + MATCH("Material #", DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH("Material #", DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A,'Par1'!$N$1)))) ) ) \end{lstlisting} Looking at Listing~\ref{Dynamic counter of SQL data column} we see a complex combination of functions that aims to filter and extract unique values from a dynamically defined range in the \texttt{DR} sheet, based on multiple conditions. Here’s a detailed breakdown of the formula. % that's all folks \end{document} % \fi ```

image

sunderme commented 3 weeks ago

txs loads cwl files depending on the \usepackage commands. Since you do not provide a complete document, default packages are loaded which does not include listings (so here your example does not make much sense unless I start to guess what packages are loaded). Please provide and test a minimal example which really works as stand-alone example. Do not make assumptions. The example needs to be open on your side with no external inputs. txs scans all inputs and interprets usepackages and more, so that does have an influence on the outcome.

jeffspieg commented 3 weeks ago

Thanks Sunderme. I see your point as txs does scan all inputs! This code can be compiled too and shows the green continuation nuisance even without the lstdefinelanguage if you'd like. No compiler errors or warnings, yet it still shows Looking at Listing... in green.

%\input{../Autotestcon/preamble} %\RCRtrue

%\input{../Autotestcon/documentclass} \documentclass[journal]{IEEEtran}

%\printtrue

%\input{../Autotestcon/pkg}

\usepackage{listings}

\lstdefinelanguage{Excel} { morekeywords={ A\$1048576, B\$1048576, C\$1048576, D\$1048576, E\$1048576, F\$1048576, G\$1048576, H\$1048576, I\$1048576, J\$1048576, K\$1048576, L\$1048576, M\$1048576, N\$1048576, O\$1048576, P\$1048576, Q\$1048576, R\$1048576, S\$1048576, T\$1048576, U\$1048576, V\$1048576, W\$1048576, X\$1048576, Y\$1048576, Z\$1048576, AND, ARRAYTOTEXT, AVERAGE, BYCOL, BYROW, CHAR, CHOOSE, CHOOSECOLS, CHOOSEROWS, COUNTA, COUNTIF, DROP, EXPAND, FILTER, HSTACK, IF, IFERROR, INDEX, INDEX, INDIRECT, ISBLANK, ISNUMBER, ISOMITTED, LAMBDA, LET, LOOKUP, MAKEARRAY, MAP, MATCH, MIN OR, RANDARRAY, REDUCE, ROW, SCAN, SEQUENCE, SORT, SORTBY, STOCKHISTORY, SUM, TAKE, TEXT, TEXTJOIN TEXTAFTER, TEXTBEFORE, TEXTSPLIT, TOCOL, TOROW, UNIQUE, VALUETOTEXT, VLOOKUP, VSTACK, WRAPCOLS, WRAPROWS, XLOOKUP XMATCH }, sensitive=false, morecomment=[l]{//}, morestring=[b]", }

\begin{document}

\title{Texstudio green bug}
\maketitle

\subsubsection{Introduction}

Dynamic counters are important to prevent end row or column counting.

\begin{lstlisting}[
    caption={Dynamic counter of SQL pulled data column}, label=Dynamic counter of SQL data column, 
    linewidth=\columnwidth, breaklines=true, 
    language=Excel]
    =UNIQUE(
    FILTER(
    INDIRECT("DR!" & CHAR(64 + MATCH(D3, DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH(D3, DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A, 'Par1'!$N$1))),
    (INDIRECT("DR!" & CHAR(64 + MATCH("D Component", DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH("D Component", DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A, 'Par1'!$N$1))) <>
    INDIRECT("DR!" & CHAR(64 + MATCH("Material #", DR!$2:$2, 0)) & "$3:" & CHAR(64 + MATCH("Material #", DR!$2:$2, 0)) & ROW(INDEX(DR!$A:$A,'Par1'!$N$1))))
    )
    )
\end{lstlisting}

Looking at Listing~\ref{Dynamic counter of SQL data column} we see a complex combination of functions that aims to filter and extract unique values from a dynamically defined range in the \texttt{DR} sheet, based on multiple conditions. Here’s a detailed breakdown of the formula.

% that's all folks

\end{document} % \fi

sunderme commented 3 weeks ago

just by entering or reloading the code, I see no issue with the highlighting. I assume that begin{lstlisting... ènd{lstlisting}`is the region of interest.

grafik
jeffspieg commented 3 weeks ago

image

For me this code has the text the wrong color and even the \end{document} is green. I have had this problem for multiple versions of Texstudio and it corrects if I select the text Comment (Ctrl-T) and Uncomment (Ctrl-T), but reoccurs in multiple places and when closing and opening the file again. Very annoying when it occurs near the beginning of a long file and then everything is green thereafter and multiple lstlisting(s) reinitiate the green even if I correct the first instance.

sunderme commented 3 weeks ago

the task at hand is to reproduce the issue.

so

  1. you have only the test document open, no other open files
  2. no changed configuration
  3. when you start txs and the session with the single test file is restored, the syntax highlighting is as you show it ?
jeffspieg commented 3 weeks ago
  1. Yes. 2. Yes. 3. No.
    When I restarted on this file the problem went away. But going back to the file where I took this code the problem still exists for 1-3. I will have to make a better example.
jeffspieg commented 1 week ago

While working on a better example I found the following: When I comment out \input{../Autotestcon/pkg}, the Green runaway font color change in Texstudio goes away.
\input{../Autotestcon/preamble} \input{../Autotestcon/documentclass} %\input{../Autotestcon/pkg} %SUCCESS here in editor but can't compile without pkg.

When i uncomment it then it comes back.

\input{../Autotestcon/preamble} \input{../Autotestcon/documentclass} \input{../Autotestcon/pkg} %FAILURE here with Green runaway in editor but no errors when compiling.

If I put everything inside pkg into a comment and save the file, the Green runaway still occurs! Why is that ? It is the same as an empty file? Did the same thing with #iffalse ... #\fi around all contents and still has Green runaway.

I changed pkg to \input{../Autotestcon/pkg2} then back to pkg and it crashed. Note pkg2 did not exist, but Texstudio crashes going back to the original similar to this backtrace:

Here's the back trace: unknown at 7ff7fbbf30f0 (TXS-Version 4.8.0 R ) [bt] 00007ff7fbb3d4a5 q+3585657 [bt] 00007ff7fbb3c6b7 q+3582091 [bt] 00007ff7fba539e9 q+2628541 [bt] 000001d8190d89f0 ??? error: 126

Saved pkg to pkgGrreen and then while changing \input{../Autotestcon/pkg} to \input{../Autotestcon/pkgGreen} just typing in Texstudio the application crashes. FYI I have never seen 4.8.0 crash before so I think we are getting closer: Texstudio crash

Texstudio is getting very adverserial the closer I get to the cause. I don't want to give the full pkg file but wanted to divide and conquer the location that is problematic. Unfortunately I found even with no contents the file is a problem.

sunderme commented 1 week ago

you can send larger test cases directly to me for debug.(sunderme@gmx.de) They are handled confidentially.