Skip to content

Commit fd0613b

Browse files
committed
Add indentation size parameter IN1, common for IN3, IN6 & IN9
1 parent 2c0367e commit fd0613b

File tree

4 files changed

+63
-33
lines changed

4 files changed

+63
-33
lines changed

example_files/example_lint_cfg.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"nsp_unary": {},
77
"nsp_trailing": {},
88
"long_lines": { "max_length": 80 },
9-
"in3": { "indentation_spaces": 4 },
10-
"continuation_line": { "indentation_spaces": 4 },
11-
"in9": { "indentation_spaces": 4 }
9+
"in1": { "indentation_spaces": 4 },
10+
"in3": {},
11+
"in6": {},
12+
"in9": {}
1213
}

src/lint/mod.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use serde::{Deserialize, Serialize};
66
use rules::{instantiate_rules, CurrentRules};
77
use rules::{spacing::{SpBraceOptions, SpPunctOptions, NspFunparOptions,
88
NspInparenOptions, NspUnaryOptions, NspTrailingOptions},
9-
indentation::{LongLineOptions, IN3Options,
10-
IN9Options, ContinuationLineOptions},
9+
indentation::{LongLineOptions, IN1Options, IN3Options,
10+
IN9Options, IN6Options},
1111
};
1212
use crate::analysis::{DMLError, IsolatedAnalysis, LocalDMLError};
1313
use crate::analysis::parsing::tree::TreeElement;
@@ -28,7 +28,23 @@ pub fn parse_lint_cfg(path: PathBuf) -> Result<LintCfg, String> {
2828

2929
pub fn maybe_parse_lint_cfg(path: PathBuf) -> Option<LintCfg> {
3030
match parse_lint_cfg(path) {
31-
Ok(cfg) => Some(cfg),
31+
Ok(mut cfg) => {
32+
let mut indentation_spaces = INDENTATION_LEVEL_DEFAULT;
33+
34+
if let Some(in1) = &cfg.in1 {
35+
indentation_spaces = in1.indentation_spaces;
36+
}
37+
if let Some(in3) = &mut cfg.in3 {
38+
in3.indentation_spaces = indentation_spaces;
39+
}
40+
if let Some(in6) = &mut cfg.in6 {
41+
in6.indentation_spaces = indentation_spaces;
42+
}
43+
if let Some(in9) = &mut cfg.in9 {
44+
in9.indentation_spaces = indentation_spaces;
45+
}
46+
Some(cfg)
47+
},
3248
Err(e) => {
3349
error!("Failed to parse linting CFG: {}", e);
3450
None
@@ -55,9 +71,11 @@ pub struct LintCfg {
5571
#[serde(default)]
5672
pub long_lines: Option<LongLineOptions>,
5773
#[serde(default)]
74+
pub in1: Option<IN1Options>,
75+
#[serde(default)]
5876
pub in3: Option<IN3Options>,
5977
#[serde(default)]
60-
pub continuation_line: Option<ContinuationLineOptions>,
78+
pub in6: Option<IN6Options>,
6179
#[serde(default)]
6280
pub in9: Option<IN9Options>,
6381
}
@@ -71,14 +89,11 @@ impl Default for LintCfg {
7189
nsp_inparen: Some(NspInparenOptions{}),
7290
nsp_unary: Some(NspUnaryOptions{}),
7391
nsp_trailing: Some(NspTrailingOptions{}),
74-
long_lines: Some(LongLineOptions {
75-
max_length: MAX_LENGTH_DEFAULT,
76-
}),
77-
in3: Some(IN3Options{indentation_spaces: 4}),
78-
continuation_line: Some(ContinuationLineOptions {
79-
indentation_spaces: INDENTATION_LEVEL_DEFAULT,
80-
}),
81-
in9: Some(IN9Options{indentation_spaces: 4}),
92+
long_lines: Some(LongLineOptions{max_length: MAX_LENGTH_DEFAULT}),
93+
in1: Some(IN1Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
94+
in3: Some(IN3Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
95+
in6: Some(IN6Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
96+
in9: Some(IN9Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
8297
}
8398
}
8499
}
@@ -131,7 +146,7 @@ pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Res
131146
}
132147

133148
// Continuation line check
134-
rules.continuation_line.check(&mut linting_errors, &lines);
149+
rules.in6.check(&mut linting_errors, &lines);
135150

136151
Ok(linting_errors)
137152
}

src/lint/rules/indentation.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ use super::Rule;
1212
pub const MAX_LENGTH_DEFAULT: u32 = 80;
1313
pub const INDENTATION_LEVEL_DEFAULT: u32 = 4;
1414

15+
fn default_indentation_spaces() -> u32 {
16+
INDENTATION_LEVEL_DEFAULT
17+
}
18+
1519
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
1620
pub struct LongLineOptions {
1721
pub max_length: u32,
@@ -29,7 +33,7 @@ impl LongLinesRule {
2933
enabled: true,
3034
max_length: long_lines.max_length,
3135
},
32-
None => LongLinesRule {
36+
None => LongLinesRule {
3337
enabled: false,
3438
max_length: MAX_LENGTH_DEFAULT,
3539
},
@@ -59,15 +63,22 @@ impl Rule for LongLinesRule {
5963
}
6064
}
6165

66+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
67+
pub struct IN1Options {
68+
pub indentation_spaces: u32,
69+
}
70+
6271
pub struct IN3Rule {
6372
pub enabled: bool,
6473
indentation_spaces: u32
6574
}
6675

6776
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
6877
pub struct IN3Options {
78+
#[serde(default = "default_indentation_spaces")]
6979
pub indentation_spaces: u32,
7080
}
81+
7182
pub struct IN3Args<'a> {
7283
members_ranges: Vec<ZeroRange>,
7384
lbrace: ZeroRange,
@@ -164,25 +175,26 @@ impl Rule for IN3Rule {
164175

165176
// IN6: Continuation Line
166177
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
167-
pub struct ContinuationLineOptions {
178+
pub struct IN6Options {
179+
#[serde(default = "default_indentation_spaces")]
168180
pub indentation_spaces: u32,
169181
}
170182

171-
pub struct ContinuationLineRule {
183+
pub struct IN6Rule {
172184
pub enabled: bool,
173185
pub indentation_spaces: u32,
174186
}
175187

176-
impl ContinuationLineRule {
177-
pub fn from_options(options: &Option<ContinuationLineOptions>) -> ContinuationLineRule {
188+
impl IN6Rule {
189+
pub fn from_options(options: &Option<IN6Options>) -> IN6Rule {
178190
match options {
179-
Some(continuation_line) => ContinuationLineRule {
191+
Some(in6) => IN6Rule {
180192
enabled: true,
181-
indentation_spaces: continuation_line.indentation_spaces,
193+
indentation_spaces: in6.indentation_spaces,
182194
},
183-
None => ContinuationLineRule {
195+
None => IN6Rule {
184196
enabled: false,
185-
indentation_spaces: INDENTATION_LEVEL_DEFAULT,
197+
indentation_spaces: 0,
186198
},
187199
}
188200
}
@@ -203,7 +215,7 @@ impl ContinuationLineRule {
203215
&logical_operators[..],
204216
&bitwise_operators[..],
205217
];
206-
218+
207219
for (i, line) in lines.iter().enumerate() {
208220
if let Some(last_char) = line.trim().chars().last() {
209221
if operators.iter().any(|ops| ops.contains(&last_char.to_string().as_str())) {
@@ -212,7 +224,7 @@ impl ContinuationLineRule {
212224
let expected_indent = line.chars().take_while(|c| c.is_whitespace()).count() + self.indentation_spaces as usize;
213225
let actual_indent = next_line.chars().take_while(|c| c.is_whitespace()).count();
214226
if actual_indent != expected_indent {
215-
let msg = ContinuationLineRule::description().to_owned();
227+
let msg = IN6Rule::description().to_owned();
216228
let dmlerror = LocalDMLError {
217229
range: Range::new(
218230
Row::new_zero_indexed((i + 1) as u32),
@@ -231,13 +243,13 @@ impl ContinuationLineRule {
231243
}
232244
}
233245

234-
impl Rule for ContinuationLineRule {
246+
impl Rule for IN6Rule {
235247
fn name() -> &'static str {
236-
"CONTINUATION_LINE"
248+
"IN6_CONTINUATION_LINE"
237249
}
238250

239251
fn description() -> &'static str {
240-
"Continuation line not indented correctly"
252+
"Continuation line not indented correctly."
241253
}
242254
}
243255

@@ -248,8 +260,10 @@ pub struct IN9Rule {
248260

249261
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
250262
pub struct IN9Options {
263+
#[serde(default = "default_indentation_spaces")]
251264
pub indentation_spaces: u32,
252265
}
266+
253267
pub struct IN9Args<'a> {
254268
case_range: ZeroRange,
255269
expected_depth: &'a mut u32,

src/lint/rules/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod tests;
77
use spacing::{SpBracesRule,
88
SpPunctRule, NspFunparRule, NspInparenRule,
99
NspUnaryRule, NspTrailingRule};
10-
use indentation::{LongLinesRule, IN3Rule, IN9Rule, ContinuationLineRule};
10+
use indentation::{LongLinesRule, IN3Rule, IN9Rule, IN6Rule};
1111
use crate::lint::LintCfg;
1212

1313
pub struct CurrentRules {
@@ -19,7 +19,7 @@ pub struct CurrentRules {
1919
pub nsp_trailing: NspTrailingRule,
2020
pub long_lines: LongLinesRule,
2121
pub in3: IN3Rule,
22-
pub continuation_line: ContinuationLineRule,
22+
pub in6: IN6Rule,
2323
pub in9: IN9Rule
2424
}
2525

@@ -33,7 +33,7 @@ pub fn instantiate_rules(cfg: &LintCfg) -> CurrentRules {
3333
nsp_trailing: NspTrailingRule { enabled: cfg.nsp_trailing.is_some() },
3434
long_lines: LongLinesRule::from_options(&cfg.long_lines),
3535
in3: IN3Rule::from_options(&cfg.in3),
36-
continuation_line: ContinuationLineRule::from_options(&cfg.continuation_line),
36+
in6: IN6Rule::from_options(&cfg.in6),
3737
in9: IN9Rule::from_options(&cfg.in9),
3838
}
3939
}

0 commit comments

Comments
 (0)