Skip to main content

gpui_component/diff/
decoration.rs

1use std::collections::HashMap;
2use std::ops::Range;
3
4use gpui::{ElementId, Hsla, SharedString};
5
6use super::{DiffLinePosition, DiffLineRange, DiffSide};
7
8/// A background that marks source lines in place, without changing the
9/// selection, copying or layout.
10///
11/// Decorations paint above the change background and below inline emphasis,
12/// search highlights and text; a selected line shows the selection instead.
13/// Where several decorations cover a line, the last supplied one is shown.
14#[derive(Clone)]
15pub struct DiffDecoration {
16    range: DiffLineRange,
17    background: Option<Hsla>,
18}
19
20impl DiffDecoration {
21    /// Marks the lines of `range`.
22    ///
23    /// A range that ends on the other side covers its start side from
24    /// `start` onwards and its end side up to `end`.
25    pub fn new(range: DiffLineRange) -> Self {
26        Self {
27            range,
28            background: None,
29        }
30    }
31    /// Sets the background, which defaults to a translucent warning color.
32    pub fn with_background(mut self, background: impl Into<Hsla>) -> Self {
33        self.background = Some(background.into());
34        self
35    }
36    pub fn range(&self) -> &DiffLineRange {
37        &self.range
38    }
39    /// The background set with [`Self::with_background`].
40    pub fn background(&self) -> Option<Hsla> {
41        self.background
42    }
43
44    fn contains(&self, side: DiffSide, line: usize) -> bool {
45        let range = &self.range;
46        if range.is_single_side() {
47            side == range.side() && (range.start()..=range.end()).contains(&line)
48        } else if side == range.side() {
49            line >= range.start()
50        } else {
51            line <= range.end()
52        }
53    }
54}
55
56/// A painted emphasis on a character span of one source line, such as the
57/// matched text of a search hit.
58///
59/// `span` is a byte range in the line's content, before tab expansion. The
60/// fill paints over syntax, inline-change emphasis and decorations, so the
61/// span stays visible on marked lines; an active text selection still shows
62/// above it.
63#[derive(Clone)]
64pub struct DiffSpanHighlight {
65    position: DiffLinePosition,
66    span: Range<usize>,
67    background: Option<Hsla>,
68    foreground: Option<Hsla>,
69    underlined: bool,
70}
71
72impl DiffSpanHighlight {
73    /// Highlights `span` of the line at `position`.
74    pub fn new(position: DiffLinePosition, span: Range<usize>) -> Self {
75        Self {
76            position,
77            span,
78            background: None,
79            foreground: None,
80            underlined: false,
81        }
82    }
83    /// Underlines the span in its text color, the way a link reads.
84    pub fn with_underline(mut self) -> Self {
85        self.underlined = true;
86        self
87    }
88    /// Sets the fill, which defaults to a translucent warning color. A fully
89    /// transparent fill leaves whatever the line shows beneath the span: for
90    /// emphasis carried by the text itself, such as a link.
91    pub fn with_background(mut self, background: impl Into<Hsla>) -> Self {
92        self.background = Some(background.into());
93        self
94    }
95    /// Sets the text color over the fill, keeping it readable on a strong one.
96    pub fn with_foreground(mut self, foreground: impl Into<Hsla>) -> Self {
97        self.foreground = Some(foreground.into());
98        self
99    }
100    pub fn position(&self) -> &DiffLinePosition {
101        &self.position
102    }
103    pub fn span(&self) -> Range<usize> {
104        self.span.clone()
105    }
106    /// The fill set with [`Self::with_background`].
107    pub fn background(&self) -> Option<Hsla> {
108        self.background
109    }
110    /// The text color set with [`Self::with_foreground`].
111    pub fn foreground(&self) -> Option<Hsla> {
112        self.foreground
113    }
114    /// True after [`Self::with_underline`].
115    pub(crate) fn is_underlined(&self) -> bool {
116        self.underlined
117    }
118}
119
120/// An interactive marker in the lane between the line numbers and the code,
121/// such as a comment or diagnostic indicator that opens a details panel.
122///
123/// The application renders it with [`super::Diff::render_gutter_marker`],
124/// usually as a small button, and owns the panel and its focus.
125#[derive(Clone)]
126pub struct DiffGutterMarker {
127    id: ElementId,
128    position: DiffLinePosition,
129    label: SharedString,
130}
131
132impl DiffGutterMarker {
133    /// Places a marker beside a source line.
134    pub fn new(id: impl Into<ElementId>, position: DiffLinePosition) -> Self {
135        Self {
136            id: id.into(),
137            position,
138            label: SharedString::default(),
139        }
140    }
141    /// Sets the accessible label that the rendered marker should announce.
142    pub fn with_label(mut self, label: impl Into<SharedString>) -> Self {
143        self.label = label.into();
144        self
145    }
146    pub fn id(&self) -> &ElementId {
147        &self.id
148    }
149    pub fn position(&self) -> &DiffLinePosition {
150        &self.position
151    }
152    pub fn label(&self) -> &SharedString {
153        &self.label
154    }
155}
156
157/// Decorations by file path, in supplied order.
158#[derive(Default)]
159pub(super) struct Decorations {
160    files: HashMap<SharedString, Vec<DiffDecoration>>,
161}
162
163impl Decorations {
164    pub(super) fn new(decorations: impl IntoIterator<Item = DiffDecoration>) -> Self {
165        let mut index = Self::default();
166        for decoration in decorations {
167            index
168                .files
169                .entry(decoration.range.path().clone())
170                .or_default()
171                .push(decoration);
172        }
173        index
174    }
175
176    /// The decoration shown on a line: the last supplied one covering it.
177    pub(super) fn at(&self, position: &DiffLinePosition) -> Option<&DiffDecoration> {
178        self.files
179            .get(position.path())?
180            .iter()
181            .rev()
182            .find(|decoration| decoration.contains(position.side(), position.line()))
183    }
184}
185
186/// Span highlights by line, in supplied order.
187#[derive(Default)]
188pub(super) struct SpanHighlights {
189    lines: HashMap<DiffLinePosition, Vec<DiffSpanHighlight>>,
190}
191
192impl SpanHighlights {
193    pub(super) fn new(highlights: impl IntoIterator<Item = DiffSpanHighlight>) -> Self {
194        let mut index = Self::default();
195        for highlight in highlights {
196            index
197                .lines
198                .entry(highlight.position.clone())
199                .or_default()
200                .push(highlight);
201        }
202        index
203    }
204
205    pub(super) fn at(&self, position: &DiffLinePosition) -> Option<&[DiffSpanHighlight]> {
206        self.lines.get(position).map(Vec::as_slice)
207    }
208}
209
210/// Gutter markers by line, keeping the first supplied for each line.
211#[derive(Default)]
212pub(super) struct GutterMarkers {
213    lines: HashMap<DiffLinePosition, DiffGutterMarker>,
214}
215
216impl GutterMarkers {
217    pub(super) fn new(markers: impl IntoIterator<Item = DiffGutterMarker>) -> Self {
218        let mut index = Self::default();
219        for marker in markers {
220            index.lines.entry(marker.position.clone()).or_insert(marker);
221        }
222        index
223    }
224
225    pub(super) fn is_empty(&self) -> bool {
226        self.lines.is_empty()
227    }
228
229    pub(super) fn at(&self, position: &DiffLinePosition) -> Option<&DiffGutterMarker> {
230        self.lines.get(position)
231    }
232}