latex-lsp / texlab

An implementation of the Language Server Protocol for LaTeX
GNU General Public License v3.0
1.49k stars 51 forks source link

Add user-defined prefixes to custom label reference and definition commands #1138

Open DasNaCl opened 2 months ago

DasNaCl commented 2 months ago

Thanks for texlab, I've been enjoying it a lot!

At the moment, there exist experimental options for custom label commands. In my work, the diagnostics for these extended labels are basically useless, since I have macros that wrap the actual label and prepend a certain prefix to it. For example:

\newcommand{\asm}[2]{\item\label[asm]{asm:#1} {#2}}
\newcommand{\goal}[2]{\item\label[goal]{goal:#1} {#2}}
\newcommand{\asmref}[1]{\Cref{asm:#1}}
\newcommand{\goalref}[1]{\Cref{goal:#1}}

I propose to add another option to the server that allows a mapping from custom label commands to a prefix, which would be considered when checking for duplicate or unused references.

To this end, I've started to work on this by extending crates/parser/src/config.rs appropriately. But, it appears that the planned change is a little more fundamental than anticipated, because base_db::semantics::tex::Label or ...::LabelKind do not appear to carry information about the actual command. If I'm not mistaken, that information is dropped in the lexer, replacing the read token (e.g., \asm) with just CommandName::LabelDefinition (see here). Since the changes do not appear to be as easy as I thought, I'd like to discuss with you if (1) this is a reasonable (experimental) feature to add, (2) the plan on how to support this aligns with the general style and structure of the project, and (3) my analysis of the code base regarding the difficulty to implement this is on point or not.

DasNaCl commented 2 months ago

It'd be incredible if there is a somewhat less ad-hoc solution to custom prefixes, i.e., texlab identifying custom label commands automatically and adding them automagically, simply because one of the documents contains e.g. \newcommand{\asm}[1]{\label{asm:#1}}. Of course, something like this would be dramatically more complicated and the question is if such complication justifies this feature.