cosmic-edit/src/line_number.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

2023-11-30 14:24:58 -07:00
use cosmic_text::{
2024-10-22 11:55:58 -06:00
Align, Attrs, AttrsList, BufferLine, Family, FontSystem, LayoutLine, LineEnding, Shaping, Wrap,
2023-11-30 14:24:58 -07:00
};
use std::collections::HashMap;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct LineNumberKey {
pub number: usize,
pub width: usize,
}
#[derive(Debug)]
pub struct LineNumberCache {
cache: HashMap<LineNumberKey, Vec<LayoutLine>>,
}
impl LineNumberCache {
pub fn new() -> Self {
Self {
cache: HashMap::new(),
}
}
pub fn clear(&mut self) {
self.cache.clear();
}
pub fn get(&mut self, font_system: &mut FontSystem, key: LineNumberKey) -> &Vec<LayoutLine> {
self.cache.entry(key).or_insert_with(|| {
//TODO: do not repeat, used in App::init
let attrs = Attrs::new().family(Family::Monospace);
let text = format!("{:width$}", key.number, width = key.width);
let mut buffer_line = BufferLine::new(
text,
LineEnding::default(),
2025-03-31 09:16:20 -06:00
AttrsList::new(&attrs),
Shaping::Advanced,
);
2023-11-30 14:24:58 -07:00
buffer_line.set_align(Some(Align::Left));
buffer_line
2024-09-06 07:29:28 -06:00
.layout(
2023-11-30 14:24:58 -07:00
font_system,
2024-06-12 08:18:22 -06:00
1.0, /* font size adjusted later */
None,
2023-11-30 14:24:58 -07:00
Wrap::None,
2024-01-17 13:39:44 -07:00
None,
2024-06-10 08:35:49 -06:00
8, /* default tab width */
2023-11-30 14:24:58 -07:00
)
.to_vec()
})
}
}