Skip to content

Commit 5ad1864

Browse files
committed
Merge branch 'feature/55-indentation-rules' into feat/in2
2 parents 38d4727 + cdd3020 commit 5ad1864

File tree

4 files changed

+71
-35
lines changed

4 files changed

+71
-35
lines changed

example_files/example_lint_cfg.json

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

src/lint/mod.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ use rules::indentation::IN2Rule;
77
use rules::{Rule, instantiate_rules, CurrentRules};
88
use rules::{spacing::{SpBraceOptions, SpPunctOptions, NspFunparOptions,
99
NspInparenOptions, NspUnaryOptions, NspTrailingOptions},
10-
indentation::{LongLineOptions, IN2Options, IN3Options,
11-
IN9Options, ContinuationLineOptions},
12-
};
10+
indentation::{LongLineOptions, IN1Options, IN3Options,
11+
IN2Options, IN9Options, IN6Options},
12+
};
1313
use crate::analysis::{DMLError, IsolatedAnalysis, LocalDMLError};
1414
use crate::analysis::parsing::tree::TreeElement;
1515
use crate::file_management::CanonPath;
1616
use crate::vfs::{Error, TextFile};
1717
use crate::analysis::parsing::structure::TopAst;
1818
use crate::lint::rules::indentation::{MAX_LENGTH_DEFAULT,
19-
INDENTATION_LEVEL_DEFAULT};
19+
INDENTATION_LEVEL_DEFAULT,
20+
setup_indentation_size
21+
};
2022

2123
pub fn parse_lint_cfg(path: PathBuf) -> Result<LintCfg, String> {
2224
debug!("Reading Lint configuration from {:?}", path);
@@ -29,7 +31,10 @@ pub fn parse_lint_cfg(path: PathBuf) -> Result<LintCfg, String> {
2931

3032
pub fn maybe_parse_lint_cfg(path: PathBuf) -> Option<LintCfg> {
3133
match parse_lint_cfg(path) {
32-
Ok(cfg) => Some(cfg),
34+
Ok(mut cfg) => {
35+
setup_indentation_size(&mut cfg);
36+
Some(cfg)
37+
},
3338
Err(e) => {
3439
error!("Failed to parse linting CFG: {}", e);
3540
None
@@ -56,11 +61,13 @@ pub struct LintCfg {
5661
#[serde(default)]
5762
pub long_lines: Option<LongLineOptions>,
5863
#[serde(default)]
64+
pub in1: Option<IN1Options>,
65+
#[serde(default)]
5966
pub in2: Option<IN2Options>,
6067
#[serde(default)]
6168
pub in3: Option<IN3Options>,
6269
#[serde(default)]
63-
pub continuation_line: Option<ContinuationLineOptions>,
70+
pub in6: Option<IN6Options>,
6471
#[serde(default)]
6572
pub in9: Option<IN9Options>,
6673
}
@@ -74,15 +81,12 @@ impl Default for LintCfg {
7481
nsp_inparen: Some(NspInparenOptions{}),
7582
nsp_unary: Some(NspUnaryOptions{}),
7683
nsp_trailing: Some(NspTrailingOptions{}),
77-
long_lines: Some(LongLineOptions {
78-
max_length: MAX_LENGTH_DEFAULT,
79-
}),
84+
long_lines: Some(LongLineOptions{max_length: MAX_LENGTH_DEFAULT}),
85+
in1: Some(IN1Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
8086
in2: Some(IN2Options{}),
81-
in3: Some(IN3Options{indentation_spaces: 4}),
82-
continuation_line: Some(ContinuationLineOptions {
83-
indentation_spaces: INDENTATION_LEVEL_DEFAULT,
84-
}),
85-
in9: Some(IN9Options{indentation_spaces: 4}),
87+
in3: Some(IN3Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
88+
in6: Some(IN6Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
89+
in9: Some(IN9Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
8690
}
8791
}
8892
}
@@ -136,7 +140,7 @@ pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Res
136140
}
137141

138142
// Continuation line check
139-
rules.continuation_line.check(&mut linting_errors, &lines);
143+
rules.in6.check(&mut linting_errors, &lines);
140144

141145
post_process_linting_errors(&mut linting_errors);
142146

src/lint/rules/indentation.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,31 @@ use crate::analysis::LocalDMLError;
88
use crate::analysis::parsing::tree::{ZeroRange, Content, TreeElement};
99
use serde::{Deserialize, Serialize};
1010
use super::Rule;
11+
use crate::lint::LintCfg;
1112

1213
pub const MAX_LENGTH_DEFAULT: u32 = 80;
1314
pub const INDENTATION_LEVEL_DEFAULT: u32 = 4;
1415

16+
fn default_indentation_spaces() -> u32 {
17+
INDENTATION_LEVEL_DEFAULT
18+
}
19+
20+
pub fn setup_indentation_size(cfg: &mut LintCfg) {
21+
let mut indentation_spaces = INDENTATION_LEVEL_DEFAULT;
22+
23+
if let Some(in1) = &cfg.in1 {
24+
indentation_spaces = in1.indentation_spaces;
25+
}
26+
if let Some(in3) = &mut cfg.in3 {
27+
in3.indentation_spaces = indentation_spaces;
28+
}
29+
if let Some(in6) = &mut cfg.in6 {
30+
in6.indentation_spaces = indentation_spaces;
31+
}
32+
if let Some(in9) = &mut cfg.in9 {
33+
in9.indentation_spaces = indentation_spaces;
34+
}
35+
}
1536
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
1637
pub struct LongLineOptions {
1738
pub max_length: u32,
@@ -29,7 +50,7 @@ impl LongLinesRule {
2950
enabled: true,
3051
max_length: long_lines.max_length,
3152
},
32-
None => LongLinesRule {
53+
None => LongLinesRule {
3354
enabled: false,
3455
max_length: MAX_LENGTH_DEFAULT,
3556
},
@@ -59,6 +80,11 @@ impl Rule for LongLinesRule {
5980
}
6081
}
6182

83+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
84+
pub struct IN1Options {
85+
pub indentation_spaces: u32,
86+
}
87+
6288
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
6389
pub struct IN2Options {}
6490

@@ -98,8 +124,10 @@ pub struct IN3Rule {
98124

99125
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
100126
pub struct IN3Options {
127+
#[serde(default = "default_indentation_spaces")]
101128
pub indentation_spaces: u32,
102129
}
130+
103131
pub struct IN3Args<'a> {
104132
members_ranges: Vec<ZeroRange>,
105133
lbrace: ZeroRange,
@@ -196,25 +224,26 @@ impl Rule for IN3Rule {
196224

197225
// IN6: Continuation Line
198226
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
199-
pub struct ContinuationLineOptions {
227+
pub struct IN6Options {
228+
#[serde(default = "default_indentation_spaces")]
200229
pub indentation_spaces: u32,
201230
}
202231

203-
pub struct ContinuationLineRule {
232+
pub struct IN6Rule {
204233
pub enabled: bool,
205234
pub indentation_spaces: u32,
206235
}
207236

208-
impl ContinuationLineRule {
209-
pub fn from_options(options: &Option<ContinuationLineOptions>) -> ContinuationLineRule {
237+
impl IN6Rule {
238+
pub fn from_options(options: &Option<IN6Options>) -> IN6Rule {
210239
match options {
211-
Some(continuation_line) => ContinuationLineRule {
240+
Some(in6) => IN6Rule {
212241
enabled: true,
213-
indentation_spaces: continuation_line.indentation_spaces,
242+
indentation_spaces: in6.indentation_spaces,
214243
},
215-
None => ContinuationLineRule {
244+
None => IN6Rule {
216245
enabled: false,
217-
indentation_spaces: INDENTATION_LEVEL_DEFAULT,
246+
indentation_spaces: 0,
218247
},
219248
}
220249
}
@@ -235,7 +264,7 @@ impl ContinuationLineRule {
235264
&logical_operators[..],
236265
&bitwise_operators[..],
237266
];
238-
267+
239268
for (i, line) in lines.iter().enumerate() {
240269
if let Some(last_char) = line.trim().chars().last() {
241270
if operators.iter().any(|ops| ops.contains(&last_char.to_string().as_str())) {
@@ -244,7 +273,7 @@ impl ContinuationLineRule {
244273
let expected_indent = line.chars().take_while(|c| c.is_whitespace()).count() + self.indentation_spaces as usize;
245274
let actual_indent = next_line.chars().take_while(|c| c.is_whitespace()).count();
246275
if actual_indent != expected_indent {
247-
let msg = ContinuationLineRule::description().to_owned();
276+
let msg = IN6Rule::description().to_owned();
248277
let dmlerror = LocalDMLError {
249278
range: Range::new(
250279
Row::new_zero_indexed((i + 1) as u32),
@@ -263,13 +292,13 @@ impl ContinuationLineRule {
263292
}
264293
}
265294

266-
impl Rule for ContinuationLineRule {
295+
impl Rule for IN6Rule {
267296
fn name() -> &'static str {
268-
"CONTINUATION_LINE"
297+
"IN6_CONTINUATION_LINE"
269298
}
270299

271300
fn description() -> &'static str {
272-
"Continuation line not indented correctly"
301+
"Continuation line not indented correctly."
273302
}
274303
}
275304

@@ -280,8 +309,10 @@ pub struct IN9Rule {
280309

281310
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
282311
pub struct IN9Options {
312+
#[serde(default = "default_indentation_spaces")]
283313
pub indentation_spaces: u32,
284314
}
315+
285316
pub struct IN9Args<'a> {
286317
case_range: ZeroRange,
287318
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, IN2Rule, IN3Rule, IN9Rule, ContinuationLineRule};
10+
use indentation::{LongLinesRule, IN2Rule, IN3Rule, IN6Rule, IN9Rule};
1111
use crate::lint::LintCfg;
1212

1313
pub struct CurrentRules {
@@ -20,7 +20,7 @@ pub struct CurrentRules {
2020
pub long_lines: LongLinesRule,
2121
pub in2: IN2Rule,
2222
pub in3: IN3Rule,
23-
pub continuation_line: ContinuationLineRule,
23+
pub in6: IN6Rule,
2424
pub in9: IN9Rule
2525
}
2626

@@ -35,7 +35,7 @@ pub fn instantiate_rules(cfg: &LintCfg) -> CurrentRules {
3535
long_lines: LongLinesRule::from_options(&cfg.long_lines),
3636
in2: IN2Rule { enabled: cfg.in2.is_some() },
3737
in3: IN3Rule::from_options(&cfg.in3),
38-
continuation_line: ContinuationLineRule::from_options(&cfg.continuation_line),
38+
in6: IN6Rule::from_options(&cfg.in6),
3939
in9: IN9Rule::from_options(&cfg.in9),
4040
}
4141
}

0 commit comments

Comments
 (0)