chistedit: move swap() onto state class
authorMartin von Zweigbergk <martinvonz@google.com>
Tue, 12 Oct 2021 09:20:46 -0700
changeset 48210 0444956ecbcf
parent 48209 0f498e03b016
child 48211 141aafac446c
chistedit: move swap() onto state class Differential Revision: https://phab.mercurial-scm.org/D11646
hgext/histedit.py
--- a/hgext/histedit.py	Tue Oct 12 09:17:59 2021 -0700
+++ b/hgext/histedit.py	Tue Oct 12 09:20:46 2021 -0700
@@ -1194,29 +1194,6 @@
 
 
 # ============ EVENTS ===============
-def swap(state, oldpos, newpos):
-    """Swap two positions and calculate necessary conflicts in
-    O(|newpos-oldpos|) time"""
-
-    rules = state.rules
-    assert 0 <= oldpos < len(rules) and 0 <= newpos < len(rules)
-
-    rules[oldpos], rules[newpos] = rules[newpos], rules[oldpos]
-
-    # TODO: swap should not know about histeditrule's internals
-    rules[newpos].pos = newpos
-    rules[oldpos].pos = oldpos
-
-    start = min(oldpos, newpos)
-    end = max(oldpos, newpos)
-    for r in pycompat.xrange(start, end + 1):
-        rules[newpos].checkconflicts(rules[r])
-        rules[oldpos].checkconflicts(rules[r])
-
-    if state.selected:
-        state.make_selection(newpos)
-
-
 def changeaction(state, pos, action):
     """Change the action state on the given position to the new action"""
     rules = state.rules
@@ -1509,12 +1486,12 @@
             newpos = min(oldpos + 1, len(rules) - 1)
             self.move_cursor(oldpos, newpos)
             if selected is not None or action == b'move-down':
-                swap(self, oldpos, newpos)
+                self.swap(oldpos, newpos)
         elif action in (b'up', b'move-up'):
             newpos = max(0, oldpos - 1)
             self.move_cursor(oldpos, newpos)
             if selected is not None or action == b'move-up':
-                swap(self, oldpos, newpos)
+                self.swap(oldpos, newpos)
         elif action == b'next-action':
             cycleaction(self, oldpos, next=True)
         elif action == b'prev-action':
@@ -1526,7 +1503,7 @@
             newrule = next((r for r in rules if r.origpos == int(ch)))
             self.move_cursor(oldpos, newrule.pos)
             if selected is not None:
-                swap(self, oldpos, newrule.pos)
+                self.swap(oldpos, newrule.pos)
         elif action.startswith(b'action-'):
             changeaction(self, oldpos, action[7:])
         elif action == b'showpatch':
@@ -1589,6 +1566,28 @@
     def make_selection(self, pos):
         self.selected = pos
 
+    def swap(self, oldpos, newpos):
+        """Swap two positions and calculate necessary conflicts in
+        O(|newpos-oldpos|) time"""
+
+        rules = self.rules
+        assert 0 <= oldpos < len(rules) and 0 <= newpos < len(rules)
+
+        rules[oldpos], rules[newpos] = rules[newpos], rules[oldpos]
+
+        # TODO: swap should not know about histeditrule's internals
+        rules[newpos].pos = newpos
+        rules[oldpos].pos = oldpos
+
+        start = min(oldpos, newpos)
+        end = max(oldpos, newpos)
+        for r in pycompat.xrange(start, end + 1):
+            rules[newpos].checkconflicts(rules[r])
+            rules[oldpos].checkconflicts(rules[r])
+
+        if self.selected:
+            self.make_selection(newpos)
+
 
 def _chisteditmain(repo, rules, stdscr):
     try: