Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive on \newenvironment #879

Closed
paaguti opened this issue Apr 12, 2023 · 2 comments · Fixed by #1250
Closed

False positive on \newenvironment #879

paaguti opened this issue Apr 12, 2023 · 2 comments · Fixed by #1250
Labels
bug Something isn't working

Comments

@paaguti
Copy link

paaguti commented Apr 12, 2023

This LaTeX fragment flags an errir

\newcounter{milestone}
\newenvironment{milestone}[1][]{%
  \refstepcounter{milestone}\par\medskip
  \setlength\parindent{-18pt}
  \textbf{\sffamily \mstext~\themilestone:}\label{ms:ms\themilestone} #1
}{\medskip\setlength\parindent{0pt}%
}

with a missing }' on \themilestone and an unexpected {`on the line afterwards.

@pfoerster pfoerster added the bug Something isn't working label Apr 22, 2023
@nolanking90
Copy link
Contributor

@pfoerster when the parser hits a label it assumes there is either a key or a command and not both, so it calls the curly_group_word() method. I tested changing this behavior to check for a key, a command, or a key followed by a command.
If we use this method instead of calling curly_group_word() it no longer gives a false positive diagnostic (and it passes all the unit tests).

    fn label_definition(&mut self) {
        self.builder.start_node(LABEL_DEFINITION.into());
        self.eat();
        self.trivia();

        if self.lexer.peek() == Some(Token::LBrack) {
            self.brack_group();
        }

        if self.lexer.peek() == Some(Token::LCurly) {
            self.builder.start_node(CURLY_GROUP_WORD.into());
            self.eat();
            self.trivia();

            if self.peek() == Some(Token::Word) || self.peek() == Some(Token::Pipe) {
                self.key();
            }

            match self.peek() {
                Some(Token::CommandName(_)) => {
                    self.content(ParserContext::default());
                }
                Some(_) | None => {}
            }

            self.expect(Token::RCurly);
            self.builder.finish_node();
        }

        self.builder.finish_node();
    }

I'm basically just inlining the curly_group_word() method, and pulling the case of a key out of the match. This could have been two if statements instead of an if and a match, but I wasn't sure how to rewrite the CommandName case using an if. I'm open to suggestions here if you think there is a better way to do it.

This makes "ms:ms" a label key in this case and gives a warning about an unused label. At first, I thought this was "correct but annoying" and wondered if I could fix that too. After thinking some more about the use case I think getting an annoying warning here is a feature not a bug.

Labels and counters really shouldn't be used this way. If you change the number or order of these environments, the counters all change, causing the labels to change. So then you have to manually change all the references to fix it. This is exactly what labels and references are supposed to be automating for you. So, maybe using the method above, which eliminates the false syntax error but always produces a warning about the label, is the best solutions.

The more reasonable use case of passing in a label as an argument to the environment does not give this unused label warning for me.

\newcounter{milestone}
\newenvironment{milestone}[1]{%
  \refstepcounter{milestone}\par\medskip
  \setlength\parindent{-18pt}
  \textbf{\sffamily \mstext~\themilestone:}%
  \label{ms:ms#1}
}%
{\medskip\setlength\parindent{0pt}}

In either case, without some macro expansion, the actual instances of the labels aren't going to be reflected in the tree, so they will be missed by any diagnostics that involve checking the labels (e.g. a reference to one of these will throw a missing reference error/warning, I assume).

Let me know what you think. I can draft a PR if you think it's worth doing.

@pfoerster
Copy link
Member

Let me know what you think. I can draft a PR if you think it's worth doing.

@nolanking90 Thank you for looking into this. Looks good to me, would definitely appreciate a PR :)

This could have been two if statements instead of an if and a match, but I wasn't sure how to rewrite the CommandName case using an if.

Something like this should work:

if let Some(Token::CommandName(_)) = self.peek() {
    self.content(ParserContext::default());
}

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 31, 2025
## [5.21.0] - 2024-10-26

### Added

- Support starred variants in "Go to References" ([#1234](latex-lsp/texlab#1234))
- Add `texlab.latexindent.replacement` setting to allow passing a replacement flag to `latexindent` ([#1222](latex-lsp/texlab#1222))
- Don't require a label to show section numbers for document symbols ([#910](latex-lsp/texlab#910))
- Support navigating to files that are part of the `TEXINPUTS` similar to `BIBINPUTS` ([#1228](latex-lsp/texlab#1228))

### Fixed

- Fix opening `untitled` documents ([#1242](latex-lsp/texlab#1242))
- Handle `\bibitem` when checking for undefined references ([#1171](latex-lsp/texlab#1171))
- Fix false-positive syntax error when using a command inside a `\label` ([#879](latex-lsp/texlab#879))

## [5.20.0] - 2024-10-08

### Added

- Add `texlab.inlayHints.maxLength` setting to allow limiting inlay hint text length ([#1212](latex-lsp/texlab#1212))
- Allow suppressing diagnostics using `% texlab: ignore` magic comments ([#1211](latex-lsp/texlab#1211))

### Fixed

- Fix enabling `texlab.build.useFileList` setting
- Make "Goto Definition" work correctly with starred commands ([#1197](latex-lsp/texlab#1197))

## [5.19.0] - 2024-07-08

### Added

- Add `texlab.build.useFileList` setting to allow controlling whether to use the `.fls` files

### Changed

- Disable using `.fls` files for project detection by default

## [5.18.0] - 2024-07-06

### Added

- Parse `.fls` files to make the project detection more reliable ([#1145](latex-lsp/texlab#1145))

### Fixed

- Fix parsing commands with unicode characters inside BibTeX entries
  ([#1147](latex-lsp/texlab#1147))
- Improve detection of included files when non-ASCII characters are used ([#923](latex-lsp/texlab#923))
- Fix resolving includes starting from files included using `\subimport` ([#1145](latex-lsp/texlab#1145))

## [5.17.0] - 2024-06-23

### Added

- Add label commands from `zref` and `zref-clever` to the list of default label commands
  ([#1140](latex-lsp/texlab#1140))
- Add `texlab.experimental.labelDefinitionPrefixes` and
  `texlab.experimental.labelReferencePrefixes` options ([#1139](latex-lsp/texlab#1139))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants