crecord: add a showsearch function
authorJordi Gutiérrez Hermoso <jordigh@octave.org>
Wed, 14 Feb 2024 22:48:09 -0500
changeset 51383 880740426a34
parent 51382 ed0b78b2a3aa
child 51384 6fb4d2dfa8e4
crecord: add a showsearch function This function takes a regex and searches either forward or backward, moving the current item to the found item, if any, and unfolding the relevant context.
mercurial/crecord.py
--- a/mercurial/crecord.py	Wed Feb 14 22:46:41 2024 -0500
+++ b/mercurial/crecord.py	Wed Feb 14 22:48:09 2024 -0500
@@ -1889,6 +1889,32 @@
                 return False
         return True
 
+    def showsearch(self, regex, forward=True):
+        if not regex:
+            return
+
+        moveattr = 'nextitem' if forward else 'previtem'
+        currentitem = getattr(self.currentselecteditem, moveattr)(
+            skipfolded=False
+        )
+
+        matches = None
+        regex = re.compile(regex)
+        while currentitem:
+            matches = regex.search(currentitem.content)
+            if matches:
+                self.currentselecteditem = currentitem
+                break
+            currentitem = getattr(currentitem, moveattr)(skipfolded=False)
+
+        # Whatever is selected should now be visible
+        unfolded = self.currentselecteditem
+        while matches and unfolded:
+            unfolded.folded = False
+            unfolded = unfolded.parentitem()
+
+        return matches
+
     def handlekeypressed(self, keypressed, test=False):
         """
         Perform actions based on pressed keys.