workspacer_syntax/
generate_trait_signature.rs

1// ---------------- [ File: workspacer-syntax/src/generate_trait_signature.rs ]
2crate::ix!();
3
4#[derive(Debug, Clone)]
5pub struct TraitSignatureGenerator(ast::Trait);
6
7impl GenerateSignature for ast::Trait {
8    fn generate_signature(&self) -> String {
9        // Just call the "with_opts" version using default SignatureOptions.
10        trace!("generate_signature called on ast::Trait (no custom opts)");
11        self.generate_signature_with_opts(&SignatureOptions::default())
12    }
13
14    fn generate_signature_with_opts(&self, opts: &SignatureOptions) -> String {
15        trace!("generate_signature_with_opts called on ast::Trait; fully_expand = {}", opts.fully_expand());
16
17        // 1) Gather trait visibility, name, generics:
18        let vis = if let Some(vis) = self.visibility() {
19            let txt = vis.syntax().text().to_string();
20            trace!("Trait visibility: {:?}", txt);
21            format!("{} ", txt)
22        } else {
23            "".to_string()
24        };
25
26        let name = self.name()
27            .map(|it| it.text().to_string())
28            .unwrap_or_else(|| "<anon_trait>".to_string());
29        trace!("Trait name: {:?}", name);
30
31        let generics = self.generic_param_list()
32            .map(|gp| gp.syntax().text().to_string())
33            .unwrap_or_default();
34        if !generics.is_empty() {
35            trace!("Trait generics: {:?}", generics);
36        }
37
38        // 2) If we're *not* fully expanding, just produce a placeholder:
39        if !*opts.fully_expand() {
40            trace!("Not fully expanding trait items; returning short signature");
41            return format!("{}trait {}{} {{ /* trait items here */ }}", vis, name, generics);
42        }
43
44
45        // 3) If we *are* fully expanding, gather the trait's associated items:
46        let mut lines = Vec::new();
47        if let Some(item_list) = self.assoc_item_list() {
48            for assoc in item_list.assoc_items() {
49                // Methods:
50                if let Some(fn_ast) = ast::Fn::cast(assoc.syntax().clone()) {
51                    let mut opts = opts.clone();
52                    opts.set_add_semicolon(true);
53                    // We can rely on our existing `GenerateSignature` for `ast::Fn`,
54                    // or define a short signature ourselves. Here, we'll just call
55                    // fn_ast.generate_signature() for consistency:
56                    let method_sig = fn_ast.generate_signature_with_opts(&opts);
57                    lines.push(method_sig);
58
59                // Associated types:
60                } else if let Some(type_ast) = ast::TypeAlias::cast(assoc.syntax().clone()) {
61                    // For now, just push the raw text. You could parse out `type Foo;`
62                    // or `type Foo = Something;` more elegantly if desired.
63                    let raw_txt = type_ast.syntax().text().to_string();
64                    lines.push(raw_txt);
65
66                // Associated consts, etc.:
67                } else if let Some(const_ast) = ast::Const::cast(assoc.syntax().clone()) {
68                    let raw_txt = const_ast.syntax().text().to_string();
69                    lines.push(raw_txt);
70                }
71            }
72        }
73
74        // 4) Format each line with indentation inside the trait braces:
75        let mut items_str = String::new();
76        for line in lines {
77            for sub_line in line.lines() {
78                items_str.push_str("    ");
79                items_str.push_str(sub_line);
80                items_str.push('\n');
81            }
82        }
83
84        if items_str.trim().is_empty() {
85            trace!("No associated items found in trait; returning empty braces");
86            format!("{}trait {}{} {{}}", vis, name, generics)
87        } else {
88            trace!("Returning fully expanded trait signature with items");
89            format!("{}trait {}{} {{\n{}}}", vis, name, generics, items_str)
90        }
91    }
92}