Allow turning off diagnostics for specific regions

Add `% texlab: warnings off` and `% texlab: warnings on` magic comments.
This commit is contained in:
hakan-demirli 2025-02-02 11:15:29 +03:00 committed by GitHub
parent eb694e66ff
commit 29bdb931e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 2 deletions

View file

@ -38,6 +38,7 @@ pub struct Semantics {
pub can_be_root: bool,
pub can_be_compiled: bool,
pub diagnostic_suppressions: Vec<TextRange>,
pub warning_suppression_ranges: Vec<(TextRange, TextRange)>,
pub bibitems: FxHashSet<Span>,
}
@ -55,6 +56,20 @@ impl Semantics {
latex::COMMENT if token.text().contains("texlab: ignore") => {
self.diagnostic_suppressions.push(token.text_range());
}
latex::COMMENT if token.text().contains("texlab: warnings off") => {
let start_range = token.text_range();
let mut current = token.clone();
while let Some(next) = current.next_token() {
current = next;
if current.kind() == latex::COMMENT
&& current.text().contains("texlab: warnings on")
{
self.warning_suppression_ranges
.push((start_range, current.text_range()));
break;
}
}
}
_ => {}
},
};

View file

@ -157,11 +157,33 @@ impl Manager {
};
let diag_line_col = document.line_index.line_col(diag_range.start());
let diag_offset = diag_range.start();
data.semantics
let is_single_line_suppressed = data
.semantics
.diagnostic_suppressions
.iter()
.map(|r| document.line_index.line_col(r.start()))
.any(|r| r.line == diag_line_col.line || r.line + 1 == diag_line_col.line)
.any(|r| r.line == diag_line_col.line || r.line + 1 == diag_line_col.line);
if is_single_line_suppressed {
return true;
}
let is_in_suppression_range =
data.semantics
.warning_suppression_ranges
.iter()
.any(|(start, end)| {
let start_line = document.line_index.line_col(start.start()).line;
let end_offset = end.end();
diag_line_col.line > start_line && diag_offset <= end_offset
});
if is_in_suppression_range {
return true;
}
false
}
}