def __init__(self, editor_widget): self.editor_widget = editor_widget self.vim_cursor = QTextEdit.ExtraSelection() self.vim_cursor.format.setForeground(QBrush(QColor('#000000'))) self.vim_cursor.format.setBackground(QBrush(QColor('#BBBBBB'))) self.selection = QTextEdit.ExtraSelection() self.selection.format.setForeground(QBrush(QColor('#A9B7C6'))) self.selection.format.setBackground(QBrush(QColor('#214283'))) self.yank_fg_color = QBrush(QColor('#B9C7D6')) self.yank_bg_color = QBrush(QColor('#7D7920')) self.hl_yank_dur = 400 # duration of highlight after yank self.hl_yank = True self.set_config_from_conf() # Order of Selections self.draw_orders_sel = { 'vim_search': 6, 'hl_yank': 7, 'vim_selection': 8, 'vim_cursor': 9 }
def update_vim_cursor(self): selection = QTextEdit.ExtraSelection() back = Qt.white # selection.format.background().color() fore = Qt.black # selection.format.foreground().color() selection.format.setBackground(fore) selection.format.setForeground(back) selection.cursor = self.editor().textCursor() selection.cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) self.editor().set_extra_selections('vim_cursor', [selection]) self.editor().update_extra_selections()
def _apply_current_line_highlight(self): if self._highlighter and self._highlight_current_line: extra_selections = [] selection = QTextEdit.ExtraSelection() line_color = self._highlighter.get_currentline_color() selection.format.setBackground(line_color) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() extra_selections.append(selection) self.setExtraSelections(extra_selections) else: self.setExtraSelections([])
def v(self, repeat): """Start Visual mode per character.""" self.visual_mode = 'char' editor = self._widget.editor() cursor = editor.textCursor() self._prev_cursor = cursor selection = QTextEdit.ExtraSelection() back = Qt.white # selection.format.background().color() fore = Qt.gray # selection.format.foreground().color() selection.format.setBackground(fore) selection.format.setForeground(back) selection.cursor = editor.textCursor() editor.set_extra_selections('vim_visual', [selection]) editor.update_extra_selections()
def search(self, txt): """Search regular expressions key inside document(from spyder_vim).""" editor = self.get_editor() cursor = QTextCursor(editor.document()) cursor.movePosition(QTextCursor.Start) # Apply the option for search is_ignorecase = CONF.get(CONF_SECTION, 'ignorecase') is_smartcase = CONF.get(CONF_SECTION, 'smartcase') if is_ignorecase is True: option = None self.vim_status.search.ignorecase = True if is_smartcase and txt.lower() != txt: option = QTextDocument.FindCaseSensitively self.vim_status.search.ignorecase = False else: option = QTextDocument.FindCaseSensitively self.vim_status.search.ignorecase = False # Find key in document forward search_stack = [] if option: cursor = editor.document().find(QRegularExpression(txt), options=option) else: cursor = editor.document().find(QRegularExpression(txt)) back = self.vim_status.search.color_bg fore = self.vim_status.search.color_fg while not cursor.isNull(): selection = QTextEdit.ExtraSelection() selection.format.setBackground(back) selection.format.setForeground(fore) selection.cursor = cursor search_stack.append(selection) if option: cursor = editor.document().find(QRegularExpression(txt), cursor, options=option) else: cursor = editor.document().find(QRegularExpression(txt), cursor) self.vim_status.cursor.set_extra_selections('vim_search', [i for i in search_stack]) editor.update_extra_selections() self.vim_status.search.selection_list = search_stack self.vim_status.search.txt_searched = txt
def onCursorChanged(self): # highlights current line, find a way not to use QTextEdit block_number = self.textCursor().blockNumber() if block_number != self.block_number: self.block_number = block_number selection = QTextEdit.ExtraSelection() selection.format.setBackground(self.current_line_background) selection.format.setProperty(QTextFormat.FullWidthSelection, True) selection.cursor = self.textCursor() selection.cursor.clearSelection() self.setExtraSelections([selection]) # emit signals for backplot etc. self.focused_line = block_number + 1 self.focusLine.emit(self.focused_line)
def V(self, repeat): """Start Visual mode per line.""" self.visual_mode = 'line' editor = self._widget.editor() cursor = editor.textCursor() self._prev_cursor = cursor selection = QTextEdit.ExtraSelection() back = Qt.white # selection.format.background().color() fore = Qt.gray # selection.format.foreground().color() selection.format.setBackground(fore) selection.format.setForeground(back) selection.cursor = editor.textCursor() selection.cursor.movePosition(QTextCursor.StartOfLine) selection.cursor.movePosition(QTextCursor.Down, QTextCursor.KeepAnchor) editor.set_extra_selections('vim_visual', [selection]) editor.update_extra_selections()
def highlight_yank(self, pos_start, pos_end): """Highlight after yank.""" if self.hl_yank is False: return cursor = self.get_cursor() editor = self.get_editor() sel = QTextEdit.ExtraSelection() sel.format.setForeground(self.yank_fg_color) sel.format.setBackground(self.yank_bg_color) sel.cursor = cursor sel.cursor.setPosition(pos_start) sel.cursor.setPosition(pos_end, QTextCursor.KeepAnchor) self.set_extra_selections("hl_yank", [sel]) QTimer.singleShot(self.hl_yank_dur, lambda: editor.clear_extra_selections("hl_yank"))