Skip to content

Commit

Permalink
Merge pull request #1767 from dtolnay/shrinkgrow
Browse files Browse the repository at this point in the history
Consolidate styles by token type length
  • Loading branch information
dtolnay authored Oct 23, 2024
2 parents 3eff940 + b5cf3b1 commit c2386c6
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 405 deletions.
41 changes: 32 additions & 9 deletions codegen/src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::workspace_path;
use anyhow::Result;
use indoc::{formatdoc, indoc};
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fs;
use syn_codegen::Definitions;

Expand Down Expand Up @@ -30,17 +31,19 @@ pub fn generate(defs: &Definitions) -> Result<()> {
\twidth: 0;
}
"};
let mut shrink = BTreeMap::new();
let mut grow = BTreeMap::new();
for (ty, repr) in &defs.tokens {
let macro_len = "Token![]".len() + repr.len();
let ty_len = ty.len();
styles.push('\n');
styles += &match Ord::cmp(&macro_len, &ty_len) {
Ordering::Less => {
shrink
.entry((macro_len, ty_len))
.or_insert_with(Vec::new)
.push(ty);
formatdoc! {"
a.struct[title=\"struct syn::token::{ty}\"] {{
\tfont-size: calc(100% * {macro_len} / {ty_len});
}}
a.struct[title=\"struct syn::token::{ty}\"]::before {{
\tcontent: \"Token![{repr}]\";
\tfont-size: calc(100% * {ty_len} / {macro_len});
Expand All @@ -49,19 +52,39 @@ pub fn generate(defs: &Definitions) -> Result<()> {
}
Ordering::Equal => unreachable!(),
Ordering::Greater => {
let padding = ".".repeat(macro_len.saturating_sub(ty.len()));
let padding = macro_len.saturating_sub(ty.len());
grow.entry(padding).or_insert_with(Vec::new).push(ty);
formatdoc! {"
a.struct[title=\"struct syn::token::{ty}\"]::before {{
\tcontent: \"Token![{repr}]\";
}}
a.struct[title=\"struct syn::token::{ty}\"]::after {{
\tcontent: \"{padding}\";
}}
"}
}
};
}
for ((macro_len, ty_len), types) in shrink {
for ty in types {
styles += &format!("\na.struct[title=\"struct syn::token::{ty}\"],");
}
styles.truncate(styles.len() - 1);
styles += &formatdoc! {"
{{
\tfont-size: calc(100% * {macro_len} / {ty_len});
}}
"};
}
for (padding, types) in grow {
for ty in types {
styles += &format!("\na.struct[title=\"struct syn::token::{ty}\"]::after,");
}
styles.truncate(styles.len() - 1);
let padding = ".".repeat(padding);
styles += &formatdoc! {"
{{
\tcontent: \"{padding}\";
}}
"};
}

let css_path = workspace_path::get("src/gen/token.css");
fs::write(css_path, styles)?;
Expand Down
Loading

0 comments on commit c2386c6

Please sign in to comment.