[gnome-builder/gnome-builder-3-18] python: add rank selector for exact tail matches



commit 8eeb2a0a128fddcb9feb79b8c0b1d648bb1186ed
Author: Christian Hergert <christian hergert me>
Date:   Sat Oct 3 14:36:59 2015 -0700

    python: add rank selector for exact tail matches
    
    This let's you discover you perform something like the following for
    function arguments (not totally correct yet, we need more rankings).
    
    discoveries.select_tail(Rank.FUNCTION, Rank.TUPLE)

 plugins/python-pack/python_indenter.py |   33 ++++++++++++++++++++++++++++++++
 1 files changed, 33 insertions(+), 0 deletions(-)
---
diff --git a/plugins/python-pack/python_indenter.py b/plugins/python-pack/python_indenter.py
index 33e255a..dfc2580 100644
--- a/plugins/python-pack/python_indenter.py
+++ b/plugins/python-pack/python_indenter.py
@@ -116,6 +116,39 @@ class Discoveries:
             return result
         return None
 
+    def select_tail(self, *ranks):
+        """
+        Does a selection on the discoveries, but walks backward
+        from the most recent to the most distant. The checks
+        are applied in an order that appears as top-down.
+        Therefore, if you want to find a tuple that is immediately
+        after a function declaration, you would use a ranking search
+        of (Rank.FUNCTION, Rank.TUPLE).
+
+        You may provide a mask instead of an exact rank.
+
+        >>> self.select_tail(Rank.FUNCTION, Rank.TUPLE)
+        """
+        self._run()
+
+        if len(ranks) == 0 or len(ranks) > len(self.discoveries):
+            return False
+
+        selection = []
+
+        node = self.discoveries[0]
+        ranks = list(ranks)
+
+        while len(ranks) and node:
+            rank = ranks.pop()
+            if node.rank & rank == 0:
+                return False
+            selection.append(node)
+            node = self._find_parent(node)
+
+        selection.reverse()
+        return selection
+
     @property
     def nearest(self):
         self._run()


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