mirror of
https://github.com/typst/typst
synced 2025-05-17 02:25:27 +08:00
search for 'allow' decorators
This commit is contained in:
parent
fdfd880f63
commit
16859f57e2
@ -81,15 +81,31 @@ impl<'a> Markup<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
node! {
|
node! {
|
||||||
/// A comment: `// something`.
|
/// A decorator: `/! allow("warning")`.
|
||||||
LineComment
|
Decorator
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> LineComment<'a> {
|
impl<'a> Decorator<'a> {
|
||||||
/// The comment's contents, excluding the initial '//' marker.
|
/// The name of the decorator, e.g. `allow`.
|
||||||
pub fn content(self) -> &'a str {
|
pub fn name(self) -> Ident<'a> {
|
||||||
let text = self.0.text();
|
self.0.cast_first_match().unwrap_or_default()
|
||||||
text.strip_prefix("//").unwrap_or(text)
|
}
|
||||||
|
|
||||||
|
/// The decorator's arguments.
|
||||||
|
pub fn arguments(self) -> impl DoubleEndedIterator<Item = Expr<'a>> {
|
||||||
|
let mut found_non_ident = false;
|
||||||
|
self.0
|
||||||
|
.children()
|
||||||
|
.filter(move |node| {
|
||||||
|
// Skip the name (first identifier).
|
||||||
|
if node.is::<Ident>() {
|
||||||
|
return found_non_ident;
|
||||||
|
} else if !node.kind().is_trivia() {
|
||||||
|
found_non_ident = true;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
})
|
||||||
|
.filter_map(Expr::from_untyped)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ use std::sync::Arc;
|
|||||||
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
|
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
|
||||||
|
|
||||||
use crate::ast::AstNode;
|
use crate::ast::AstNode;
|
||||||
use crate::{FileId, Span, SyntaxKind};
|
use crate::{is_newline, FileId, Span, SyntaxKind};
|
||||||
|
|
||||||
/// A node in the untyped syntax tree.
|
/// A node in the untyped syntax tree.
|
||||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||||
@ -148,6 +148,21 @@ impl SyntaxNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The amount of newlines in this node or its descendants,
|
||||||
|
/// capped at 256, that is, a return value of 256 means that the total
|
||||||
|
/// amount of newlines may be 256 or larger.
|
||||||
|
pub fn newlines(&self) -> u8 {
|
||||||
|
match &self.0 {
|
||||||
|
Repr::Leaf(_) | Repr::Error(_) => self
|
||||||
|
.text()
|
||||||
|
.chars()
|
||||||
|
.filter(|c| is_newline(*c))
|
||||||
|
.take(u8::MAX as usize)
|
||||||
|
.count() as u8,
|
||||||
|
Repr::Inner(inner) => inner.newlines,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The error messages for this node and its descendants.
|
/// The error messages for this node and its descendants.
|
||||||
pub fn errors(&self) -> Vec<SyntaxError> {
|
pub fn errors(&self) -> Vec<SyntaxError> {
|
||||||
if !self.erroneous() {
|
if !self.erroneous() {
|
||||||
@ -381,6 +396,11 @@ struct InnerNode {
|
|||||||
descendants: usize,
|
descendants: usize,
|
||||||
/// Whether this node or any of its children are erroneous.
|
/// Whether this node or any of its children are erroneous.
|
||||||
erroneous: bool,
|
erroneous: bool,
|
||||||
|
/// The (capped) amount of newlines in this node's descendants.
|
||||||
|
/// This is solely used to tell whether this node contains 0, 1, 2 or more
|
||||||
|
/// newlines. As such, this number is capped at 256, even though there may
|
||||||
|
/// be more newlines inside this node.
|
||||||
|
newlines: u8,
|
||||||
/// The upper bound of this node's numbering range.
|
/// The upper bound of this node's numbering range.
|
||||||
upper: u64,
|
upper: u64,
|
||||||
/// This node's children, losslessly make up this node.
|
/// This node's children, losslessly make up this node.
|
||||||
@ -407,6 +427,7 @@ impl InnerNode {
|
|||||||
kind,
|
kind,
|
||||||
len,
|
len,
|
||||||
span: Span::detached(),
|
span: Span::detached(),
|
||||||
|
newlines: 0,
|
||||||
descendants,
|
descendants,
|
||||||
erroneous,
|
erroneous,
|
||||||
upper: 0,
|
upper: 0,
|
||||||
@ -791,36 +812,40 @@ impl<'a> LinkedNode<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the first sibling comment node at the line above this node.
|
/// Get the first sibling decorator node at the line above this node.
|
||||||
/// This is done by moving backwards until the rightmost newline, and then
|
/// This is done by moving backwards until the rightmost newline, and then
|
||||||
/// checking for comments before the previous newline.
|
/// checking for decorators before the previous newline.
|
||||||
pub fn prev_attached_comment(&self) -> Option<Self> {
|
pub fn prev_attached_decorator(&self) -> Option<Self> {
|
||||||
if self.kind() == SyntaxKind::Parbreak
|
let mut cursor = self.prev_sibling_inner()?;
|
||||||
|| self.kind() == SyntaxKind::Space && self.text().contains('\n')
|
let mut newlines = cursor.newlines();
|
||||||
{
|
let mut at_previous_line = false;
|
||||||
// We hit a newline, so let's check for comments before the next
|
while newlines < 2 {
|
||||||
// newline.
|
if at_previous_line {
|
||||||
let mut sibling_before_newline = self.prev_sibling_inner()?;
|
if cursor.kind() == SyntaxKind::Decorator {
|
||||||
while sibling_before_newline.kind().is_trivia()
|
// Decorators are attached if they're in a consecutive
|
||||||
&& !matches!(
|
// previous line.
|
||||||
sibling_before_newline.kind(),
|
return Some(cursor);
|
||||||
SyntaxKind::Space | SyntaxKind::LineComment | SyntaxKind::Parbreak
|
}
|
||||||
)
|
|
||||||
{
|
|
||||||
sibling_before_newline = sibling_before_newline.prev_sibling_inner()?;
|
|
||||||
}
|
}
|
||||||
if sibling_before_newline.kind() == SyntaxKind::LineComment {
|
|
||||||
// Found a comment on the previous line
|
if newlines == 1 {
|
||||||
Some(sibling_before_newline)
|
if at_previous_line {
|
||||||
} else {
|
// No decorators at the previous line.
|
||||||
// No comments on the previous line
|
// Don't move onto the line before the previous, since then
|
||||||
None
|
// the decorator wouldn't be attached.
|
||||||
|
return None;
|
||||||
|
} else {
|
||||||
|
at_previous_line = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
self.prev_sibling_inner()
|
cursor = cursor.prev_sibling_inner()?;
|
||||||
.as_ref()
|
newlines = cursor.newlines();
|
||||||
.and_then(Self::prev_attached_comment)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Found a parbreak or something else with two or more newlines.
|
||||||
|
// Can't have an attached decorator there.
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the next non-trivia sibling node.
|
/// Get the next non-trivia sibling node.
|
||||||
|
@ -351,13 +351,13 @@ fn check_warning_suppressed(
|
|||||||
// Walk the parent nodes to check for a warning suppression in the
|
// Walk the parent nodes to check for a warning suppression in the
|
||||||
// previous line.
|
// previous line.
|
||||||
while let Some(node) = searched_node {
|
while let Some(node) = searched_node {
|
||||||
if let Some(sibling) = node.prev_attached_comment() {
|
let mut searched_decorator = node.prev_attached_decorator();
|
||||||
if let Some(comment) = sibling.cast::<ast::LineComment>() {
|
while let Some(sibling) = searched_decorator {
|
||||||
if matches!(parse_warning_suppression(comment.content()), Some(suppressed) if identifier.name() == suppressed)
|
let decorator = sibling.cast::<ast::Decorator>().unwrap();
|
||||||
{
|
if check_decorator_suppresses_warning(decorator, identifier) {
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
searched_decorator = sibling.prev_attached_decorator();
|
||||||
}
|
}
|
||||||
searched_node = node.parent();
|
searched_node = node.parent();
|
||||||
}
|
}
|
||||||
@ -365,20 +365,24 @@ fn check_warning_suppressed(
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: replace this ad-hoc solution
|
fn check_decorator_suppresses_warning(
|
||||||
// Expects a comment '//! allow("identifier")
|
decorator: ast::Decorator,
|
||||||
fn parse_warning_suppression(comment: &str) -> Option<&str> {
|
warning: &diag::Identifier,
|
||||||
const ALLOW_SEGMENT: &str = "! allow(\"";
|
) -> bool {
|
||||||
if !comment.starts_with(ALLOW_SEGMENT) {
|
if decorator.name().as_str() != "allow" {
|
||||||
return None;
|
return false;
|
||||||
}
|
|
||||||
let after_allow = comment.get(ALLOW_SEGMENT.len()..)?.trim();
|
|
||||||
let (suppressed_identifier, rest) = after_allow.split_once('"')?;
|
|
||||||
if rest.trim() != ")" {
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(suppressed_identifier)
|
for argument in decorator.arguments() {
|
||||||
|
let ast::Expr::Str(str) = argument else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if warning.name() == str.get() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The route the engine took during compilation. This is used to detect
|
/// The route the engine took during compilation. This is used to detect
|
||||||
|
Loading…
x
Reference in New Issue
Block a user