Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/analysis/parsing/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl TreeElement for ParenExpressionContent {
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -353,7 +353,7 @@ impl TreeElement for CastContent {
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/analysis/parsing/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ impl TreeElement for CompoundContent {
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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
Expand Down Expand Up @@ -555,7 +555,7 @@ impl TreeElement for WhileContent {
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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);
}
}
Expand Down Expand Up @@ -872,7 +872,7 @@ impl TreeElement for ForContent {
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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);
}
}
Expand Down Expand Up @@ -1032,7 +1032,7 @@ impl TreeElement for SwitchCase {
}
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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)
Expand Down Expand Up @@ -1122,7 +1122,7 @@ impl TreeElement for SwitchContent {
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>,
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);
}
}
Expand Down Expand Up @@ -1852,7 +1852,7 @@ impl TreeElement for StatementContent {
}
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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()));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/analysis/parsing/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -730,8 +730,8 @@ impl TreeElement for ObjectStatementsContent {
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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)
Expand Down Expand Up @@ -1938,7 +1938,7 @@ impl TreeElement for DMLObjectContent {
}
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/parsing/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub trait TreeElement {

fn style_check(&self, acc: &mut Vec<DMLStyleError>, 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() {
Expand Down
12 changes: 6 additions & 6 deletions src/analysis/parsing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl TreeElement for StructTypeContent {
errors
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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 {
Expand Down Expand Up @@ -138,8 +138,8 @@ impl TreeElement for LayoutContent {
errors
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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 {
Expand Down Expand Up @@ -316,8 +316,8 @@ impl TreeElement for BitfieldsContent {
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, 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
Expand Down
34 changes: 32 additions & 2 deletions src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl LinterAnalysis {
fn begin_style_check(ast: TopAst, file: &str, rules: &CurrentRules) -> Result<Vec<DMLStyleError>, Error> {
let (mut invalid_lint_annot, lint_annot) = obtain_lint_annotations(file);
let mut linting_errors: Vec<DMLStyleError> = 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();
Expand Down Expand Up @@ -426,6 +426,36 @@ fn remove_disabled_lints(errors: &mut Vec<DMLStyleError>,
);
}

// 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
Expand All @@ -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;
Expand Down
Loading