@@ -6,7 +6,7 @@ use ruff_python_parser::{TokenKind, Tokens};
6
6
use ruff_source_file:: Locator ;
7
7
use ruff_text_size:: { Ranged , TextRange } ;
8
8
9
- use crate :: registry:: AsRule ;
9
+ use crate :: registry:: { AsRule , Rule } ;
10
10
use crate :: rules:: pycodestyle:: rules:: logical_lines:: {
11
11
extraneous_whitespace, indentation, missing_whitespace, missing_whitespace_after_keyword,
12
12
missing_whitespace_around_operator, redundant_backslash, space_after_comma,
@@ -45,36 +45,111 @@ pub(crate) fn check_logical_lines(
45
45
let mut prev_indent_level = None ;
46
46
let indent_char = stylist. indentation ( ) . as_char ( ) ;
47
47
48
+ let enforce_space_around_operator = settings. rules . any_enabled ( & [
49
+ Rule :: MultipleSpacesBeforeOperator ,
50
+ Rule :: MultipleSpacesAfterOperator ,
51
+ Rule :: TabBeforeOperator ,
52
+ Rule :: TabAfterOperator ,
53
+ ] ) ;
54
+ let enforce_whitespace_around_named_parameter_equals = settings. rules . any_enabled ( & [
55
+ Rule :: UnexpectedSpacesAroundKeywordParameterEquals ,
56
+ Rule :: MissingWhitespaceAroundParameterEquals ,
57
+ ] ) ;
58
+ let enforce_missing_whitespace_around_operator = settings. rules . any_enabled ( & [
59
+ Rule :: MissingWhitespaceAroundOperator ,
60
+ Rule :: MissingWhitespaceAroundArithmeticOperator ,
61
+ Rule :: MissingWhitespaceAroundBitwiseOrShiftOperator ,
62
+ Rule :: MissingWhitespaceAroundModuloOperator ,
63
+ ] ) ;
64
+ let enforce_missing_whitespace = settings. rules . enabled ( Rule :: MissingWhitespace ) ;
65
+ let enforce_space_after_comma = settings
66
+ . rules
67
+ . any_enabled ( & [ Rule :: MultipleSpacesAfterComma , Rule :: TabAfterComma ] ) ;
68
+ let enforce_extraneous_whitespace = settings. rules . any_enabled ( & [
69
+ Rule :: WhitespaceAfterOpenBracket ,
70
+ Rule :: WhitespaceBeforeCloseBracket ,
71
+ Rule :: WhitespaceBeforePunctuation ,
72
+ ] ) ;
73
+ let enforce_whitespace_around_keywords = settings. rules . any_enabled ( & [
74
+ Rule :: MultipleSpacesAfterKeyword ,
75
+ Rule :: MultipleSpacesBeforeKeyword ,
76
+ Rule :: TabAfterKeyword ,
77
+ Rule :: TabBeforeKeyword ,
78
+ ] ) ;
79
+ let enforce_missing_whitespace_after_keyword =
80
+ settings. rules . enabled ( Rule :: MissingWhitespaceAfterKeyword ) ;
81
+ let enforce_whitespace_before_comment = settings. rules . any_enabled ( & [
82
+ Rule :: TooFewSpacesBeforeInlineComment ,
83
+ Rule :: NoSpaceAfterInlineComment ,
84
+ Rule :: NoSpaceAfterBlockComment ,
85
+ Rule :: MultipleLeadingHashesForBlockComment ,
86
+ ] ) ;
87
+ let enforce_whitespace_before_parameters =
88
+ settings. rules . enabled ( Rule :: WhitespaceBeforeParameters ) ;
89
+ let enforce_redundant_backslash = settings. rules . enabled ( Rule :: RedundantBackslash ) ;
90
+ let enforce_indentation = settings. rules . any_enabled ( & [
91
+ Rule :: IndentationWithInvalidMultiple ,
92
+ Rule :: NoIndentedBlock ,
93
+ Rule :: UnexpectedIndentation ,
94
+ Rule :: IndentationWithInvalidMultipleComment ,
95
+ Rule :: NoIndentedBlockComment ,
96
+ Rule :: UnexpectedIndentationComment ,
97
+ Rule :: OverIndented ,
98
+ ] ) ;
99
+
48
100
for line in & LogicalLines :: from_tokens ( tokens, locator) {
49
101
if line. flags ( ) . contains ( TokenFlags :: OPERATOR ) {
50
- space_around_operator ( & line, & mut context) ;
51
- whitespace_around_named_parameter_equals ( & line, & mut context) ;
52
- missing_whitespace_around_operator ( & line, & mut context) ;
53
- missing_whitespace ( & line, & mut context) ;
102
+ if enforce_space_around_operator {
103
+ space_around_operator ( & line, & mut context) ;
104
+ }
105
+
106
+ if enforce_whitespace_around_named_parameter_equals {
107
+ whitespace_around_named_parameter_equals ( & line, & mut context) ;
108
+ }
109
+
110
+ if enforce_missing_whitespace_around_operator {
111
+ missing_whitespace_around_operator ( & line, & mut context) ;
112
+ }
113
+
114
+ if enforce_missing_whitespace {
115
+ missing_whitespace ( & line, & mut context) ;
116
+ }
54
117
}
55
- if line. flags ( ) . contains ( TokenFlags :: PUNCTUATION ) {
118
+
119
+ if line. flags ( ) . contains ( TokenFlags :: PUNCTUATION ) && enforce_space_after_comma {
56
120
space_after_comma ( & line, & mut context) ;
57
121
}
58
122
59
123
if line
60
124
. flags ( )
61
125
. intersects ( TokenFlags :: OPERATOR | TokenFlags :: BRACKET | TokenFlags :: PUNCTUATION )
126
+ && enforce_extraneous_whitespace
62
127
{
63
128
extraneous_whitespace ( & line, & mut context) ;
64
129
}
65
130
66
131
if line. flags ( ) . contains ( TokenFlags :: KEYWORD ) {
67
- whitespace_around_keywords ( & line, & mut context) ;
68
- missing_whitespace_after_keyword ( & line, & mut context) ;
132
+ if enforce_whitespace_around_keywords {
133
+ whitespace_around_keywords ( & line, & mut context) ;
134
+ }
135
+
136
+ if enforce_missing_whitespace_after_keyword {
137
+ missing_whitespace_after_keyword ( & line, & mut context) ;
138
+ }
69
139
}
70
140
71
- if line. flags ( ) . contains ( TokenFlags :: COMMENT ) {
141
+ if line. flags ( ) . contains ( TokenFlags :: COMMENT ) && enforce_whitespace_before_comment {
72
142
whitespace_before_comment ( & line, locator, & mut context) ;
73
143
}
74
144
75
145
if line. flags ( ) . contains ( TokenFlags :: BRACKET ) {
76
- whitespace_before_parameters ( & line, & mut context) ;
77
- redundant_backslash ( & line, locator, indexer, & mut context) ;
146
+ if enforce_whitespace_before_parameters {
147
+ whitespace_before_parameters ( & line, & mut context) ;
148
+ }
149
+
150
+ if enforce_redundant_backslash {
151
+ redundant_backslash ( & line, locator, indexer, & mut context) ;
152
+ }
78
153
}
79
154
80
155
// Extract the indentation level.
@@ -92,16 +167,18 @@ pub(crate) fn check_logical_lines(
92
167
93
168
let indent_size = 4 ;
94
169
95
- for kind in indentation (
96
- & line,
97
- prev_line. as_ref ( ) ,
98
- indent_char,
99
- indent_level,
100
- prev_indent_level,
101
- indent_size,
102
- ) {
103
- if settings. rules . enabled ( kind. rule ( ) ) {
104
- context. push_diagnostic ( Diagnostic :: new ( kind, range) ) ;
170
+ if enforce_indentation {
171
+ for kind in indentation (
172
+ & line,
173
+ prev_line. as_ref ( ) ,
174
+ indent_char,
175
+ indent_level,
176
+ prev_indent_level,
177
+ indent_size,
178
+ ) {
179
+ if settings. rules . enabled ( kind. rule ( ) ) {
180
+ context. push_diagnostic ( Diagnostic :: new ( kind, range) ) ;
181
+ }
105
182
}
106
183
}
107
184
0 commit comments