<commit_before>from pyqode.qt import QtWidgets
from pyqode.core import api


class MoveCursorCommand(QtWidgets.QUndoCommand):
    def __init__(self, new_pos, prev_pos, editor):
        super(MoveCursorCommand, self).__init__(
            '(Goto line %d)' % (new_pos[0] + 1))
        self._new_pos = new_pos
        self._prev_pos = prev_pos
        self._editor = editor

    def _move(self, line, column):
        self._editor.blockSignals(True)
        api.TextHelper(self._editor).goto_line(line, column)
        self._editor.blockSignals(False)
        try:
            caret_mode = self._editor.modes.get('CaretLineHighlighterMode')
        except KeyError:
            pass
        else:
            caret_mode.refresh()

    def redo(self):
        self._move(*self._new_pos)

    def undo(self):
        self._move(*self._prev_pos)


class CursorHistoryMode(api.Mode):
    def __init__(self):
        super(CursorHistoryMode, self).__init__()
        self._prev_pos = 0, 0
        self.undo_stack = QtWidgets.QUndoStack()

    def on_state_changed(self, state):
        if state:
            menu = QtWidgets.QMenu(self.editor)
            menu.setTitle('Cursor history')
            self.action_undo = self.undo_stack.createUndoAction(self.editor)
            self.action_undo.setShortcut('Ctrl+Alt+Z')
            menu.addAction(self.action_undo)
            self.action_redo = self.undo_stack.createRedoAction(self.editor)
            self.action_redo.setShortcut('Ctrl+Alt+Y')
            menu.addAction(self.action_redo)
            self.editor.add_action(menu.menuAction())
            self.editor.cursorPositionChanged.connect(
                self._on_cursor_position_changed)
        else:
            self.editor.cursorPositionChanged.disconnect(
                self._on_cursor_position_changed)
            self.editor.remove_action(self.action_undo)
            self.editor.remove_action(self.action_redo)

    def _on_cursor_position_changed(self):
        new_pos = api.TextHelper(self.editor).cursor_position()
        if new_pos[0] != self._prev_pos[0]:
            # only record when line changed
            cmd = MoveCursorCommand(new_pos, self._prev_pos, self.editor)
            self.undo_stack.push(cmd)
        self._prev_pos = new_pos<commit_msg>CursorHistoryMode: fix issues if user want to select multiple lines<commit_after>from pyqode.qt import QtWidgets
from pyqode.core import api


class MoveCursorCommand(QtWidgets.QUndoCommand):
    def __init__(self, new_pos, prev_pos, editor):
        super(MoveCursorCommand, self).__init__(
            '(Goto line %d)' % (new_pos[0] + 1))
        self._new_pos = new_pos
        self._prev_pos = prev_pos
        self._editor = editor

    def _move(self, line, column):
        self._editor.blockSignals(True)
        api.TextHelper(self._editor).goto_line(line, column)
        self._editor.blockSignals(False)
        try:
            caret_mode = self._editor.modes.get('CaretLineHighlighterMode')
        except KeyError:
            pass
        else:
            caret_mode.refresh()

    def redo(self):
        self._move(*self._new_pos)

    def undo(self):
        self._move(*self._prev_pos)


class CursorHistoryMode(api.Mode):
    def __init__(self):
        super(CursorHistoryMode, self).__init__()
        self._prev_pos = 0, 0
        self.undo_stack = QtWidgets.QUndoStack()

    def on_state_changed(self, state):
        if state:
            menu = QtWidgets.QMenu(self.editor)
            menu.setTitle('Cursor history')
            self.action_undo = self.undo_stack.createUndoAction(self.editor)
            self.action_undo.setShortcut('Ctrl+Alt+Z')
            menu.addAction(self.action_undo)
            self.action_redo = self.undo_stack.createRedoAction(self.editor)
            self.action_redo.setShortcut('Ctrl+Alt+Y')
            menu.addAction(self.action_redo)
            self.editor.add_action(menu.menuAction())
            self.editor.cursorPositionChanged.connect(
                self._on_cursor_position_changed)
        else:
            self.editor.cursorPositionChanged.disconnect(
                self._on_cursor_position_changed)
            self.editor.remove_action(self.action_undo)
            self.editor.remove_action(self.action_redo)

    def _on_cursor_position_changed(self):
        if self.editor.textCursor().hasSelection():
            return
        new_pos = api.TextHelper(self.editor).cursor_position()
        if new_pos[0] != self._prev_pos[0]:
            # only record when line changed
            cmd = MoveCursorCommand(new_pos, self._prev_pos, self.editor)
            self.undo_stack.push(cmd)
        self._prev_pos = new_pos
{'fraction_non_alphanumeric': 0.09844117019004911, 'fraction_numerical': 0.0025624599615631004, 'mean_word_length': 2.4491899852724597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2538033', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1402, 'n_words': 221}