[gi-docgen/class-hierarchy: 2/2] Fix the types hierarchy generation




commit 30e0df5387ded4ac51f5a324abaf8a736c85bd09
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Mon May 24 19:20:16 2021 +0100

    Fix the types hierarchy generation
    
    Use the ancestors list of each class to build up the types hierarchy, to
    avoid stopping midway through the hierarchy of a type that inherits from
    a type in a different namespace.
    
    This allows us to drop the intermediate `GInitiallyUnowned` hierarchy,
    as now `GObject.InitiallyUnowned` is a recognised type.
    
    Fixes: #48

 gidocgen/gdgenerate.py | 21 +++++++--------------
 gidocgen/gir/ast.py    | 32 +++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 19 deletions(-)
---
diff --git a/gidocgen/gdgenerate.py b/gidocgen/gdgenerate.py
index 306d78e..8b78259 100644
--- a/gidocgen/gdgenerate.py
+++ b/gidocgen/gdgenerate.py
@@ -2318,9 +2318,6 @@ def gen_types_hierarchy(config, theme_config, output_dir, jinja_env, repository)
     # All GObject sub-types
     objects_tree = repository.get_class_hierarchy(root="GObject.Object")
 
-    # All GInitiallyUnowned sub-types
-    unowned_tree = repository.get_class_hierarchy(root="GObject.InitiallyUnowned")
-
     # All GTypeInstance sub-types
     typed_tree = repository.get_class_hierarchy()
 
@@ -2328,9 +2325,12 @@ def gen_types_hierarchy(config, theme_config, output_dir, jinja_env, repository)
 
     def dump_tree(node, out):
         for k in node:
-            out.append(f"<li class=\"type\"><a href=\"class.{k}.html\"><code>{k}</code></a>")
+            if '.' in k:
+                out.append(f'<li class="type"><code>{k}</code>')
+            else:
+                out.append(f'<li class="type"><a href="class.{k}.html"><code>{k}</code></a>')
             if len(node[k]) != 0:
-                out.append("<ul class=\"type\">")
+                out.append('<ul class="type">')
                 dump_tree(node[k], out)
                 out.append("</ul>")
             out.append("</li>")
@@ -2344,19 +2344,12 @@ def gen_types_hierarchy(config, theme_config, output_dir, jinja_env, repository)
         res += ["</ul>"]
         res += ["</div>"]
 
-    if len(unowned_tree) != 0:
-        res += ["<div class=\"docblock\">"]
-        res += ["<ul class=\"type root\">"]
-        res += [" <li class=\"type\"><code>GInitiallyUnowned</code></li><ul class=\"type\">"]
-        dump_tree(unowned_tree, res)
-        res += [" </ul></li>"]
-        res += ["</ul>"]
-        res += ["</div>"]
-
     if len(typed_tree) != 0:
         res += ["<div class=\"docblock\">"]
         res += ["<ul class=\"type root\">"]
+        res += [" <li class=\"type\"><code>GTypeInstance</code></li><ul class=\"type\">"]
         dump_tree(typed_tree, res)
+        res += [" </ul></li>"]
         res += ["</ul>"]
         res += ["</div>"]
 
diff --git a/gidocgen/gir/ast.py b/gidocgen/gir/ast.py
index 6c499e1..e911477 100644
--- a/gidocgen/gir/ast.py
+++ b/gidocgen/gir/ast.py
@@ -1110,13 +1110,35 @@ class Repository:
 
     def get_class_hierarchy(self, root=None):
         flat_tree = []
+        seen_types = {}
+
+        def window(iterable, size=2):
+            i = iter(iterable)
+            win = []
+            for e in range(0, size):
+                win.append(next(i))
+            yield win
+            for e in i:
+                win = win[1:] + [e]
+                yield win
+
         for cls in self.namespace.get_classes():
-            name = cls.name
-            if cls.parent is not None:
-                parent = cls.parent.name
+            if cls.parent is None:
+                flat_tree.append((cls.name, None))
+                continue
+
+            if len(cls.ancestors) < 2:
+                flat_tree.append((cls.name, cls.ancestors[0].name))
             else:
-                parent = None
-            flat_tree.append((name, parent))
+                flat_tree.append((cls.name, cls.ancestors[0].name))
+                for chunk in window(cls.ancestors, size=2):
+                    if chunk[0].name in seen_types:
+                        continue
+                    if len(chunk) == 2:
+                        flat_tree.append((chunk[0].name, chunk[1].name))
+                    else:
+                        flat_tree.append((chunk[0].name, None))
+                    seen_types[chunk[0].name] = 1
 
         def subtree(cls, rel):
             return {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]