comparison hgext/histedit.py @ 48219:fb30ad66a572

chistedit: move movecursor() onto state class Differential Revision: https://phab.mercurial-scm.org/D11643
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 12 Oct 2021 09:00:51 -0700
parents c7690fabce5f
children 7d2464b6e30b
comparison
equal deleted inserted replaced
48218:c7690fabce5f 48219:fb30ad66a572
1192 self.conflicts.remove(other) 1192 self.conflicts.remove(other)
1193 return self.conflicts 1193 return self.conflicts
1194 1194
1195 1195
1196 # ============ EVENTS =============== 1196 # ============ EVENTS ===============
1197 def movecursor(state, oldpos, newpos):
1198 """Change the rule/changeset that the cursor is pointing to, regardless of
1199 current mode (you can switch between patches from the view patch window)."""
1200 state.pos = newpos
1201
1202 mode, _ = state.mode
1203 if mode == MODE_RULES:
1204 # Scroll through the list by updating the view for MODE_RULES, so that
1205 # even if we are not currently viewing the rules, switching back will
1206 # result in the cursor's rule being visible.
1207 modestate = state.modes[MODE_RULES]
1208 if newpos < modestate[b'line_offset']:
1209 modestate[b'line_offset'] = newpos
1210 elif newpos > modestate[b'line_offset'] + state.page_height - 1:
1211 modestate[b'line_offset'] = newpos - state.page_height + 1
1212
1213 # Reset the patch view region to the top of the new patch.
1214 state.modes[MODE_PATCH][b'line_offset'] = 0
1215
1216
1217 def changemode(state, mode): 1197 def changemode(state, mode):
1218 curmode, _ = state.mode 1198 curmode, _ = state.mode
1219 state.mode = (mode, curmode) 1199 state.mode = (mode, curmode)
1220 if mode == MODE_PATCH: 1200 if mode == MODE_PATCH:
1221 state.modes[MODE_PATCH][b'patchcontents'] = state.patch_contents() 1201 state.modes[MODE_PATCH][b'patchcontents'] = state.patch_contents()
1536 ) 1516 )
1537 if action is None: 1517 if action is None:
1538 return 1518 return
1539 if action in (b'down', b'move-down'): 1519 if action in (b'down', b'move-down'):
1540 newpos = min(oldpos + 1, len(rules) - 1) 1520 newpos = min(oldpos + 1, len(rules) - 1)
1541 movecursor(self, oldpos, newpos) 1521 self.move_cursor(oldpos, newpos)
1542 if selected is not None or action == b'move-down': 1522 if selected is not None or action == b'move-down':
1543 swap(self, oldpos, newpos) 1523 swap(self, oldpos, newpos)
1544 elif action in (b'up', b'move-up'): 1524 elif action in (b'up', b'move-up'):
1545 newpos = max(0, oldpos - 1) 1525 newpos = max(0, oldpos - 1)
1546 movecursor(self, oldpos, newpos) 1526 self.move_cursor(oldpos, newpos)
1547 if selected is not None or action == b'move-up': 1527 if selected is not None or action == b'move-up':
1548 swap(self, oldpos, newpos) 1528 swap(self, oldpos, newpos)
1549 elif action == b'next-action': 1529 elif action == b'next-action':
1550 cycleaction(self, oldpos, next=True) 1530 cycleaction(self, oldpos, next=True)
1551 elif action == b'prev-action': 1531 elif action == b'prev-action':
1553 elif action == b'select': 1533 elif action == b'select':
1554 selected = oldpos if selected is None else None 1534 selected = oldpos if selected is None else None
1555 makeselection(self, selected) 1535 makeselection(self, selected)
1556 elif action == b'goto' and int(ch) < len(rules) and len(rules) <= 10: 1536 elif action == b'goto' and int(ch) < len(rules) and len(rules) <= 10:
1557 newrule = next((r for r in rules if r.origpos == int(ch))) 1537 newrule = next((r for r in rules if r.origpos == int(ch)))
1558 movecursor(self, oldpos, newrule.pos) 1538 self.move_cursor(oldpos, newrule.pos)
1559 if selected is not None: 1539 if selected is not None:
1560 swap(self, oldpos, newrule.pos) 1540 swap(self, oldpos, newrule.pos)
1561 elif action.startswith(b'action-'): 1541 elif action.startswith(b'action-'):
1562 changeaction(self, oldpos, action[7:]) 1542 changeaction(self, oldpos, action[7:])
1563 elif action == b'showpatch': 1543 elif action == b'showpatch':
1590 with repo.ui.configoverride(overrides, source=b'histedit'): 1570 with repo.ui.configoverride(overrides, source=b'histedit'):
1591 displayer.show(rule.ctx) 1571 displayer.show(rule.ctx)
1592 displayer.close() 1572 displayer.close()
1593 return displayer.hunk[rule.ctx.rev()].splitlines() 1573 return displayer.hunk[rule.ctx.rev()].splitlines()
1594 1574
1575 def move_cursor(self, oldpos, newpos):
1576 """Change the rule/changeset that the cursor is pointing to, regardless of
1577 current mode (you can switch between patches from the view patch window)."""
1578 self.pos = newpos
1579
1580 mode, _ = self.mode
1581 if mode == MODE_RULES:
1582 # Scroll through the list by updating the view for MODE_RULES, so that
1583 # even if we are not currently viewing the rules, switching back will
1584 # result in the cursor's rule being visible.
1585 modestate = self.modes[MODE_RULES]
1586 if newpos < modestate[b'line_offset']:
1587 modestate[b'line_offset'] = newpos
1588 elif newpos > modestate[b'line_offset'] + self.page_height - 1:
1589 modestate[b'line_offset'] = newpos - self.page_height + 1
1590
1591 # Reset the patch view region to the top of the new patch.
1592 self.modes[MODE_PATCH][b'line_offset'] = 0
1593
1595 1594
1596 def _chisteditmain(repo, rules, stdscr): 1595 def _chisteditmain(repo, rules, stdscr):
1597 try: 1596 try:
1598 curses.use_default_colors() 1597 curses.use_default_colors()
1599 except curses.error: 1598 except curses.error:
1650 sizes = state.layout() 1649 sizes = state.layout()
1651 curmode, unused = state.mode 1650 curmode, unused = state.mode
1652 if curmode != oldmode: 1651 if curmode != oldmode:
1653 state.page_height = sizes[b'main'][0] 1652 state.page_height = sizes[b'main'][0]
1654 # Adjust the view to fit the current screen size. 1653 # Adjust the view to fit the current screen size.
1655 movecursor(state, state.pos, state.pos) 1654 state.move_cursor(state.pos, state.pos)
1656 1655
1657 # Pack the windows against the top, each pane spread across the 1656 # Pack the windows against the top, each pane spread across the
1658 # full width of the screen. 1657 # full width of the screen.
1659 y, x = (0, 0) 1658 y, x = (0, 0)
1660 helpwin, y, x = drawvertwin(sizes[b'help'], y, x) 1659 helpwin, y, x = drawvertwin(sizes[b'help'], y, x)