This commit is contained in:
gregorni 2023-07-20 18:39:42 +02:00
commit 54da7fa6b9
10 changed files with 229 additions and 23 deletions

View file

@ -92,6 +92,23 @@ def get_xml(namespace: str, version: str):
return _xml_cache[filename] 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: class GirType:
@property @property
def doc(self) -> T.Optional[str]: def doc(self) -> T.Optional[str]:
@ -236,6 +253,8 @@ TNode = T.TypeVar("TNode", bound="GirNode")
class GirNode: class GirNode:
xml_tag: str
def __init__(self, container: T.Optional["GirNode"], tl: typelib.Typelib) -> None: def __init__(self, container: T.Optional["GirNode"], tl: typelib.Typelib) -> None:
self.container = container self.container = container
self.tl = tl self.tl = tl
@ -252,7 +271,8 @@ class GirNode:
def xml(self): def xml(self):
for el in self.container.xml.children: for el in self.container.xml.children:
if el.attrs.get("name") == self.name: if el.attrs.get("name") == self.name:
return el if el.tag == self.xml_tag:
return el
@cached_property @cached_property
def glib_type_name(self) -> str: def glib_type_name(self) -> str:
@ -291,10 +311,17 @@ class GirNode:
except: except:
# Not a huge deal, but if you want docs in the language server you # Not a huge deal, but if you want docs in the language server you
# should ensure .gir files are installed # 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) return "\n\n---\n\n".join(sections)
@property
def online_docs(self) -> T.Optional[str]:
return None
@property @property
def signature(self) -> T.Optional[str]: def signature(self) -> T.Optional[str]:
return None return None
@ -305,6 +332,8 @@ class GirNode:
class Property(GirNode): class Property(GirNode):
xml_tag = "property"
def __init__(self, klass: T.Union["Class", "Interface"], tl: typelib.Typelib): def __init__(self, klass: T.Union["Class", "Interface"], tl: typelib.Typelib):
super().__init__(klass, tl) super().__init__(klass, tl)
@ -318,7 +347,7 @@ class Property(GirNode):
@cached_property @cached_property
def signature(self): 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 @property
def writable(self) -> bool: def writable(self) -> bool:
@ -328,31 +357,80 @@ class Property(GirNode):
def construct_only(self) -> bool: def construct_only(self) -> bool:
return self.tl.PROP_CONSTRUCT_ONLY == 1 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: def __init__(self, container: GirNode, tl: typelib.Typelib) -> None:
super().__init__(container, tl) 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): class Signal(GirNode):
xml_tag = "glib:signal"
def __init__( def __init__(
self, klass: T.Union["Class", "Interface"], tl: typelib.Typelib self, klass: T.Union["Class", "Interface"], tl: typelib.Typelib
) -> None: ) -> None:
super().__init__(klass, tl) super().__init__(klass, tl)
# if parameters := xml.get_elements('parameters'):
# self.params = [Parameter(self, child) for child in parameters[0].get_elements('parameter')] @cached_property
# else: def gir_signature(self) -> Signature:
# self.params = [] return Signature(self, self.tl.SIGNAL_SIGNATURE)
@property @property
def signature(self): def signature(self):
# TODO: fix args = ", ".join(
# args = ", ".join([f"{p.type_name} {p.name}" for p in self.params]) [f"{a.type.full_name} {a.name}" for a in self.gir_signature.args]
args = "" )
return f"signal {self.container.name}.{self.name} ({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): class Interface(GirNode, GirType):
xml_tag = "interface"
def __init__(self, ns: "Namespace", tl: typelib.Typelib): def __init__(self, ns: "Namespace", tl: typelib.Typelib):
super().__init__(ns, tl) super().__init__(ns, tl)
@ -403,8 +481,17 @@ class Interface(GirNode, GirType):
return True return True
return False 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): class Class(GirNode, GirType):
xml_tag = "class"
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None: def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
super().__init__(ns, tl) super().__init__(ns, tl)
@ -515,6 +602,13 @@ class Class(GirNode, GirType):
for impl in self.implements: for impl in self.implements:
yield from impl.signals.values() 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): class TemplateType(GirType):
def __init__(self, name: str, parent: T.Optional[GirType]): def __init__(self, name: str, parent: T.Optional[GirType]):
@ -571,6 +665,8 @@ class TemplateType(GirType):
class EnumMember(GirNode): class EnumMember(GirNode):
xml_tag = "member"
def __init__(self, enum: "Enumeration", tl: typelib.Typelib) -> None: def __init__(self, enum: "Enumeration", tl: typelib.Typelib) -> None:
super().__init__(enum, tl) super().__init__(enum, tl)
@ -596,6 +692,8 @@ class EnumMember(GirNode):
class Enumeration(GirNode, GirType): class Enumeration(GirNode, GirType):
xml_tag = "enumeration"
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None: def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
super().__init__(ns, tl) super().__init__(ns, tl)
@ -617,8 +715,17 @@ class Enumeration(GirNode, GirType):
def assignable_to(self, type: GirType) -> bool: def assignable_to(self, type: GirType) -> bool:
return type == self 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): class Boxed(GirNode, GirType):
xml_tag = "glib:boxed"
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None: def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
super().__init__(ns, tl) super().__init__(ns, tl)
@ -629,8 +736,17 @@ class Boxed(GirNode, GirType):
def assignable_to(self, type) -> bool: def assignable_to(self, type) -> bool:
return type == self 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): class Bitfield(Enumeration):
xml_tag = "bitfield"
def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None: def __init__(self, ns: "Namespace", tl: typelib.Typelib) -> None:
super().__init__(ns, tl) super().__init__(ns, tl)
@ -724,6 +840,10 @@ class Namespace(GirNode):
else: else:
return self.get_type(type_name) 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): class Repository(GirNode):
def __init__(self, tl: typelib.Typelib) -> None: def __init__(self, tl: typelib.Typelib) -> None:

View file

@ -24,12 +24,26 @@ from .values import Value
class AdwBreakpointCondition(AstNode): class AdwBreakpointCondition(AstNode):
grammar = ["condition", "(", UseQuoted("condition"), Match(")").expected()] grammar = [
UseExact("kw", "condition"),
"(",
UseQuoted("condition"),
Match(")").expected(),
]
@property @property
def condition(self) -> str: def condition(self) -> str:
return self.tokens["condition"] 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() @validate()
def unique(self): def unique(self):
self.validate_unique_in_parent("Duplicate condition statement") self.validate_unique_in_parent("Duplicate condition statement")
@ -68,9 +82,16 @@ class AdwBreakpointSetter(AstNode):
return None return None
@property @property
def gir_property(self): def gir_property(self) -> T.Optional[gir.Property]:
if self.gir_class is not None and not isinstance(self.gir_class, ExternType): 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) return self.gir_class.properties.get(self.property_name)
else:
return None
@context(ValueTypeCtx) @context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx: def value_type(self) -> ValueTypeCtx:
@ -81,6 +102,20 @@ class AdwBreakpointSetter(AstNode):
return ValueTypeCtx(type, allow_null=True) 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") @validate("object")
def object_exists(self): def object_exists(self):
if self.object is None: if self.object is None:

View file

@ -55,6 +55,13 @@ class Object(AstNode):
def content(self) -> ObjectContent: def content(self) -> ObjectContent:
return self.children[ObjectContent][0] 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 @property
def gir_class(self) -> GirType: def gir_class(self) -> GirType:
if self.class_name is None: if self.class_name is None:

View file

@ -98,6 +98,7 @@ def get_types(gir):
def _get_docs(gir, name): def _get_docs(gir, name):
name = name.replace("-", "_")
if gir_type := ( if gir_type := (
gir.get_type("AccessibleProperty", "Gtk").members.get(name) gir.get_type("AccessibleProperty", "Gtk").members.get(name)
or gir.get_type("AccessibleRelation", "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): def a11y_name_completer(ast_node, match_variables):
for name, type in get_types(ast_node.root.gir).items(): for name, type in get_types(ast_node.root.gir).items():
yield Completion( 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),
) )

View file

@ -9,6 +9,14 @@ from .types import TypeName
class ExtListItemFactory(AstNode): class ExtListItemFactory(AstNode):
grammar = [UseExact("id", "template"), Optional(TypeName), ObjectContent] 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 @property
def type_name(self) -> T.Optional[TypeName]: def type_name(self) -> T.Optional[TypeName]:
if len(self.children[TypeName]) == 1: if len(self.children[TypeName]) == 1:

View file

@ -35,6 +35,13 @@ class Menu(AstNode):
def id(self) -> str: def id(self) -> str:
return self.tokens["id"] return self.tokens["id"]
@property
def signature(self) -> str:
if self.id:
return f"Gio.Menu {self.id}"
else:
return "Gio.Menu"
@property @property
def tag(self) -> str: def tag(self) -> str:
return self.tokens["tag"] return self.tokens["tag"]

View file

@ -44,6 +44,13 @@ class Template(Object):
def id(self) -> str: def id(self) -> str:
return "template" 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 @property
def gir_class(self) -> GirType: def gir_class(self) -> GirType:
if isinstance(self.class_name.gir_type, ExternType): if isinstance(self.class_name.gir_type, ExternType):

View file

@ -312,14 +312,20 @@ class IdentLiteral(AstNode):
@docs() @docs()
def docs(self) -> T.Optional[str]: def docs(self) -> T.Optional[str]:
type = self.context[ValueTypeCtx].value_type expected_type = self.context[ValueTypeCtx].value_type
if isinstance(type, gir.Enumeration): if isinstance(expected_type, gir.BoolType):
if member := type.members.get(self.ident): return None
elif isinstance(expected_type, gir.Enumeration):
if member := expected_type.members.get(self.ident):
return member.doc return member.doc
else: else:
return type.doc return expected_type.doc
elif isinstance(type, gir.GirNode): elif self.ident == "null" and self.context[ValueTypeCtx].allow_null:
return type.doc 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: else:
return None return None

View file

@ -118,6 +118,7 @@ class Typelib:
HEADER_FUNCTION_BLOB_SIZE = Field(0x3E, "u16") HEADER_FUNCTION_BLOB_SIZE = Field(0x3E, "u16")
HEADER_CALLBACK_BLOB_SIZE = Field(0x40, "u16") HEADER_CALLBACK_BLOB_SIZE = Field(0x40, "u16")
HEADER_SIGNAL_BLOB_SIZE = Field(0x42, "u16") HEADER_SIGNAL_BLOB_SIZE = Field(0x42, "u16")
HEADER_ARG_BLOB_SIZE = Field(0x46, "u16")
HEADER_PROPERTY_BLOB_SIZE = Field(0x48, "u16") HEADER_PROPERTY_BLOB_SIZE = Field(0x48, "u16")
HEADER_FIELD_BLOB_SIZE = Field(0x4A, "u16") HEADER_FIELD_BLOB_SIZE = Field(0x4A, "u16")
HEADER_VALUE_BLOB_SIZE = Field(0x4C, "u16") HEADER_VALUE_BLOB_SIZE = Field(0x4C, "u16")
@ -132,6 +133,13 @@ class Typelib:
DIR_ENTRY_OFFSET = Field(0x8, "pointer") DIR_ENTRY_OFFSET = Field(0x8, "pointer")
DIR_ENTRY_NAMESPACE = Field(0x8, "string") 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_OFFSET = Field(0x0, "u32")
ATTR_NAME = Field(0x0, "string") ATTR_NAME = Field(0x0, "string")
ATTR_VALUE = Field(0x0, "string") ATTR_VALUE = Field(0x0, "string")
@ -180,6 +188,11 @@ class Typelib:
PROP_CONSTRUCT_ONLY = Field(0x4, "u32", 4, 1) PROP_CONSTRUCT_ONLY = Field(0x4, "u32", 4, 1)
PROP_TYPE = Field(0xC, "u32") 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_NAME = Field(0x4, "string")
VALUE_VALUE = Field(0x8, "i32") VALUE_VALUE = Field(0x8, "i32")

View file

@ -2,7 +2,7 @@ FROM fedora:latest
RUN dnf install -y meson gcc g++ python3-pip gobject-introspection-devel \ RUN dnf install -y meson gcc g++ python3-pip gobject-introspection-devel \
python3-devel python3-gobject git diffutils xorg-x11-server-Xvfb \ 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 dnf build-dep -y gtk4 libadwaita
RUN pip3 install furo mypy sphinx coverage black isort RUN pip3 install furo mypy sphinx coverage black isort