1use std::{ops::Range, rc::Rc, sync::Arc};
2
3use gpui::{AnyElement, Context, HighlightStyle, Hsla, SharedString, Window};
4use ropey::Rope;
5
6use super::{EditorState, FoldRange, InputEdit};
7use crate::SemanticThemeTokens;
8
9pub trait HighlightStyleResolver: Send + Sync {
14 fn style(&self, name: &str) -> Option<HighlightStyle>;
15}
16
17#[derive(Default)]
18struct NoHighlightStyles;
19
20impl HighlightStyleResolver for NoHighlightStyles {
21 fn style(&self, _: &str) -> Option<HighlightStyle> {
22 None
23 }
24}
25
26pub trait InputHighlighter {
31 fn language(&self) -> SharedString;
32
33 fn update(
34 &mut self,
35 edit: Option<InputEdit>,
36 text: &Rope,
37 folding: bool,
38 window: &mut Window,
39 cx: &mut Context<EditorState>,
40 );
41
42 fn update_batch(
49 &mut self,
50 edits: &[(InputEdit, Rope)],
51 folding: bool,
52 window: &mut Window,
53 cx: &mut Context<EditorState>,
54 ) {
55 for (edit, text) in edits {
56 self.update(Some(*edit), text, folding, window, cx);
57 }
58 }
59
60 fn styles(
63 &self,
64 range: &Range<usize>,
65 resolver: &dyn HighlightStyleResolver,
66 ) -> Vec<(Range<usize>, HighlightStyle)>;
67
68 fn fold_ranges(&self, text: &Rope) -> Vec<FoldRange>;
69
70 fn fold_ranges_for_edit(&self, range: Range<usize>, text: &Rope) -> Vec<FoldRange> {
75 let _ = range;
76 self.fold_ranges(text)
77 }
78}
79
80pub type InputHighlighterFactory = Rc<dyn Fn(&str) -> Option<Box<dyn InputHighlighter>>>;
81pub type SharedHighlightStyleResolver = Arc<dyn HighlightStyleResolver>;
82pub type FoldIconRenderer = Rc<dyn Fn(usize, bool) -> AnyElement>;
83pub type FoldPlaceholderRenderer = Rc<dyn Fn(usize) -> AnyElement>;
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub enum SyntaxContext {
91 Code,
92 String,
93 Comment,
94}
95
96pub trait SyntaxContextProvider {
101 fn context_at(&self, text: &Rope, offset: usize) -> SyntaxContext;
102}
103
104#[derive(Clone, Copy, Default)]
105pub struct DiagnosticColors {
106 pub error: Hsla,
107 pub warning: Hsla,
108 pub info: Hsla,
109 pub hint: Hsla,
110}
111
112#[derive(Clone)]
114pub struct InputEditorStyle {
115 pub foreground: Hsla,
116 pub muted_foreground: Hsla,
117 pub background: Hsla,
118 pub border: Hsla,
119 pub selection: Hsla,
120 pub caret: Hsla,
121 pub diagnostics: DiagnosticColors,
122 pub highlight_styles: SharedHighlightStyleResolver,
123 pub editor_invisible: Option<Hsla>,
124 pub editor_active_line: Option<Hsla>,
125 pub editor_gutter_background: Option<Hsla>,
126 pub fold_icon_renderer: Option<FoldIconRenderer>,
127}
128
129impl InputEditorStyle {
130 pub fn resolved(&self, tokens: &SemanticThemeTokens) -> Self {
143 let colors = &tokens.colors;
144 let unset = |value: Hsla| value.a == 0.;
145 let or = |value: Hsla, fallback: Hsla| if unset(value) { fallback } else { value };
146
147 let foreground = or(self.foreground, colors.foreground);
148 let mut selection = self.selection;
149 if unset(selection) {
150 selection = colors.accent;
151 selection.a = 0.4;
153 }
154
155 Self {
156 foreground,
157 muted_foreground: or(self.muted_foreground, colors.muted_foreground),
158 background: or(self.background, colors.surface),
159 border: or(self.border, colors.border),
160 selection,
161 caret: or(self.caret, foreground),
162 diagnostics: DiagnosticColors {
165 error: or(self.diagnostics.error, colors.destructive),
166 ..self.diagnostics
167 },
168 ..self.clone()
169 }
170 }
171}
172
173impl Default for InputEditorStyle {
174 fn default() -> Self {
175 Self {
176 foreground: Hsla::default(),
177 muted_foreground: Hsla::default(),
178 background: Hsla::default(),
179 border: Hsla::default(),
180 selection: Hsla::default(),
181 caret: Hsla::default(),
182 diagnostics: DiagnosticColors::default(),
183 highlight_styles: Arc::new(NoHighlightStyles),
184 editor_invisible: None,
185 editor_active_line: None,
186 editor_gutter_background: None,
187 fold_icon_renderer: None,
188 }
189 }
190}
191
192#[cfg(test)]
193mod tests {
194 use gpui::hsla;
195
196 use super::InputEditorStyle;
197 use crate::SemanticThemeTokens;
198
199 fn dark() -> SemanticThemeTokens {
200 let mut tokens = SemanticThemeTokens::default();
201 tokens.colors.foreground = hsla(0., 0., 0.98, 1.0);
202 tokens.colors.muted_foreground = hsla(0., 0., 0.64, 1.0);
203 tokens.colors.surface = hsla(0., 0., 0.04, 1.0);
204 tokens.colors.border = hsla(0., 0., 0.15, 1.0);
205 tokens.colors.accent = hsla(0.6, 0.5, 0.5, 1.0);
206 tokens
207 }
208
209 #[test]
210 fn an_unprojected_style_takes_its_ink_from_the_palette() {
211 let tokens = dark();
212 let resolved = InputEditorStyle::default().resolved(&tokens);
213
214 assert_eq!(resolved.foreground, tokens.colors.foreground);
215 assert_eq!(resolved.caret, tokens.colors.foreground);
216 assert_eq!(resolved.muted_foreground, tokens.colors.muted_foreground);
217 assert_eq!(resolved.background, tokens.colors.surface);
218 assert_eq!(resolved.border, tokens.colors.border);
219 for colour in [
222 resolved.foreground,
223 resolved.caret,
224 resolved.muted_foreground,
225 resolved.selection,
226 ] {
227 assert!(colour.a > 0., "{colour:?} is still invisible");
228 }
229 }
230
231 #[test]
232 fn a_selection_stays_translucent_enough_to_read_through() {
233 let resolved = InputEditorStyle::default().resolved(&dark());
234 assert_eq!(resolved.selection.a, 0.4);
235 }
236
237 #[test]
238 fn projected_colours_are_kept_verbatim() {
239 let chosen = hsla(0.3, 0.4, 0.5, 1.0);
240 let style = InputEditorStyle {
241 foreground: chosen,
242 caret: chosen,
243 ..Default::default()
244 };
245 let resolved = style.resolved(&dark());
246
247 assert_eq!(resolved.foreground, chosen);
248 assert_eq!(resolved.caret, chosen);
249 assert_eq!(resolved.border, dark().colors.border);
251 }
252
253 #[test]
254 fn resolution_never_consumes_its_own_output() {
255 let projected = InputEditorStyle::default();
259 let first = projected.resolved(&dark());
260
261 let mut light = SemanticThemeTokens::default();
262 light.colors.foreground = hsla(0., 0., 0.04, 1.0);
263 let second = projected.resolved(&light);
264
265 assert_ne!(first.foreground, second.foreground);
266 assert_eq!(second.foreground, light.colors.foreground);
267 }
268}