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) -> GrammarConfig {
76 GrammarConfig {
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
92pub type GrammarConfig = LanguageConfig;
94
95impl LanguageConfig {
96 pub fn has_grammar(&self) -> bool {
97 false
98 }
99}
100
101use schemars::JsonSchema;
104use serde::{Deserialize, Serialize};
105use serde_repr::{Deserialize_repr, Serialize_repr};
106use std::{
107 collections::HashMap,
108 sync::{LazyLock, Mutex},
109};
110
111#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
112#[serde(rename_all = "lowercase")]
113pub enum FontStyle {
114 Normal,
115 Italic,
116 Underline,
117}
118
119#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, JsonSchema, Serialize_repr, Deserialize_repr)]
120#[repr(u16)]
121pub enum FontWeightContent {
122 Thin = 100,
123 ExtraLight = 200,
124 Light = 300,
125 Normal = 400,
126 Medium = 500,
127 Semibold = 600,
128 Bold = 700,
129 ExtraBold = 800,
130 Black = 900,
131}
132
133#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
134#[serde(default)]
135pub struct ThemeStyle {
136 #[serde(deserialize_with = "crate::deserialize_optional_hsla")]
137 pub color: Option<gpui::Hsla>,
138 pub font_style: Option<FontStyle>,
139 pub font_weight: Option<FontWeightContent>,
140}
141
142impl From<ThemeStyle> for HighlightStyle {
143 fn from(style: ThemeStyle) -> Self {
144 HighlightStyle {
145 color: style.color,
146 font_weight: style.font_weight.map(|w| match w {
147 FontWeightContent::Thin => gpui::FontWeight::THIN,
148 FontWeightContent::ExtraLight => gpui::FontWeight::EXTRA_LIGHT,
149 FontWeightContent::Light => gpui::FontWeight::LIGHT,
150 FontWeightContent::Normal => gpui::FontWeight::NORMAL,
151 FontWeightContent::Medium => gpui::FontWeight::MEDIUM,
152 FontWeightContent::Semibold => gpui::FontWeight::SEMIBOLD,
153 FontWeightContent::Bold => gpui::FontWeight::BOLD,
154 FontWeightContent::ExtraBold => gpui::FontWeight::EXTRA_BOLD,
155 FontWeightContent::Black => gpui::FontWeight::BLACK,
156 }),
157 font_style: style.font_style.map(|s| match s {
158 FontStyle::Normal => gpui::FontStyle::Normal,
159 FontStyle::Italic => gpui::FontStyle::Italic,
160 FontStyle::Underline => gpui::FontStyle::Normal,
161 }),
162 ..Default::default()
163 }
164 }
165}
166
167#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
168pub struct SyntaxColors {
169 pub attribute: Option<ThemeStyle>,
170 pub boolean: Option<ThemeStyle>,
171 pub comment: Option<ThemeStyle>,
172 pub comment_doc: Option<ThemeStyle>,
173 pub constant: Option<ThemeStyle>,
174 pub constructor: Option<ThemeStyle>,
175 pub embedded: Option<ThemeStyle>,
176 pub emphasis: Option<ThemeStyle>,
177 #[serde(rename = "emphasis.strong")]
178 pub emphasis_strong: Option<ThemeStyle>,
179 #[serde(rename = "enum")]
180 pub enum_: Option<ThemeStyle>,
181 pub function: Option<ThemeStyle>,
182 pub hint: Option<ThemeStyle>,
183 pub keyword: Option<ThemeStyle>,
184 pub label: Option<ThemeStyle>,
185 #[serde(rename = "link_text")]
186 pub link_text: Option<ThemeStyle>,
187 #[serde(rename = "link_uri")]
188 pub link_uri: Option<ThemeStyle>,
189 pub number: Option<ThemeStyle>,
190 pub operator: Option<ThemeStyle>,
191 pub predictive: Option<ThemeStyle>,
192 pub preproc: Option<ThemeStyle>,
193 pub primary: Option<ThemeStyle>,
194 pub property: Option<ThemeStyle>,
195 pub punctuation: Option<ThemeStyle>,
196 #[serde(rename = "punctuation.bracket")]
197 pub punctuation_bracket: Option<ThemeStyle>,
198 #[serde(rename = "punctuation.delimiter")]
199 pub punctuation_delimiter: Option<ThemeStyle>,
200 #[serde(rename = "punctuation.list_marker")]
201 pub punctuation_list_marker: Option<ThemeStyle>,
202 #[serde(rename = "punctuation.special")]
203 pub punctuation_special: Option<ThemeStyle>,
204 pub string: Option<ThemeStyle>,
205 #[serde(rename = "string.escape")]
206 pub string_escape: Option<ThemeStyle>,
207 #[serde(rename = "string.regex")]
208 pub string_regex: Option<ThemeStyle>,
209 #[serde(rename = "string.special")]
210 pub string_special: Option<ThemeStyle>,
211 #[serde(rename = "string.special.symbol")]
212 pub string_special_symbol: Option<ThemeStyle>,
213 pub tag: Option<ThemeStyle>,
214 #[serde(rename = "tag.doctype")]
215 pub tag_doctype: Option<ThemeStyle>,
216 #[serde(rename = "text.code.span")]
217 pub text_code_span: Option<ThemeStyle>,
218 #[serde(rename = "text.literal")]
219 pub text_literal: Option<ThemeStyle>,
220 pub title: Option<ThemeStyle>,
221 #[serde(rename = "type")]
222 pub type_: Option<ThemeStyle>,
223 pub variable: Option<ThemeStyle>,
224 #[serde(rename = "variable.special")]
225 pub variable_special: Option<ThemeStyle>,
226 pub variant: Option<ThemeStyle>,
227}
228
229impl SyntaxColors {
230 pub fn style(&self, name: &str) -> Option<HighlightStyle> {
231 if name.is_empty() {
232 return None;
233 }
234
235 let style = match name {
236 "attribute" => self.attribute,
237 "boolean" => self.boolean,
238 "comment" => self.comment,
239 "comment.doc" => self.comment_doc,
240 "constant" => self.constant,
241 "constructor" => self.constructor,
242 "embedded" => self.embedded,
243 "emphasis" => self.emphasis,
244 "emphasis.strong" => self.emphasis_strong,
245 "enum" => self.enum_,
246 "function" => self.function,
247 "hint" => self.hint,
248 "keyword" => self.keyword,
249 "label" => self.label,
250 "link_text" => self.link_text,
251 "link_uri" => self.link_uri,
252 "number" => self.number,
253 "operator" => self.operator,
254 "predictive" => self.predictive,
255 "preproc" => self.preproc,
256 "primary" => self.primary,
257 "property" => self.property,
258 "punctuation" => self.punctuation,
259 "punctuation.bracket" => self.punctuation_bracket,
260 "punctuation.delimiter" => self.punctuation_delimiter,
261 "punctuation.list_marker" => self.punctuation_list_marker,
262 "punctuation.special" => self.punctuation_special,
263 "string" => self.string,
264 "string.escape" => self.string_escape,
265 "string.regex" => self.string_regex,
266 "string.special" => self.string_special,
267 "string.special.symbol" => self.string_special_symbol,
268 "tag" => self.tag,
269 "tag.doctype" => self.tag_doctype,
270 "text.code.span" => self.text_code_span,
271 "text.literal" => self.text_literal,
272 "title" => self.title,
273 "type" => self.type_,
274 "variable" => self.variable,
275 "variable.special" => self.variable_special,
276 "variant" => self.variant,
277 _ => None,
278 }
279 .map(|s| s.into());
280
281 if style.is_some() {
282 style
283 } else if name.contains('.') {
284 name.split('.').next().and_then(|prefix| self.style(prefix))
285 } else {
286 None
287 }
288 }
289
290 pub fn style_for_index(&self, index: usize) -> Option<HighlightStyle> {
291 const HIGHLIGHT_NAMES: [&str; 41] = [
292 "attribute",
293 "boolean",
294 "comment",
295 "comment.doc",
296 "constant",
297 "constructor",
298 "embedded",
299 "emphasis",
300 "emphasis.strong",
301 "enum",
302 "function",
303 "hint",
304 "keyword",
305 "label",
306 "link_text",
307 "link_uri",
308 "number",
309 "operator",
310 "predictive",
311 "preproc",
312 "primary",
313 "property",
314 "punctuation",
315 "punctuation.bracket",
316 "punctuation.delimiter",
317 "punctuation.list_marker",
318 "punctuation.special",
319 "string",
320 "string.escape",
321 "string.regex",
322 "string.special",
323 "string.special.symbol",
324 "tag",
325 "tag.doctype",
326 "text.code.span",
327 "text.literal",
328 "title",
329 "type",
330 "variable",
331 "variable.special",
332 "variant",
333 ];
334
335 HIGHLIGHT_NAMES.get(index).and_then(|name| self.style(name))
336 }
337}
338
339#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, JsonSchema, Serialize, Deserialize)]
340pub struct StatusColors {
341 }
343
344impl StatusColors {
345 pub fn error(&self, _cx: &gpui::App) -> gpui::Hsla {
346 gpui::Hsla::default()
347 }
348
349 pub fn error_background(&self, _cx: &gpui::App) -> gpui::Hsla {
350 gpui::Hsla::default()
351 }
352
353 pub fn error_border(&self, _cx: &gpui::App) -> gpui::Hsla {
354 gpui::Hsla::default()
355 }
356
357 pub fn warning(&self, _cx: &gpui::App) -> gpui::Hsla {
358 gpui::Hsla::default()
359 }
360
361 pub fn warning_background(&self, _cx: &gpui::App) -> gpui::Hsla {
362 gpui::Hsla::default()
363 }
364
365 pub fn warning_border(&self, _cx: &gpui::App) -> gpui::Hsla {
366 gpui::Hsla::default()
367 }
368
369 pub fn info(&self, _cx: &gpui::App) -> gpui::Hsla {
370 gpui::Hsla::default()
371 }
372
373 pub fn info_background(&self, _cx: &gpui::App) -> gpui::Hsla {
374 gpui::Hsla::default()
375 }
376
377 pub fn info_border(&self, _cx: &gpui::App) -> gpui::Hsla {
378 gpui::Hsla::default()
379 }
380
381 pub fn success(&self, _cx: &gpui::App) -> gpui::Hsla {
382 gpui::Hsla::default()
383 }
384
385 pub fn success_background(&self, _cx: &gpui::App) -> gpui::Hsla {
386 gpui::Hsla::default()
387 }
388
389 pub fn success_border(&self, _cx: &gpui::App) -> gpui::Hsla {
390 gpui::Hsla::default()
391 }
392
393 pub fn hint(&self, _cx: &gpui::App) -> gpui::Hsla {
394 gpui::Hsla::default()
395 }
396
397 pub fn hint_background(&self, _cx: &gpui::App) -> gpui::Hsla {
398 gpui::Hsla::default()
399 }
400
401 pub fn hint_border(&self, _cx: &gpui::App) -> gpui::Hsla {
402 gpui::Hsla::default()
403 }
404}
405
406#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
407#[serde(default)]
408pub struct HighlightThemeStyle {
409 #[serde(
410 rename = "editor.background",
411 deserialize_with = "crate::deserialize_optional_hsla"
412 )]
413 pub editor_background: Option<gpui::Hsla>,
414 #[serde(
415 rename = "editor.foreground",
416 deserialize_with = "crate::deserialize_optional_hsla"
417 )]
418 pub editor_foreground: Option<gpui::Hsla>,
419 #[serde(
420 rename = "editor.active_line.background",
421 deserialize_with = "crate::deserialize_optional_hsla"
422 )]
423 pub editor_active_line: Option<gpui::Hsla>,
424 #[serde(
425 rename = "editor.line_number",
426 deserialize_with = "crate::deserialize_optional_hsla"
427 )]
428 pub editor_line_number: Option<gpui::Hsla>,
429 #[serde(
430 rename = "editor.active_line_number",
431 deserialize_with = "crate::deserialize_optional_hsla"
432 )]
433 pub editor_active_line_number: Option<gpui::Hsla>,
434 #[serde(
435 rename = "editor.invisible",
436 deserialize_with = "crate::deserialize_optional_hsla"
437 )]
438 pub editor_invisible: Option<gpui::Hsla>,
439 #[serde(
440 rename = "editor.gutter.background",
441 deserialize_with = "crate::deserialize_optional_hsla"
442 )]
443 pub editor_gutter_background: Option<gpui::Hsla>,
444 #[serde(flatten)]
445 pub status: StatusColors,
446 #[serde(rename = "syntax")]
447 pub syntax: SyntaxColors,
448}
449
450impl JsonSchema for HighlightThemeStyle {
451 fn schema_name() -> Cow<'static, str> {
452 "HighlightThemeStyle".into()
453 }
454
455 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
456 schemars::json_schema!({ "type": "object" })
457 }
458}
459
460#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
461pub struct HighlightTheme {
462 pub name: String,
463 #[serde(default)]
464 pub appearance: crate::ThemeMode,
465 pub style: HighlightThemeStyle,
466}
467
468impl JsonSchema for HighlightTheme {
469 fn schema_name() -> Cow<'static, str> {
470 "HighlightTheme".into()
471 }
472
473 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
474 schemars::json_schema!({ "type": "object" })
475 }
476}
477
478impl std::ops::Deref for HighlightTheme {
479 type Target = SyntaxColors;
480
481 fn deref(&self) -> &Self::Target {
482 &self.style.syntax
483 }
484}
485
486impl HighlightTheme {
487 pub fn default_dark() -> std::sync::Arc<Self> {
488 use crate::DEFAULT_THEME_COLORS;
489 DEFAULT_THEME_COLORS[&crate::ThemeMode::Dark].1.clone()
490 }
491
492 pub fn default_light() -> std::sync::Arc<Self> {
493 use crate::DEFAULT_THEME_COLORS;
494 DEFAULT_THEME_COLORS[&crate::ThemeMode::Light].1.clone()
495 }
496}
497
498impl gpui_base::input::HighlightStyleResolver for HighlightTheme {
499 fn style(&self, name: &str) -> Option<HighlightStyle> {
500 self.style.syntax.style(name)
501 }
502}
503
504pub struct LanguageRegistry {
506 languages: Mutex<HashMap<SharedString, GrammarConfig>>,
507}
508
509impl LanguageRegistry {
510 pub fn singleton() -> &'static LazyLock<LanguageRegistry> {
511 static INSTANCE: LazyLock<LanguageRegistry> = LazyLock::new(|| LanguageRegistry {
512 languages: Mutex::new(HashMap::new()),
513 });
514 &INSTANCE
515 }
516
517 pub fn register(&self, lang: &str, config: &GrammarConfig) {
518 self.languages
519 .lock()
520 .unwrap()
521 .insert(lang.to_string().into(), config.clone());
522 }
523
524 pub(crate) fn editing_language_name(&self, name: &str) -> SharedString {
525 self.languages
526 .lock()
527 .unwrap()
528 .get_key_value(name)
529 .map(|(name, _)| name.clone())
530 .unwrap_or_else(|| super::language_name(name))
531 }
532
533 pub fn languages(&self) -> Vec<SharedString> {
534 self.languages.lock().unwrap().keys().cloned().collect()
535 }
536
537 pub fn language(&self, name: &str) -> Option<GrammarConfig> {
538 self.languages.lock().unwrap().get(name).cloned()
539 }
540}
541
542#[cfg(test)]
543mod registry_compat_tests {
544 use super::*;
545
546 #[test]
547 fn registrations_preserve_exact_names_without_alias_fallback() {
548 let registry = LanguageRegistry {
549 languages: Mutex::new(HashMap::new()),
550 };
551 registry.register(
552 "json",
553 &LanguageConfig {
554 name: "canonical".into(),
555 },
556 );
557 assert!(registry.language("jsonc").is_none());
558 registry.register(
559 "jsonc",
560 &LanguageConfig {
561 name: "custom alias".into(),
562 },
563 );
564 registry.register(
565 "JSON",
566 &LanguageConfig {
567 name: "custom uppercase".into(),
568 },
569 );
570 assert_eq!(registry.language("json").unwrap().name, "canonical");
571 assert_eq!(registry.language("jsonc").unwrap().name, "custom alias");
572 assert_eq!(registry.language("JSON").unwrap().name, "custom uppercase");
573 assert!(registry.language("Json").is_none());
574 let mut names = registry.languages();
575 names.sort();
576 assert_eq!(names, vec!["JSON", "json", "jsonc"]);
577 assert_eq!(registry.editing_language_name("jsonc"), "jsonc");
578 assert_eq!(registry.editing_language_name("JSON"), "JSON");
579 assert_eq!(registry.editing_language_name("pyi"), "python");
580 }
581}