Skip to content

Commit 38d4727

Browse files
committed
Add post process method for linting error
This post process method removes other errors if they are in the same line ad in2 errors. This is because in2 is reported when there are tabs in the document, which causes miss calculations in indentation levels.
1 parent 5033d08 commit 38d4727

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

example_files/example_lint_cfg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"nsp_unary": {},
77
"nsp_trailing": {},
88
"long_lines": { "max_length": 80 },
9+
"in2": {},
910
"in3": { "indentation_spaces": 4 },
1011
"continuation_line": { "indentation_spaces": 4 },
1112
"in9": { "indentation_spaces": 4 }

src/analysis/parsing/parser.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,8 @@ impl <'a> FileParser<'a> {
369369
self.advance();
370370
},
371371
TokenKind::Whitespace => {
372-
let slice = self.lexer.slice();
373-
for ch in slice.chars() {
374-
if ch == '\t' {
375-
self.current_column += 4;
376-
} else {
377-
self.current_column += 1;
378-
}
379-
}
372+
// TODO: this might not work correctly with tabs
373+
self.current_column += self.lexer.slice().len() as u32;
380374
self.advance();
381375
},
382376
TokenKind::MultilineComment => {

src/lint/mod.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use std::fs;
33
use std::path::{Path, PathBuf};
44
use log::{debug, error, trace};
55
use serde::{Deserialize, Serialize};
6-
use rules::{instantiate_rules, CurrentRules};
6+
use rules::indentation::IN2Rule;
7+
use rules::{Rule, instantiate_rules, CurrentRules};
78
use rules::{spacing::{SpBraceOptions, SpPunctOptions, NspFunparOptions,
89
NspInparenOptions, NspUnaryOptions, NspTrailingOptions},
9-
indentation::{LongLineOptions, IN2Options, IN3Options,
10-
IN9Options, ContinuationLineOptions},
11-
};
10+
indentation::{LongLineOptions, IN2Options, IN3Options,
11+
IN9Options, ContinuationLineOptions},
12+
};
1213
use crate::analysis::{DMLError, IsolatedAnalysis, LocalDMLError};
1314
use crate::analysis::parsing::tree::TreeElement;
1415
use crate::file_management::CanonPath;
@@ -137,9 +138,26 @@ pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Res
137138
// Continuation line check
138139
rules.continuation_line.check(&mut linting_errors, &lines);
139140

141+
post_process_linting_errors(&mut linting_errors);
142+
140143
Ok(linting_errors)
141144
}
142145

146+
fn post_process_linting_errors(errors: &mut Vec<LocalDMLError>) {
147+
// Collect in2 ranges
148+
let in2_ranges: Vec<_> = errors.iter()
149+
.filter(|error| error.description == IN2Rule::description())
150+
.map(|error| error.range)
151+
.collect();
152+
153+
// Remove linting errors that are in in2 rows
154+
errors.retain(|error| {
155+
!in2_ranges.iter().any(|range|
156+
(range.row_start == error.range.row_start || range.row_end == error.range.row_end)
157+
&& error.description != IN2Rule::description())
158+
});
159+
}
160+
143161
#[derive(Copy, Clone)]
144162
pub struct AuxParams {
145163
pub depth: u32,
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use crate::lint::rules::tests::common::set_up;
2+
use crate::lint::rules::tests::indentation::assert_indentation;
3+
4+
pub static IN2_USING_TAB_INDENT: &str = "
5+
bank BankA {
6+
group GroupB {
7+
param some_param = this.REG_C;
8+
register REG_C[osdml_reg_idx < ...] is (some_template) {
9+
param other_param = 0;
10+
field Field_D {
11+
is osdml_write;
12+
method write_action(uint64 value) {
13+
if (value == 1) {
14+
log info, 3: \"Writing Field_D\";
15+
}
16+
}
17+
}
18+
method is_even(int value) -> (uint32) {
19+
if (value % 2 == 0) {
20+
return true;
21+
} else
22+
return false;
23+
}
24+
}
25+
}
26+
}
27+
";
28+
#[test]
29+
fn in2_using_tab_indent() {
30+
let rules = set_up();
31+
assert_indentation(IN2_USING_TAB_INDENT, 6, rules);
32+
}
33+
34+
pub static IN2_USING_SPACE_INDENT: &str = "
35+
bank BankA {
36+
group GroupB {
37+
param some_param = this.REG_C;
38+
register REG_C[osdml_reg_idx < ...] is (some_template) {
39+
param other_param = 0;
40+
field Field_D {
41+
is osdml_write;
42+
method write_action(uint64 value) {
43+
if (value == 1) {
44+
log info, 3: \"Writing Field_D\";
45+
}
46+
}
47+
}
48+
method is_even(int value) -> (uint32) {
49+
if (value % 2 == 0) {
50+
return true;
51+
} else
52+
return false;
53+
}
54+
}
55+
}
56+
}
57+
";
58+
#[test]
59+
fn in2_using_space_indent() {
60+
let rules = set_up();
61+
assert_indentation(IN2_USING_SPACE_INDENT, 0, rules);
62+
}

0 commit comments

Comments
 (0)