22// SPDX-License-Identifier: Apache-2.0 and MIT
33use log:: error;
44
5+ use crate :: lint:: rules:: indentation:: IndentEmptyLoopArgs ;
56use crate :: span:: Range ;
67use crate :: analysis:: parsing:: lexer:: TokenKind ;
8+ use crate :: analysis:: parsing:: statement;
79use crate :: analysis:: parsing:: parser:: { Token , doesnt_understand_tokens,
810 FileParser , Parse , ParseContext ,
911 FileInfo } ;
@@ -21,10 +23,13 @@ use crate::analysis::parsing::misc::{Initializer, InitializerContent, CDecl,
2123 ident_filter, objident_filter} ;
2224use crate :: analysis:: parsing:: structure:: { parse_vardecl, VarDecl } ;
2325use crate :: analysis:: LocalDMLError ;
24- use crate :: lint:: rules:: spacing:: { NspInparenArgs ,
25- SpBracesArgs ,
26- SpPunctArgs } ;
27- use crate :: lint:: rules:: CurrentRules ;
26+ use crate :: lint:: { DMLStyleError ,
27+ rules:: { CurrentRules ,
28+ indentation:: { IndentCodeBlockArgs , IndentClosingBraceArgs , IndentParenExprArgs , IndentSwitchCaseArgs } ,
29+ spacing:: { NspInparenArgs ,
30+ SpBracesArgs ,
31+ SpPunctArgs } } ,
32+ AuxParams } ;
2833use crate :: vfs:: TextFile ;
2934
3035fn statement_contexts ( context : & ParseContext )
@@ -139,8 +144,13 @@ impl TreeElement for CompoundContent {
139144 fn subs ( & self ) -> TreeElements < ' _ > {
140145 create_subs ! ( & self . lbrace, & self . statements, & self . rbrace)
141146 }
142- fn evaluate_rules ( & self , acc : & mut Vec < LocalDMLError > , rules : & CurrentRules ) {
147+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , aux : AuxParams ) {
143148 rules. sp_brace . check ( acc, SpBracesArgs :: from_compound ( self ) ) ;
149+ rules. indent_code_block . check ( acc, IndentCodeBlockArgs :: from_compound_content ( self , aux. depth ) ) ;
150+ rules. indent_closing_brace . check ( acc, IndentClosingBraceArgs :: from_compound_content ( self , aux. depth ) ) ;
151+ }
152+ fn should_increment_depth ( & self ) -> bool {
153+ true
144154 }
145155}
146156
@@ -192,7 +202,7 @@ impl TreeElement for VariableDeclContent {
192202 fn post_parse_sanity ( & self , _file : & TextFile ) -> Vec < LocalDMLError > {
193203 self . decls . ensure_named ( )
194204 }
195- fn evaluate_rules ( & self , acc : & mut Vec < LocalDMLError > , rules : & CurrentRules ) {
205+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , _aux : AuxParams ) {
196206 rules. sp_punct . check ( acc, SpPunctArgs :: from_variable_decl ( self ) ) ;
197207 }
198208}
@@ -424,8 +434,9 @@ impl TreeElement for IfContent {
424434 & self . truebranch,
425435 & self . elsebranch)
426436 }
427- fn evaluate_rules ( & self , acc : & mut Vec < LocalDMLError > , rules : & CurrentRules ) {
437+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , _aux : AuxParams ) {
428438 rules. nsp_inparen . check ( acc, NspInparenArgs :: from_if ( self ) ) ;
439+ rules. indent_paren_expr . check ( acc, IndentParenExprArgs :: from_if ( self ) ) ;
429440 }
430441}
431442
@@ -536,6 +547,10 @@ impl TreeElement for WhileContent {
536547 & self . rparen,
537548 & self . statement)
538549 }
550+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , aux : AuxParams ) {
551+ rules. indent_paren_expr . check ( acc, IndentParenExprArgs :: from_while ( self ) ) ;
552+ rules. indent_empty_loop . check ( acc, IndentEmptyLoopArgs :: from_while_content ( self , aux. depth ) ) ;
553+ }
539554}
540555
541556impl Parse < StatementContent > for WhileContent {
@@ -583,6 +598,9 @@ impl TreeElement for DoContent {
583598 & self . rparen,
584599 & self . semi)
585600 }
601+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , _aux : AuxParams ) {
602+ rules. indent_paren_expr . check ( acc, IndentParenExprArgs :: from_do_while ( self ) ) ;
603+ }
586604}
587605
588606impl Parse < StatementContent > for DoContent {
@@ -844,6 +862,10 @@ impl TreeElement for ForContent {
844862 & self . rparen,
845863 & self . statement)
846864 }
865+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , aux : AuxParams ) {
866+ rules. indent_paren_expr . check ( acc, IndentParenExprArgs :: from_for ( self ) ) ;
867+ rules. indent_empty_loop . check ( acc, IndentEmptyLoopArgs :: from_for_content ( self , aux. depth ) ) ;
868+ }
847869}
848870
849871impl Parse < StatementContent > for ForContent {
@@ -1000,6 +1022,14 @@ impl TreeElement for SwitchCase {
10001022 Self :: Default ( default, colon) => create_subs ! ( default , colon) ,
10011023 }
10021024 }
1025+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , aux : AuxParams ) {
1026+ rules. indent_switch_case . check ( acc, IndentSwitchCaseArgs :: from_switch_case ( self , aux. depth ) ) ;
1027+ }
1028+ fn should_increment_depth ( & self ) -> bool {
1029+ matches ! ( self , SwitchCase :: Statement ( statement)
1030+ if !matches!( * statement. content,
1031+ Content :: Some ( statement:: StatementContent :: Compound ( _) ) ) )
1032+ }
10031033}
10041034
10051035fn parse_switchcase ( context : & ParseContext , stream : & mut FileParser < ' _ > , file_info : & FileInfo )
@@ -1080,6 +1110,12 @@ impl TreeElement for SwitchContent {
10801110 & self . cases,
10811111 & self . rbrace)
10821112 }
1113+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > ,
1114+ rules : & CurrentRules , aux : AuxParams )
1115+ {
1116+ rules. indent_closing_brace . check ( acc, IndentClosingBraceArgs :: from_switch_content ( self , aux. depth ) ) ;
1117+ rules. indent_paren_expr . check ( acc, IndentParenExprArgs :: from_switch ( self ) ) ;
1118+ }
10831119}
10841120
10851121impl Parse < StatementContent > for SwitchContent {
@@ -1573,6 +1609,9 @@ impl TreeElement for ForeachContent {
15731609 & self . rparen,
15741610 & self . statement)
15751611 }
1612+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , _aux : AuxParams ) {
1613+ rules. indent_paren_expr . check ( acc, IndentParenExprArgs :: from_foreach ( self ) ) ;
1614+ }
15761615}
15771616
15781617impl Parse < StatementContent > for ForeachContent {
@@ -1709,7 +1748,7 @@ impl TreeElement for ExpressionStmtContent {
17091748 fn subs ( & self ) -> TreeElements < ' _ > {
17101749 create_subs ! ( & self . expression, & self . semi)
17111750 }
1712- fn evaluate_rules ( & self , acc : & mut Vec < LocalDMLError > , rules : & CurrentRules ) {
1751+ fn evaluate_rules ( & self , acc : & mut Vec < DMLStyleError > , rules : & CurrentRules , _aux : AuxParams ) {
17131752 rules. sp_punct . check ( acc, SpPunctArgs :: from_expression_stmt ( self ) ) ;
17141753 }
17151754}
0 commit comments