-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinter.py
More file actions
143 lines (104 loc) · 4.09 KB
/
Copy pathlinter.py
File metadata and controls
143 lines (104 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from re import Match, Pattern
from typing import Iterable
from SublimeLinter.lint.base_linter.php_linter import PhpLinter
from SublimeLinter.lint.linter import LintMatch
from sublime import FindFlags
SELECTOR = "embedding.php"
REPORTING_FORMAT = "github"
class Mago(PhpLinter):
cmd: str = f'mago analyze --reporting-format {REPORTING_FORMAT} "$file"'
defaults: "dict[str, str | bool]" = {"selector": SELECTOR, "disable": True}
error_types: str = "note|notice|help|warning|error"
regex_short: str = (
r"^(?P<filename>.+?):"
r"(?P<line>\d+):"
r"(?P<col>\d+): "
rf"(?P<error_type>{error_types})"
r"\[(?P<code>.+?)\]: "
r"(?P<message>.+)"
)
regex_medium: str = (
r"^(?P<filename>.+?):"
r"(?P<line>\d+):"
r"(?P<col>\d+): "
rf"(?P<error_type>{error_types})"
r"\[(?P<code>.+?)\]: "
r"(?P<message>.+(\s+=\s+.+)+)"
)
regex_github: str = (
rf"^::(?P<error_type>{error_types}) "
r"file=(?P<filename>.+?),"
r"line=(?P<line>\d+),"
r"endLine=(?P<end_line>\d+),"
r"col=(?P<col>\d+),"
r"endColumn=(?P<end_col>\d+),"
r"title=(?P<code>.+?)::"
r"(?P<message>.+)"
)
regex: "None | str | Pattern[str]" = regex_github
multiline: bool = True
def split_match(self, match: "Match[str]") -> LintMatch:
error = super().split_match(match)
error.message = error.message.replace("%0A%0AHelp", "\n\n💡 Help")
error.message = error.message.replace("%0A>", "\n👉 ")
error.message = error.message.replace("%0A%0A", "\n\n➖ ")
error.message = error.message.replace("%0A", "\n➖ ")
return error
class MagoAnalyze(Mago):
defaults: "dict[str, str | bool]" = {"selector": SELECTOR, "disable": False}
name: str = "mago-analyze"
class MagoLint(Mago):
name: str = "mago-lint"
defaults: "dict[str, str | bool]" = {"selector": SELECTOR, "disable": False}
cmd: str = f'mago lint --reporting-format {REPORTING_FORMAT} "$file"'
class MagoGuard(Mago):
name: str = "mago-guard"
defaults: "dict[str, str | bool]" = {"selector": SELECTOR, "disable": False}
cmd: str = f'mago guard --reporting-format {REPORTING_FORMAT} "$file"'
class MagoFormat(Mago):
name: str = "mago-format"
defaults: "dict[str, str | bool]" = {"selector": SELECTOR, "disable": False}
cmd: str = 'mago format --dry-run "$file"'
regex: "None | str | Pattern[str]" = None
def find_errors(self, output: str) -> Iterable[LintMatch]:
output_lines: list[str] = []
lint_matches: Iterable[LintMatch] = []
for output_line in output.splitlines():
if (
not output_line.startswith('-')
and not output_line.startswith('+')
):
continue
if (
output_line.startswith('---')
or output_line.startswith('+++')
):
continue
output_lines.append(output_line)
index = 0
while index < len(output_lines):
filtered_output_line = output_lines[index]
if filtered_output_line.startswith('-'):
region = self.view.find(
filtered_output_line[1:],
0,
FindFlags.LITERAL,
)
line = self.view.rowcol(region.a)[0]
message = ''
next_index = index + 1
while next_index < len(output_lines):
next_filtered_output_line = output_lines[next_index]
if not next_filtered_output_line.startswith('+'):
break
if not message:
message += '\n'
message += next_filtered_output_line[1:] + '\n'
next_index += 1
lint_matches.append(LintMatch({
'line': line,
'end_line': line,
'message': message,
}))
index += 1
return lint_matches