WIP – Add Syntax themes to importer

Co-Authored-By: Marshall Bowers <1486634+maxdeviant@users.noreply.github.com>
This commit is contained in:
Nate Butler 2023-11-09 12:30:53 -05:00
parent 4b5ca3e420
commit efd1db1b09
19 changed files with 553 additions and 1724 deletions

View file

@ -1,6 +1,7 @@
mod theme_printer;
mod util;
mod vscode;
mod vscode_syntax;
use std::fs::{self, File};
use std::io::Write;

View file

@ -2,8 +2,8 @@ use std::fmt::{self, Debug};
use gpui::{Hsla, Rgba};
use theme::{
Appearance, PlayerColor, PlayerColors, StatusColors, StatusColorsRefinement, SyntaxTheme,
SystemColors, ThemeColorsRefinement, UserTheme, UserThemeFamily, UserThemeStylesRefinement,
Appearance, PlayerColor, PlayerColors, StatusColorsRefinement, SystemColors,
ThemeColorsRefinement, UserSyntaxTheme, UserTheme, UserThemeFamily, UserThemeStylesRefinement,
};
struct RawSyntaxPrinter<'a>(&'a str);
@ -30,6 +30,17 @@ impl<'a, D: Debug> Debug for IntoPrinter<'a, D> {
}
}
pub struct OptionPrinter<'a, T>(&'a Option<T>);
impl<'a, T: Debug> Debug for OptionPrinter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Some(value) => write!(f, "Some({:?})", value),
None => write!(f, "None"),
}
}
}
pub struct VecPrinter<'a, T>(&'a Vec<T>);
impl<'a, T: Debug> Debug for VecPrinter<'a, T> {
@ -93,6 +104,16 @@ impl<'a> Debug for UserThemeStylesRefinementPrinter<'a> {
f.debug_struct("UserThemeStylesRefinement")
.field("colors", &ThemeColorsRefinementPrinter(&self.0.colors))
.field("status", &StatusColorsRefinementPrinter(&self.0.status))
.field(
"syntax",
&OptionPrinter(
&self
.0
.syntax
.as_ref()
.map(|syntax| UserSyntaxThemePrinter(syntax)),
),
)
.finish()
}
}
@ -316,11 +337,11 @@ impl<'a> Debug for PlayerColorPrinter<'a> {
}
}
pub struct SyntaxThemePrinter<'a>(&'a SyntaxTheme);
pub struct UserSyntaxThemePrinter<'a>(&'a UserSyntaxTheme);
impl<'a> Debug for SyntaxThemePrinter<'a> {
impl<'a> Debug for UserSyntaxThemePrinter<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SyntaxTheme")
f.debug_struct("UserSyntaxTheme")
.field(
"highlights",
&VecPrinter(

View file

@ -1,9 +1,13 @@
use anyhow::Result;
use gpui::{Hsla, Rgba};
use serde::Deserialize;
use theme::{StatusColorsRefinement, ThemeColorsRefinement, UserTheme, UserThemeStylesRefinement};
use theme::{
StatusColorsRefinement, ThemeColorsRefinement, UserSyntaxTheme, UserTheme,
UserThemeStylesRefinement,
};
use crate::util::Traverse;
use crate::vscode_syntax::VsCodeTokenColor;
use crate::ThemeMetadata;
#[derive(Deserialize, Debug)]
@ -18,6 +22,8 @@ pub struct VsCodeTheme {
#[serde(rename = "semanticHighlighting")]
pub semantic_highlighting: Option<bool>,
pub colors: VsCodeColors,
#[serde(rename = "tokenColors")]
pub token_colors: Vec<VsCodeTokenColor>,
}
#[derive(Debug, Deserialize)]
@ -413,7 +419,7 @@ pub struct VsCodeColors {
pub list_filter_widget_no_matches_outline: Option<String>,
}
fn try_parse_color(color: &str) -> Result<Hsla> {
pub(crate) fn try_parse_color(color: &str) -> Result<Hsla> {
Ok(Rgba::try_from(color)?.into())
}
@ -618,12 +624,23 @@ impl VsCodeThemeConverter {
..Default::default()
};
let mut highlight_styles = Vec::new();
for token_color in self.theme.token_colors {
highlight_styles.extend(token_color.highlight_styles()?);
}
let syntax_theme = UserSyntaxTheme {
highlights: highlight_styles,
};
Ok(UserTheme {
name: self.theme_metadata.name.into(),
appearance,
styles: UserThemeStylesRefinement {
colors: theme_colors_refinements,
status: status_color_refinements,
syntax: Some(syntax_theme),
},
})
}

View file

@ -0,0 +1,261 @@
// Create ThemeSyntaxRefinement
// Map tokenColors style to HighlightStyle (fontStyle, foreground, background)
// Take in the scopes from the tokenColors and try to match each to our HighlightStyles
use anyhow::Result;
use serde::Deserialize;
use theme::UserHighlightStyle;
use crate::{util::Traverse, vscode::try_parse_color};
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum VsCodeTokenScope {
One(String),
Many(Vec<String>),
}
#[derive(Debug, Deserialize)]
pub struct VsCodeTokenColor {
pub scope: Option<VsCodeTokenScope>,
pub settings: VsCodeTokenColorSettings,
}
#[derive(Debug, Deserialize)]
pub struct VsCodeTokenColorSettings {
pub foreground: Option<String>,
pub background: Option<String>,
#[serde(rename = "fontStyle")]
pub font_style: Option<String>,
}
impl VsCodeTokenColor {
pub fn highlight_styles(&self) -> Result<Vec<(String, UserHighlightStyle)>> {
let mut highlight_styles = Vec::new();
let scope = match self.scope {
Some(VsCodeTokenScope::One(ref scope)) => vec![scope.clone()],
Some(VsCodeTokenScope::Many(ref scopes)) => scopes.clone(),
None => return Ok(Vec::new()),
};
for scope in &scope {
let Some(syntax_token) = Self::to_zed_token(&scope) else {
continue;
};
let highlight_style = UserHighlightStyle {
color: self
.settings
.foreground
.as_ref()
.traverse(|color| try_parse_color(&color))?,
};
if highlight_style.is_empty() {
continue;
}
highlight_styles.push((syntax_token, highlight_style));
}
Ok(highlight_styles)
}
fn to_zed_token(scope: &str) -> Option<String> {
match scope {
"attribute" => Some("attribute".to_string()),
"boolean" => Some("boolean".to_string()),
"comment" => Some("comment".to_string()),
"comment.doc" => Some("comment.doc".to_string()),
"punctuation"
| "punctuation.accessor"
| "punctuation.definition.array.begin.json"
| "punctuation.definition.array.end.json"
| "punctuation.definition.dictionary.begin.json"
| "punctuation.definition.dictionary.end.json"
| "punctuation.definition.markdown"
| "punctuation.definition.tag"
| "punctuation.definition.tag.begin"
| "punctuation.definition.tag.end"
| "punctuation.definition.template-expression"
| "punctuation.definition.variable"
| "punctuation.section"
| "punctuation.section.embedded"
| "punctuation.section.embedded.begin"
| "punctuation.section.embedded.end"
| "punctuation.separator"
| "punctuation.separator.array.json"
| "punctuation.separator.dictionary.key-value.json"
| "punctuation.separator.dictionary.pair.json" => Some("punctuation".to_string()),
// ---
"constant" | "character" | "language" | "language.python" | "numeric" | "other"
| "other.symbol" => Some("something".to_string()),
"entity"
| "name"
| "name.class"
| "name.filename.find-in-files"
| "name.function"
| "name.function.python"
| "name.import"
| "name.package"
| "name.tag"
| "name.type"
| "name.type.class.python"
| "other.attribute-name"
| "other.inherited-class" => Some("something".to_string()),
"markup" | "bold" | "changed" | "deleted" | "heading" | "heading.setext"
| "inline.raw" | "italic" | "list" | "quote" | "raw" | "raw.inline" | "strike"
| "table" | "underline.link" => Some("something".to_string()),
"source" => Some("something".to_string()),
"storage" => Some("something".to_string()),
"string" => Some("something".to_string()),
"support" => Some("something".to_string()),
"text" => Some("something".to_string()),
"token" => Some("something".to_string()),
"variable" => Some("something".to_string()),
_ => None,
}
}
}
// "comment" => ""
// "constant.character" => ""
// "constant.language" => ""
// "constant.language.python" => ""
// "constant.numeric" => ""
// "constant.numeric.line-number.find-in-files - match" => ""
// "constant.numeric.line-number.match" => ""
// "constant.other" => ""
// "constant.other.symbol" => ""
// "entity.name" => ""
// "entity.name.class" => ""
// "entity.name.filename.find-in-files" => ""
// "entity.name.function" => ""
// "entity.name.function.python" => ""
// "entity.name.import" => ""
// "entity.name.package" => ""
// "entity.name.tag" => ""
// "entity.name.type" => ""
// "entity.name.type.class.python" => ""
// "entity.other.attribute-name" => ""
// "entity.other.inherited-class" => ""
// "invalid" => ""
// "keyword" => ""
// "keyword.control.from" => ""
// "keyword.control.import" => ""
// "keyword.operator" => ""
// "keyword.other.new" => ""
// "markup.bold markup.italic" => ""
// "markup.bold" => ""
// "markup.changed" => ""
// "markup.deleted" => ""
// "markup.heading entity.name" => ""
// "markup.heading" => ""
// "markup.heading.setext" => ""
// "markup.inline.raw" => ""
// "markup.inserted" => ""
// "markup.inserted" => ""
// "markup.italic markup.bold" => ""
// "markup.italic" => ""
// "markup.list punctuation.definition.list.begin" => ""
// "markup.list" => ""
// "markup.quote" => ""
// "markup.raw" => ""
// "markup.raw.inline" => ""
// "markup.strike" => ""
// "markup.table" => ""
// "markup.underline.link" => ""
// "message.error" => ""
// "meta.decorator punctuation.decorator" => ""
// "meta.decorator variable.other" => ""
// "meta.diff" => ""
// "meta.diff.header" => ""
// "meta.embedded" => ""
// "meta.function-call" => ""
// "meta.function-call.generic" => ""
// "meta.import" => ""
// "meta.parameter" => ""
// "meta.preprocessor" => ""
// "meta.separator" => ""
// "meta.tag.sgml" => ""
// "punctuation.accessor" => ""
// "punctuation.definition.array.begin.json" => ""
// "punctuation.definition.array.end.json" => ""
// "punctuation.definition.dictionary.begin.json" => ""
// "punctuation.definition.dictionary.end.json" => ""
// "punctuation.definition.markdown" => ""
// "punctuation.definition.tag" => ""
// "punctuation.definition.tag.begin" => ""
// "punctuation.definition.tag.end" => ""
// "punctuation.definition.template-expression" => ""
// "punctuation.definition.variable" => ""
// "punctuation.section" => ""
// "punctuation.section.embedded" => ""
// "punctuation.section.embedded.begin" => ""
// "punctuation.section.embedded.end" => ""
// "punctuation.separator" => ""
// "punctuation.separator.array.json" => ""
// "punctuation.separator.dictionary.key-value.json" => ""
// "punctuation.separator.dictionary.pair.json" => ""
// "punctuation.terminator" => ""
// "source.c storage.type" => ""
// "source.css entity.name.tag" => ""
// "source.css support.type" => ""
// "source.go storage.type" => ""
// "source.groovy.embedded" => ""
// "source.haskell storage.type" => ""
// "source.java storage.type" => ""
// "source.java storage.type.primitive" => ""
// "source.less entity.name.tag" => ""
// "source.less support.type" => ""
// "source.python" => ""
// "source.ruby variable.other.readwrite" => ""
// "source.sass entity.name.tag" => ""
// "source.sass support.type" => ""
// "source.scss entity.name.tag" => ""
// "source.scss support.type" => ""
// "source.stylus entity.name.tag" => ""
// "source.stylus support.type" => ""
// "source.ts" => ""
// "storage" => ""
// "storage.modifier" => ""
// "storage.modifier.async" => ""
// "storage.modifier.tsx" => ""
// "storage.type.annotation" => ""
// "storage.type.function" => ""
// "string" => ""
// "string.other.link" => ""
// "string.regexp" => ""
// "support.class" => ""
// "support.class.component" => ""
// "support.constant" => ""
// "support.function" => ""
// "support.function.construct" => ""
// "support.function.go" => ""
// "support.macro" => ""
// "support.other.variable" => ""
// "support.type" => ""
// "support.type.exception" => ""
// "support.type.primitive" => ""
// "support.type.property-name" => ""
// "support.type.python" => ""
// "text.html.markdown markup.inline.raw" => ""
// "text.html.markdown meta.dummy.line-break" => ""
// "token.debug-token" => ""
// "token.error-token" => ""
// "token.info-token" => ""
// "token.warn-token" => ""
// "variable" => ""
// "variable.annotation" => ""
// "variable.function" => ""
// "variable.language" => ""
// "variable.member" => ""
// "variable.object.property" => ""
// "variable.other" => ""
// "variable.parameter" => ""
// "variable.parameter.function-call" => ""