1use gpui::{HighlightStyle, SharedString};
7use std::time::Duration;
8use std::{borrow::Cow, ops::Range};
9
10pub struct SyntaxHighlighter;
12
13impl SyntaxHighlighter {
14 pub fn new(_language: impl AsRef<str>) -> Self {
15 Self
16 }
17
18 pub fn highlight(&self, _text: &ropey::Rope) -> Vec<(Range<usize>, HighlightStyle)> {
19 Vec::new()
20 }
21
22 pub fn styles(
23 &self,
24 range: &Range<usize>,
25 _theme: &HighlightTheme,
26 ) -> Vec<(Range<usize>, HighlightStyle)> {
27 vec![(range.clone(), HighlightStyle::default())]
29 }
30
31 pub fn update(
32 &mut self,
33 _edit: Option<crate::input::InputEdit>,
34 _text: &ropey::Rope,
35 _timeout: Option<Duration>,
36 ) -> bool {
37 true
39 }
40
41 pub fn edit_tree(&mut self, _edit: Option<crate::input::InputEdit>, _text: &ropey::Rope) {
42 }
44
45 pub fn language(&self) -> &SharedString {
46 static EMPTY: SharedString = SharedString::new_static("");
47 &EMPTY
48 }
49
50 pub fn text(&self) -> &ropey::Rope {
51 static EMPTY_ROPE: LazyLock<ropey::Rope> = LazyLock::new(ropey::Rope::new);
52 &EMPTY_ROPE
53 }
54
55 pub fn tree(&self) -> Option<&crate::input::Tree> {
56 None
57 }
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
62pub enum Language {
63 Unknown,
64}
65
66impl Language {
67 pub fn from_str(_name: &str) -> Self {
68 Language::Unknown
69 }
70
71 pub fn name(&self) -> &'static str {
72 "unknown"
73 }
74
75 pub fn config(&self) -> LanguageConfig {
76 LanguageConfig {
77 name: "unknown".into(),
78 }
79 }
80
81 pub fn all() -> impl Iterator<Item = Self> {
82 std::iter::once(Language::Unknown)
83 }
84}
85
86#[derive(Debug, Clone, PartialEq, Eq)]
88pub struct LanguageConfig {
89 pub name: SharedString,
90}
91
92impl LanguageConfig {
93 pub fn has_grammar(&self) -> bool {
94 false
95 }
96}
97
98use schemars::JsonSchema;
101use serde::{Deserialize, Serialize};
102use serde_repr::{Deserialize_repr, Serialize_repr};
103use std::{
104 collections::HashMap,
105 sync::{LazyLock, Mutex},
106};
107
108#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
109#[serde(rename_all = "lowercase")]
110pub enum FontStyle {
111 Normal,
112 Italic,
113 Underline,
114}
115
116#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, JsonSchema, Serialize_repr, Deserialize_repr)]
117#[repr(u16)]
118pub enum FontWeightContent {
119 Thin = 100,
120 ExtraLight = 200,
121 Light = 300,
122 Normal = 400,
123 Medium = 500,
124 Semibold = 600,
125 Bold = 700,
126 ExtraBold = 800,
127 Black = 900,
128}
129
130#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
131#[serde(default)]
132pub struct ThemeStyle {
133 #[serde(deserialize_with = "crate::deserialize_optional_hsla")]
134 pub color: Option<gpui::Hsla>,
135 pub font_style: Option<FontStyle>,
136 pub font_weight: Option<FontWeightContent>,
137}
138
139impl From<ThemeStyle> for HighlightStyle {
140 fn from(style: ThemeStyle) -> Self {
141 HighlightStyle {
142 color: style.color,
143 font_weight: style.font_weight.map(|w| match w {
144 FontWeightContent::Thin => gpui::FontWeight::THIN,
145 FontWeightContent::ExtraLight => gpui::FontWeight::EXTRA_LIGHT,
146 FontWeightContent::Light => gpui::FontWeight::LIGHT,
147 FontWeightContent::Normal => gpui::FontWeight::NORMAL,
148 FontWeightContent::Medium => gpui::FontWeight::MEDIUM,
149 FontWeightContent::Semibold => gpui::FontWeight::SEMIBOLD,
150 FontWeightContent::Bold => gpui::FontWeight::BOLD,
151 FontWeightContent::ExtraBold => gpui::FontWeight::EXTRA_BOLD,
152 FontWeightContent::Black => gpui::FontWeight::BLACK,
153 }),
154 font_style: style.font_style.map(|s| match s {
155 FontStyle::Normal => gpui::FontStyle::Normal,
156 FontStyle::Italic => gpui::FontStyle::Italic,
157 FontStyle::Underline => gpui::FontStyle::Normal,
158 }),
159 ..Default::default()
160 }
161 }
162}
163
164#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
165pub struct SyntaxColors {
166 pub attribute: Option<ThemeStyle>,
167 pub boolean: Option<ThemeStyle>,
168 pub comment: Option<ThemeStyle>,
169 pub comment_doc: Option<ThemeStyle>,
170 pub constant: Option<ThemeStyle>,
171 pub constructor: Option<ThemeStyle>,
172 pub embedded: Option<ThemeStyle>,
173 pub emphasis: Option<ThemeStyle>,
174 #[serde(rename = "emphasis.strong")]
175 pub emphasis_strong: Option<ThemeStyle>,
176 #[serde(rename = "enum")]
177 pub enum_: Option<ThemeStyle>,
178 pub function: Option<ThemeStyle>,
179 pub hint: Option<ThemeStyle>,
180 pub keyword: Option<ThemeStyle>,
181 pub label: Option<ThemeStyle>,
182 #[serde(rename = "link_text")]
183 pub link_text: Option<ThemeStyle>,
184 #[serde(rename = "link_uri")]
185 pub link_uri: Option<ThemeStyle>,
186 pub number: Option<ThemeStyle>,
187 pub operator: Option<ThemeStyle>,
188 pub predictive: Option<ThemeStyle>,
189 pub preproc: Option<ThemeStyle>,
190 pub primary: Option<ThemeStyle>,
191 pub property: Option<ThemeStyle>,
192 pub punctuation: Option<ThemeStyle>,
193 #[serde(rename = "punctuation.bracket")]
194 pub punctuation_bracket: Option<ThemeStyle>,
195 #[serde(rename = "punctuation.delimiter")]
196 pub punctuation_delimiter: Option<ThemeStyle>,
197 #[serde(rename = "punctuation.list_marker")]
198 pub punctuation_list_marker: Option<ThemeStyle>,
199 #[serde(rename = "punctuation.special")]
200 pub punctuation_special: Option<ThemeStyle>,
201 pub string: Option<ThemeStyle>,
202 #[serde(rename = "string.escape")]
203 pub string_escape: Option<ThemeStyle>,
204 #[serde(rename = "string.regex")]
205 pub string_regex: Option<ThemeStyle>,
206 #[serde(rename = "string.special")]
207 pub string_special: Option<ThemeStyle>,
208 #[serde(rename = "string.special.symbol")]
209 pub string_special_symbol: Option<ThemeStyle>,
210 pub tag: Option<ThemeStyle>,
211 #[serde(rename = "tag.doctype")]
212 pub tag_doctype: Option<ThemeStyle>,
213 #[serde(rename = "text.code.span")]
214 pub text_code_span: Option<ThemeStyle>,
215 #[serde(rename = "text.literal")]
216 pub text_literal: Option<ThemeStyle>,
217 pub title: Option<ThemeStyle>,
218 #[serde(rename = "type")]
219 pub type_: Option<ThemeStyle>,
220 pub variable: Option<ThemeStyle>,
221 #[serde(rename = "variable.special")]
222 pub variable_special: Option<ThemeStyle>,
223 pub variant: Option<ThemeStyle>,
224}
225
226impl SyntaxColors {
227 pub fn style(&self, name: &str) -> Option<HighlightStyle> {
228 if name.is_empty() {
229 return None;
230 }
231
232 let style = match name {
233 "attribute" => self.attribute,
234 "boolean" => self.boolean,
235 "comment" => self.comment,
236 "comment.doc" => self.comment_doc,
237 "constant" => self.constant,
238 "constructor" => self.constructor,
239 "embedded" => self.embedded,
240 "emphasis" => self.emphasis,
241 "emphasis.strong" => self.emphasis_strong,
242 "enum" => self.enum_,
243 "function" => self.function,
244 "hint" => self.hint,
245 "keyword" => self.keyword,
246 "label" => self.label,
247 "link_text" => self.link_text,
248 "link_uri" => self.link_uri,
249 "number" => self.number,
250 "operator" => self.operator,
251 "predictive" => self.predictive,
252 "preproc" => self.preproc,
253 "primary" => self.primary,
254 "property" => self.property,
255 "punctuation" => self.punctuation,
256 "punctuation.bracket" => self.punctuation_bracket,
257 "punctuation.delimiter" => self.punctuation_delimiter,
258 "punctuation.list_marker" => self.punctuation_list_marker,
259 "punctuation.special" => self.punctuation_special,
260 "string" => self.string,
261 "string.escape" => self.string_escape,
262 "string.regex" => self.string_regex,
263 "string.special" => self.string_special,
264 "string.special.symbol" => self.string_special_symbol,
265 "tag" => self.tag,
266 "tag.doctype" => self.tag_doctype,
267 "text.code.span" => self.text_code_span,
268 "text.literal" => self.text_literal,
269 "title" => self.title,
270 "type" => self.type_,
271 "variable" => self.variable,
272 "variable.special" => self.variable_special,
273 "variant" => self.variant,
274 _ => None,
275 }
276 .map(|s| s.into());
277
278 if style.is_some() {
279 style
280 } else if name.contains('.') {
281 name.split('.').next().and_then(|prefix| self.style(prefix))
282 } else {
283 None
284 }
285 }
286
287 pub fn style_for_index(&self, index: usize) -> Option<HighlightStyle> {
288 const HIGHLIGHT_NAMES: [&str; 41] = [
289 "attribute",
290 "boolean",
291 "comment",
292 "comment.doc",
293 "constant",
294 "constructor",
295 "embedded",
296 "emphasis",
297 "emphasis.strong",
298 "enum",
299 "function",
300 "hint",
301 "keyword",
302 "label",
303 "link_text",
304 "link_uri",
305 "number",
306 "operator",
307 "predictive",
308 "preproc",
309 "primary",
310 "property",
311 "punctuation",
312 "punctuation.bracket",
313 "punctuation.delimiter",
314 "punctuation.list_marker",
315 "punctuation.special",
316 "string",
317 "string.escape",
318 "string.regex",
319 "string.special",
320 "string.special.symbol",
321 "tag",
322 "tag.doctype",
323 "text.code.span",
324 "text.literal",
325 "title",
326 "type",
327 "variable",
328 "variable.special",
329 "variant",
330 ];
331
332 HIGHLIGHT_NAMES.get(index).and_then(|name| self.style(name))
333 }
334}
335
336#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, JsonSchema, Serialize, Deserialize)]
337pub struct StatusColors {
338 }
340
341impl StatusColors {
342 pub fn error(&self, _cx: &gpui::App) -> gpui::Hsla {
343 gpui::Hsla::default()
344 }
345
346 pub fn error_background(&self, _cx: &gpui::App) -> gpui::Hsla {
347 gpui::Hsla::default()
348 }
349
350 pub fn error_border(&self, _cx: &gpui::App) -> gpui::Hsla {
351 gpui::Hsla::default()
352 }
353
354 pub fn warning(&self, _cx: &gpui::App) -> gpui::Hsla {
355 gpui::Hsla::default()
356 }
357
358 pub fn warning_background(&self, _cx: &gpui::App) -> gpui::Hsla {
359 gpui::Hsla::default()
360 }
361
362 pub fn warning_border(&self, _cx: &gpui::App) -> gpui::Hsla {
363 gpui::Hsla::default()
364 }
365
366 pub fn info(&self, _cx: &gpui::App) -> gpui::Hsla {
367 gpui::Hsla::default()
368 }
369
370 pub fn info_background(&self, _cx: &gpui::App) -> gpui::Hsla {
371 gpui::Hsla::default()
372 }
373
374 pub fn info_border(&self, _cx: &gpui::App) -> gpui::Hsla {
375 gpui::Hsla::default()
376 }
377
378 pub fn success(&self, _cx: &gpui::App) -> gpui::Hsla {
379 gpui::Hsla::default()
380 }
381
382 pub fn success_background(&self, _cx: &gpui::App) -> gpui::Hsla {
383 gpui::Hsla::default()
384 }
385
386 pub fn success_border(&self, _cx: &gpui::App) -> gpui::Hsla {
387 gpui::Hsla::default()
388 }
389
390 pub fn hint(&self, _cx: &gpui::App) -> gpui::Hsla {
391 gpui::Hsla::default()
392 }
393
394 pub fn hint_background(&self, _cx: &gpui::App) -> gpui::Hsla {
395 gpui::Hsla::default()
396 }
397
398 pub fn hint_border(&self, _cx: &gpui::App) -> gpui::Hsla {
399 gpui::Hsla::default()
400 }
401}
402
403#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
404#[serde(default)]
405pub struct HighlightThemeStyle {
406 #[serde(rename = "editor.background", deserialize_with = "crate::deserialize_optional_hsla")]
407 pub editor_background: Option<gpui::Hsla>,
408 #[serde(rename = "editor.foreground", deserialize_with = "crate::deserialize_optional_hsla")]
409 pub editor_foreground: Option<gpui::Hsla>,
410 #[serde(rename = "editor.active_line.background", deserialize_with = "crate::deserialize_optional_hsla")]
411 pub editor_active_line: Option<gpui::Hsla>,
412 #[serde(rename = "editor.line_number", deserialize_with = "crate::deserialize_optional_hsla")]
413 pub editor_line_number: Option<gpui::Hsla>,
414 #[serde(rename = "editor.active_line_number", deserialize_with = "crate::deserialize_optional_hsla")]
415 pub editor_active_line_number: Option<gpui::Hsla>,
416 #[serde(rename = "editor.invisible", deserialize_with = "crate::deserialize_optional_hsla")]
417 pub editor_invisible: Option<gpui::Hsla>,
418 #[serde(rename = "editor.gutter.background", deserialize_with = "crate::deserialize_optional_hsla")]
419 pub editor_gutter_background: Option<gpui::Hsla>,
420 #[serde(flatten)]
421 pub status: StatusColors,
422 #[serde(rename = "syntax")]
423 pub syntax: SyntaxColors,
424}
425
426impl JsonSchema for HighlightThemeStyle {
427 fn schema_name() -> Cow<'static, str> {
428 "HighlightThemeStyle".into()
429 }
430
431 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
432 schemars::json_schema!({ "type": "object" })
433 }
434}
435
436#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
437pub struct HighlightTheme {
438 pub name: String,
439 #[serde(default)]
440 pub appearance: crate::ThemeMode,
441 pub style: HighlightThemeStyle,
442}
443
444impl JsonSchema for HighlightTheme {
445 fn schema_name() -> Cow<'static, str> {
446 "HighlightTheme".into()
447 }
448
449 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
450 schemars::json_schema!({ "type": "object" })
451 }
452}
453
454impl std::ops::Deref for HighlightTheme {
455 type Target = SyntaxColors;
456
457 fn deref(&self) -> &Self::Target {
458 &self.style.syntax
459 }
460}
461
462impl HighlightTheme {
463 pub fn default_dark() -> std::sync::Arc<Self> {
464 use crate::DEFAULT_THEME_COLORS;
465 DEFAULT_THEME_COLORS[&crate::ThemeMode::Dark].1.clone()
466 }
467
468 pub fn default_light() -> std::sync::Arc<Self> {
469 use crate::DEFAULT_THEME_COLORS;
470 DEFAULT_THEME_COLORS[&crate::ThemeMode::Light].1.clone()
471 }
472}
473
474impl gpui_base::input::HighlightStyleResolver for HighlightTheme {
475 fn style(&self, name: &str) -> Option<HighlightStyle> {
476 self.style.syntax.style(name)
477 }
478}
479
480pub struct LanguageRegistry {
482 languages: Mutex<HashMap<SharedString, LanguageConfig>>,
483}
484
485impl LanguageRegistry {
486 pub fn singleton() -> &'static LazyLock<LanguageRegistry> {
487 static INSTANCE: LazyLock<LanguageRegistry> = LazyLock::new(|| LanguageRegistry {
488 languages: Mutex::new(HashMap::new()),
489 });
490 &INSTANCE
491 }
492
493 pub fn register(&self, lang: &str, config: &LanguageConfig) {
494 self.languages
495 .lock()
496 .unwrap()
497 .insert(lang.to_string().into(), config.clone());
498 }
499
500 pub fn languages(&self) -> Vec<SharedString> {
501 self.languages.lock().unwrap().keys().cloned().collect()
502 }
503
504 pub fn language(&self, name: &str) -> Option<LanguageConfig> {
505 self.languages.lock().unwrap().get(name).cloned()
506 }
507}