Skip to content

Commit

Permalink
Don't treat \label inside macros as label commands (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster authored May 21, 2024
1 parent bc9c4a9 commit fd49c49
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Remove unused `texlab.rootDirectory` setting

### Fixed

- Don't treat `\label{}` and `\ref{}` with macro parameter as label commands ([#1119](https://github.com/latex-lsp/texlab/issues/1119))

## [5.16.0] - 2024-05-01

### Added
Expand Down
63 changes: 40 additions & 23 deletions crates/base-db/src/semantics/tex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ impl Semantics {
return;
};

let name = Span::from(&name);
if name.text.contains('#') {
return;
}

let full_range = latex::small_range(&label);
let mut objects = Vec::new();
for node in label.syntax().ancestors() {
Expand Down Expand Up @@ -185,7 +190,7 @@ impl Semantics {

self.labels.push(Label {
kind: LabelKind::Definition,
name: Span::from(&name),
name,
targets: objects,
full_range,
});
Expand All @@ -198,44 +203,56 @@ impl Semantics {

let full_range = latex::small_range(&label);
for name in name_list.keys() {
self.labels.push(Label {
kind: LabelKind::Reference,
name: Span::from(&name),
targets: Vec::new(),
full_range,
});
let name = Span::from(&name);
if !name.text.contains('#') {
self.labels.push(Label {
kind: LabelKind::Reference,
name,
targets: Vec::new(),
full_range,
});
}
}
}

fn process_label_reference_range(&mut self, label: latex::LabelReferenceRange) {
let full_range = latex::small_range(&label);
if let Some(from) = label.from().and_then(|group| group.key()) {
self.labels.push(Label {
kind: LabelKind::ReferenceRange,
name: Span::from(&from),
targets: Vec::new(),
full_range,
});
let name = Span::from(&from);
if !name.text.contains('#') {
self.labels.push(Label {
kind: LabelKind::ReferenceRange,
name,
targets: Vec::new(),
full_range,
});
}
}

if let Some(to) = label.to().and_then(|group| group.key()) {
self.labels.push(Label {
kind: LabelKind::ReferenceRange,
name: Span::from(&to),
targets: Vec::new(),
full_range,
});
let name = Span::from(&to);
if !name.text.contains('#') {
self.labels.push(Label {
kind: LabelKind::ReferenceRange,
name,
targets: Vec::new(),
full_range,
});
}
}
}

fn process_citation(&mut self, citation: latex::Citation) {
let full_range = latex::small_range(&citation);
if let Some(list) = citation.key_list() {
for key in list.keys() {
self.citations.push(Citation {
name: Span::from(&key),
full_range,
});
let name = Span::from(&key);
if !name.text.contains('#') {
self.citations.push(Citation {
name: Span::from(&key),
full_range,
});
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/diagnostics/src/citations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn detect_undefined_citations<'a>(

for citation in &data.semantics.citations {
let name = citation.name_text();
if name != "*" && !entries.contains(name) && !name.contains('#') {
if name != "*" && !entries.contains(name) {
let diagnostic = Diagnostic::Tex(citation.name.range, TexError::UndefinedCitation);
results
.entry(document.uri.clone())
Expand Down

0 comments on commit fd49c49

Please sign in to comment.