James-Yu / LaTeX-Workshop

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

Intellisense support for custom commands #4200

Closed 0xdw closed 4 months ago

0xdw commented 4 months ago

For example:

Assume I want to preserve consistent figure names across my paper, and it is a lengthy report. When citing my figures, I must always write 'Figure \ref{fig:temperature-distribution}', correct? So, what I do is develop a custom command where I just need to alter it in one place if any modification is required, and I would only use '\fig{temperature-distribution}' as a reference.

% The custom figure referencing command
\newcommand{\fig}[1]{\figurename{ \ref{fig:#1}}}
% Example figure
\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{Graphics/Temperature-Distribution.pdf}
    \caption{Temperature Distribution}
    \label{fig:temperature-distribution}
\end{figure}

But with normal referencing, Figure \ref{fig:temperature-distribution} it shows this when I start typing, but not in \fig{temperature-distribution}.

image

This is just an example command that I actually use. There are more custom commands, such as wrapping the Example figure into a single command and expecting it to show accessible graphic files. But it's not.

So my request is, is it possible to make that list appear in the custom commands as well?

James-Yu commented 4 months ago

It won't be supported, unfortunately. What you request need a full-fledge macro expansion engine, which is far beyond the scope of this extension.

jlelong commented 4 months ago

A lot of things can already be done without a full-fledge parser.

We use the following regex to decide if a reference completion should be triggered

/(?:\\hyperref\[([^\]]*)(?!\])$)|(?:(?:\\(?!hyper)[a-zA-Z]*ref[a-zA-Z]*\*?(?:\[[^[\]]*\])?){([^}]*)$)|(?:\\[Cc][a-z]*refrange\*?{[^{}]*}{([^}]*)$)/

So, if you rename your to \figref or \reffig for instance, you will have the completion.

0xdw commented 4 months ago

A lot of things can already be done without a full-fledge parser.

We use the following regex to decide if a reference completion should be triggered

/(?:\\hyperref\[([^\]]*)(?!\])$)|(?:(?:\\(?!hyper)[a-zA-Z]*ref[a-zA-Z]*\*?(?:\[[^[\]]*\])?){([^}]*)$)|(?:\\[Cc][a-z]*refrange\*?{[^{}]*}{([^}]*)$)/

So, if you rename your to \figref or \reffig for instance, you will have the completion.

Thank you so much for the tip. It works perfectly! ❤️

image