mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Merge branch 'main' of https://gitlab.gnome.org/jwestman/blueprint-compiler into formatter
This commit is contained in:
commit
54da7fa6b9
10 changed files with 229 additions and 23 deletions
|
@ -92,6 +92,23 @@ def get_xml(namespace: str, version: str):
|
|||
return _xml_cache[filename]
|
||||
|
||||
|
||||
ONLINE_DOCS = {
|
||||
"Adw-1": "https://gnome.pages.gitlab.gnome.org/libadwaita/doc/1-latest/",
|
||||
"Gdk-4.0": "https://docs.gtk.org/gdk4/",
|
||||
"GdkPixbuf-2.0": "https://docs.gtk.org/gdk-pixbuf/",
|
||||
"Gio-2.0": "https://docs.gtk.org/gio/",
|
||||
"GLib-2.0": "https://docs.gtk.org/glib/",
|
||||
"GModule-2.0": "https://docs.gtk.org/gmodule/",
|
||||
"GObject-2.0": "https://docs.gtk.org/gobject/",
|
||||
"Gsk-4.0": "https://docs.gtk.org/gsk4/",
|
||||
"Gtk-4.0": "https://docs.gtk.org/gtk4/",
|
||||
"GtkSource-5": "https://gnome.pages.gitlab.gnome.org/gtksourceview/gtksourceview5",
|
||||
"Pango-1.0": "https://docs.gtk.org/Pango/",
|
||||
"Shumate-1.0": "https://gnome.pages.gitlab.gnome.org/libshumate/",
|
||||
"WebKit2-4.1": "https://webkitgtk.org/reference/webkit2gtk/stable/",
|
||||
}
|
||||
|
||||
|
||||
class GirType:
|
||||
@property
|
||||
def doc(self) -> T.Optional[str]:
|
||||
|
@ -236,6 +253,8 @@ TNode = T.TypeVar("TNode", bound="GirNode")
|
|||
|
||||
|
||||
class GirNode:
|
||||
xml_tag: str
|
||||
|
||||
def __init__(self, container: T.Optional["GirNode"], tl: typelib.Typelib) -> None:
|
||||
self.container = container
|
||||
self.tl = tl
|
||||
|
@ -252,6 +271,7 @@ class GirNode:
|
|||
def xml(self):
|
||||
for el in self.container.xml.children:
|
||||
if el.attrs.get("name") == self.name:
|
||||
if el.tag == self.xml_tag:
|
||||
return el
|
||||
|
||||
@cached_property
|
||||
|
@ -291,10 +311,17 @@ class GirNode:
|
|||
except:
|
||||
# Not a huge deal, but if you want docs in the language server you
|
||||
# should ensure .gir files are installed
|
||||
pass
|
||||
sections.append("Documentation is not installed")
|
||||
|
||||
if self.online_docs:
|
||||
sections.append(f"[Online documentation]({self.online_docs})")
|
||||
|
||||
return "\n\n---\n\n".join(sections)
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
return None
|
||||
|
||||
@property
|
||||
def signature(self) -> T.Optional[str]:
|
||||
return None
|
||||
|
@ -305,6 +332,8 @@ class GirNode:
|
|||
|
||||
|
||||
class Property(GirNode):
|
||||
xml_tag = "property"
|
||||
|
||||
def __init__(self, klass: T.Union["Class", "Interface"], tl: typelib.Typelib):
|
||||
super().__init__(klass, tl)
|
||||
|
||||
|
@ -318,7 +347,7 @@ class Property(GirNode):
|
|||
|
||||
@cached_property
|
||||
def signature(self):
|
||||
return f"{self.full_name} {self.container.name}.{self.name}"
|
||||
return f"{self.type.full_name} {self.container.name}:{self.name}"
|
||||
|
||||
@property
|
||||
def writable(self) -> bool:
|
||||
|
@ -328,31 +357,80 @@ class Property(GirNode):
|
|||
def construct_only(self) -> bool:
|
||||
return self.tl.PROP_CONSTRUCT_ONLY == 1
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
if ns := self.get_containing(Namespace).online_docs:
|
||||
assert self.container is not None
|
||||
return f"{ns}property.{self.container.name}.{self.name}.html"
|
||||
else:
|
||||
return None
|
||||
|
||||
class Parameter(GirNode):
|
||||
|
||||
class Argument(GirNode):
|
||||
def __init__(self, container: GirNode, tl: typelib.Typelib) -> None:
|
||||
super().__init__(container, tl)
|
||||
|
||||
@cached_property
|
||||
def name(self) -> str:
|
||||
return self.tl.ARG_NAME
|
||||
|
||||
@cached_property
|
||||
def type(self) -> GirType:
|
||||
return self.get_containing(Repository)._resolve_type_id(self.tl.ARG_TYPE)
|
||||
|
||||
|
||||
class Signature(GirNode):
|
||||
def __init__(self, container: GirNode, tl: typelib.Typelib) -> None:
|
||||
super().__init__(container, tl)
|
||||
|
||||
@cached_property
|
||||
def args(self) -> T.List[Argument]:
|
||||
n_arguments = self.tl.SIGNATURE_N_ARGUMENTS
|
||||
blob_size = self.tl.header.HEADER_ARG_BLOB_SIZE
|
||||
result = []
|
||||
for i in range(n_arguments):
|
||||
entry = self.tl.SIGNATURE_ARGUMENTS[i * blob_size]
|
||||
result.append(Argument(self, entry))
|
||||
return result
|
||||
|
||||
@cached_property
|
||||
def return_type(self) -> GirType:
|
||||
return self.get_containing(Repository)._resolve_type_id(
|
||||
self.tl.SIGNATURE_RETURN_TYPE
|
||||
)
|
||||
|
||||
|
||||
class Signal(GirNode):
|
||||
xml_tag = "glib:signal"
|
||||
|
||||
def __init__(
|
||||
self, klass: T.Union["Class", "Interface"], tl: typelib.Typelib
|
||||
) -> None:
|
||||
super().__init__(klass, tl)
|
||||
# if parameters := xml.get_elements('parameters'):
|
||||
# self.params = [Parameter(self, child) for child in parameters[0].get_elements('parameter')]
|
||||
# else:
|
||||
# self.params = []
|
||||
|
||||
@cached_property
|
||||
def gir_signature(self) -> Signature:
|
||||
return Signature(self, self.tl.SIGNAL_SIGNATURE)
|
||||
|
||||
@property
|
||||
def signature(self):
|
||||
# TODO: fix
|
||||
# args = ", ".join([f"{p.type_name} {p.name}" for p in self.params])
|
||||
args = ""
|
||||
return f"signal {self.container.name}.{self.name} ({args})"
|
||||
args = ", ".join(
|
||||
[f"{a.type.full_name} {a.name}" for a in self.gir_signature.args]
|
||||
)
|
||||
return f"signal {self.container.full_name}::{self.name} ({args})"
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
if ns := self.get_containing(Namespace).online_docs:
|
||||
assert self.container is not None
|
||||
return f"{ns}signal.{self.container.name}.{self.name}.html"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class Interface(GirNode, GirType):
|
||||
xml_tag = "interface"
|
||||
|
||||
def __init__(self, ns: "Namespace", tl: typelib.Typelib):
|
||||
super().__init__(ns, tl)
|
||||
|
||||
|
@ -403,8 +481,17 @@ class Interface(GirNode, GirType):
|
|||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
if ns := self.get_containing(Namespace).online_docs:
|
||||
return f"{ns}interface.{self.name}.html"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class Class(GirNode, GirType):
|
||||
xml_tag = "class"
|
||||
|
||||
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
|
||||
super().__init__(ns, tl)
|
||||
|
||||
|
@ -515,6 +602,13 @@ class Class(GirNode, GirType):
|
|||
for impl in self.implements:
|
||||
yield from impl.signals.values()
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
if ns := self.get_containing(Namespace).online_docs:
|
||||
return f"{ns}class.{self.name}.html"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class TemplateType(GirType):
|
||||
def __init__(self, name: str, parent: T.Optional[GirType]):
|
||||
|
@ -571,6 +665,8 @@ class TemplateType(GirType):
|
|||
|
||||
|
||||
class EnumMember(GirNode):
|
||||
xml_tag = "member"
|
||||
|
||||
def __init__(self, enum: "Enumeration", tl: typelib.Typelib) -> None:
|
||||
super().__init__(enum, tl)
|
||||
|
||||
|
@ -596,6 +692,8 @@ class EnumMember(GirNode):
|
|||
|
||||
|
||||
class Enumeration(GirNode, GirType):
|
||||
xml_tag = "enumeration"
|
||||
|
||||
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
|
||||
super().__init__(ns, tl)
|
||||
|
||||
|
@ -617,8 +715,17 @@ class Enumeration(GirNode, GirType):
|
|||
def assignable_to(self, type: GirType) -> bool:
|
||||
return type == self
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
if ns := self.get_containing(Namespace).online_docs:
|
||||
return f"{ns}enum.{self.name}.html"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class Boxed(GirNode, GirType):
|
||||
xml_tag = "glib:boxed"
|
||||
|
||||
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
|
||||
super().__init__(ns, tl)
|
||||
|
||||
|
@ -629,8 +736,17 @@ class Boxed(GirNode, GirType):
|
|||
def assignable_to(self, type) -> bool:
|
||||
return type == self
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
if ns := self.get_containing(Namespace).online_docs:
|
||||
return f"{ns}boxed.{self.name}.html"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class Bitfield(Enumeration):
|
||||
xml_tag = "bitfield"
|
||||
|
||||
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
|
||||
super().__init__(ns, tl)
|
||||
|
||||
|
@ -724,6 +840,10 @@ class Namespace(GirNode):
|
|||
else:
|
||||
return self.get_type(type_name)
|
||||
|
||||
@property
|
||||
def online_docs(self) -> T.Optional[str]:
|
||||
return ONLINE_DOCS.get(f"{self.name}-{self.version}")
|
||||
|
||||
|
||||
class Repository(GirNode):
|
||||
def __init__(self, tl: typelib.Typelib) -> None:
|
||||
|
|
|
@ -24,12 +24,26 @@ from .values import Value
|
|||
|
||||
|
||||
class AdwBreakpointCondition(AstNode):
|
||||
grammar = ["condition", "(", UseQuoted("condition"), Match(")").expected()]
|
||||
grammar = [
|
||||
UseExact("kw", "condition"),
|
||||
"(",
|
||||
UseQuoted("condition"),
|
||||
Match(")").expected(),
|
||||
]
|
||||
|
||||
@property
|
||||
def condition(self) -> str:
|
||||
return self.tokens["condition"]
|
||||
|
||||
@docs("kw")
|
||||
def keyword_docs(self):
|
||||
klass = self.root.gir.get_type("Breakpoint", "Adw")
|
||||
if klass is None:
|
||||
return None
|
||||
prop = klass.properties.get("condition")
|
||||
assert isinstance(prop, gir.Property)
|
||||
return prop.doc
|
||||
|
||||
@validate()
|
||||
def unique(self):
|
||||
self.validate_unique_in_parent("Duplicate condition statement")
|
||||
|
@ -68,9 +82,16 @@ class AdwBreakpointSetter(AstNode):
|
|||
return None
|
||||
|
||||
@property
|
||||
def gir_property(self):
|
||||
if self.gir_class is not None and not isinstance(self.gir_class, ExternType):
|
||||
def gir_property(self) -> T.Optional[gir.Property]:
|
||||
if (
|
||||
self.gir_class is not None
|
||||
and not isinstance(self.gir_class, ExternType)
|
||||
and self.property_name is not None
|
||||
):
|
||||
assert isinstance(self.gir_class, gir.Class)
|
||||
return self.gir_class.properties.get(self.property_name)
|
||||
else:
|
||||
return None
|
||||
|
||||
@context(ValueTypeCtx)
|
||||
def value_type(self) -> ValueTypeCtx:
|
||||
|
@ -81,6 +102,20 @@ class AdwBreakpointSetter(AstNode):
|
|||
|
||||
return ValueTypeCtx(type, allow_null=True)
|
||||
|
||||
@docs("object")
|
||||
def object_docs(self):
|
||||
if self.object is not None:
|
||||
return f"```\n{self.object.signature}\n```"
|
||||
else:
|
||||
return None
|
||||
|
||||
@docs("property")
|
||||
def property_docs(self):
|
||||
if self.gir_property is not None:
|
||||
return self.gir_property.doc
|
||||
else:
|
||||
return None
|
||||
|
||||
@validate("object")
|
||||
def object_exists(self):
|
||||
if self.object is None:
|
||||
|
|
|
@ -55,6 +55,13 @@ class Object(AstNode):
|
|||
def content(self) -> ObjectContent:
|
||||
return self.children[ObjectContent][0]
|
||||
|
||||
@property
|
||||
def signature(self) -> str:
|
||||
if self.id:
|
||||
return f"{self.class_name.gir_type.full_name} {self.id}"
|
||||
else:
|
||||
return f"{self.class_name.gir_type.full_name}"
|
||||
|
||||
@property
|
||||
def gir_class(self) -> GirType:
|
||||
if self.class_name is None:
|
||||
|
|
|
@ -98,6 +98,7 @@ def get_types(gir):
|
|||
|
||||
|
||||
def _get_docs(gir, name):
|
||||
name = name.replace("-", "_")
|
||||
if gir_type := (
|
||||
gir.get_type("AccessibleProperty", "Gtk").members.get(name)
|
||||
or gir.get_type("AccessibleRelation", "Gtk").members.get(name)
|
||||
|
@ -197,7 +198,9 @@ def a11y_completer(ast_node, match_variables):
|
|||
def a11y_name_completer(ast_node, match_variables):
|
||||
for name, type in get_types(ast_node.root.gir).items():
|
||||
yield Completion(
|
||||
name, CompletionItemKind.Property, docs=_get_docs(ast_node.root.gir, type)
|
||||
name,
|
||||
CompletionItemKind.Property,
|
||||
docs=_get_docs(ast_node.root.gir, type.name),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -9,6 +9,14 @@ from .types import TypeName
|
|||
class ExtListItemFactory(AstNode):
|
||||
grammar = [UseExact("id", "template"), Optional(TypeName), ObjectContent]
|
||||
|
||||
@property
|
||||
def id(self) -> str:
|
||||
return "template"
|
||||
|
||||
@property
|
||||
def signature(self) -> str:
|
||||
return f"template {self.gir_class.full_name}"
|
||||
|
||||
@property
|
||||
def type_name(self) -> T.Optional[TypeName]:
|
||||
if len(self.children[TypeName]) == 1:
|
||||
|
|
|
@ -35,6 +35,13 @@ class Menu(AstNode):
|
|||
def id(self) -> str:
|
||||
return self.tokens["id"]
|
||||
|
||||
@property
|
||||
def signature(self) -> str:
|
||||
if self.id:
|
||||
return f"Gio.Menu {self.id}"
|
||||
else:
|
||||
return "Gio.Menu"
|
||||
|
||||
@property
|
||||
def tag(self) -> str:
|
||||
return self.tokens["tag"]
|
||||
|
|
|
@ -44,6 +44,13 @@ class Template(Object):
|
|||
def id(self) -> str:
|
||||
return "template"
|
||||
|
||||
@property
|
||||
def signature(self) -> str:
|
||||
if self.parent_type:
|
||||
return f"template {self.gir_class.full_name} : {self.parent_type.gir_type.full_name}"
|
||||
else:
|
||||
return f"template {self.gir_class.full_name}"
|
||||
|
||||
@property
|
||||
def gir_class(self) -> GirType:
|
||||
if isinstance(self.class_name.gir_type, ExternType):
|
||||
|
|
|
@ -312,14 +312,20 @@ class IdentLiteral(AstNode):
|
|||
|
||||
@docs()
|
||||
def docs(self) -> T.Optional[str]:
|
||||
type = self.context[ValueTypeCtx].value_type
|
||||
if isinstance(type, gir.Enumeration):
|
||||
if member := type.members.get(self.ident):
|
||||
expected_type = self.context[ValueTypeCtx].value_type
|
||||
if isinstance(expected_type, gir.BoolType):
|
||||
return None
|
||||
elif isinstance(expected_type, gir.Enumeration):
|
||||
if member := expected_type.members.get(self.ident):
|
||||
return member.doc
|
||||
else:
|
||||
return type.doc
|
||||
elif isinstance(type, gir.GirNode):
|
||||
return type.doc
|
||||
return expected_type.doc
|
||||
elif self.ident == "null" and self.context[ValueTypeCtx].allow_null:
|
||||
return None
|
||||
elif object := self.context[ScopeCtx].objects.get(self.ident):
|
||||
return f"```\n{object.signature}\n```"
|
||||
elif self.root.is_legacy_template(self.ident):
|
||||
return f"```\n{self.root.template.signature}\n```"
|
||||
else:
|
||||
return None
|
||||
|
||||
|
|
|
@ -118,6 +118,7 @@ class Typelib:
|
|||
HEADER_FUNCTION_BLOB_SIZE = Field(0x3E, "u16")
|
||||
HEADER_CALLBACK_BLOB_SIZE = Field(0x40, "u16")
|
||||
HEADER_SIGNAL_BLOB_SIZE = Field(0x42, "u16")
|
||||
HEADER_ARG_BLOB_SIZE = Field(0x46, "u16")
|
||||
HEADER_PROPERTY_BLOB_SIZE = Field(0x48, "u16")
|
||||
HEADER_FIELD_BLOB_SIZE = Field(0x4A, "u16")
|
||||
HEADER_VALUE_BLOB_SIZE = Field(0x4C, "u16")
|
||||
|
@ -132,6 +133,13 @@ class Typelib:
|
|||
DIR_ENTRY_OFFSET = Field(0x8, "pointer")
|
||||
DIR_ENTRY_NAMESPACE = Field(0x8, "string")
|
||||
|
||||
ARG_NAME = Field(0x0, "string")
|
||||
ARG_TYPE = Field(0xC, "u32")
|
||||
|
||||
SIGNATURE_RETURN_TYPE = Field(0x0, "u32")
|
||||
SIGNATURE_N_ARGUMENTS = Field(0x6, "u16")
|
||||
SIGNATURE_ARGUMENTS = Field(0x8, "offset")
|
||||
|
||||
ATTR_OFFSET = Field(0x0, "u32")
|
||||
ATTR_NAME = Field(0x0, "string")
|
||||
ATTR_VALUE = Field(0x0, "string")
|
||||
|
@ -180,6 +188,11 @@ class Typelib:
|
|||
PROP_CONSTRUCT_ONLY = Field(0x4, "u32", 4, 1)
|
||||
PROP_TYPE = Field(0xC, "u32")
|
||||
|
||||
SIGNAL_DEPRECATED = Field(0x0, "u16", 0, 1)
|
||||
SIGNAL_DETAILED = Field(0x0, "u16", 5, 1)
|
||||
SIGNAL_NAME = Field(0x4, "string")
|
||||
SIGNAL_SIGNATURE = Field(0xC, "pointer")
|
||||
|
||||
VALUE_NAME = Field(0x4, "string")
|
||||
VALUE_VALUE = Field(0x8, "i32")
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ FROM fedora:latest
|
|||
|
||||
RUN dnf install -y meson gcc g++ python3-pip gobject-introspection-devel \
|
||||
python3-devel python3-gobject git diffutils xorg-x11-server-Xvfb \
|
||||
appstream-devel "dnf-command(builddep)"
|
||||
appstream-devel dbus-x11 "dnf-command(builddep)"
|
||||
RUN dnf build-dep -y gtk4 libadwaita
|
||||
RUN pip3 install furo mypy sphinx coverage black isort
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue