Skip to content

Commit

Permalink
Handle \bibitem when checking undefined references (#1244)
Browse files Browse the repository at this point in the history
Add a bibitem list to the latex Semantics struct, bibitem to CommandName and SyntaxKind enums, and handlers for processing bibitem nodes in a tex document.
Added checks against the bibitem list for undefined references and unused references.
  • Loading branch information
nolanking90 authored Oct 20, 2024
1 parent 410adf5 commit d9ac8a1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 15 deletions.
15 changes: 14 additions & 1 deletion crates/base-db/src/semantics/tex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Semantics {
pub can_be_root: bool,
pub can_be_compiled: bool,
pub diagnostic_suppressions: Vec<TextRange>,
pub bibitems: FxHashSet<Span>,
}

impl Semantics {
Expand All @@ -49,7 +50,7 @@ impl Semantics {
}
latex::SyntaxElement::Token(token) => match token.kind() {
latex::COMMAND_NAME => {
self.commands.push(Span::command(&token));
self.commands.push(Span::command(&token));
}
latex::COMMENT if token.text().contains("texlab: ignore") => {
self.diagnostic_suppressions.push(token.text_range());
Expand Down Expand Up @@ -85,6 +86,8 @@ impl Semantics {
self.process_theorem_definition(theorem_def);
} else if let Some(graphics_path) = latex::GraphicsPath::cast(node.clone()) {
self.process_graphics_path(graphics_path);
} else if let Some(bibitem) = latex::BibItem::cast(node.clone()) {
self.process_bibitem(bibitem);
}
}

Expand Down Expand Up @@ -334,6 +337,16 @@ impl Semantics {
self.graphics_paths.insert(path.to_string());
}
}

fn process_bibitem(&mut self, bibitem: latex::BibItem) {
if let Some(name) = bibitem.name() {
if let Some(key) = name.key() {
self.bibitems.insert(
Span::from(&key)
);
}
}
}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
Expand Down
46 changes: 32 additions & 14 deletions crates/diagnostics/src/citations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ pub fn detect_undefined_citations<'a>(
) -> Option<()> {
let data = document.data.as_tex()?;

let bibitems: FxHashSet<&str> = data.semantics.bibitems.iter()
.map(|bib| bib.text.as_str())
.collect();

let entries: FxHashSet<&str> = Entry::find_all(project)
.map(|(_, entry)| entry.name_text())
.collect();

for citation in &data.semantics.citations {
let name = citation.name_text();
if name != "*" && !entries.contains(name) {
if name != "*" && !entries.contains(name) && !bibitems.contains(name) {
let diagnostic = Diagnostic::Tex(citation.name.range, TexError::UndefinedCitation);
results
.entry(document.uri.clone())
Expand All @@ -41,24 +45,38 @@ pub fn detect_unused_entries<'a>(
document: &'a Document,
results: &mut FxHashMap<Url, Vec<Diagnostic>>,
) -> Option<()> {
let data = document.data.as_bib()?;

// If this is a huge bibliography, then don't bother checking for unused entries.
if data.semantics.entries.len() > MAX_UNUSED_ENTRIES {
return None;
}

let citations: FxHashSet<&str> = Citation::find_all(project)
.map(|(_, citation)| citation.name_text())
.collect();

for entry in &data.semantics.entries {
if !citations.contains(entry.name.text.as_str()) {
let diagnostic = Diagnostic::Bib(entry.name.range, BibError::UnusedEntry);
results
.entry(document.uri.clone())
.or_default()
.push(diagnostic);
if let Some(data) = document.data.as_bib()
{
// If this is a huge bibliography, then don't bother checking for unused entries.
if data.semantics.entries.len() > MAX_UNUSED_ENTRIES {
return None;
}

for entry in &data.semantics.entries {
if !citations.contains(entry.name.text.as_str()) {
let diagnostic = Diagnostic::Bib(entry.name.range, BibError::UnusedEntry);
results
.entry(document.uri.clone())
.or_default()
.push(diagnostic);
}
}
};

if let Some(tex_data) = document.data.as_tex(){
for bibitem in &tex_data.semantics.bibitems {
if !citations.contains(bibitem.text.as_str()) {
let diagnostic = Diagnostic::Bib(bibitem.range, BibError::UnusedEntry);
results
.entry(document.uri.clone())
.or_default()
.push(diagnostic);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions crates/parser/src/latex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl<'a> Parser<'a> {
CommandName::EndBlockComment => self.generic_command(),
CommandName::VerbatimBlock => self.verbatim_block(),
CommandName::GraphicsPath => self.graphics_path(),
CommandName::BibItem => self.bibitem(),
},
}
}
Expand Down Expand Up @@ -1235,6 +1236,18 @@ impl<'a> Parser<'a> {
}
}
}

fn bibitem(&mut self) {
self.builder.start_node(BIBITEM.into());
self.eat();
self.trivia();

if self.lexer.peek() == Some(Token::LCurly) {
self.curly_group_word();
}

self.builder.finish_node();
}
}

pub fn parse_latex(text: &str, config: &SyntaxConfig) -> GreenNode {
Expand Down
1 change: 1 addition & 0 deletions crates/parser/src/latex/lexer/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub fn classify(name: &str, config: &SyntaxConfig) -> CommandName {
"iffalse" => CommandName::BeginBlockComment,
"fi" => CommandName::EndBlockComment,
"verb" => CommandName::VerbatimBlock,
"bibitem" => CommandName::BibItem,

_ if config.citation_commands.contains(name) => CommandName::Citation,
_ if config.label_definition_commands.contains(name) => CommandName::LabelDefinition,
Expand Down
1 change: 1 addition & 0 deletions crates/parser/src/latex/lexer/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub enum CommandName {
BeginBlockComment,
EndBlockComment,
VerbatimBlock,
BibItem,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
Expand Down
12 changes: 12 additions & 0 deletions crates/syntax/src/latex/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,15 @@ impl GraphicsPath {
self.syntax().descendants().filter_map(CurlyGroupWord::cast)
}
}

cst_node!(BibItem, BIBITEM);

impl BibItem {
pub fn command(&self) -> Option<SyntaxToken> {
self.syntax().first_token()
}

pub fn name(&self) -> Option<CurlyGroupWord> {
self.syntax().children().find_map(CurlyGroupWord::cast)
}
}
1 change: 1 addition & 0 deletions crates/syntax/src/latex/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum SyntaxKind {
ENVIRONMENT_DEFINITION,
GRAPHICS_PATH,
BLOCK_COMMENT,
BIBITEM,
ROOT,
}

Expand Down

0 comments on commit d9ac8a1

Please sign in to comment.