Skip to content

Commit

Permalink
Render token type names using Token! macro in documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 23, 2024
1 parent bd28fa8 commit 2e47ae3
Show file tree
Hide file tree
Showing 5 changed files with 1,062 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ required-features = ["full", "parsing"]
[package.metadata.docs.rs]
all-features = true
targets = ["x86_64-unknown-linux-gnu"]
rustdoc-args = ["--generate-link-to-definition"]
rustdoc-args = ["--generate-link-to-definition", "--extend-css=src/gen/token.css"]

[package.metadata.playground]
features = ["full", "visit", "visit-mut", "fold", "extra-traits"]
Expand Down
1 change: 1 addition & 0 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ json = ["syn-codegen/serde"]
anyhow = "1"
color-backtrace = "0.6"
indexmap = { version = "2", features = ["serde"] }
indoc = "2"
inflections = "1.1"
prettyplease = "0.2.3"
proc-macro2 = { version = "1.0.20", features = ["span-locations"] }
Expand Down
49 changes: 49 additions & 0 deletions codegen/src/css.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::workspace_path;
use anyhow::Result;
use indoc::{formatdoc, indoc};
use std::fs;
use syn_codegen::Definitions;

pub fn generate(defs: &Definitions) -> Result<()> {
let mut styles = String::new();
for ty in defs.tokens.keys() {
styles += &format!("a.struct[title=\"struct syn::token::{ty}\"],\n");
}
styles.truncate(styles.len() - 2);
styles += indoc! {"
{
\tdisplay: inline-block;
\tcolor: transparent;
\twhite-space: nowrap;
}
"};
styles.push('\n');
for ty in defs.tokens.keys() {
styles += &format!("a.struct[title=\"struct syn::token::{ty}\"]::before,\n");
}
styles.truncate(styles.len() - 2);
styles += indoc! {"
{
\tdisplay: inline-block;
\tcolor: var(--type-link-color);
\twidth: 0;
}
"};
for (ty, repr) in &defs.tokens {
let padding = ".".repeat((repr.len() + 8).saturating_sub(ty.len()));
styles += &formatdoc! {"
a.struct[title=\"struct syn::token::{ty}\"]::before {{
\tcontent: \"Token![{repr}]\";
}}
a.struct[title=\"struct syn::token::{ty}\"]::after {{
\tcontent: \"{padding}\";
}}
"};
}

let css_path = workspace_path::get("src/gen/token.css");
fs::write(css_path, styles)?;
Ok(())
}
2 changes: 2 additions & 0 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

mod cfg;
mod clone;
mod css;
mod debug;
mod eq;
mod file;
Expand Down Expand Up @@ -52,5 +53,6 @@ fn main() -> anyhow::Result<()> {
visit::generate(&defs)?;
visit_mut::generate(&defs)?;
snapshot::generate(&defs)?;
css::generate(&defs)?;
Ok(())
}
Loading

0 comments on commit 2e47ae3

Please sign in to comment.