51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
from typing import Optional, List
|
|
|
|
from highlight import Highlight
|
|
from highlighted_range import HighlightedRange
|
|
from line import Line
|
|
from PyQt6.QtCore import *
|
|
from PyQt6.QtGui import *
|
|
from PyQt6.QtWidgets import *
|
|
|
|
from settings import Settings
|
|
|
|
|
|
class HighlightSelection(Highlight):
|
|
start_byte = 0
|
|
end_byte = 0
|
|
|
|
def set_start(self, start_byte):
|
|
self.start_byte = start_byte
|
|
|
|
def set_end_byte(self, end_byte):
|
|
self.end_byte = end_byte
|
|
|
|
def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]:
|
|
begin = min(self.start_byte, self.end_byte)
|
|
end = max(self.start_byte, self.end_byte)
|
|
|
|
if line.intersects(begin, end):
|
|
if line.includes_byte(begin):
|
|
start_byte_in_line = begin - line.byte_offset()
|
|
else:
|
|
start_byte_in_line = 0
|
|
start_char = line.byte_index_to_char_index(start_byte_in_line)
|
|
|
|
if line.includes_byte(end):
|
|
length_in_bytes = end - line.byte_offset() - start_byte_in_line
|
|
end_char = line.byte_index_to_char_index(start_byte_in_line + length_in_bytes)
|
|
else:
|
|
# renders the highlighting to the end of the line
|
|
# this is how selections usually behave
|
|
length_in_bytes = Settings.max_line_length() - start_byte_in_line
|
|
|
|
# note: this mixes chars and bytes, but that should not matter, because
|
|
# it just means that we render the highlight into the invisible range on the right
|
|
end_char = start_char + length_in_bytes
|
|
|
|
length_in_chars = end_char - start_char
|
|
|
|
return [HighlightedRange(start_char, length_in_chars, brush=QBrush(QColor(156, 215, 255)), pen=Qt.PenStyle.NoPen)]
|
|
else:
|
|
return None
|