Skip to content

Commit b181789

Browse files
Add other config implementation
1 parent ed37242 commit b181789

File tree

3 files changed

+71
-29
lines changed

3 files changed

+71
-29
lines changed

src/ecsls/config.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ def set_opts(self, opts: LSPAny, ls: LanguageServer):
3232

3333
def _read_conf(self, confpath):
3434
with open(confpath, "rb") as f:
35-
self.__conf = tomli.load(f)
35+
self.__conf = tomli.load(f).get("reports", {
36+
"ignore": [],
37+
"severity_levels": True,
38+
"merge": "multiline",
39+
"text": {
40+
"level": False,
41+
"code": True,
42+
"description": True
43+
}
44+
})
3645

3746
def read(self, filepath):
3847
path = Path(filepath)
@@ -43,5 +52,13 @@ def read(self, filepath):
4352

4453
path = path.parent
4554

46-
def get(self, key, default):
47-
return self.__conf.get(key, default)
55+
def get(self, key, typ, default):
56+
v = self.__conf.get(key)
57+
58+
if v is None:
59+
return default
60+
61+
if not isinstance(v, typ):
62+
return default
63+
64+
return v

src/ecsls/server.py

+34-22
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424

2525

2626
class LineRange(Range):
27+
2728
def __init__(self, line: int, last_line: int = 0):
29+
conf = Config.instance()
30+
is_multiline = conf.get("merge", str, "multiline") != "multiplier"
31+
end = last_line if last_line and is_multiline else (line - 1)
32+
2833
super().__init__(
2934
start=Position(line - 1, 1),
30-
end=Position(last_line or (line - 1), 80)
35+
end=Position(end, 80)
3136
)
3237

3338

@@ -37,23 +42,24 @@ def __init__(self, line: int, last_line: int = 0):
3742
ReportType.INFO: DiagnosticSeverity.Hint
3843
}
3944

40-
def _merge_cf4s(reports: List[Report]) -> List[Report]:
45+
def _merge_reports(reports: List[Report]) -> List[Report]:
4146
out_reports = []
42-
cf4s: Dict[int, Report] = {}
43-
44-
for report in sorted(reports, key=lambda r: r.line):
45-
if report.rule != 'C-F4':
46-
out_reports.append(report)
47-
continue
48-
49-
for k in cf4s.values():
50-
if k.is_mergeable(report.line):
51-
k.merge(report)
52-
break
53-
else:
54-
cf4s[report.line] = report
55-
56-
out_reports.extend(cf4s.values())
47+
48+
for rule in set(report.rule for report in reports):
49+
m = {}
50+
51+
for report in sorted(reports, key=lambda r: r.line):
52+
if report.rule != rule:
53+
continue
54+
55+
for k in m.values():
56+
if k.is_mergeable(report.line):
57+
k.merge(report)
58+
break
59+
else:
60+
m[report.line] = report
61+
62+
out_reports.extend(m.values())
5763
return out_reports
5864

5965

@@ -72,13 +78,19 @@ def get_diagnostics(text_doc: Document):
7278

7379
return [
7480
Diagnostic(
75-
range=LineRange(report.line, report.last_line),
81+
range=LineRange(
82+
report.line,
83+
report.last_line
84+
),
7685
message=report.message,
7786
source="Json Server",
78-
severity=SEVERITIES[report.type],
79-
)
80-
for report in _merge_cf4s(reports)
81-
if report.rule not in conf.get("ignore", [])
87+
severity=(
88+
SEVERITIES[report.type]
89+
if conf.get("severity_levels", bool, True)
90+
else DiagnosticSeverity.Hint
91+
)
92+
) for report in _merge_reports(reports)
93+
if report.rule not in conf.get("ignore", list, [])
8294
]
8395

8496
@server.feature(INITIALIZE)

src/ecsls/vera.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,30 @@ def from_string(cls, line: str) -> Optional[Report]:
5252
return None
5353

5454
line, typ, rule = match.groups()
55-
5655
return cls(line=int(line), type=ReportType(typ), rule=rule)
5756

5857
@property
5958
def message(self) -> str:
6059
msg = populate_descriptions()
61-
desc = str(msg.get(self.rule))
6260

63-
times = f" x{self.count}" * (self.count > 1)
64-
return f"{self.rule}: {desc}" + times
61+
conf = Config.instance()
62+
text = conf.get("text", dict, {})
63+
64+
desc_members = []
6565

66+
if text.get("level", False):
67+
desc_members.append(str(self.type))
68+
if text.get("code", True):
69+
desc_members.append(self.rule)
70+
if text.get("description", True):
71+
desc_members.append(str(msg.get(self.rule)))
72+
73+
desc = ':'.join(desc_members)
74+
if conf.get("merge", str, "multiline") != "multiplier":
75+
return desc
76+
77+
times = f" x{self.count}" * (self.count > 1)
78+
return desc + times
6679

6780
def populate_descriptions():
6881
descriptions = {}

0 commit comments

Comments
 (0)