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

Bugfix bibitem undefined #1244

Merged
merged 5 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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