Skip to content

Commit cdd3020

Browse files
authored
Merge pull request #4 from kevv87/feat/common-indentation-param-in1
Add indentation size parameter IN1, common for IN3, IN6 & IN9
2 parents 2c0367e + e81cf1c commit cdd3020

File tree

4 files changed

+70
-34
lines changed

4 files changed

+70
-34
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: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ 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;
1414
use crate::file_management::CanonPath;
1515
use crate::vfs::{Error, TextFile};
1616
use crate::analysis::parsing::structure::TopAst;
1717
use crate::lint::rules::indentation::{MAX_LENGTH_DEFAULT,
18-
INDENTATION_LEVEL_DEFAULT};
18+
INDENTATION_LEVEL_DEFAULT,
19+
setup_indentation_size
20+
};
1921

2022
pub fn parse_lint_cfg(path: PathBuf) -> Result<LintCfg, String> {
2123
debug!("Reading Lint configuration from {:?}", path);
@@ -28,7 +30,10 @@ pub fn parse_lint_cfg(path: PathBuf) -> Result<LintCfg, String> {
2830

2931
pub fn maybe_parse_lint_cfg(path: PathBuf) -> Option<LintCfg> {
3032
match parse_lint_cfg(path) {
31-
Ok(cfg) => Some(cfg),
33+
Ok(mut cfg) => {
34+
setup_indentation_size(&mut cfg);
35+
Some(cfg)
36+
},
3237
Err(e) => {
3338
error!("Failed to parse linting CFG: {}", e);
3439
None
@@ -55,9 +60,11 @@ pub struct LintCfg {
5560
#[serde(default)]
5661
pub long_lines: Option<LongLineOptions>,
5762
#[serde(default)]
63+
pub in1: Option<IN1Options>,
64+
#[serde(default)]
5865
pub in3: Option<IN3Options>,
5966
#[serde(default)]
60-
pub continuation_line: Option<ContinuationLineOptions>,
67+
pub in6: Option<IN6Options>,
6168
#[serde(default)]
6269
pub in9: Option<IN9Options>,
6370
}
@@ -71,14 +78,11 @@ impl Default for LintCfg {
7178
nsp_inparen: Some(NspInparenOptions{}),
7279
nsp_unary: Some(NspUnaryOptions{}),
7380
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}),
81+
long_lines: Some(LongLineOptions{max_length: MAX_LENGTH_DEFAULT}),
82+
in1: Some(IN1Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
83+
in3: Some(IN3Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
84+
in6: Some(IN6Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
85+
in9: Some(IN9Options{indentation_spaces: INDENTATION_LEVEL_DEFAULT}),
8286
}
8387
}
8488
}
@@ -131,7 +135,7 @@ pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Res
131135
}
132136

133137
// Continuation line check
134-
rules.continuation_line.check(&mut linting_errors, &lines);
138+
rules.in6.check(&mut linting_errors, &lines);
135139

136140
Ok(linting_errors)
137141
}

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,15 +80,22 @@ 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
pub struct IN3Rule {
6389
pub enabled: bool,
6490
indentation_spaces: u32
6591
}
6692

6793
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
6894
pub struct IN3Options {
95+
#[serde(default = "default_indentation_spaces")]
6996
pub indentation_spaces: u32,
7097
}
98+
7199
pub struct IN3Args<'a> {
72100
members_ranges: Vec<ZeroRange>,
73101
lbrace: ZeroRange,
@@ -164,25 +192,26 @@ impl Rule for IN3Rule {
164192

165193
// IN6: Continuation Line
166194
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
167-
pub struct ContinuationLineOptions {
195+
pub struct IN6Options {
196+
#[serde(default = "default_indentation_spaces")]
168197
pub indentation_spaces: u32,
169198
}
170199

171-
pub struct ContinuationLineRule {
200+
pub struct IN6Rule {
172201
pub enabled: bool,
173202
pub indentation_spaces: u32,
174203
}
175204

176-
impl ContinuationLineRule {
177-
pub fn from_options(options: &Option<ContinuationLineOptions>) -> ContinuationLineRule {
205+
impl IN6Rule {
206+
pub fn from_options(options: &Option<IN6Options>) -> IN6Rule {
178207
match options {
179-
Some(continuation_line) => ContinuationLineRule {
208+
Some(in6) => IN6Rule {
180209
enabled: true,
181-
indentation_spaces: continuation_line.indentation_spaces,
210+
indentation_spaces: in6.indentation_spaces,
182211
},
183-
None => ContinuationLineRule {
212+
None => IN6Rule {
184213
enabled: false,
185-
indentation_spaces: INDENTATION_LEVEL_DEFAULT,
214+
indentation_spaces: 0,
186215
},
187216
}
188217
}
@@ -203,7 +232,7 @@ impl ContinuationLineRule {
203232
&logical_operators[..],
204233
&bitwise_operators[..],
205234
];
206-
235+
207236
for (i, line) in lines.iter().enumerate() {
208237
if let Some(last_char) = line.trim().chars().last() {
209238
if operators.iter().any(|ops| ops.contains(&last_char.to_string().as_str())) {
@@ -212,7 +241,7 @@ impl ContinuationLineRule {
212241
let expected_indent = line.chars().take_while(|c| c.is_whitespace()).count() + self.indentation_spaces as usize;
213242
let actual_indent = next_line.chars().take_while(|c| c.is_whitespace()).count();
214243
if actual_indent != expected_indent {
215-
let msg = ContinuationLineRule::description().to_owned();
244+
let msg = IN6Rule::description().to_owned();
216245
let dmlerror = LocalDMLError {
217246
range: Range::new(
218247
Row::new_zero_indexed((i + 1) as u32),
@@ -231,13 +260,13 @@ impl ContinuationLineRule {
231260
}
232261
}
233262

234-
impl Rule for ContinuationLineRule {
263+
impl Rule for IN6Rule {
235264
fn name() -> &'static str {
236-
"CONTINUATION_LINE"
265+
"IN6_CONTINUATION_LINE"
237266
}
238267

239268
fn description() -> &'static str {
240-
"Continuation line not indented correctly"
269+
"Continuation line not indented correctly."
241270
}
242271
}
243272

@@ -248,8 +277,10 @@ pub struct IN9Rule {
248277

249278
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
250279
pub struct IN9Options {
280+
#[serde(default = "default_indentation_spaces")]
251281
pub indentation_spaces: u32,
252282
}
283+
253284
pub struct IN9Args<'a> {
254285
case_range: ZeroRange,
255286
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)