dymokomi/luce-ui
Source of release 0.19.3
0.19.3 / src / widgets / text_editor / decorations.lucb
#==============================================================================================
#
# widgets/text_editor/decorations - Highlights, color chips, folds, change bars and AI lines
#
#==============================================================================================
extend TextEditor:
## Mark the 1-based line range the AI is working on; its gutter shows 45° stripes.
## Pass 0, 0 (or call clear_ai_lines) to remove the marking.
pub func set_ai_lines(first: i64, last: i64):
self.ai_first = first if first <= last else last
self.ai_last = last if last >= first else first
self.box.value().invalidate()
## Remove the AI line marking; the same as `set_ai_lines(0, 0)`.
pub func clear_ai_lines():
self.ai_first = 0
self.ai_last = 0
self.box.value().invalidate()
## Replace every highlight with this complete, ordered set.
pub func set_highlights(highlights: const Highlight[]) -> !:
try self.highlights.replace(0, (i64)self.document.length(), highlights)
self.box.value().invalidate()
## Publish a bounded analysis result: replace the highlights within
## `start..<end` with `highlights`, which lie inside it, computed against
## document `version`. A stale result (another version) is ignored before
## validation, so it cannot recolor newer text; returns whether it was applied.
## Highlights already applied shift with later edits on their own.
pub func update_highlights(start: i64, end: i64, highlights: const Highlight[], version: i64) -> bool!:
if version != self.version():
return false
if start < 0 or end < start or end > self.length():
error(result.invalid, "highlight interval is outside the document")
try self.highlights.replace(start, end, highlights)
self.box.value().invalidate()
return true
## The color of the highlight covering scalar `offset`, or none.
pub func highlight_at(offset: i64) -> gpu.Color?:
return self.highlights.color_at(offset)
## Replace the color-chip set (offsets into the current document, ascending). A
## language service republishes this on each change, alongside the highlights.
pub func set_swatches(marks: const Swatch[]) -> !:
try self.swatches.replace(marks)
self.box.value().invalidate()
## The chips that reserve layout columns. Only when soft-wrap is off, so wrapped
## rendering keeps the projection's scalar-based column layout; wrapped mode draws
## the chips compactly in place instead. Column math and painting share this view.
func active_swatches() -> const Swatch[]:
return self.swatches.values() if not self.wrap else self.swatches.values()[..<0]
## Called with the chip's offset when a color chip is clicked.
pub func on_swatch_activate(callback: interop.Callback[i64, unit]) -> interop.Reference[interop.Connection]!:
return try self.swatch_activated.connect(callback)
## The document offset of the color chip under editor-local (x, y), or -1. Only
## when soft-wrap is off, where chips reserve their two cells before the scalar.
func swatch_at(x: f64, y: f64) -> i64:
if self.wrap or self.swatches.values().length == 0:
return -1
let advance = self.font.value().advance()
let row = (usize)(math.max(0.0, y + self.scroll_y) / self.font.value().line_height())
if row >= self.projection.visible_count():
return -1
let line = self.projection.line(self.projection.physical_row(row))
let origin = self.gutter_width() + advance - self.scroll_x
for mark in self.swatches.values():
if mark.offset >= (i64)line.start and mark.offset <= (i64)line.end:
let col = (i64)editing.column(&self.document, (usize)mark.offset, self.swatches.values())
let chip_x = origin + (f64)(col - (i64)swatch_span) * advance
if x >= chip_x and x < chip_x + (f64)swatch_span * advance:
return mark.offset
return -1
## The editor-local point just below the chip at `offset`, to anchor a popup.
pub func swatch_anchor(offset: i64) -> Point:
let advance = self.font.value().advance()
let col = (i64)editing.column(&self.document, (usize)offset, self.swatches.values())
let chip_x = self.gutter_width() + advance - self.scroll_x + (f64)(col - (i64)swatch_span) * advance
let row = self.projection.visual_row((usize)offset)
return Point(chip_x, (f64)(row + 1) * self.font.value().line_height() - self.scroll_y + drawing.vertical_padding(self.font))
## The literal color token (`#rgb` or `#rrggbb`) at a chip offset, for seeding and
## for replacing it in place.
pub func swatch_token(offset: i64) -> interop.Owned[str]!:
let length = 1 + self.hex_run((usize)offset, self.document.length())
return try self.document.text((usize)offset, (usize)offset + length)
## The first color chip's offset, or -1 — for harnesses that drive a chip click.
pub func first_swatch() -> i64:
let marks = self.swatches.values()
return marks[0].offset if marks.length > 0 else -1
## Scan the document for CSS-style hex colors (`#rgb` or `#rrggbb`) and draw a color
## chip before each leading `#`. A convenience over set_swatches for editing themes and
## stylesheets; call it after each change, next to syntax highlighting. Offsets come
## straight from scalar positions, so the chips stay aligned in non-ASCII documents.
pub func set_hex_swatches() -> !:
let length = self.document.length()
var count: usize = 0
var index: usize = 0
while index < length:
let run = self.hex_run(index, length)
if run == 3 or run == 6:
count += 1
index += 1 + run
else:
index += 1
let marks = try new Swatch[count] --- in memory.heap
defer free(marks) in memory.heap
var written: usize = 0
index = 0
while index < length and written < count:
let run = self.hex_run(index, length)
if run == 3 or run == 6:
marks[written] = Swatch((i64)index, self.hex_color(index + 1, run))
written += 1
index += 1 + run
else:
index += 1
try self.swatches.replace(marks[..<written])
self.box.value().invalidate()
## Consecutive hex digits following a `#` at `at` (0 when that scalar is not `#`),
## capped at seven so a run longer than six fails the 3/6-digit test.
func hex_run(at: usize, length: usize) -> usize:
if self.document.scalar(at) != (u32)'#':
return 0
var run: usize = 0
var index = at + 1
while index < length and run < 7 and hex_digit(self.document.scalar(index)) >= 0:
run += 1
index += 1
return run
## Decode `count` (3 or 6) hex digits at `at` into a linear-light color, matching
## the sRGB decoding used for configured theme colors.
func hex_color(at: usize, count: usize) -> gpu.Color:
var channels: f64[3] = [0.0, 0.0, 0.0]
for channel in 0..<3:
var value: i64 = 0
if count == 6:
value = hex_digit(self.document.scalar(at + (usize)channel * 2)) * 16 + hex_digit(self.document.scalar(at + (usize)channel * 2 + 1))
else:
let digit = hex_digit(self.document.scalar(at + (usize)channel))
value = digit * 16 + digit
channels[channel] = transfer.srgb_decode((f64)value / 255.0)
return gpu.Color(channels[0], channels[1], channels[2])
## Fold headers and endpoints are scalar offsets; user-facing line numbers
## are one-based, matching line(). Collapsing never edits source or history.
## Replaces every fold range; returns false, changing nothing, when `version`
## is stale.
pub func set_folds(ranges: const FoldRange[], version: i64) -> bool!:
if version != self.version():
return false
try self.folds.replace(ranges, &self.document)
try self.finish_folding()
return true
## Collapse or expand the fold whose header is on 1-based `line`, returning
## whether a fold starts there. A caret the collapse hides moves to the end of
## the header line.
pub func set_folded(line: i64, collapsed: bool) -> bool!:
try self.refresh_view()
if line < 1 or line > (i64)self.projection.line_count():
error(result.invalid, "fold line is outside the document")
let changed = self.folds.set(self.projection.line((usize)(line - 1)).start, collapsed)
if changed:
try self.finish_folding()
return changed
## Flip the fold whose header is on 1-based `line`; see `set_folded`.
pub func toggle_fold(line: i64) -> bool!:
return try self.set_folded(line, not self.is_folded(line))
## Whether a collapsed fold starts on 1-based `line`.
pub func is_folded(line: i64) -> bool:
if line < 1 or line > (i64)self.projection.line_count():
return false
let index = self.folds.at(self.projection.line((usize)(line - 1)).start) else return false
return self.folds.values()[index].collapsed
pub func fold_all() -> !:
self.folds.set_all(true)
try self.finish_folding()
pub func unfold_all() -> !:
self.folds.set_all(false)
try self.finish_folding()
## Set the saved text to diff against for gutter change bars, or clear it. The
## bars update as the buffer diverges from this baseline; pass the current text
## (for example after a save) to clear them.
pub func set_baseline(text: str) -> !:
let replacement = try interop.copy_text(text)
if let previous = self.baseline:
previous.release()
self.baseline = replacement
self.diff_version = -1
self.box.value().invalidate()
pub func clear_baseline():
if let previous = self.baseline:
previous.release()
self.baseline = none
self.diff.clear()
self.diff_version = -1
self.box.value().invalidate()
## Whether the text differed from the baseline when the view last refreshed.
pub func has_changes() -> bool:
return self.diff.present()