Edit book: Fix highlighting for special characters not visible when the cursor is on the line with the special character

This commit is contained in:
Kovid Goyal 2023-11-13 13:22:44 +05:30
parent 3badcba7b1
commit 9741a12e02
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 35 additions and 8 deletions

View File

@ -4,14 +4,16 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from collections import defaultdict, deque
from qt.core import (
QTextBlock, QTextBlockUserData, QTextCursor, QTextFormat, QTextLayout, QTimer,
)
from qt.core import QTextCursor, QTextBlockUserData, QTextLayout, QTimer
from ..themes import highlight_to_char_format
from calibre.gui2.widgets import BusyCursor
from calibre.utils.icu import utf16_length
from polyglot.builtins import iteritems
from ..themes import highlight_to_char_format
def run_loop(user_data, state_map, formats, text):
state = user_data.state
@ -240,3 +242,11 @@ class SyntaxHighlighter:
elif r.start + r.length >= preedit_start:
r.length += preedit_length
layout.setFormats(formats)
def formats_for_line(self, block: QTextBlock, start, length):
layout = block.layout()
start_in_block = start - block.position()
limit = start_in_block + length
for f in layout.formats():
if f.start >= start_in_block and f.start < limit and f.format.hasProperty(QTextFormat.Property.BackgroundBrush):
yield f

View File

@ -331,7 +331,7 @@ class TextEdit(PlainTextEdit):
def update_extra_selections(self, instant=True):
sel = []
if self.current_cursor_line is not None:
sel.append(self.current_cursor_line)
sel.extend(self.current_cursor_line)
if self.current_search_mark is not None:
sel.append(self.current_search_mark)
if instant and not self.highlighter.has_requests and self.smarts is not None:
@ -627,7 +627,24 @@ class TextEdit(PlainTextEdit):
sel.format.setProperty(QTextFormat.Property.FullWidthSelection, True)
sel.cursor = self.textCursor()
sel.cursor.clearSelection()
self.current_cursor_line = sel
self.current_cursor_line = [sel]
# apply any formats that have a backgroud over the cursor line format
# to ensure they are visible
c = self.textCursor()
block = c.block()
c.clearSelection()
c.select(QTextCursor.SelectionType.LineUnderCursor)
start = min(c.anchor(), c.position())
length = max(c.anchor(), c.position()) - start
for f in self.highlighter.formats_for_line(block, start, length):
sel = QTextEdit.ExtraSelection()
c = self.textCursor()
c.setPosition(f.start + block.position())
c.setPosition(c.position() + f.length, QTextCursor.MoveMode.KeepAnchor)
sel.cursor, sel.format = c, f.format
self.current_cursor_line.append(sel)
self.update_extra_selections(instant=False)
# Update the cursor line's line number in the line number area
try:

View File

@ -66,7 +66,7 @@ SOLARIZED = \
Statement fg={green} bold
Keyword fg={green}
Special fg={red}
SpecialCharacter bg={base02}
SpecialCharacter bg={base03}
Error us=wave uc={red}
SpellError us=wave uc={orange}
@ -113,7 +113,7 @@ THEMES = {
Special fg={special}
Error us=wave uc=red
SpellError us=wave uc=orange
SpecialCharacter bg={cursor_loc}
SpecialCharacter bg=666666
Link fg=cyan
BadLink fg={string} us=wave uc=red
@ -160,7 +160,7 @@ THEMES = {
Statement fg={keyword}
Keyword fg={keyword}
Special fg={special} italic
SpecialCharacter bg={cursor_loc}
SpecialCharacter bg=afafaf
Error us=wave uc=red
SpellError us=wave uc=magenta
Link fg=blue