From 0c5369cd9d2492f7da8ed47f0879f730c92b3378 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 12 May 2019 10:59:38 +0000 Subject: [PATCH] Fix drawing of non-top-level anchors --- cheuph/element_supply.py | 53 +++++++++++++++++----------------------- cheuph/tree_display.py | 5 ++-- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/cheuph/element_supply.py b/cheuph/element_supply.py index 885a969..9f5ca4e 100644 --- a/cheuph/element_supply.py +++ b/cheuph/element_supply.py @@ -69,14 +69,6 @@ class ElementSupply(abc.ABC): return children - # There is not a clear-cut way to get the previous or next "sibling" of an - # element that is itself a child of the implicit root (None), since - # get_children() doesn't accept None in its element_id argument. - # - # Because of this, the get_previous_id() and get_next_id() functions are - # abstract (until I decide to change the signature of get_children(), that - # is :P). - def get_previous_id(self, element_id: Id) -> Optional[Id]: """ Get the id of an element's previous sibling (i. e. the sibling just @@ -139,28 +131,29 @@ class ElementSupply(abc.ABC): else: return None -# def get_furthest_ancestor_id(self, -# element_id: Id, -# root_id: Optional[Id] = None, -# ) -> Id: -# current_id = element_id -# -# while True: -# parent_id = self.get_parent_id(current_id) -# -# if parent_id == root_id: -# return current_id -# elif parent_id is None: -# raise TreeException("Reached implicit root before hitting specified root") -# -# current_id = parent_id -# -# def get_furthest_ancestor(self, -# element_id: Id, -# root_id: Optional[Id] = None, -# ) -> Element: -# return self.get(self.get_furthest_ancestor_id(element_id, -# root_id=root_id)) + def get_furthest_ancestor_id(self, + element_id: Id, + root_id: Optional[Id] = None, + ) -> Id: + current_id = element_id + + while True: + parent_id = self.get_parent_id(current_id) + + if parent_id == root_id: + return current_id + elif parent_id is None: + raise TreeException( + "Reached implicit root before hitting specified root") + + current_id = parent_id + + def get_furthest_ancestor(self, + element_id: Id, + root_id: Optional[Id] = None, + ) -> Element: + return self.get(self.get_furthest_ancestor_id(element_id, + root_id=root_id)) class MemoryElementSupply(ElementSupply): """ diff --git a/cheuph/tree_display.py b/cheuph/tree_display.py index a51bd6e..d75f923 100644 --- a/cheuph/tree_display.py +++ b/cheuph/tree_display.py @@ -195,9 +195,10 @@ class TreeDisplay: self._rendered = None return - anchor_tree = self._render_tree(self.anchor_id) + ancestor_id = self._supply.get_furthest_ancestor_id(self.anchor_id) + ancestor_tree = self._render_tree(ancestor_id) - self._rendered = TreeList(anchor_tree, self.anchor_id) + self._rendered = TreeList(ancestor_tree, self.anchor_id) self._rendered.offset_by(self.anchor_offset) self._fill_screen_upwards()