Debian Codegraph @

blueprintcompiler/linter_rules/prefer_unicode_chars.py

raw
1 import re
2 from dataclasses import dataclass
3
4 from .. import annotations
5 from ..errors import CompileWarning
6 from ..language import Property
7 from ..lsp_utils import CodeAction
8 from ..utils import Range, adjust_quote_pos
9 from .utils import LinterRule
10
11
12 @dataclass
13 class Subrule:
14 pattern: re.Pattern
15 message: str
16 replace_desc: str
17 replace_with: str
18 group: int = 0
19
20
21 PATTERNS: dict[str, Subrule] = {
22 "ellipsis": Subrule(
23 re.compile(r"\S *(\.{3,4})"),
24 "Prefer using an ellipsis (<…>, U+2026) instead of <{0}>",
25 "replace <{0}> with <\u2026>",
26 "\u2026",
27 group=1,
28 ),
29 "bullet-list": Subrule(
30 re.compile(r"^ *(\*|-) +.*$", re.MULTILINE),
31 "Prefer using a bullet (<•>, U+2022) instead of <{0}> at the start of a line",
32 "replace <{0}> with <\u2022>",
33 "\u2022",
34 group=1,
35 ),
36 "quote-marks": Subrule(
37 re.compile(r'"(\S.*\S)"'),
38 'Prefer using genuine quote marks (<“>, U+201C, and <”>, U+201D) instead of <">',
39 'replace <"..."> with <\u201c...\u201d>',
40 "\u201c{0}\u201d",
41 ),
42 "apostrophe": Subrule(
43 re.compile(r"\S+(')\S*"),
44 "Prefer using a right single quote (<’>, U+2019) instead of <'> to denote an apostrophe",
45 "replace <'> with <\u2019>",
46 "\u2019",
47 group=1,
48 ),
49 "multiplication": Subrule(
50 re.compile(r"[0-9,.]*[0-9]\s*\S*\s*(x)\s*([0-9,.]*[0-9]\s*\S*\s)?"),
51 "Prefer using a multiplication sign (<×>, U+00D7), instead of <x>",
52 "replace <x> with <\u00d7>",
53 "\u00d7",
54 group=1,
55 ),
56 "units": Subrule(
57 re.compile(r"\b(([0-9,.]*[0-9])(?!x\b)([^0-9\s]+))\b"),
58 "When a number is displayed with units, the two should be separated by a narrow no-break space (< >, U+202F)",
59 "insert <\u202f>",
60 "{1}\u202f{2}",
61 group=1,
62 ),
63 }
64
65
66 class PreferUnicodeChars(LinterRule):
67 id = "use_unicode"
68 severity = "suggestion"
69 category = "hig"
70
71 def check(self, type, child, stack):
72 for property in child.content.children[Property]:
73 if annotations.is_property_user_facing_string(property.gir_property):
74 self.check_property(property)
75
76 def check_property(self, property):
77 string, range = self.get_string_value(property)
78 if string is None:
79 return
80
81 for name, pattern in PATTERNS.items():
82 for match in pattern.pattern.finditer(string):
83
84 def replace(string: str):
85 return string.format(*match.groups())
86
87 message = replace(pattern.message)
88
89 start_adjusted = adjust_quote_pos(
90 range.text, match.start(pattern.group)
91 )
92 end_adjusted = adjust_quote_pos(range.text, match.end(pattern.group))
93 problem_range = Range(
94 range.start + start_adjusted,
95 range.start + end_adjusted,
96 original_text=range.original_text,
97 )
98
99 self.problems.append(
100 CompileWarning(
101 message,
102 problem_range,
103 actions=[
104 CodeAction(
105 replace(pattern.replace_desc),
106 replace(pattern.replace_with),
107 )
108 ],
109 id=self.id,
110 )
111 )