James-Yu / LaTeX-Workshop

Boost LaTeX typesetting efficiency with preview, compile, autocomplete, colorize, and more.
MIT License
10.66k stars 530 forks source link

Add source to warning messages #4173

Closed Tartasprint closed 7 months ago

Tartasprint commented 7 months ago

I noticed Latex-Workshop shows the package name when there is a PackageError, but not when there is a PackageWarning.

I would like to have the package name printed, so I can get a better understanding of the warning messages without having to look into the LaTeX compiler log.

I searched for 'Warning' in the codebase, and found about latexlog.ts. I guess the regex in there are for parsing the compiler log. I found a line that used the latexWarn regex, changed the message generated to include the package name, followed by a colon and a space to get a readable message.

I tested it by changing latexlog.js in my vscode extensions directory; this is a very small change and shouldn't introduce any bugs.

Would you consider merging this little change ?

James-Yu commented 7 months ago

Please show the warning message before and after the patch.

Tartasprint commented 7 months ago

For this source file:

\documentclass{article}
\begin{document}
La
\PackageWarning{package-1}{Something went wrong}
\PackageWarning{package-2}{Something went wrong}
\PackageError{package-1}{error text}{help text}
\PackageError{package-2}{error text}{help text}

\end{document}

I get the following messages (before vs after the patch):

[{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 8,
    "message": "Package package-1: error text.\n",
    "source": "LaTeX",
    "startLineNumber": 6,
    "startColumn": 1,
    "endLineNumber": 6,
    "endColumn": 65536
},{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 8,
    "message": "Package package-2: error text.\n",
    "source": "LaTeX",
    "startLineNumber": 7,
    "startColumn": 1,
    "endLineNumber": 7,
    "endColumn": 65536
},{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 4,
-   "message": "Something went wrong.\n",
+   "message": "Package package-1: Something went wrong.\n",
    "source": "LaTeX",
    "startLineNumber": 4,
    "startColumn": 1,
    "endLineNumber": 4,
    "endColumn": 65536
},{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 4,
-   "message": "Something went wrong.\n",
+   "message": "Package package-2: Something went wrong.\n",
    "source": "LaTeX",
    "startLineNumber": 5,
    "startColumn": 1,
    "endLineNumber": 5,
    "endColumn": 65536
}]

It makes also the printing of the warning messages more consistent with the printing of the error messages.

By sending this I just realised there is a trailing new line at the end of diagnostics, that causes the diagnostics to have a small arrow to show the "full" error message. This is meant for messages that span several lines, but is useless if the last line is empty. I will soon propose another change to fix this.

Tartasprint commented 7 months ago

Here are the new produced diagnostics:

[{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 8,
-   "message": "Package package-1: error text.\n",
+   "message": "Package package-1: error text.",
    "source": "LaTeX",
    "startLineNumber": 6,
    "startColumn": 1,
    "endLineNumber": 6,
    "endColumn": 65536
},{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 8,
-   "message": "Package package-2: error text.\n",
+   "message": "Package package-2: error text.",
    "source": "LaTeX",
    "startLineNumber": 7,
    "startColumn": 1,
    "endLineNumber": 7,
    "endColumn": 65536
},{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 4,
-   "message": "Something went wrong.\n",
+   "message": "Package package-1: Something went wrong.",
    "source": "LaTeX",
    "startLineNumber": 4,
    "startColumn": 1,
    "endLineNumber": 4,
    "endColumn": 65536
},{
    "resource": "##########.tex",
    "owner": "LaTeX",
    "severity": 4,
-   "message": "Something went wrong.\n",
+   "message": "Package package-2: Something went wrong.",
    "source": "LaTeX",
    "startLineNumber": 5,
    "startColumn": 1,
    "endLineNumber": 5,
    "endColumn": 65536
}]
Tartasprint commented 7 months ago

I messed up the branch by updating it from your repo, then trying to remove the commit GitHub put itself...

James-Yu commented 7 months ago

Thanks for your contribution!