| 1 |
from ..errors import CodeAction, CompileWarning |
| 2 |
from ..language import Child, Property |
| 3 |
from ..utils import TextEdit |
| 4 |
from .utils import LinterRule |
| 5 |
|
| 6 |
|
| 7 |
class PreferAdwBin(LinterRule): |
| 8 |
id = "use_adw_bin" |
| 9 |
severity = "suggestion" |
| 10 |
category = "technical" |
| 11 |
platform = "adw" |
| 12 |
|
| 13 |
def check(self, type, child, stack): |
| 14 |
# rule suggestion/prefer-adwbin |
| 15 |
# FIXME: Only if use Adw is in scope and no Gtk.Box properties are used |
| 16 |
children = child.content.children[Child] |
| 17 |
if type == "Gtk.Box" and len(children) == 1: |
| 18 |
additional_edits = [] |
| 19 |
if "Adw" not in child.root.gir.namespaces: |
| 20 |
additional_edits.append( |
| 21 |
TextEdit(child.root.import_range("Adw"), f"\nusing Adw 1;") |
| 22 |
) |
| 23 |
|
| 24 |
for prop in child.content.children[Property]: |
| 25 |
if prop.name in ( |
| 26 |
"baseline-child", |
| 27 |
"baseline-position", |
| 28 |
"orientation", |
| 29 |
"spacing", |
| 30 |
): |
| 31 |
additional_edits.append( |
| 32 |
TextEdit(prop.range.with_preceding_whitespace, "") |
| 33 |
) |
| 34 |
|
| 35 |
problem = CompileWarning( |
| 36 |
f"Use Adw.Bin instead of a Gtk.Box for a single child", |
| 37 |
child.signature_range, |
| 38 |
actions=[ |
| 39 |
CodeAction( |
| 40 |
"Use Adw.Bin", |
| 41 |
"Adw.Bin", |
| 42 |
edit_range=child.class_name.range, |
| 43 |
additional_edits=additional_edits, |
| 44 |
) |
| 45 |
], |
| 46 |
id=self.id, |
| 47 |
) |
| 48 |
self.problems.append(problem) |