From 9741a12e02119b95a7b89a45a87bc9ecd09a7616 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 13 Nov 2023 13:22:44 +0530 Subject: [PATCH] Edit book: Fix highlighting for special characters not visible when the cursor is on the line with the special character --- .../gui2/tweak_book/editor/syntax/base.py | 16 +++++++++++--- src/calibre/gui2/tweak_book/editor/text.py | 21 +++++++++++++++++-- src/calibre/gui2/tweak_book/editor/themes.py | 6 +++--- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/calibre/gui2/tweak_book/editor/syntax/base.py b/src/calibre/gui2/tweak_book/editor/syntax/base.py index 72e9aff14fd3..6d5893cceb05 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/base.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/base.py @@ -4,14 +4,16 @@ __copyright__ = '2013, Kovid Goyal ' 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 @@ def apply_format_changes(self, block, formats): 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 diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index c365bf7b40bd..650520e2a990 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -331,7 +331,7 @@ def go_to_line(self, lnum, col=None): 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 @@ def highlight_cursor_line(self): 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: diff --git a/src/calibre/gui2/tweak_book/editor/themes.py b/src/calibre/gui2/tweak_book/editor/themes.py index 7f634a3eb034..b69ca83c28f4 100644 --- a/src/calibre/gui2/tweak_book/editor/themes.py +++ b/src/calibre/gui2/tweak_book/editor/themes.py @@ -66,7 +66,7 @@ def default_theme(): 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 @@ def default_theme(): 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 @@ def default_theme(): 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