def highlight_selected_word(self): import keyword self.__clear_occurrences() text = self._text_under_cursor() if not text: return # Do not highlight keywords if text in keyword.kwlist or text == 'self': return result = self._get_find_index_results(text, False, True)[1] selections = [] for start, end in result: selection = extra_selection.ExtraSelection(self.textCursor(), start_pos=start, end_pos=end) selection.set_full_width() # FIXME: from theme selection.set_background(resources.get_color('SearchResult')) selections.append(selection) line = selection.cursor.blockNumber() Marker = scrollbar.marker marker = Marker(line, resources.get_color('SearchResult'), 0) self._scrollbar.add_marker('occurrence', marker) self.add_extra_selections('occurrences', selections)
def show_run_cursor(self): """Highlight momentarily a piece of code""" cursor = self.textCursor() if self.has_selection(): # Get selection range start_pos, end_pos = cursor.selectionStart(), cursor.selectionEnd() else: # If no selected text, highlight current line cursor.movePosition(QTextCursor.StartOfLine) start_pos = cursor.position() cursor.movePosition(QTextCursor.EndOfLine) end_pos = cursor.position() # Create extra selection selection = extra_selection.ExtraSelection(cursor, start_pos=start_pos, end_pos=end_pos) selection.set_background("gray") self.add_extra_selections("run_cursor", [selection]) # Clear selection for show correctly the extra selection cursor.clearSelection() self.setTextCursor(cursor) # Remove extra selection after 0.3 seconds QTimer.singleShot(300, lambda: self.clear_extra_selections("run_cursor"))
def _highlight_checkers(self, neditable): """Add checker selections to the Editor""" # Remove selections if they exists self.clear_extra_selections('checker') self._scrollbar.remove_marker("checker") # Get checkers from neditable checkers = neditable.sorted_checkers selections = [] for items in checkers: checker, color, _ = items lines = checker.checks.keys() for line in lines: # Scrollbar marker self._scrollbar.add_marker("checker", line, color, priority=1) ms = checker.checks[line] for (col_start, col_end), message in ms: selection = extra_selection.ExtraSelection( self.textCursor(), start_line=line, col_start=col_start, col_end=col_end) selection.set_underline(color) selections.append(selection) self.add_extra_selections('checker', selections)
def highlight_selected_word(self): import keyword self.__clear_occurrences() text = self._text_under_cursor() if not text: return # Do not highlight keywords if text in keyword.kwlist or text == 'self': return result = self._get_find_index_results(text, False, True)[1] # self._scrollbar.remove_marker('result') selections = [] for start, end in result: selection = extra_selection.ExtraSelection(self.textCursor(), start_pos=start, end_pos=end) selection.set_full_width() # FIXME: from theme selection.set_background('#333842') selections.append(selection) line = selection.cursor.blockNumber() # self._scrollbar.remove_marker('result') # Marker = scrollbar.marker # marker = Marker(line, 'red', 0) # self._scrollbar.add_marker('result', marker) # if line not in self.__occurrences: # self.__occurrences.append(line) self.add_extra_selections('occurrences', selections)
def highlight_selected_word(self): """Highlight word under cursor""" # Clear previous selections self.__word_occurrences.clear() self.clear_extra_selections("occurrences") if self.extra_selections("find"): # No re-highlight occurrences when have "find" extra selections return word = self.word_under_cursor().selectedText() if not word: return results = self._get_find_index_results(word, cs=False, wo=True)[1] selections = [] append = selections.append # On very big files where a lots of occurrences can be found, # this freeze the editor during a few seconds. So, we can limit of 500 # and make sure the editor will always remain responsive for start_pos, end_pos in results[:500]: selection = extra_selection.ExtraSelection(self.textCursor(), start_pos=start_pos, end_pos=end_pos) color = resources.COLOR_SCHEME.get("editor.occurrence") selection.set_background(color) append(selection) # TODO: highlight results in scrollbar # FIXME: from settings # line = selection.cursor.blockNumber() # Marker = scrollbar.marker # marker = Marker(line, resources.get_color("SearchResult"), 0) # self._scrollbar.add_marker("find", marker) self.add_extra_selections("occurrences", selections)
def _highlight_checkers(self, neditable): """Add checker selections to the Editor""" # Remove selections if they exists self._extra_selections.remove("checker") self._scrollbar.remove_marker("checker") # Get checkers from neditable checkers = neditable.sorted_checkers selections = [] append = selections.append # Reduce name look-ups for better speed for items in checkers: checker, color, _ = items lines = list(checker.checks.keys()) lines.sort() for line in lines[:self._MAX_CHECKER_SELECTIONS]: cursor = self.textCursor() # Scrollbar marker self._scrollbar.add_marker("checker", line, color, priority=1) ms = checker.checks[line] for (col_start, col_end), _, _ in ms: selection = extra_selection.ExtraSelection( cursor, start_line=line, col_start=col_start, col_end=col_end) selection.set_underline(color) append(selection) self._extra_selections.add("checker", selections)
def find_matches(self, search, case_sensitive=False, whole_word=False, backward=False, find_next=True): flags = QTextDocument.FindFlags() if case_sensitive: flags = QTextDocument.FindCaseSensitively if whole_word: flags |= QTextDocument.FindWholeWords if backward: flags |= QTextDocument.FindBackward if find_next or backward: self.moveCursor(QTextCursor.NoMove) else: self.moveCursor(QTextCursor.StartOfWord) found = self.find(search, flags) cursor = self.textCursor() if not found: # Original cursor is saved for restore later cursor = self.textCursor() # Move cursor to beginning of document to search again if backward: self.moveCursor(QTextCursor.End) else: self.moveCursor(QTextCursor.Start) found = self.find(search, flags) if not found: # self.clear_extra_selections() # Restore cursor self.setTextCursor(cursor) self.clear_extra_selections('searchs') return 0, [] index, results = self._get_find_index_results(search, case_sensitive, whole_word) # TODO: obtain line numbers for highlight in scrollbar # TODO: cambiar el 2 # FIXME: clear its ok? self.clear_extra_selections('searchs') if len(search) > 2: ss = [] append = ss.append results = results[:500] for start, end in results: s = extra_selection.ExtraSelection(self.textCursor(), start_pos=start, end_pos=end) s.set_full_width() c = QColor('yellow') c.setAlpha(40) s.set_background(c) s.set_outline('gray') append(s) self.add_extra_selections('searchs', ss) return index, results
def show_link(self, cursor): start_s, end_s = cursor.selectionStart(), cursor.selectionEnd() selection = extra_selection.ExtraSelection(cursor, start_pos=start_s, end_pos=end_s) link_color = resources.COLOR_SCHEME.get("editor.link.navigate") selection.set_underline(link_color, style=1) selection.set_foreground(link_color) self._extra_selections.add("link", selection) self.__link_pressed = True
def highlight_found_results(self, text, cs=False, wo=False): """Highlight all found results from find/replace widget""" index, results = self._get_find_index_results(text, cs=cs, wo=wo) selections = [] append = selections.append color = resources.COLOR_SCHEME.get("editor.search.result") for start, end in results: selection = extra_selection.ExtraSelection(self.textCursor(), start_pos=start, end_pos=end) selection.set_background(color) selection.set_foreground(utils.get_inverted_color(color)) append(selection) line = selection.cursor.blockNumber() self._scrollbar.add_marker("find", line, color) self._extra_selections.add("find", selections) return index, len(results)
def mouseMoveEvent(self, event): if event.modifiers() == Qt.ControlModifier: cursor = self.cursorForPosition(event.pos()) cursor = self.word_under_cursor(cursor) if self.__link_cursor == cursor: return if not cursor.selectedText(): return self.__link_cursor = cursor start, end = cursor.selectionStart(), cursor.selectionEnd() selection = extra_selection.ExtraSelection(cursor, start_pos=start, end_pos=end) link_color = resources.COLOR_SCHEME.get("editor.link.navigate") selection.set_underline(link_color) selection.set_foreground(link_color) self.add_extra_selections("link", [selection]) self.viewport().setCursor(Qt.PointingHandCursor) else: self.clear_link() # Restore mouse cursor if settings say hide while typing if self.viewport().cursor().shape() == Qt.BlankCursor: self.viewport().setCursor(Qt.IBeamCursor) '''if event.modifiers() == Qt.ControlModifier: if self.__link_selection is not None: return cursor = self.cursorForPosition(event.pos()) # Check that the mouse was actually on the text somewhere on_text = self.cursorRect(cursor).right() >= event.x() if on_text: cursor.select(QTextCursor.WordUnderCursor) selection_start = cursor.selectionStart() selection_end = cursor.selectionEnd() self.__link_selection = extra_selection.ExtraSelection( cursor, start_pos=selection_start, end_pos=selection_end ) self.__link_selection.set_underline("red") self.__link_selection.set_full_width() self.add_extra_selection(self.__link_selection) self.viewport().setCursor(Qt.PointingHandCursor)''' super(NEditor, self).mouseMoveEvent(event)
def _highlight_checkers(self, neditable): """Add checker selections to the Editor""" # Remove selections if they exists # self.__checker_extra_selections.clear() # self.clear_extra_selections('checker') # Get checkers from neditable checkers = neditable.sorted_checkers self.highlight_checker_updated.emit(checkers) # markers = [] # self._scrollbar.remove_marker('checker') for items in checkers: checker, color, _ = items lines = checker.checks.keys() # FIXME: for line in lines: # Make extra selections selection = extra_selection.ExtraSelection(self.textCursor(), start_line=line) selection.set_underline(color) selection.set_full_width()
def mouseMoveEvent(self, event): position = event.pos() cursor = self.cursorForPosition(position) block = cursor.block() line = block.layout().lineForTextPosition(cursor.positionInBlock()) # Only handle tool tip for text cursor if mouse is within the # block for the text cursor if position.x() <= self.blockBoundingGeometry(block).left() + \ line.naturalTextRect().right(): column = cursor.positionInBlock() line = self.line_from_position(position.y()) checkers = self._neditable.sorted_checkers for items in checkers: checker, _, _ = items messages_on_this_line = checker.message(line) if messages_on_this_line is not None: for (start, end), message in messages_on_this_line: if column >= start and column <= end: QToolTip.showText(self.mapToGlobal(position), message, self) if event.modifiers() == Qt.ControlModifier: cursor = self.cursorForPosition(event.pos()) cursor = self.word_under_cursor(cursor) if self.__link_cursor == cursor: return if not cursor.selectedText(): return self.__link_cursor = cursor start, end = cursor.selectionStart(), cursor.selectionEnd() selection = extra_selection.ExtraSelection(cursor, start_pos=start, end_pos=end) link_color = resources.COLOR_SCHEME.get("editor.link.navigate") selection.set_underline(link_color) selection.set_foreground(link_color) self.add_extra_selections("link", [selection]) self.viewport().setCursor(Qt.PointingHandCursor) else: self.clear_link() # Restore mouse cursor if settings say hide while typing if self.viewport().cursor().shape() == Qt.BlankCursor: self.viewport().setCursor(Qt.IBeamCursor) '''if event.modifiers() == Qt.ControlModifier: if self.__link_selection is not None: return cursor = self.cursorForPosition(event.pos()) # Check that the mouse was actually on the text somewhere on_text = self.cursorRect(cursor).right() >= event.x() if on_text: cursor.select(QTextCursor.WordUnderCursor) selection_start = cursor.selectionStart() selection_end = cursor.selectionEnd() self.__link_selection = extra_selection.ExtraSelection( cursor, start_pos=selection_start, end_pos=selection_end ) self.__link_selection.set_underline("red") self.__link_selection.set_full_width() self.add_extra_selection(self.__link_selection) self.viewport().setCursor(Qt.PointingHandCursor)''' super(NEditor, self).mouseMoveEvent(event)