gpui_component/diff/
decoration.rs1use std::collections::HashMap;
2use std::ops::Range;
3
4use gpui::{ElementId, Hsla, SharedString};
5
6use super::{DiffLinePosition, DiffLineRange, DiffSide};
7
8#[derive(Clone)]
15pub struct DiffDecoration {
16 range: DiffLineRange,
17 background: Option<Hsla>,
18}
19
20impl DiffDecoration {
21 pub fn new(range: DiffLineRange) -> Self {
26 Self {
27 range,
28 background: None,
29 }
30 }
31 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 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#[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 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 pub fn with_underline(mut self) -> Self {
85 self.underlined = true;
86 self
87 }
88 pub fn with_background(mut self, background: impl Into<Hsla>) -> Self {
92 self.background = Some(background.into());
93 self
94 }
95 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 pub fn background(&self) -> Option<Hsla> {
108 self.background
109 }
110 pub fn foreground(&self) -> Option<Hsla> {
112 self.foreground
113 }
114 pub(crate) fn is_underlined(&self) -> bool {
116 self.underlined
117 }
118}
119
120#[derive(Clone)]
126pub struct DiffGutterMarker {
127 id: ElementId,
128 position: DiffLinePosition,
129 label: SharedString,
130}
131
132impl DiffGutterMarker {
133 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 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#[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 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#[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#[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}