Skip to content

Commit ed37242

Browse files
Add config key to ignore rules
1 parent f9a501c commit ed37242

File tree

4 files changed

+69
-37
lines changed

4 files changed

+69
-37
lines changed

flake.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
version = "0.0.1";
3434
src = ./.;
3535

36-
propagatedBuildInputs = [ pypkgs.pygls ];
36+
propagatedBuildInputs = [ pypkgs.pygls pypkgs.tomli ];
3737
nativeBuildInputs = with pkgs; [
3838
makeWrapper
3939
];
4040

4141
postPatch = ''
42-
substituteInPlace src/ecsls/vera.py --replace \
42+
substituteInPlace src/ecsls/config.py --replace \
4343
'self.path = "./banana-coding-style-checker"' \
4444
'self.path = "${ruleset}"'
4545
'';

src/ecsls/config.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import annotations
2+
3+
import os
4+
from pathlib import Path
5+
6+
import tomli
7+
from lsprotocol.types import LSPAny
8+
from pygls.server import LanguageServer
9+
10+
class Config:
11+
_instance = None
12+
13+
@classmethod
14+
def instance(cls) -> Config:
15+
if cls._instance is None:
16+
cls._instance = cls()
17+
18+
return cls._instance
19+
20+
def __init__(self):
21+
self.__conf = {}
22+
self.path = "./banana-coding-style-checker"
23+
24+
def set_opts(self, opts: LSPAny, ls: LanguageServer):
25+
if opts is None:
26+
return
27+
28+
self.path = opts.get("path", self.path)
29+
ls.show_message(f"=> PATH = [{self.path}]")
30+
if not os.path.exists(self.path):
31+
raise ValueError
32+
33+
def _read_conf(self, confpath):
34+
with open(confpath, "rb") as f:
35+
self.__conf = tomli.load(f)
36+
37+
def read(self, filepath):
38+
path = Path(filepath)
39+
40+
while path != '/':
41+
if (path / "ecsls.toml").exists():
42+
return self._read_conf(path / "ecsls.toml")
43+
44+
path = path.parent
45+
46+
def get(self, key, default):
47+
return self.__conf.get(key, default)

src/ecsls/server.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import tempfile
22
from typing import Dict, List
33

4+
from .config import Config
45
from .version import __version__
5-
from .vera import get_vera_output, Report, ReportType, CONFIG
6+
from .vera import get_vera_output, Report, ReportType
67

78
from pygls.server import LanguageServer
89
from pygls.workspace import Document
@@ -59,7 +60,10 @@ def _merge_cf4s(reports: List[Report]) -> List[Report]:
5960
def get_diagnostics(text_doc: Document):
6061
content = text_doc.source
6162
filename = ".mk" if text_doc.filename == "Makefile" else text_doc.filename
62-
63+
64+
conf = Config.instance()
65+
conf.read(text_doc.uri)
66+
6367
with tempfile.NamedTemporaryFile(suffix=filename) as tf:
6468
tf.write(content.encode())
6569
tf.flush()
@@ -74,11 +78,12 @@ def get_diagnostics(text_doc: Document):
7478
severity=SEVERITIES[report.type],
7579
)
7680
for report in _merge_cf4s(reports)
81+
if report.rule not in conf.get("ignore", [])
7782
]
7883

7984
@server.feature(INITIALIZE)
8085
async def initialize(ls: LanguageServer, params: InitializeParams):
81-
CONFIG.set_opts(params.initialization_options, ls)
86+
Config.instance().set_opts(params.initialization_options, ls)
8287

8388

8489
@server.feature(TEXT_DOCUMENT_DID_OPEN)

src/ecsls/vera.py

+12-32
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,29 @@
11
from __future__ import annotations
22

3+
import re
4+
import subprocess
5+
36
from dataclasses import dataclass
47
from enum import StrEnum
58

6-
import os
79
import re
810
import subprocess
911
from typing import Final, List, Optional
1012

11-
from lsprotocol.types import LSPAny
12-
from pygls.server import LanguageServer
13-
14-
15-
class Config:
16-
_instance = None
17-
18-
@classmethod
19-
def instance(cls):
20-
if cls._instance is None:
21-
cls._instance = cls()
22-
23-
return cls._instance
24-
25-
def __init__(self):
26-
self.path = "./banana-coding-style-checker"
13+
from .config import Config
2714

28-
def set_opts(self, opts: LSPAny, ls: LanguageServer):
29-
if opts is None:
30-
return
31-
32-
self.path = opts.get("path", self.path)
33-
ls.show_message(f"=> PATH = [{self.path}]")
34-
if not os.path.exists(self.path):
35-
raise ValueError
3615

16+
REPORT_FORMAT: Final[re.Pattern] = re.compile(
17+
r"^[^:]+:(?P<line>\d+):\s?(?P<type>MAJOR|MINOR|INFO):(?P<rule>C-\w\d)$"
18+
)
3719

3820
class ReportType(StrEnum):
3921
MAJOR = "MAJOR"
4022
MINOR = "MINOR"
4123
INFO = "INFO"
4224

4325

26+
4427
@dataclass
4528
class Report:
4629
line: int
@@ -80,16 +63,12 @@ def message(self) -> str:
8063
times = f" x{self.count}" * (self.count > 1)
8164
return f"{self.rule}: {desc}" + times
8265

83-
CONFIG = Config.instance()
84-
REPORT_FORMAT: Final[re.Pattern] = re.compile(
85-
r"^[^:]+:(?P<line>\d+):\s?(?P<type>MAJOR|MINOR|INFO):(?P<rule>C-\w\d)$"
86-
)
87-
8866

8967
def populate_descriptions():
9068
descriptions = {}
9169

92-
with open(f"{CONFIG.path}/vera/code_to_comment") as f:
70+
conf = Config.instance()
71+
with open(f"{conf.path}/vera/code_to_comment") as f:
9372
content = f.read()
9473

9574
for line in content.split("\n"):
@@ -115,13 +94,14 @@ def parse_vera_output(raw_report: str) -> List[Report]:
11594

11695

11796
def get_vera_output(filename: str) -> List[Report]:
97+
conf = Config.instance()
11898
out = subprocess.run(
11999
(
120100
"vera++",
121101
"--profile",
122102
"epitech",
123103
"--root",
124-
f"{CONFIG.path}/vera",
104+
f"{conf.path}/vera",
125105
filename,
126106
),
127107
capture_output=True,

0 commit comments

Comments
 (0)