Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

ui_macros: Remove `DerivePathStr` macro (#30862)

Marshall Bowers03419da6f153
diff --git a/Cargo.lock b/Cargo.lock
index 5869e7f9fab..300f3a24d49 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -15696,8 +15696,6 @@ dependencies = [
name = "ui_macros"
version = "0.1.0"
dependencies = [
- "convert_case 0.8.0",
- "proc-macro2",
"quote",
"syn 1.0.109",
"workspace-hack",
diff --git a/crates/gpui_macros/src/derive_path_static_str.rs b/crates/gpui_macros/src/derive_path_static_str.rs
deleted file mode 100644
index 80ac813c4a7..00000000000
--- a/crates/gpui_macros/src/derive_path_static_str.rs
+++ /dev/null
@@ -1,73 +0,0 @@
-use proc_macro::TokenStream;
-use quote::quote;
-use syn::{Attribute, Data, DeriveInput, Lit, Meta, NestedMeta, parse_macro_input};
-
-pub fn derive_path_static_str(input: TokenStream) -> TokenStream {
- let input = parse_macro_input!(input as DeriveInput);
- let name = &input.ident;
-
- let prefix = get_attr_value(&input.attrs, "prefix").unwrap_or_else(|| "".to_string());
- let suffix = get_attr_value(&input.attrs, "suffix").unwrap_or_else(|| "".to_string());
- let delimiter = get_attr_value(&input.attrs, "delimiter").unwrap_or_else(|| "/".to_string());
-
- let path_str_impl = impl_path_str(name, &input.data, &prefix, &suffix, &delimiter);
-
- let expanded = quote! {
- impl #name {
- pub fn path_str(&self) -> &'static str {
- #path_str_impl
- }
- }
- };
-
- TokenStream::from(expanded)
-}
-
-fn impl_path_str(
- name: &syn::Ident,
- data: &Data,
- prefix: &str,
- suffix: &str,
- delimiter: &str,
-) -> proc_macro2::TokenStream {
- match *data {
- Data::Enum(ref data) => {
- let match_arms = data.variants.iter().map(|variant| {
- let ident = &variant.ident;
- let path = format!("{}{}{}{}{}", prefix, delimiter, ident, delimiter, suffix);
- quote! {
- #name::#ident => #path,
- }
- });
-
- quote! {
- match self {
- #(#match_arms)*
- }
- }
- }
- _ => panic!("DerivePathStr only supports enums"),
- }
-}
-
-fn get_attr_value(attrs: &[Attribute], key: &str) -> Option<String> {
- attrs
- .iter()
- .filter(|attr| attr.path.is_ident("derive_path_static_str"))
- .find_map(|attr| {
- if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
- meta_list.nested.iter().find_map(|nested_meta| {
- if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
- if name_value.path.is_ident(key) {
- if let Lit::Str(lit_str) = &name_value.lit {
- return Some(lit_str.value());
- }
- }
- }
- None
- })
- } else {
- None
- }
- })
-}
diff --git a/crates/gpui_macros/src/gpui_macros.rs b/crates/gpui_macros/src/gpui_macros.rs
index 497476965d4..7e1b39cf688 100644
--- a/crates/gpui_macros/src/gpui_macros.rs
+++ b/crates/gpui_macros/src/gpui_macros.rs
@@ -1,6 +1,5 @@
mod derive_app_context;
mod derive_into_element;
-mod derive_path_static_str;
mod derive_render;
mod derive_visual_context;
mod register_action;
@@ -31,12 +30,6 @@ pub fn derive_render(input: TokenStream) -> TokenStream {
derive_render::derive_render(input)
}
-#[proc_macro_derive(PathStaticStr)]
-#[doc(hidden)]
-pub fn derive_path_static_str(input: TokenStream) -> TokenStream {
- derive_path_static_str::derive_path_static_str(input)
-}
-
/// #[derive(AppContext)] is used to create a context out of anything that holds a `&mut App`
/// Note that a `#[app]` attribute is required to identify the variable holding the &mut App.
///
diff --git a/crates/ui/src/tests.rs b/crates/ui/src/tests.rs
deleted file mode 100644
index 3f26326a056..00000000000
--- a/crates/ui/src/tests.rs
+++ /dev/null
@@ -1 +0,0 @@
-mod path_str;
diff --git a/crates/ui/src/tests/path_str.rs b/crates/ui/src/tests/path_str.rs
deleted file mode 100644
index 90598d6eb4c..00000000000
--- a/crates/ui/src/tests/path_str.rs
+++ /dev/null
@@ -1,35 +0,0 @@
-// We need to test [ui_macros::DerivePathStr] here as we can't invoke it
-// in the `ui_macros` crate.
-#[cfg(test)]
-mod tests {
- use strum::EnumString;
- use ui_macros::{DerivePathStr, path_str};
-
- #[test]
- fn test_derive_path_str_with_prefix() {
- #[derive(Debug, EnumString, DerivePathStr)]
- #[strum(serialize_all = "snake_case")]
- #[path_str(prefix = "test_prefix")]
- enum SomeAsset {
- FooBar,
- Baz,
- }
-
- assert_eq!(SomeAsset::FooBar.path(), "test_prefix/foo_bar");
- assert_eq!(SomeAsset::Baz.path(), "test_prefix/baz");
- }
-
- #[test]
- fn test_derive_path_str_with_prefix_and_suffix() {
- #[derive(Debug, EnumString, DerivePathStr)]
- #[strum(serialize_all = "snake_case")]
- #[path_str(prefix = "test_prefix", suffix = ".svg")]
- enum SomeAsset {
- FooBar,
- Baz,
- }
-
- assert_eq!(SomeAsset::FooBar.path(), "test_prefix/foo_bar.svg");
- assert_eq!(SomeAsset::Baz.path(), "test_prefix/baz.svg");
- }
-}
diff --git a/crates/ui/src/ui.rs b/crates/ui/src/ui.rs
index f0d93e2e27b..dadc5ecdd12 100644
--- a/crates/ui/src/ui.rs
+++ b/crates/ui/src/ui.rs
@@ -11,7 +11,6 @@ pub mod component_prelude;
mod components;
pub mod prelude;
mod styles;
-mod tests;
mod traits;
pub mod utils;
diff --git a/crates/ui_macros/Cargo.toml b/crates/ui_macros/Cargo.toml
index 5699b25d6cf..d468c133d40 100644
--- a/crates/ui_macros/Cargo.toml
+++ b/crates/ui_macros/Cargo.toml
@@ -13,8 +13,6 @@ path = "src/ui_macros.rs"
proc-macro = true
[dependencies]
-convert_case.workspace = true
-proc-macro2.workspace = true
quote.workspace = true
syn.workspace = true
workspace-hack.workspace = true
diff --git a/crates/ui_macros/src/derive_path_str.rs b/crates/ui_macros/src/derive_path_str.rs
deleted file mode 100644
index e17471fb18e..00000000000
--- a/crates/ui_macros/src/derive_path_str.rs
+++ /dev/null
@@ -1,105 +0,0 @@
-use convert_case::{Case, Casing};
-use proc_macro::TokenStream;
-use quote::quote;
-use syn::{Attribute, Data, DeriveInput, Lit, Meta, NestedMeta, parse_macro_input};
-
-pub fn derive_path_str(input: TokenStream) -> TokenStream {
- let input = parse_macro_input!(input as DeriveInput);
- let name = &input.ident;
-
- let prefix = get_attr_value(&input.attrs, "prefix").expect("prefix attribute is required");
- let suffix = get_attr_value(&input.attrs, "suffix").unwrap_or_else(|| "".to_string());
-
- let serialize_all = get_strum_serialize_all(&input.attrs);
- let path_str_impl = impl_path_str(name, &input.data, &prefix, &suffix, serialize_all);
-
- let expanded = quote! {
- impl #name {
- pub fn path(&self) -> &'static str {
- #path_str_impl
- }
- }
- };
-
- TokenStream::from(expanded)
-}
-
-fn impl_path_str(
- name: &syn::Ident,
- data: &Data,
- prefix: &str,
- suffix: &str,
- serialize_all: Option<String>,
-) -> proc_macro2::TokenStream {
- match *data {
- Data::Enum(ref data) => {
- let match_arms = data.variants.iter().map(|variant| {
- let ident = &variant.ident;
- let variant_name = if let Some(ref case) = serialize_all {
- match case.as_str() {
- "snake_case" => ident.to_string().to_case(Case::Snake),
- "lowercase" => ident.to_string().to_lowercase(),
- _ => ident.to_string(),
- }
- } else {
- ident.to_string()
- };
- let path = format!("{}/{}{}", prefix, variant_name, suffix);
- quote! {
- #name::#ident => #path,
- }
- });
-
- quote! {
- match self {
- #(#match_arms)*
- }
- }
- }
- _ => panic!("DerivePathStr only supports enums"),
- }
-}
-
-fn get_strum_serialize_all(attrs: &[Attribute]) -> Option<String> {
- attrs
- .iter()
- .filter(|attr| attr.path.is_ident("strum"))
- .find_map(|attr| {
- if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
- meta_list.nested.iter().find_map(|nested_meta| {
- if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
- if name_value.path.is_ident("serialize_all") {
- if let Lit::Str(lit_str) = &name_value.lit {
- return Some(lit_str.value());
- }
- }
- }
- None
- })
- } else {
- None
- }
- })
-}
-
-fn get_attr_value(attrs: &[Attribute], key: &str) -> Option<String> {
- attrs
- .iter()
- .filter(|attr| attr.path.is_ident("path_str"))
- .find_map(|attr| {
- if let Ok(Meta::List(meta_list)) = attr.parse_meta() {
- meta_list.nested.iter().find_map(|nested_meta| {
- if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
- if name_value.path.is_ident(key) {
- if let Lit::Str(lit_str) = &name_value.lit {
- return Some(lit_str.value());
- }
- }
- }
- None
- })
- } else {
- None
- }
- })
-}
diff --git a/crates/ui_macros/src/ui_macros.rs b/crates/ui_macros/src/ui_macros.rs
index aa0b72455a6..db002e7bb3d 100644
--- a/crates/ui_macros/src/ui_macros.rs
+++ b/crates/ui_macros/src/ui_macros.rs
@@ -1,59 +1,8 @@
-mod derive_path_str;
mod derive_register_component;
mod dynamic_spacing;
use proc_macro::TokenStream;
-/// Derives the `path` method for an enum.
-///
-/// This macro generates a `path` method for each variant of the enum, which returns a string
-/// representation of the enum variant's path. The path is constructed using a prefix and
-/// optionally a suffix, which are specified using attributes.
-///
-/// # Attributes
-///
-/// - `#[path_str(prefix = "...")]`: Required. Specifies the prefix for all paths.
-/// - `#[path_str(suffix = "...")]`: Optional. Specifies a suffix for all paths.
-/// - `#[strum(serialize_all = "...")]`: Optional. Specifies the case conversion for variant names.
-///
-/// # Example
-///
-/// ```
-/// use strum::EnumString;
-/// use ui_macros::{path_str, DerivePathStr};
-///
-/// #[derive(EnumString, DerivePathStr)]
-/// #[path_str(prefix = "my_prefix", suffix = ".txt")]
-/// #[strum(serialize_all = "snake_case")]
-/// enum MyEnum {
-/// VariantOne,
-/// VariantTwo,
-/// }
-///
-/// // These assertions would work if we could instantiate the enum
-/// // assert_eq!(MyEnum::VariantOne.path(), "my_prefix/variant_one.txt");
-/// // assert_eq!(MyEnum::VariantTwo.path(), "my_prefix/variant_two.txt");
-/// ```
-///
-/// # Panics
-///
-/// This macro will panic if used on anything other than an enum.
-#[proc_macro_derive(DerivePathStr, attributes(path_str))]
-pub fn derive_path_str(input: TokenStream) -> TokenStream {
- derive_path_str::derive_path_str(input)
-}
-
-/// A marker attribute for use with `DerivePathStr`.
-///
-/// This attribute is used to specify the prefix and suffix for the `path` method
-/// generated by `DerivePathStr`. It doesn't modify the input and is only used as a
-/// marker for the derive macro.
-#[proc_macro_attribute]
-pub fn path_str(_args: TokenStream, input: TokenStream) -> TokenStream {
- // This attribute doesn't modify the input, it's just a marker
- input
-}
-
/// Generates the DynamicSpacing enum used for density-aware spacing in the UI.
#[proc_macro]
pub fn derive_dynamic_spacing(input: TokenStream) -> TokenStream {