use crate::time::Instant;
use crate::{
components::{
edit_buffer::{snap_down, snap_up},
text_edit::doc::{Doc, Env},
},
Id, Vec2,
};
use std::ops::Range;
#[derive(Clone, Debug)]
pub(crate) struct LinkInfo {
pub id: Id,
pub range: Range<usize>,
pub url: Option<String>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum MenuTarget {
Text,
Link(usize),
}
pub(crate) struct BlockState {
pub text: String,
pub display: Option<String>,
pub cut: Option<usize>,
pub doc: Doc,
pub built: Option<Id>,
pub rev: u64,
pub scope: Id,
pub ord: usize,
pub size: Vec2,
pub links: Vec<LinkInfo>,
pub menu: Option<MenuTarget>,
pub copied: Option<Instant>,
pub last_frame: u64,
pub access: Option<super::access::Lines>,
}
impl BlockState {
pub fn new(text: &str, env: Env) -> Self {
Self {
text: text.to_owned(),
display: None,
cut: None,
doc: Doc::new(text, env),
built: None,
rev: 0,
scope: Id::new(0),
ord: 0,
size: Vec2::ZERO,
links: Vec::new(),
menu: None,
copied: None,
last_frame: 0,
access: None,
}
}
pub fn shown(&self) -> &str {
self.display.as_deref().unwrap_or(&self.text)
}
pub fn set_text(&mut self, text: &str) -> bool {
if self.text == text {
return false;
}
self.text.clear();
self.text.push_str(text);
self.rev += 1;
self.display = None;
self.cut = None;
self.built = None;
true
}
pub fn clamp(&self, byte: usize) -> usize {
snap_down(&self.text, byte.min(self.text.len()))
}
pub fn to_display(&self, range: Range<usize>) -> Range<usize> {
let Some(cut) = self.cut else {
return range;
};
let end = self.shown().len();
let map = |b: usize| if b > cut { end } else { b };
range.start.min(cut)..map(range.end)
}
pub fn to_source(&self, byte: usize) -> usize {
match self.cut {
Some(cut) if byte > cut => self.text.len(),
_ => byte,
}
}
pub fn copy_range(&self, range: Range<usize>, urls: bool) -> String {
let len = self.text.len();
let a = snap_down(&self.text, range.start.min(len));
let b = snap_up(&self.text, range.end.min(len));
if a >= b {
return String::new();
}
let mut out = String::with_capacity(b - a);
let mut at = a;
if urls {
for link in &self.links {
let Some(url) = link.url.as_deref() else {
continue;
};
if link.range.end > a && link.range.start < b {
let end = link.range.end.min(b);
out.push_str(&self.text[at..end]);
out.push_str(" (");
out.push_str(url);
out.push(')');
at = end;
}
}
}
out.push_str(&self.text[at..b]);
out
}
}