| 1 | use std::{ops::Range, rc::Rc}; |
| 2 | |
| 3 | use gpui::{Bounds, Half, Pixels, ShapedLine, TextAlign, px}; |
| 4 | |
| 5 | use super::{WrappingIndent, display_map::LineLayout}; |
| 6 | |
| 7 | #[derive(Clone, Default)] |
| 8 | pub(crate) struct WhitespaceIndicators { |
| 9 | pub(crate) space: ShapedLine, |
| 10 | pub(crate) tab: ShapedLine, |
| 11 | } |
| 12 | |
| 13 | #[derive(Clone)] |
| 14 | pub(super) struct LastLayout { |
| 15 | pub(super) visible_range: Range<usize>, |
| 16 | pub(super) visible_buffer_lines: Vec<usize>, |
| 17 | pub(super) visible_line_byte_offsets: Vec<usize>, |
| 18 | pub(super) visible_top: Pixels, |
| 19 | pub(super) visible_range_offset: Range<usize>, |
| 20 | pub(super) lines: Rc<Vec<LineLayout>>, |
| 21 | pub(super) line_height: Pixels, |
| 22 | pub(super) wrap_width: Option<Pixels>, |
| 23 | pub(super) wrapping_indent: WrappingIndent, |
| 24 | pub(super) line_number_width: Pixels, |
| 25 | /// Width of one space in the editor font. |
| 26 | /// |
| 27 | /// Past the end of a line there are no glyphs to hit-test against, so this is the |
| 28 | /// step used to measure how far past the end a pointer sits. |
| 29 | pub(super) space_width: Pixels, |
| 30 | pub(super) cursor_bounds: Option<Bounds<Pixels>>, |
| 31 | pub(super) text_align: TextAlign, |
| 32 | pub(super) content_width: Pixels, |
| 33 | } |
| 34 | |
| 35 | impl LastLayout { |
| 36 | pub(crate) fn line(&self, row: usize) -> Option<&LineLayout> { |
| 37 | let pos = self.visible_buffer_lines.binary_search(&row).ok()?; |
| 38 | self.lines.get(pos) |
| 39 | } |
| 40 | |
| 41 | pub(super) fn alignment_offset(&self, line_width: Pixels) -> Pixels { |
| 42 | match self.text_align { |
| 43 | TextAlign::Left => px(0.), |
| 44 | TextAlign::Center => (self.content_width - line_width).half().max(px(0.)), |
| 45 | TextAlign::Right => (self.content_width - line_width).max(px(0.)), |
| 46 | } |
| 47 | } |
| 48 | } |