Patchwork D11643: chistedit: move movecursor() onto state class

login
register
mail settings
Submitter phabricator
Date Oct. 12, 2021, 5:07 p.m.
Message ID <differential-rev-PHID-DREV-gnwbol3ohm2ahpqmxayj-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49967/
State Superseded
Headers show

Comments

phabricator - Oct. 12, 2021, 5:07 p.m.
martinvonz created this revision.
Herald added a reviewer: durin42.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D11643

AFFECTED FILES
  hgext/histedit.py

CHANGE DETAILS




To: martinvonz, durin42, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -1194,26 +1194,6 @@ 
 
 
 # ============ EVENTS ===============
-def movecursor(state, oldpos, newpos):
-    """Change the rule/changeset that the cursor is pointing to, regardless of
-    current mode (you can switch between patches from the view patch window)."""
-    state.pos = newpos
-
-    mode, _ = state.mode
-    if mode == MODE_RULES:
-        # Scroll through the list by updating the view for MODE_RULES, so that
-        # even if we are not currently viewing the rules, switching back will
-        # result in the cursor's rule being visible.
-        modestate = state.modes[MODE_RULES]
-        if newpos < modestate[b'line_offset']:
-            modestate[b'line_offset'] = newpos
-        elif newpos > modestate[b'line_offset'] + state.page_height - 1:
-            modestate[b'line_offset'] = newpos - state.page_height + 1
-
-    # Reset the patch view region to the top of the new patch.
-    state.modes[MODE_PATCH][b'line_offset'] = 0
-
-
 def changemode(state, mode):
     curmode, _ = state.mode
     state.mode = (mode, curmode)
@@ -1538,12 +1518,12 @@ 
             return
         if action in (b'down', b'move-down'):
             newpos = min(oldpos + 1, len(rules) - 1)
-            movecursor(self, oldpos, newpos)
+            self.move_cursor(oldpos, newpos)
             if selected is not None or action == b'move-down':
                 swap(self, oldpos, newpos)
         elif action in (b'up', b'move-up'):
             newpos = max(0, oldpos - 1)
-            movecursor(self, oldpos, newpos)
+            self.move_cursor(oldpos, newpos)
             if selected is not None or action == b'move-up':
                 swap(self, oldpos, newpos)
         elif action == b'next-action':
@@ -1555,7 +1535,7 @@ 
             makeselection(self, selected)
         elif action == b'goto' and int(ch) < len(rules) and len(rules) <= 10:
             newrule = next((r for r in rules if r.origpos == int(ch)))
-            movecursor(self, oldpos, newrule.pos)
+            self.move_cursor(oldpos, newrule.pos)
             if selected is not None:
                 swap(self, oldpos, newrule.pos)
         elif action.startswith(b'action-'):
@@ -1592,6 +1572,25 @@ 
             displayer.close()
         return displayer.hunk[rule.ctx.rev()].splitlines()
 
+    def move_cursor(self, oldpos, newpos):
+        """Change the rule/changeset that the cursor is pointing to, regardless of
+        current mode (you can switch between patches from the view patch window)."""
+        self.pos = newpos
+
+        mode, _ = self.mode
+        if mode == MODE_RULES:
+            # Scroll through the list by updating the view for MODE_RULES, so that
+            # even if we are not currently viewing the rules, switching back will
+            # result in the cursor's rule being visible.
+            modestate = self.modes[MODE_RULES]
+            if newpos < modestate[b'line_offset']:
+                modestate[b'line_offset'] = newpos
+            elif newpos > modestate[b'line_offset'] + self.page_height - 1:
+                modestate[b'line_offset'] = newpos - self.page_height + 1
+
+        # Reset the patch view region to the top of the new patch.
+        self.modes[MODE_PATCH][b'line_offset'] = 0
+
 
 def _chisteditmain(repo, rules, stdscr):
     try:
@@ -1652,7 +1651,7 @@ 
             if curmode != oldmode:
                 state.page_height = sizes[b'main'][0]
                 # Adjust the view to fit the current screen size.
-                movecursor(state, state.pos, state.pos)
+                state.move_cursor(state.pos, state.pos)
 
             # Pack the windows against the top, each pane spread across the
             # full width of the screen.