Skip to main content

gpui_component_macros/
lib.rs

1use proc_macro::TokenStream;
2use quote::quote;
3use syn::parse::{Parse, ParseStream};
4
5mod crate_path;
6mod derive_into_plot;
7
8/// Input for icon_name! macro: EnumName, "path", [optional derives]
9struct IconNameInput {
10    enum_name: syn::Ident,
11    _comma: syn::Token![,],
12    path: syn::LitStr,
13    derives: Option<(
14        syn::Token![,],
15        syn::punctuated::Punctuated<syn::Path, syn::Token![,]>,
16    )>,
17}
18
19impl Parse for IconNameInput {
20    fn parse(input: ParseStream) -> syn::Result<Self> {
21        let enum_name = input.parse()?;
22        let _comma = input.parse()?;
23        let path = input.parse()?;
24
25        // Check if there's an optional derives list
26        let derives = if input.peek(syn::Token![,]) {
27            let comma = input.parse()?;
28            let content;
29            syn::bracketed!(content in input);
30            let derives = content.parse_terminated(syn::Path::parse, syn::Token![,])?;
31            Some((comma, derives))
32        } else {
33            None
34        };
35
36        Ok(IconNameInput {
37            enum_name,
38            _comma,
39            path,
40            derives,
41        })
42    }
43}
44
45#[proc_macro_derive(IntoPlot)]
46pub fn derive_into_plot(input: TokenStream) -> TokenStream {
47    derive_into_plot::derive_into_plot(input)
48}
49
50/// Convert an SVG filename to PascalCase identifier.
51///
52/// Strips `.svg` extension, splits on separators (`-`, `_`, `.`),
53/// and capitalizes each word following Rust naming conventions.
54///
55/// # Examples
56///
57/// ```ignore
58/// assert_eq!(pascal_case("arrow-right.svg"), "ArrowRight");
59/// assert_eq!(pascal_case("some_icon_name.svg"), "SomeIconName");
60/// assert_eq!(pascal_case("icon-123.svg"), "Icon123");
61/// ```
62fn pascal_case(filename: &str) -> String {
63    filename
64        .strip_suffix(".svg")
65        .unwrap_or(filename)
66        .split(|c: char| c == '-' || c == '_' || c == '.')
67        .filter(|part| !part.is_empty())
68        .map(|word| {
69            let mut chars = word.chars();
70            match chars.next() {
71                None => String::new(),
72                Some(first) if first.is_ascii_digit() => word.to_string(),
73                Some(first) => {
74                    let mut result = String::with_capacity(word.len());
75                    result.extend(first.to_uppercase());
76                    result.push_str(&chars.as_str().to_lowercase());
77                    result
78                }
79            }
80        })
81        .collect()
82}
83
84/// Generate a custom icon enum and its `IconNamed` impl by scanning a directory of SVG files.
85///
86/// Accepts an enum name, a path, and optionally a list of additional derive traits.
87/// Each `.svg` file becomes an enum variant using PascalCase conversion.
88///
89/// The path may be either:
90///
91/// - **A literal path** (the common case), resolved relative to the calling crate's
92///   `CARGO_MANIFEST_DIR`. Use this when the icons live inside your own package.
93/// - **An env-var reference** of the form `"$NAME"`, where `NAME` names a build-time
94///   environment variable whose value is the absolute path to the icons directory.
95///   Use this when the icons live in *another* crate and the path is plumbed
96///   through cargo's `links` / `DEP_<X>_<KEY>` propagation mechanism. The default
97///   `IconName` enum in `gpui-component` uses this pattern to consume icons from
98///   `gpui-kit-assets` without a sibling-crate reference, which would
99///   otherwise break `cargo vendor` and `cargo publish`.
100///
101/// # Example
102///
103/// ```ignore
104/// // Literal path (relative to the calling crate's CARGO_MANIFEST_DIR)
105/// icon_named!(IconName, "icons");
106///
107/// // Env-var reference (resolved at macro expansion time)
108/// icon_named!(IconName, "$GPUI_KIT_DEFAULT_ICONS_DIR");
109///
110/// // With custom derives
111/// icon_named!(IconName, "icons", [Debug, Copy, PartialEq, Eq]);
112/// ```
113#[proc_macro]
114pub fn icon_named(input: TokenStream) -> TokenStream {
115    let IconNameInput {
116        enum_name,
117        path,
118        derives,
119        ..
120    } = syn::parse_macro_input!(input as IconNameInput);
121
122    let raw_path = path.value();
123
124    // Resolve the path. A leading `$` switches us into env-var mode: the
125    // remainder of the string is an env var name whose value (set by the
126    // caller's `build.rs` via `cargo:rustc-env=`) is the absolute path of
127    // the icons directory. Otherwise treat the string as a path relative
128    // to the calling crate's `CARGO_MANIFEST_DIR`, the original behavior.
129    let icons_dir = if let Some(env_name) = raw_path.strip_prefix('$') {
130        let env_value = std::env::var(env_name).unwrap_or_else(|_| {
131            panic!(
132                "icon_named!: env var `{env_name}` is not set at expansion time. \
133                 Ensure the calling crate's build.rs propagates it via \
134                 `cargo:rustc-env={env_name}=<absolute path>`."
135            )
136        });
137        std::path::PathBuf::from(env_value)
138    } else {
139        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
140        std::path::Path::new(&manifest_dir).join(&raw_path)
141    };
142
143    let mut entries: Vec<(String, String)> = Vec::new();
144
145    let dir = std::fs::read_dir(&icons_dir).unwrap_or_else(|e| {
146        panic!(
147            "generate_icon_enum: failed to read '{}': {}",
148            icons_dir.display(),
149            e
150        )
151    });
152
153    for entry in dir {
154        let entry = entry.expect("failed to read directory entry");
155        let filename = entry.file_name().to_string_lossy().to_string();
156        if filename.ends_with(".svg") {
157            let variant_name = pascal_case(&filename);
158            let path = format!("icons/{}", filename);
159            entries.push((variant_name, path));
160        }
161    }
162
163    entries.sort_by(|a, b| a.0.cmp(&b.0));
164
165    let variants: Vec<proc_macro2::Ident> = entries
166        .iter()
167        .map(|(name, _)| proc_macro2::Ident::new(name, proc_macro2::Span::call_site()))
168        .collect();
169    let paths: Vec<&str> = entries.iter().map(|(_, p)| p.as_str()).collect();
170
171    // Build derive list: always include IntoElement and Clone, then add custom derives
172    let derive_attrs = if let Some((_, custom_derives)) = derives {
173        let derives_vec: Vec<_> = custom_derives.iter().collect();
174        quote! {
175            #[derive(IntoElement, Clone, #(#derives_vec),*)]
176        }
177    } else {
178        quote! {
179            #[derive(IntoElement, Clone)]
180        }
181    };
182
183    let expanded = quote! {
184        #derive_attrs
185
186        pub enum #enum_name {
187            #(#variants,)*
188        }
189
190        impl IconNamed for #enum_name {
191            fn path(self) -> SharedString {
192                match self {
193                    #(Self::#variants => #paths,)*
194                }
195                .into()
196            }
197        }
198    };
199
200    TokenStream::from(expanded)
201}
202
203#[cfg(test)]
204mod tests {
205    use super::*;
206
207    #[test]
208    fn test_pascal_case_basic() {
209        assert_eq!(pascal_case("arrow-right.svg"), "ArrowRight");
210        assert_eq!(pascal_case("home.svg"), "Home");
211        assert_eq!(pascal_case("x-circle.svg"), "XCircle");
212
213        assert_eq!(pascal_case("some_icon_name.svg"), "SomeIconName");
214        assert_eq!(pascal_case("arrow_up_down.svg"), "ArrowUpDown");
215
216        assert_eq!(pascal_case("kebab-case_mixed.svg"), "KebabCaseMixed");
217        assert_eq!(pascal_case("icon-with_under.svg"), "IconWithUnder");
218
219        assert_eq!(pascal_case("icon-123.svg"), "Icon123");
220        assert_eq!(pascal_case("arrow-2x.svg"), "Arrow2x");
221        assert_eq!(pascal_case("24-hour.svg"), "24Hour");
222
223        assert_eq!(pascal_case("arrow--right.svg"), "ArrowRight");
224        assert_eq!(pascal_case("icon__name.svg"), "IconName");
225        assert_eq!(pascal_case("multiple---dash.svg"), "MultipleDash");
226
227        assert_eq!(pascal_case("a.svg"), "A");
228        assert_eq!(pascal_case("-leading.svg"), "Leading");
229        assert_eq!(pascal_case("trailing-.svg"), "Trailing");
230        assert_eq!(pascal_case("-.svg"), "");
231
232        assert_eq!(pascal_case("arrow-right"), "ArrowRight");
233        assert_eq!(pascal_case("home"), "Home");
234
235        assert_eq!(pascal_case("hello.svg"), "Hello");
236        assert_eq!(pascal_case("WORLD.svg"), "World");
237        assert_eq!(pascal_case("iOS-icon.svg"), "IosIcon");
238        assert_eq!(pascal_case("API-key.svg"), "ApiKey");
239    }
240}