use rnix::{ast::Ident, NixLanguage, SyntaxKind};
use rowan::{api::SyntaxNode, ast::AstNode};
const PRAGMA_SKIP: &str = "deadnix: skip";
#[derive(Debug, Clone)]
pub struct Binding {
pub name: Ident,
pub decl_node: SyntaxNode<NixLanguage>,
mortal: bool,
}
impl Binding {
pub fn new(name: Ident, decl_node: SyntaxNode<NixLanguage>, mortal: bool) -> Self {
Binding {
name,
decl_node,
mortal,
}
}
pub fn is_mortal(&self) -> bool {
self.mortal
}
pub fn starts_with_underscore(&self) -> bool {
self.name.syntax().text().char_at(0.into()) == Some('_')
}
pub fn has_pragma_skip(&self) -> bool {
let mut line_breaks = 0;
let mut token = self.decl_node.first_token().unwrap();
while let Some(prev) = token.prev_token() {
token = prev;
match token.kind() {
SyntaxKind::TOKEN_WHITESPACE => {
line_breaks += token.text().matches('\n').count();
if line_breaks > 1 {
break;
}
}
SyntaxKind::TOKEN_COMMENT if token.text().contains(PRAGMA_SKIP) => return true,
_ => {}
}
}
false
}
}