Skip to content

Generate a visualization of the editor's hierarchical message system tree #2499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
43f5323
Feat: implement the hierarchical tree for visualization
MohdMohsin97 Mar 29, 2025
f9ee826
rename HierarchicalTree trait function
MohdMohsin97 Mar 29, 2025
11e104f
Merge branch 'GraphiteEditor:master' into hierarchical-tree
MohdMohsin97 Apr 11, 2025
0afbaf5
Merge branch 'hierarchical-tree' of https://github.com/MohdMohsin97/G…
MohdMohsin97 Apr 11, 2025
0f9e491
feat: change the HierarchicalTree from String to DebugMessageTree struct
MohdMohsin97 Apr 12, 2025
d1a0294
Merge branch 'master' into hierarchical-tree
Keavon Apr 18, 2025
e232c5d
Nits
Keavon Apr 18, 2025
9fec71c
Merge branch 'GraphiteEditor:master' into hierarchical-tree
MohdMohsin97 Apr 19, 2025
9f5ad94
feat: impliment proc macro to extract field from messagedata structs
MohdMohsin97 Apr 19, 2025
9395617
Merge branch 'hierarchical-tree' of https://github.com/MohdMohsin97/G…
MohdMohsin97 Apr 19, 2025
dcfbb97
update the hierarchical-tree for hanlder data
MohdMohsin97 Apr 23, 2025
1e11239
Merge branch 'GraphiteEditor:master' into hierarchical-tree
MohdMohsin97 Apr 27, 2025
730db02
Merge branch 'GraphiteEditor:master' into hierarchical-tree
MohdMohsin97 May 1, 2025
fc9e258
feat: added message handler struct to hierarchical tree
MohdMohsin97 May 1, 2025
1af7c6e
Merge branch 'hierarchical-tree' of https://github.com/MohdMohsin97/G…
MohdMohsin97 May 1, 2025
160a9d0
Merge branch 'GraphiteEditor:master' into hierarchical-tree
MohdMohsin97 May 1, 2025
ee272e5
Merge branch 'hierarchical-tree' of https://github.com/MohdMohsin97/G…
MohdMohsin97 May 1, 2025
c2c629d
feat: add the line number to message handler struct
MohdMohsin97 May 10, 2025
c2c6ac5
feat: added handler path to tree and NITS
MohdMohsin97 May 12, 2025
6d4c29c
Merge branch 'master' into hierarchical-tree
MohdMohsin97 May 13, 2025
d034c4c
clean the white spaces in type string
MohdMohsin97 May 19, 2025
396ed0c
Merge branch 'hierarchical-tree' of https://github.com/MohdMohsin97/G…
MohdMohsin97 May 19, 2025
0a9741f
Merge branch 'master' into hierarchical-tree
Keavon May 19, 2025
4ee1bcf
Merge branch 'master' into hierarchical-tree
Keavon May 19, 2025
065af10
fixes some white spaces
MohdMohsin97 May 25, 2025
007610b
Merge branch 'hierarchical-tree' of https://github.com/MohdMohsin97/G…
MohdMohsin97 May 25, 2025
7bc5397
feat: added path to message enum in hierarchical tree
MohdMohsin97 May 26, 2025
ca8dbfb
feat: add file creation of hierarchical message system tree
MohdMohsin97 May 28, 2025
cff1d81
Merge branch 'GraphiteEditor:master' into hierarchical-tree
MohdMohsin97 May 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: change the HierarchicalTree from String to DebugMessageTree struct
  • Loading branch information
MohdMohsin97 committed Apr 12, 2025
commit 0f9e49183663a1968777b48d05807edf63d5b380
2 changes: 2 additions & 0 deletions editor/src/messages/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod debug_message_handler;

pub mod utility_types;

pub use utility_types::DebugMessageTree;

#[doc(inline)]
pub use debug_message::{DebugMessage, DebugMessageDiscriminant};
#[doc(inline)]
Expand Down
30 changes: 30 additions & 0 deletions editor/src/messages/debug/utility_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,33 @@ pub enum MessageLoggingVerbosity {
Names,
Contents,
}

pub struct DebugMessageTree {
name: String,
variants: Option<Vec<DebugMessageTree>>,
}

impl DebugMessageTree {
pub fn new(name: &str) -> DebugMessageTree {
DebugMessageTree {
name: name.to_string(),
variants: None,
}
}

pub fn add_variant(&mut self, variant: DebugMessageTree) {
if let Some(variants) = &mut self.variants {
variants.push(variant);
} else {
self.variants = Some(vec![variant]);
}
}

pub fn name(&self) -> &str {
&self.name
}

pub fn variants(&self) -> Option<&Vec<DebugMessageTree>> {
self.variants.as_ref()
}
}
35 changes: 35 additions & 0 deletions editor/src/messages/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,38 @@ impl specta::Type for MessageDiscriminant {
specta::DataType::Any
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn generate_message_tree() {
let res = Message::build_message_tree();
println!("{}", res.name());
if let Some(variants) = res.variants() {
for (i, variant) in variants.iter().enumerate() {
let is_last = i == variants.len() - 1;
print_tree_node(variant, "", is_last);
}
}
}

fn print_tree_node(tree: &DebugMessageTree, prefix: &str, is_last: bool) {
// Print the current node
let branch = if is_last { "└── " } else { "├── " };
println!("{}{}{}", prefix, branch, tree.name());

// Prepare prefix for children
let child_prefix = if is_last { format!("{} ", prefix) } else { format!("{}│ ", prefix) };

// Print children if any
if let Some(variants) = tree.variants() {
let len = variants.len();
for (i, variant) in variants.iter().enumerate() {
let is_last_child = i == len - 1;
print_tree_node(variant, &child_prefix, is_last_child);
}
}
}
}
4 changes: 2 additions & 2 deletions editor/src/messages/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Root
pub use crate::utility_traits::{ActionList, AsMessage, MessageHandler, ToDiscriminant, TransitiveChild, HierarchicalTree};
pub use crate::utility_traits::{ActionList, AsMessage, HierarchicalTree, MessageHandler, ToDiscriminant, TransitiveChild};

// Message, MessageData, MessageDiscriminant, MessageHandler
pub use crate::messages::animation::{AnimationMessage, AnimationMessageDiscriminant, AnimationMessageHandler};
pub use crate::messages::broadcast::{BroadcastMessage, BroadcastMessageDiscriminant, BroadcastMessageHandler};
pub use crate::messages::debug::{DebugMessage, DebugMessageDiscriminant, DebugMessageHandler};
pub use crate::messages::debug::{DebugMessage, DebugMessageDiscriminant, DebugMessageHandler, DebugMessageTree};
pub use crate::messages::dialog::export_dialog::{ExportDialogMessage, ExportDialogMessageData, ExportDialogMessageDiscriminant, ExportDialogMessageHandler};
pub use crate::messages::dialog::new_document_dialog::{NewDocumentDialogMessage, NewDocumentDialogMessageDiscriminant, NewDocumentDialogMessageHandler};
pub use crate::messages::dialog::preferences_dialog::{PreferencesDialogMessage, PreferencesDialogMessageData, PreferencesDialogMessageDiscriminant, PreferencesDialogMessageHandler};
Expand Down
3 changes: 1 addition & 2 deletions editor/src/utility_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ pub trait Hint {
}

pub trait HierarchicalTree {
fn generate_hierarchical_tree() -> Vec<String>;
fn generate_enum_variants(depth: usize, tree: &mut Vec<String>);
fn build_message_tree() -> DebugMessageTree;
}
33 changes: 17 additions & 16 deletions proc-macros/src/hierarchical_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ pub fn generate_hierarchical_tree(input: TokenStream) -> syn::Result<TokenStream
_ => return Err(syn::Error::new(Span::call_site(), "Tried to derive HierarchicalTree for non-enum")),
};

let variant_prints = data.variants.iter().enumerate().map(|(index, variant)| {
let build_message_tree = data.variants.iter().map(|variant| {
let variant_type = &variant.ident;
let is_last = index == data.variants.len() - 1;
let tree_symbol = if is_last { "└──" } else { "├──" };

let has_child = variant
.attrs
Expand All @@ -25,32 +23,35 @@ pub fn generate_hierarchical_tree(input: TokenStream) -> syn::Result<TokenStream
if let Fields::Unnamed(fields) = &variant.fields {
let field_type = &fields.unnamed.first().unwrap().ty;
quote! {
tree.push(format!("{}{}{}", "│ ".repeat(depth), #tree_symbol, stringify!(#variant_type)));
<#field_type>::generate_enum_variants(depth + 1, tree);
{
let mut variant_tree = DebugMessageTree::new(stringify!(#variant_type));
let field_name = stringify!(#field_type);
if "Message" == &field_name[field_name.len().saturating_sub(7)..] {
// The field is a Message type, recursively build its tree
let sub_tree = #field_type::build_message_tree();
variant_tree.add_variant(sub_tree);
}
message_tree.add_variant(variant_tree);
}
}
} else {
quote! {
tree.push(format!("{}{}{}", "│ ".repeat(depth), #tree_symbol, stringify!(#variant_type)));
message_tree.add_variant(DebugMessageTree::new(stringify!(#variant_type)));
}
}
} else {
quote! {
tree.push(format!("{}{}{}", "│ ".repeat(depth), #tree_symbol, stringify!(#variant_type)));
message_tree.add_variant(DebugMessageTree::new(stringify!(#variant_type)));
}
}
});

let res = quote! {
impl HierarchicalTree for #input_type {
fn generate_hierarchical_tree() -> Vec<String> {
let mut hierarchical_tree = Vec::new();
hierarchical_tree.push(format!("{}", stringify!(#input_type)));
Self::generate_enum_variants(0, &mut hierarchical_tree);
hierarchical_tree
}

fn generate_enum_variants(depth: usize, tree: &mut Vec<String>) {
#(#variant_prints)*
fn build_message_tree() -> DebugMessageTree {
let mut message_tree = DebugMessageTree::new(stringify!(#input_type));
#(#build_message_tree)*
message_tree
}
}
};
Expand Down