BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / draft_style.rs

draft_style.rs

draft_style.rs 7.5 KiB Download Raw
1use std::ops::Range;
2
3use gpui_kit::component::input::{RangeDecoration, RangeDecorationStyle, TextDecoration};
4use gpui_kit::*;
5use teams_core::{Draft, LineKind, MarkKind};
6
7use crate::theme;
8
9const CODE_PILL_RADIUS: f32 = 4.;
10const CODE_BLOCK_RADIUS: f32 = 6.;
11const QUOTE_BAR_WIDTH: f32 = 3.;
12const QUOTE_INDENT: f32 = 12.;
13const CODE_INDENT: f32 = 10.;
14const LIST_LEVEL_INDENT: f32 = 18.;
15
16/// How the composer paints a draft: text styles, fills and the list markers to hang under.
17pub struct DraftStyle {
18 pub text: Vec<TextDecoration>,
19 pub ranges: Vec<RangeDecoration>,
20 pub markers: Vec<Range<usize>>,
21 pub indents: Vec<(usize, Pixels)>,
22}
23
24fn line_indent(kind: &LineKind) -> f32 {
25 match kind {
26 LineKind::Text => 0.,
27 LineKind::Quote => QUOTE_INDENT,
28 LineKind::Code(_) => CODE_INDENT,
29 LineKind::Bullet(depth) | LineKind::Numbered(depth) => {
30 LIST_LEVEL_INDENT * f32::from(*depth)
31 }
32 }
33}
34
35fn color(color: Hsla) -> HighlightStyle {
36 HighlightStyle {
37 color: Some(color),
38 ..Default::default()
39 }
40}
41
42fn underline(color: Option<Hsla>) -> UnderlineStyle {
43 UnderlineStyle {
44 thickness: px(1.),
45 color,
46 wavy: false,
47 }
48}
49
50fn mark_style(kind: &MarkKind) -> HighlightStyle {
51 let mut style = HighlightStyle::default();
52 match kind {
53 MarkKind::Bold => style.font_weight = Some(FontWeight::BOLD),
54 MarkKind::Italic => style.font_style = Some(FontStyle::Italic),
55 MarkKind::Underline => style.underline = Some(underline(None)),
56 MarkKind::Strike => {
57 style.color = Some(theme::text_muted());
58 style.strikethrough = Some(StrikethroughStyle {
59 thickness: px(1.),
60 color: None,
61 });
62 }
63 MarkKind::Code => style.color = Some(theme::text_strong()),
64 MarkKind::Link(_) => {
65 style.color = Some(theme::accent_text());
66 style.underline = Some(underline(Some(theme::accent_text())));
67 }
68 }
69 style
70}
71
72/// From the start of the first line of `lines` to the end of the last; Block and Bar cover
73/// every line starting inside it, ends included, so empty lines count too.
74fn block_range(draft: &Draft, lines: Range<usize>) -> Range<usize> {
75 let line_ranges = draft.line_ranges();
76 line_ranges[lines.start].start..line_ranges[lines.end - 1].end
77}
78
79pub fn draft_style(draft: &Draft, mono: SharedString) -> DraftStyle {
80 let mut text = Vec::new();
81 let mut ranges = Vec::new();
82 let mut markers = Vec::new();
83 let indents = draft
84 .line_ranges()
85 .into_iter()
86 .zip(draft.lines())
87 .map(|(line, kind)| (line.start, px(line_indent(kind))))
88 .filter(|(_, indent)| *indent > px(0.))
89 .collect();
90 for mark in draft.marks() {
91 let decoration = TextDecoration::new(mark.range.clone(), mark_style(&mark.kind));
92 if mark.kind == MarkKind::Code {
93 text.push(decoration.with_font_family(mono.clone()));
94 ranges.push(
95 RangeDecoration::new(mark.range.clone())
96 .with_style(RangeDecorationStyle::Pill)
97 .with_color(theme::inline_code_fill(false))
98 .with_border(theme::inline_code_border())
99 .with_radius(px(CODE_PILL_RADIUS)),
100 );
101 } else {
102 text.push(decoration);
103 }
104 }
105 let kinds = draft.lines();
106 let mut index = 0;
107 while index < kinds.len() {
108 let kind = &kinds[index];
109 let run_end = index
110 + kinds[index..]
111 .iter()
112 .take_while(|other| *other == kind)
113 .count();
114 for line in index..run_end {
115 let marker = draft.marker_range(line);
116 let content = draft.content_range(line);
117 if !marker.is_empty() {
118 markers.push(marker.clone());
119 if kind.is_list() {
120 text.push(TextDecoration::new(marker, color(theme::text_soft())));
121 }
122 }
123 if content.is_empty() {
124 continue;
125 }
126 match kind {
127 LineKind::Quote => {
128 text.push(TextDecoration::new(content, color(theme::text_soft())))
129 }
130 LineKind::Code(_) => text.push(
131 TextDecoration::new(content, color(theme::text_strong()))
132 .with_font_family(mono.clone()),
133 ),
134 _ => {}
135 }
136 }
137 let block = match kind {
138 LineKind::Code(_) => Some(
139 RangeDecoration::new(block_range(draft, index..run_end))
140 .with_style(RangeDecorationStyle::Block)
141 .with_color(theme::code_surface())
142 .with_radius(px(CODE_BLOCK_RADIUS)),
143 ),
144 LineKind::Quote => Some(
145 RangeDecoration::new(block_range(draft, index..run_end))
146 .with_style(RangeDecorationStyle::Bar)
147 .with_color(theme::border_strong())
148 .with_radius(px(QUOTE_BAR_WIDTH)),
149 ),
150 _ => None,
151 };
152 ranges.extend(block);
153 index = run_end;
154 }
155 DraftStyle {
156 text,
157 ranges,
158 markers,
159 indents,
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use gpui_kit::component::input::RangeDecorationStyle;
166 use teams_core::{Draft, DraftLine, LineKind};
167
168 use super::{block_range, draft_style};
169
170 fn line(kind: LineKind, content: &str) -> DraftLine {
171 DraftLine {
172 kind,
173 number: None,
174 content: content.into(),
175 marks: Vec::new(),
176 }
177 }
178
179 #[test]
180 fn list_markers_hang_and_code_gets_a_pill() {
181 let draft = Draft::from_markdown("- one `x`\n```\nfn\n```");
182 let style = draft_style(&draft, "Mono".into());
183 assert_eq!(style.markers, [draft.marker_range(0)]);
184 assert_eq!(
185 style
186 .ranges
187 .iter()
188 .map(|range| range.style())
189 .collect::<Vec<_>>(),
190 [RangeDecorationStyle::Pill, RangeDecorationStyle::Block]
191 );
192 }
193
194 #[test]
195 fn nesting_quotes_and_code_become_line_indents() {
196 let draft = Draft::from_markdown("- a\n - b\n> q\n```\nx\n```");
197 let style = draft_style(&draft, "Mono".into());
198 let starts = draft.line_ranges();
199 assert_eq!(
200 style.indents,
201 [
202 (starts[1].start, gpui_kit::px(18.)),
203 (starts[2].start, gpui_kit::px(12.)),
204 (starts[3].start, gpui_kit::px(10.)),
205 ]
206 );
207 assert!(!draft.text().contains('\u{2003}'));
208 }
209
210 #[test]
211 fn a_code_block_covers_its_empty_last_line_before_text() {
212 let draft = Draft::from_lines(&[
213 line(LineKind::Code(None), "x"),
214 line(LineKind::Code(None), ""),
215 line(LineKind::Text, "after"),
216 ]);
217 let block = block_range(&draft, 0..2);
218 let empty_line_start = draft.line_ranges()[1].start;
219 assert!(block.contains(&empty_line_start) || block.end == empty_line_start);
220 assert!(block.end < draft.line_ranges()[2].start);
221 }
222
223 #[test]
224 fn a_lone_code_line_still_gets_a_block() {
225 let draft = Draft::from_lines(&[line(LineKind::Code(None), "")]);
226 let style = draft_style(&draft, "Mono".into());
227 assert_eq!(style.ranges.len(), 1);
228 assert_eq!(style.ranges[0].style(), RangeDecorationStyle::Block);
229 }
230}