Skip to content

Commit

Permalink
Use QRegularExpression for highlighting search terms in completion
Browse files Browse the repository at this point in the history
Fixes wrong text colored after four-byte characters.
  • Loading branch information
arza-zara authored and andrewcarlotti committed Aug 16, 2020
1 parent 35d116d commit 8140a7c
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions qutebrowser/completion/completiondelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import html

from PyQt5.QtWidgets import QStyle, QStyleOptionViewItem, QStyledItemDelegate
from PyQt5.QtCore import QRectF, QSize, Qt
from PyQt5.QtCore import QRectF, QRegularExpression, QSize, Qt
from PyQt5.QtGui import (QIcon, QPalette, QTextDocument, QTextOption,
QAbstractTextDocumentLayout, QSyntaxHighlighter,
QTextCharFormat)
Expand All @@ -41,14 +41,21 @@ def __init__(self, doc, pattern, color):
super().__init__(doc)
self._format = QTextCharFormat()
self._format.setForeground(color)
self._pattern = pattern
self._expression = QRegularExpression(
pattern,
QRegularExpression.CaseInsensitiveOption
)

def highlightBlock(self, text):
"""Override highlightBlock for custom highlighting."""
for match in re.finditer(self._pattern, text, re.IGNORECASE):
start, end = match.span()
length = end - start
self.setFormat(start, length, self._format)
match_iterator = self._expression.globalMatch(text)
while match_iterator.hasNext():
match = match_iterator.next()
self.setFormat(
match.capturedStart(),
match.capturedLength(),
self._format
)


class CompletionItemDelegate(QStyledItemDelegate):
Expand Down

0 comments on commit 8140a7c

Please sign in to comment.