gpui_base/input/editor/spacers.rs
1use std::{ops::Range, rc::Rc};
2
3use gpui::{App, Context, Hsla};
4
5use super::{EditorMode, InputBaseState};
6
7/// Blank rows an editor shows above or below a buffer line.
8///
9/// A spacer row holds no text: it has no line number, the caret never stands
10/// on it, and line decorations, selections and search matches pass it by.
11/// Two editors shown side by side pad their shorter lines with spacers, so that
12/// lines paired across them start level however each one wraps.
13#[derive(Clone, Debug, PartialEq)]
14pub struct Spacer {
15 line: usize,
16 rows: usize,
17 above: bool,
18 background: Option<Hsla>,
19}
20
21impl Spacer {
22 /// Create `rows` blank rows below a zero-based buffer line, after its last
23 /// soft-wrapped row.
24 pub fn below(line: usize, rows: usize) -> Self {
25 Self {
26 line,
27 rows,
28 above: false,
29 background: None,
30 }
31 }
32
33 /// Create `rows` blank rows above a zero-based buffer line, before its first
34 /// row.
35 pub fn above(line: usize, rows: usize) -> Self {
36 Self {
37 above: true,
38 ..Self::below(line, rows)
39 }
40 }
41
42 /// The zero-based buffer line the rows stand beside.
43 pub fn line(&self) -> usize {
44 self.line
45 }
46
47 /// How many blank rows there are.
48 pub fn rows(&self) -> usize {
49 self.rows
50 }
51
52 /// Whether the rows stand above the line rather than below it.
53 pub fn is_above(&self) -> bool {
54 self.above
55 }
56
57 /// The color the rows are filled with, if any.
58 pub fn background(&self) -> Option<Hsla> {
59 self.background
60 }
61
62 /// Fill the rows with `color`, gutter included.
63 pub fn with_background(mut self, color: Hsla) -> Self {
64 self.background = Some(color);
65 self
66 }
67}
68
69/// Supplies the spacers of an editor.
70///
71/// Unlike line decorations, spacers move every row below them, so the editor
72/// asks for those of every line at once and keeps the answer: when the provider
73/// is set, after an edit, after the text is wrapped again or a fold changes,
74/// and when [`InputBaseState::refresh_spacers`] is called. A spacer on a line
75/// hidden by a fold, or past the last line, is not shown.
76pub trait SpacerProvider {
77 /// Return the spacers of the buffer lines within `lines`.
78 fn spacers(&self, lines: Range<usize>, cx: &App) -> Vec<Spacer>;
79}
80
81impl InputBaseState<EditorMode> {
82 /// Set the provider of this editor's spacers, or `None` to show none.
83 ///
84 /// The provider is asked during the next layout, after the text has been
85 /// wrapped to the editor's width, so it can read how many rows each line
86 /// takes from [`InputBaseState::display_map`] — of this editor and of
87 /// another one it pads against.
88 pub fn set_spacer_provider(
89 &mut self,
90 provider: Option<Rc<dyn SpacerProvider>>,
91 cx: &mut Context<Self>,
92 ) {
93 self.extras.spacer_provider = provider;
94 self.spacers_asked = None;
95 cx.notify();
96 }
97
98 /// Ask the spacer provider again during the next layout.
99 ///
100 /// The editor asks by itself after its own rows change; call this when
101 /// something the answer depends on changes elsewhere, such as the rows of
102 /// another editor this one is padded against.
103 pub fn refresh_spacers(&mut self, cx: &mut Context<Self>) {
104 self.spacers_asked = None;
105 cx.notify();
106 }
107}