def __init__(self, editor, window): super(ReTextFakeVimHandler, self).__init__(window) self.__window = window self.__editor = editor self.__blockSelection = BlockSelection(self.__editor) self.__blockSelection.hide() self.__searchSelections = [] fm = self.__editor.fontMetrics() self.__cursorWidth = fm.averageCharWidth() self.__oldCursorWidth = self.__editor.cursorWidth() self.__editor.setCursorWidth(self.__cursorWidth) self.__handler = FakeVimHandler(self.__editor, self) self.__proxy = Proxy(self.__window, self.__editor, self) self.__handler.installEventFilter() self.__handler.setupWidget() self.__handler.handleCommand( 'source {home}/.vimrc'.format(home=QDir.homePath())) self.__saveAction = None self.__quitAction = None # Update selections if cursor changes because of current line can be highlighted. self.__editor.cursorPositionChanged.connect(self.__updateSelections)
def __init__(self, editor, window): super(ReTextFakeVimHandler, self).__init__(window) self.__window = window self.__editor = editor self.__blockSelection = BlockSelection(self.__editor) self.__blockSelection.hide() self.__searchSelections = [] fm = self.__editor.fontMetrics() self.__cursorWidth = fm.averageCharWidth() self.__oldCursorWidth = self.__editor.cursorWidth() self.__editor.setCursorWidth(self.__cursorWidth) self.__handler = FakeVimHandler(self.__editor, self) self.__proxy = Proxy(self.__window, self.__editor, self) self.__handler.installEventFilter() self.__handler.setupWidget() self.__handler.handleCommand( 'source {home}/.vimrc'.format(home = QDir.homePath())) self.__saveAction = None self.__quitAction = None # Update selections if cursor changes because of current line can be highlighted. self.__editor.cursorPositionChanged.connect(self.__updateSelections)
class ReTextFakeVimHandler(QObject): """ Editor widget driven by FakeVim. """ def __init__(self, editor, window): super(ReTextFakeVimHandler, self).__init__(window) self.__window = window self.__editor = editor self.__blockSelection = BlockSelection(self.__editor) self.__blockSelection.hide() self.__searchSelections = [] fm = self.__editor.fontMetrics() self.__cursorWidth = fm.averageCharWidth() self.__oldCursorWidth = self.__editor.cursorWidth() self.__editor.setCursorWidth(self.__cursorWidth) self.__handler = FakeVimHandler(self.__editor, self) self.__proxy = Proxy(self.__window, self.__editor, self) self.__handler.installEventFilter() self.__handler.setupWidget() self.__handler.handleCommand( 'source {home}/.vimrc'.format(home=QDir.homePath())) self.__saveAction = None self.__quitAction = None # Update selections if cursor changes because of current line can be highlighted. self.__editor.cursorPositionChanged.connect(self.__updateSelections) def remove(self): self.__editor.setOverwriteMode(False) self.__editor.setCursorWidth(self.__oldCursorWidth) self.__blockSelection.deleteLater() self.__updateSelections([]) self.deleteLater() def handler(self): return self.__handler def setBlockSelection(self, enabled): self.__editor.setCursorWidth(self.__cursorWidth) self.__blockSelection.setVisible(enabled) if enabled: self.__blockSelection.updateSelection(self.__editor.textCursor()) # Shift text cursor into the block selection. tc = self.__editor.textCursor() if self.__columnForPosition( tc.anchor()) < self.__columnForPosition(tc.position()): self.__editor.setCursorWidth(-self.__cursorWidth) def setSaveAction(self, saveAction): self.__saveAction = saveAction def setQuitAction(self, quitAction): self.__quitAction = quitAction def save(self): if self.__saveAction: self.__saveAction.trigger() def quit(self): if self.__quitAction: self.__quitAction.trigger() def hasBlockSelection(self): return self.__blockSelection.isVisible() def highlightMatches(self, pattern): cur = self.__editor.textCursor() re = QRegExp(pattern) cur = self.__editor.document().find(re) a = cur.position() searchSelections = [] while not cur.isNull(): if cur.hasSelection(): selection = QTextEdit.ExtraSelection() selection.format.setBackground(Qt.GlobalColor.yellow) selection.format.setForeground(Qt.GlobalColor.black) selection.cursor = cur searchSelections.append(selection) else: cur.movePosition(QTextCursor.MoveOperation.NextCharacter) cur = self.__editor.document().find(re, cur) b = cur.position() if a == b: cur.movePosition(QTextCursor.MoveOperation.NextCharacter) cur = self.__editor.document().find(re, cur) b = cur.position() if (a == b): break a = b self.__updateSelections(searchSelections) def __updateSelections(self, searchSelections=None): oldSelections = self.__editor.extraSelections() for selection in self.__searchSelections: for i in range(len(oldSelections) - 1, 0, -1): if selection.cursor == oldSelections[i].cursor: oldSelections.pop(i) break if searchSelections != None: self.__searchSelections = searchSelections self.__editor.setExtraSelections(oldSelections + self.__searchSelections) def __columnForPosition(self, position): return position - self.__editor.document().findBlock( position).position()
class ReTextFakeVimHandler (QObject): """ Editor widget driven by FakeVim. """ def __init__(self, editor, window): super(ReTextFakeVimHandler, self).__init__(window) self.__window = window self.__editor = editor self.__blockSelection = BlockSelection(self.__editor) self.__blockSelection.hide() self.__searchSelections = [] fm = self.__editor.fontMetrics() self.__cursorWidth = fm.averageCharWidth() self.__oldCursorWidth = self.__editor.cursorWidth() self.__editor.setCursorWidth(self.__cursorWidth) self.__handler = FakeVimHandler(self.__editor, self) self.__proxy = Proxy(self.__window, self.__editor, self) self.__handler.installEventFilter() self.__handler.setupWidget() self.__handler.handleCommand( 'source {home}/.vimrc'.format(home = QDir.homePath())) self.__saveAction = None self.__quitAction = None # Update selections if cursor changes because of current line can be highlighted. self.__editor.cursorPositionChanged.connect(self.__updateSelections) def remove(self): self.__editor.setOverwriteMode(False) self.__editor.setCursorWidth(self.__oldCursorWidth) self.__blockSelection.deleteLater() self.__updateSelections([]) self.deleteLater() def handler(self): return self.__handler def setBlockSelection(self, enabled): self.__editor.setCursorWidth(self.__cursorWidth) self.__blockSelection.setVisible(enabled) if enabled: self.__blockSelection.updateSelection(self.__editor.textCursor()) # Shift text cursor into the block selection. tc = self.__editor.textCursor() if self.__columnForPosition(tc.anchor()) < self.__columnForPosition(tc.position()): self.__editor.setCursorWidth(-self.__cursorWidth) def setSaveAction(self, saveAction): self.__saveAction = saveAction def setQuitAction(self, quitAction): self.__quitAction = quitAction def save(self): if self.__saveAction: self.__saveAction.trigger() def quit(self): if self.__quitAction: self.__quitAction.trigger() def hasBlockSelection(self): return self.__BlockSelection.isVisible() def highlightMatches(self, pattern): cur = self.__editor.textCursor() re = QRegExp(pattern) cur = self.__editor.document().find(re) a = cur.position() searchSelections = [] while not cur.isNull(): if cur.hasSelection(): selection = QTextEdit.ExtraSelection() selection.format.setBackground(Qt.yellow) selection.format.setForeground(Qt.black) selection.cursor = cur searchSelections.append(selection) else: cur.movePosition(QTextCursor.NextCharacter) cur = self.__editor.document().find(re, cur) b = cur.position() if a == b: cur.movePosition(QTextCursor.NextCharacter) cur = self.__editor.document().find(re, cur) b = cur.position() if (a == b): break a = b self.__updateSelections(searchSelections) def __updateSelections(self, searchSelections = None): oldSelections = self.__editor.extraSelections() for selection in self.__searchSelections: for i in range(len(oldSelections) - 1, 0, -1): if selection.cursor == oldSelections[i].cursor: oldSelections.pop(i) break if searchSelections != None: self.__searchSelections = searchSelections self.__editor.setExtraSelections(oldSelections + self.__searchSelections) def __columnForPosition(self, position): return position - self.__editor.document().findBlock(position).position()