use ktrs_ast::{Ast, NodeId};
use ktrs_syntax::SyntaxKind::{FILE_ANNOTATION_LIST, IMPORT_LIST, PACKAGE_DIRECTIVE};
use crate::ast_node_edit::AstNodeEdit;
use crate::ast_node_extension::AstNodeExtension;
use crate::editorconfig::{CODE_STYLE_PROPERTY, CodeStyleValue, EditorConfig};
use crate::rule::{About, Emit, RuleId, RuleV2, TokenSet, TraversalState};
use crate::rules::STANDARD_RULE_ABOUT;
const VISITED_TYPES: TokenSet = TokenSet::create(&[FILE_ANNOTATION_LIST, PACKAGE_DIRECTIVE, IMPORT_LIST]);
#[derive(Default)]
pub struct BlankLineBeforeFileAnnotation {
traversal_state: TraversalState,
}
impl RuleV2 for BlankLineBeforeFileAnnotation {
fn rule_id(&self) -> RuleId {
RuleId("standard:blank-line-before-file-annotation")
}
fn visited_types(&self) -> Option<TokenSet> {
Some(VISITED_TYPES)
}
fn about(&self) -> About {
STANDARD_RULE_ABOUT
}
fn is_experimental(&self) -> bool {
true
}
fn traversal_state(&self) -> Option<TraversalState> {
Some(self.traversal_state)
}
fn before_first_node(&mut self, editor_config: &EditorConfig) {
if editor_config.get(&CODE_STYLE_PROPERTY) == CodeStyleValue::IntellijIdea {
self.traversal_state.stop_traversal_of_ast();
}
}
fn before_visit_child_nodes(&mut self, ast: &mut Ast, node: NodeId, emit: &mut Emit<'_>) {
match ast.element_type(node) {
FILE_ANNOTATION_LIST => {
if let Some(insert_before_node) = Some(node).filter(|&it| !is_blank_line(ast, ast.prev_leaf(it))) {
emit(ast, ast.start_offset(insert_before_node), "Expected a blank line before the file annotation(s)", true).if_autocorrect_allowed(
|| {
let indent = "\n".to_owned() + &ast.indent(node);
ast.upsert_whitespace_before_me(insert_before_node, &indent);
},
);
}
self.traversal_state.stop_traversal_of_ast();
}
PACKAGE_DIRECTIVE | IMPORT_LIST => self.traversal_state.stop_traversal_of_ast(),
_ => {}
}
}
}
fn is_blank_line(ast: &Ast, node: Option<NodeId>) -> bool {
node.is_none_or(|it| ast.is_white_space(it) && ast.leaf_text(it).matches('\n').count() > 1)
}