Finish passing Syntax from VSCode themes to Zed Themes

Co-Authored-By: Marshall Bowers <1486634+maxdeviant@users.noreply.github.com>
This commit is contained in:
Nate Butler 2023-11-09 14:41:26 -05:00
parent ff053890cf
commit 54157eb99a
20 changed files with 1797 additions and 470 deletions

View file

@ -15,5 +15,6 @@ log.workspace = true
rust-embed.workspace = true
serde.workspace = true
simplelog = "0.9"
strum = { version = "0.25.0", features = ["derive"] }
theme = { package = "theme2", path = "../theme2", features = ["importing-themes"] }
uuid.workspace = true

View file

@ -158,9 +158,10 @@ fn main() -> Result<()> {
use gpui::rgba;
#[allow(unused)]
use crate::{{
Appearance, StatusColorsRefinement, ThemeColorsRefinement, UserHighlightStyle, UserSyntaxTheme,
UserTheme, UserThemeFamily, UserThemeStylesRefinement,
UserTheme, UserThemeFamily, UserThemeStylesRefinement, UserFontWeight, UserFontStyle
}};
pub fn {theme_family_slug}() -> UserThemeFamily {{

View file

@ -1,15 +1,18 @@
use anyhow::Result;
use gpui::{Hsla, Rgba};
use indexmap::IndexMap;
use strum::IntoEnumIterator;
use theme::{
StatusColorsRefinement, ThemeColorsRefinement, UserFontStyle, UserFontWeight, UserSyntaxTheme,
UserTheme, UserThemeStylesRefinement,
StatusColorsRefinement, ThemeColorsRefinement, UserFontStyle, UserFontWeight,
UserHighlightStyle, UserSyntaxTheme, UserTheme, UserThemeStylesRefinement,
};
use crate::util::Traverse;
use crate::vscode::VsCodeTheme;
use crate::ThemeMetadata;
use super::{VsCodeTokenScope, ZedSyntaxToken};
pub(crate) fn try_parse_color(color: &str) -> Result<Hsla> {
Ok(Rgba::try_from(color)?.into())
}
@ -47,16 +50,7 @@ impl VsCodeThemeConverter {
let status_color_refinements = self.convert_status_colors()?;
let theme_colors_refinements = self.convert_theme_colors()?;
let mut highlight_styles = IndexMap::new();
for token_color in self.theme.token_colors {
highlight_styles.extend(token_color.highlight_styles()?);
}
let syntax_theme = UserSyntaxTheme {
highlights: highlight_styles.into_iter().collect(),
};
let syntax_theme = self.convert_syntax_theme()?;
Ok(UserTheme {
name: self.theme_metadata.name.into(),
@ -259,4 +253,114 @@ impl VsCodeThemeConverter {
..Default::default()
})
}
fn convert_syntax_theme(&self) -> Result<UserSyntaxTheme> {
let mut highlight_styles = IndexMap::new();
for syntax_token in ZedSyntaxToken::iter() {
let vscode_scope = syntax_token.to_vscode();
let token_color = self
.theme
.token_colors
.iter()
.find(|token_color| match token_color.scope {
Some(VsCodeTokenScope::One(ref scope)) => scope == vscode_scope,
Some(VsCodeTokenScope::Many(ref scopes)) => {
scopes.contains(&vscode_scope.to_string())
}
None => false,
});
let Some(token_color) = token_color else {
continue;
};
let highlight_style = UserHighlightStyle {
color: token_color
.settings
.foreground
.as_ref()
.traverse(|color| try_parse_color(&color))?,
font_style: token_color
.settings
.font_style
.as_ref()
.and_then(|style| try_parse_font_style(&style)),
font_weight: token_color
.settings
.font_style
.as_ref()
.and_then(|style| try_parse_font_weight(&style)),
};
if highlight_style.is_empty() {
continue;
}
highlight_styles.insert(syntax_token.to_string(), highlight_style);
}
Ok(UserSyntaxTheme {
highlights: highlight_styles.into_iter().collect(),
})
// let mut highlight_styles = IndexMap::new();
// for token_color in self.theme.token_colors {
// highlight_styles.extend(token_color.highlight_styles()?);
// }
// let syntax_theme = UserSyntaxTheme {
// highlights: highlight_styles.into_iter().collect(),
// };
// pub fn highlight_styles(&self) -> Result<IndexMap<String, UserHighlightStyle>> {
// let mut highlight_styles = IndexMap::new();
// for syntax_token in ZedSyntaxToken::iter() {
// let scope = syntax_token.to_scope();
// // let token_color =
// }
// let scope = match self.scope {
// Some(VsCodeTokenScope::One(ref scope)) => vec![scope.clone()],
// Some(VsCodeTokenScope::Many(ref scopes)) => scopes.clone(),
// None => return Ok(IndexMap::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))?,
// font_style: self
// .settings
// .font_style
// .as_ref()
// .and_then(|style| try_parse_font_style(&style)),
// font_weight: self
// .settings
// .font_style
// .as_ref()
// .and_then(|style| try_parse_font_weight(&style)),
// };
// if highlight_style.is_empty() {
// continue;
// }
// highlight_styles.insert(syntax_token, highlight_style);
// }
// Ok(highlight_styles)
// }
}
}

View file

@ -1,14 +1,5 @@
// 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 indexmap::IndexMap;
use serde::Deserialize;
use theme::UserHighlightStyle;
use crate::util::Traverse;
use crate::vscode::{try_parse_color, try_parse_font_style, try_parse_font_weight};
use strum::EnumIter;
#[derive(Debug, Deserialize)]
#[serde(untagged)]
@ -31,243 +22,145 @@ pub struct VsCodeTokenColorSettings {
pub font_style: Option<String>,
}
impl VsCodeTokenColor {
pub fn highlight_styles(&self) -> Result<IndexMap<String, UserHighlightStyle>> {
let mut highlight_styles = IndexMap::new();
#[derive(Debug, PartialEq, Copy, Clone, EnumIter)]
pub enum ZedSyntaxToken {
SyntaxAttribute,
SyntaxBoolean,
SyntaxComment,
SyntaxCommentDoc,
SyntaxConstant,
SyntaxConstructor,
SyntaxEmbedded,
SyntaxEmphasis,
SyntaxEmphasisStrong,
SyntaxEnum,
SyntaxFunction,
SyntaxHint,
SyntaxKeyword,
SyntaxLabel,
SyntaxLinkText,
SyntaxLinkUri,
SyntaxNumber,
SyntaxOperator,
SyntaxPredictive,
SyntaxPreproc,
SyntaxPrimary,
SyntaxProperty,
SyntaxPunctuation,
SyntaxPunctuationBracket,
SyntaxPunctuationDelimiter,
SyntaxPunctuationListMarker,
SyntaxPunctuationSpecial,
SyntaxString,
SyntaxStringEscape,
SyntaxStringRegex,
SyntaxStringSpecial,
SyntaxStringSpecialSymbol,
SyntaxTag,
SyntaxTextLiteral,
SyntaxTitle,
SyntaxType,
SyntaxVariable,
SyntaxVariableSpecial,
SyntaxVariant,
}
let scope = match self.scope {
Some(VsCodeTokenScope::One(ref scope)) => vec![scope.clone()],
Some(VsCodeTokenScope::Many(ref scopes)) => scopes.clone(),
None => return Ok(IndexMap::new()),
};
impl std::fmt::Display for ZedSyntaxToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use ZedSyntaxToken::*;
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))?,
font_style: self
.settings
.font_style
.as_ref()
.and_then(|style| try_parse_font_style(&style)),
font_weight: self
.settings
.font_style
.as_ref()
.and_then(|style| try_parse_font_weight(&style)),
};
if highlight_style.is_empty() {
continue;
write!(
f,
"{}",
match self {
SyntaxAttribute => "attribute",
SyntaxBoolean => "boolean",
SyntaxComment => "comment",
SyntaxCommentDoc => "comment.doc",
SyntaxConstant => "constant",
SyntaxConstructor => "constructor",
SyntaxEmbedded => "embedded",
SyntaxEmphasis => "emphasis",
SyntaxEmphasisStrong => "emphasis.strong",
SyntaxEnum => "enum",
SyntaxFunction => "function",
SyntaxHint => "hint",
SyntaxKeyword => "keyword",
SyntaxLabel => "label",
SyntaxLinkText => "link_text",
SyntaxLinkUri => "link_uri",
SyntaxNumber => "number",
SyntaxOperator => "operator",
SyntaxPredictive => "predictive",
SyntaxPreproc => "preproc",
SyntaxPrimary => "primary",
SyntaxProperty => "property",
SyntaxPunctuation => "punctuation",
SyntaxPunctuationBracket => "punctuation.bracket",
SyntaxPunctuationDelimiter => "punctuation.delimiter",
SyntaxPunctuationListMarker => "punctuation.list_marker",
SyntaxPunctuationSpecial => "punctuation.special",
SyntaxString => "string",
SyntaxStringEscape => "string.escape",
SyntaxStringRegex => "string.regex",
SyntaxStringSpecial => "string.special",
SyntaxStringSpecialSymbol => "string.special.symbol",
SyntaxTag => "tag",
SyntaxTextLiteral => "text.literal",
SyntaxTitle => "title",
SyntaxType => "type",
SyntaxVariable => "variable",
SyntaxVariableSpecial => "variable.special",
SyntaxVariant => "variant",
}
highlight_styles.insert(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" => ""
impl ZedSyntaxToken {
pub fn to_vscode(&self) -> &'static str {
use ZedSyntaxToken::*;
match self {
SyntaxAttribute => "entity.other.attribute-name",
SyntaxBoolean => "constant.language",
SyntaxComment => "comment",
SyntaxCommentDoc => "comment.block.documentation",
SyntaxConstant => "constant.character",
SyntaxConstructor => "entity.name.function.definition.special.constructor",
SyntaxEmbedded => "embedded",
SyntaxEmphasis => "emphasis",
SyntaxEmphasisStrong => "emphasis.strong",
SyntaxEnum => "support.type.enum",
SyntaxFunction => "entity.name.function",
SyntaxHint => "hint",
SyntaxKeyword => "keyword",
SyntaxLabel => "label",
SyntaxLinkText => "link_text",
SyntaxLinkUri => "link_uri",
SyntaxNumber => "number",
SyntaxOperator => "operator",
SyntaxPredictive => "predictive",
SyntaxPreproc => "preproc",
SyntaxPrimary => "primary",
SyntaxProperty => "variable.object.property", //"variable.other.field"
SyntaxPunctuation => "punctuation",
SyntaxPunctuationBracket => "punctuation.bracket",
SyntaxPunctuationDelimiter => "punctuation.delimiter",
SyntaxPunctuationListMarker => "punctuation.list_marker",
SyntaxPunctuationSpecial => "punctuation.special",
SyntaxString => "string",
SyntaxStringEscape => "string.escape",
SyntaxStringRegex => "string.regex",
SyntaxStringSpecial => "string.special",
SyntaxStringSpecialSymbol => "string.special.symbol",
SyntaxTag => "tag",
SyntaxTextLiteral => "text.literal",
SyntaxTitle => "title",
SyntaxType => "entity.name.type",
SyntaxVariable => "variable.language",
SyntaxVariableSpecial => "variable.special",
SyntaxVariant => "variant",
}
}
}