Skip to content

Commit

Permalink
Edit book: Fix highlighting for special characters not visible when t…
Browse files Browse the repository at this point in the history
…he cursor is on the line with the special character
  • Loading branch information
kovidgoyal committed Nov 13, 2023
1 parent 3badcba commit 9741a12
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/calibre/gui2/tweak_book/editor/syntax/base.py
Expand Up @@ -4,14 +4,16 @@
__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
Expand Down Expand Up @@ -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
21 changes: 19 additions & 2 deletions src/calibre/gui2/tweak_book/editor/text.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions src/calibre/gui2/tweak_book/editor/themes.py
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9741a12

Please sign in to comment.