57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
from typing import Optional, List
|
|
|
|
from highlight import Highlight
|
|
from highlighted_range import HighlightedRange
|
|
from line import Line
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtGui import QBrush, QColor
|
|
|
|
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
|
|
|
|
start_column = line.char_to_column(start_char)
|
|
end_column = line.char_to_column(end_char)
|
|
if end_column >= 0:
|
|
length_in_columns = end_column - start_column
|
|
else:
|
|
length_in_columns = 4096
|
|
|
|
return [HighlightedRange(start_column, length_in_columns, brush=QBrush(QColor(156, 215, 255, 192)),
|
|
pen=Qt.PenStyle.NoPen)]
|
|
else:
|
|
return None
|