From 4dccabcd47b3471b692a626f9fa4a6f00db66be6 Mon Sep 17 00:00:00 2001 From: Jonatan Waern Date: Mon, 12 Jan 2026 11:02:58 +0100 Subject: [PATCH] Add indentation handler Signed-off-by: Jonatan Waern --- src/analysis/parsing/expression.rs | 6 +++--- src/analysis/parsing/statement.rs | 14 ++++++------ src/analysis/parsing/structure.rs | 8 +++---- src/analysis/parsing/tree.rs | 2 +- src/analysis/parsing/types.rs | 12 +++++------ src/lint/mod.rs | 34 ++++++++++++++++++++++++++++-- 6 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/analysis/parsing/expression.rs b/src/analysis/parsing/expression.rs index 8d8f0b6a..df8c1586 100644 --- a/src/analysis/parsing/expression.rs +++ b/src/analysis/parsing/expression.rs @@ -184,7 +184,7 @@ impl TreeElement for ParenExpressionContent { fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.indent_paren_expr.check(IndentParenExprArgs::from_paren_expression(self), acc); rules.break_func_call_open_paren.check( - BreakFuncCallOpenParenArgs::from_paren_expression(self, aux.depth), acc); + BreakFuncCallOpenParenArgs::from_paren_expression(self, aux.depth.depth()), acc); } } @@ -236,7 +236,7 @@ impl TreeElement for FunctionCallContent { rules.sp_punct.check(SpPunctArgs::from_function_call(self), acc); rules.indent_paren_expr.check(IndentParenExprArgs::from_function_call(self), acc); rules.break_func_call_open_paren - .check(BreakFuncCallOpenParenArgs::from_function_call(self, aux.depth), acc); + .check(BreakFuncCallOpenParenArgs::from_function_call(self, aux.depth.depth()), acc); } } @@ -353,7 +353,7 @@ impl TreeElement for CastContent { fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.indent_paren_expr.check(IndentParenExprArgs::from_cast(self), acc); rules.break_func_call_open_paren - .check(BreakFuncCallOpenParenArgs::from_cast(self, aux.depth), acc); + .check(BreakFuncCallOpenParenArgs::from_cast(self, aux.depth.depth()), acc); } } diff --git a/src/analysis/parsing/statement.rs b/src/analysis/parsing/statement.rs index 5d3da738..202f51e7 100644 --- a/src/analysis/parsing/statement.rs +++ b/src/analysis/parsing/statement.rs @@ -151,8 +151,8 @@ impl TreeElement for CompoundContent { } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.sp_brace.check(SpBracesArgs::from_compound(self), acc); - rules.indent_code_block.check(IndentCodeBlockArgs::from_compound_content(self, aux.depth), acc); - rules.indent_closing_brace.check(IndentClosingBraceArgs::from_compound_content(self, aux.depth), acc); + rules.indent_code_block.check(IndentCodeBlockArgs::from_compound_content(self, aux.depth.depth()), acc); + rules.indent_closing_brace.check(IndentClosingBraceArgs::from_compound_content(self, aux.depth.depth()), acc); } fn should_increment_depth(&self) -> bool { true @@ -555,7 +555,7 @@ impl TreeElement for WhileContent { } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.indent_paren_expr.check(IndentParenExprArgs::from_while(self), acc); - rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_while_content(self, aux.depth), acc); + rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_while_content(self, aux.depth.depth()), acc); rules.sp_reserved.check(SpReservedArgs::from_while(self), acc); } } @@ -872,7 +872,7 @@ impl TreeElement for ForContent { } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.indent_paren_expr.check(IndentParenExprArgs::from_for(self), acc); - rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_for_content(self, aux.depth), acc); + rules.indent_empty_loop.check(IndentEmptyLoopArgs::from_for_content(self, aux.depth.depth()), acc); rules.sp_reserved.check(SpReservedArgs::from_for(self), acc); } } @@ -1032,7 +1032,7 @@ impl TreeElement for SwitchCase { } } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { - rules.indent_switch_case.check(IndentSwitchCaseArgs::from_switch_case(self, aux.depth), acc); + rules.indent_switch_case.check(IndentSwitchCaseArgs::from_switch_case(self, aux.depth.depth()), acc); } fn should_increment_depth(&self) -> bool { matches!(self, SwitchCase::Statement(statement) @@ -1122,7 +1122,7 @@ impl TreeElement for SwitchContent { fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { - rules.indent_closing_brace.check(IndentClosingBraceArgs::from_switch_content(self, aux.depth), acc); + rules.indent_closing_brace.check(IndentClosingBraceArgs::from_switch_content(self, aux.depth.depth()), acc); rules.indent_paren_expr.check(IndentParenExprArgs::from_switch(self), acc); } } @@ -1852,7 +1852,7 @@ impl TreeElement for StatementContent { } } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { - rules.indent_continuation_line.check(acc, IndentContinuationLineArgs::from_statement_content(self, aux.depth)); + rules.indent_continuation_line.check(acc, IndentContinuationLineArgs::from_statement_content(self, aux.depth.depth())); } } diff --git a/src/analysis/parsing/structure.rs b/src/analysis/parsing/structure.rs index ee9e647b..6db0b17e 100644 --- a/src/analysis/parsing/structure.rs +++ b/src/analysis/parsing/structure.rs @@ -244,7 +244,7 @@ impl TreeElement for MethodContent { rules.nsp_inparen.check(NspInparenArgs::from_method(self), acc); rules.sp_punct.check(SpPunctArgs::from_method(self), acc); rules.indent_paren_expr.check(IndentParenExprArgs::from_method(self), acc); - rules.break_func_call_open_paren.check(BreakFuncCallOpenParenArgs::from_method(self, aux.depth), acc); + rules.break_func_call_open_paren.check(BreakFuncCallOpenParenArgs::from_method(self, aux.depth.depth()), acc); rules.break_method_output.check(BreakMethodOutputArgs::from_method(self), acc); } } @@ -730,8 +730,8 @@ impl TreeElement for ObjectStatementsContent { } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.sp_brace.check(SpBracesArgs::from_obj_stmts(self), acc); - rules.indent_code_block.check(IndentCodeBlockArgs::from_obj_stmts_content(self, aux.depth), acc); - rules.indent_closing_brace.check(IndentClosingBraceArgs::from_obj_stmts_content(self, aux.depth), acc); + rules.indent_code_block.check(IndentCodeBlockArgs::from_obj_stmts_content(self, aux.depth.depth()), acc); + rules.indent_closing_brace.check(IndentClosingBraceArgs::from_obj_stmts_content(self, aux.depth.depth()), acc); } fn should_increment_depth(&self) -> bool { matches!(self, ObjectStatementsContent::List(lbrace, list, rbrace) @@ -1938,7 +1938,7 @@ impl TreeElement for DMLObjectContent { } } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { - rules.indent_continuation_line.check(acc, IndentContinuationLineArgs::from_dml_object_content(self, aux.depth)); + rules.indent_continuation_line.check(acc, IndentContinuationLineArgs::from_dml_object_content(self, aux.depth.depth())); } } diff --git a/src/analysis/parsing/tree.rs b/src/analysis/parsing/tree.rs index 102f18ee..df3fe4a1 100644 --- a/src/analysis/parsing/tree.rs +++ b/src/analysis/parsing/tree.rs @@ -108,7 +108,7 @@ pub trait TreeElement { fn style_check(&self, acc: &mut Vec, rules: &CurrentRules, mut aux: AuxParams) { if self.should_increment_depth() { - aux.depth += 1; + aux.depth.increase_depth(); } self.evaluate_rules(acc, rules, aux); for sub in self.subs() { diff --git a/src/analysis/parsing/types.rs b/src/analysis/parsing/types.rs index 7335632d..60ca3d3d 100644 --- a/src/analysis/parsing/types.rs +++ b/src/analysis/parsing/types.rs @@ -55,8 +55,8 @@ impl TreeElement for StructTypeContent { errors } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { - rules.indent_code_block.check(IndentCodeBlockArgs::from_struct_type_content(self, aux.depth), acc); - rules.indent_closing_brace.check(IndentClosingBraceArgs::from_struct_type_content(self, aux.depth), acc); + rules.indent_code_block.check(IndentCodeBlockArgs::from_struct_type_content(self, aux.depth.depth()), acc); + rules.indent_closing_brace.check(IndentClosingBraceArgs::from_struct_type_content(self, aux.depth.depth()), acc); rules.sp_brace.check(SpBracesArgs::from_struct_type_content(self), acc); } fn should_increment_depth(&self) -> bool { @@ -138,8 +138,8 @@ impl TreeElement for LayoutContent { errors } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { - rules.indent_code_block.check(IndentCodeBlockArgs::from_layout_content(self, aux.depth), acc); - rules.indent_closing_brace.check(IndentClosingBraceArgs::from_layout_content(self, aux.depth), acc); + rules.indent_code_block.check(IndentCodeBlockArgs::from_layout_content(self, aux.depth.depth()), acc); + rules.indent_closing_brace.check(IndentClosingBraceArgs::from_layout_content(self, aux.depth.depth()), acc); rules.sp_brace.check(SpBracesArgs::from_layout_content(self), acc); } fn should_increment_depth(&self) -> bool { @@ -316,8 +316,8 @@ impl TreeElement for BitfieldsContent { } fn evaluate_rules(&self, acc: &mut Vec, rules: &CurrentRules, aux: AuxParams) { rules.sp_brace.check(SpBracesArgs::from_bitfields_content(self), acc); - rules.indent_code_block.check(IndentCodeBlockArgs::from_bitfields_content(self, aux.depth), acc); - rules.indent_closing_brace.check(IndentClosingBraceArgs::from_bitfields_content(self, aux.depth), acc); + rules.indent_code_block.check(IndentCodeBlockArgs::from_bitfields_content(self, aux.depth.depth()), acc); + rules.indent_closing_brace.check(IndentClosingBraceArgs::from_bitfields_content(self, aux.depth.depth()), acc); } fn should_increment_depth(&self) -> bool { true diff --git a/src/lint/mod.rs b/src/lint/mod.rs index f5d2210f..f42da765 100644 --- a/src/lint/mod.rs +++ b/src/lint/mod.rs @@ -245,7 +245,7 @@ impl LinterAnalysis { fn begin_style_check(ast: TopAst, file: &str, rules: &CurrentRules) -> Result, Error> { let (mut invalid_lint_annot, lint_annot) = obtain_lint_annotations(file); let mut linting_errors: Vec = vec![]; - ast.style_check(&mut linting_errors, rules, AuxParams { depth: 0 }); + ast.style_check(&mut linting_errors, rules, AuxParams { depth: DepthHandler::new() }); // Per line checks let lines: Vec<&str> = file.lines().collect(); @@ -426,6 +426,36 @@ fn remove_disabled_lints(errors: &mut Vec, ); } +// Responsible for abstract tracking of depth in terms of levels, +// regardless of indentation sizing +#[derive(Copy, Clone, Debug, Default)] +pub struct DepthHandler { + depth: u32, + ignore_next_increase: bool, +} + +impl DepthHandler { + pub fn new() -> DepthHandler { + DepthHandler::default() + } + + pub fn depth(&self) -> u32 { + self.depth + } + + pub fn increase_depth(&mut self) { + if self.ignore_next_increase { + self.ignore_next_increase = false; + } else { + self.depth += 1; + } + } + + pub fn force_depth(&mut self, new_depth: u32) { + self.depth = new_depth; + self.ignore_next_increase = true; + } +} // AuxParams is an extensible struct. // It can be used for any data that needs @@ -437,7 +467,7 @@ pub struct AuxParams { // the correct indentation level for a node in the AST. // Individual nodes update depth to affect level of their // nested TreeElements. See more in src/lint/README.md - pub depth: u32, + pub depth: DepthHandler, } pub mod rules;