88use std:: collections:: HashMap ;
99
1010use crate :: meta:: ClassId ;
11- use crate :: registry:: plugin:: { ITraitImpl , InherentImpl , PluginItem , Struct } ;
11+ use crate :: obj:: GodotClass ;
12+
13+ /// Piece of information that is gathered by the self-registration ("plugin") system.
14+ ///
15+ /// You should not manually construct this struct, but rather use [`DocsPlugin::new()`].
16+ #[ derive( Debug ) ]
17+ pub struct DocsPlugin {
18+ /// The name of the class to register docs for.
19+ class_name : ClassId ,
20+
21+ /// The actual item being registered.
22+ item : DocsItem ,
23+ }
24+
25+ impl DocsPlugin {
26+ /// Creates a new `DocsPlugin`, automatically setting the `class_name` to the values defined in [`GodotClass`].
27+ pub fn new < T : GodotClass > ( item : DocsItem ) -> Self {
28+ Self {
29+ class_name : T :: class_id ( ) ,
30+ item,
31+ }
32+ }
33+ }
34+
35+ type ITraitImplDocs = & ' static str ;
36+
37+ #[ derive( Debug ) ]
38+ pub enum DocsItem {
39+ /// Docs for `#[derive(GodotClass)] struct MyClass`.
40+ Struct ( StructDocs ) ,
41+ /// Docs for `#[godot_api] impl MyClass`.
42+ InherentImpl ( InherentImplDocs ) ,
43+ /// Docs for `#[godot_api] impl ITrait for MyClass`.
44+ ITraitImpl ( ITraitImplDocs ) ,
45+ }
1246
1347/// Created for documentation on
1448/// ```ignore
@@ -29,7 +63,7 @@ pub struct StructDocs {
2963 pub members : & ' static str ,
3064}
3165
32- /// Keeps documentation for inherent `impl` blocks, such as:
66+ /// Keeps documentation for inherent `impl` blocks (primary and secondary) , such as:
3367/// ```ignore
3468/// #[godot_api]
3569/// impl Struct {
@@ -46,18 +80,19 @@ pub struct StructDocs {
4680/// }
4781/// ```
4882/// All fields are XML parts, escaped where necessary.
49- #[ derive( Default , Copy , Clone , Debug ) ]
83+ #[ derive( Default , Clone , Debug ) ]
5084pub struct InherentImplDocs {
5185 pub methods : & ' static str ,
52- pub signals_block : & ' static str ,
53- pub constants_block : & ' static str ,
86+ pub signals : & ' static str ,
87+ pub constants : & ' static str ,
5488}
5589
5690#[ derive( Default ) ]
5791struct DocPieces {
5892 definition : StructDocs ,
59- inherent : InherentImplDocs ,
60- virtual_methods : & ' static str ,
93+ methods : Vec < & ' static str > ,
94+ signals : Vec < & ' static str > ,
95+ constants : Vec < & ' static str > ,
6196}
6297
6398/// This function scours the registered plugins to find their documentation pieces,
@@ -76,24 +111,27 @@ struct DocPieces {
76111#[ doc( hidden) ]
77112pub fn gather_xml_docs ( ) -> impl Iterator < Item = String > {
78113 let mut map = HashMap :: < ClassId , DocPieces > :: new ( ) ;
79- crate :: private:: iterate_plugins ( |x| {
114+ crate :: private:: iterate_docs_plugins ( |x| {
80115 let class_name = x. class_name ;
81-
82- match x. item {
83- PluginItem :: InherentImpl ( InherentImpl { docs, .. } ) => {
84- map. entry ( class_name) . or_default ( ) . inherent = docs
116+ match & x. item {
117+ DocsItem :: Struct ( s) => {
118+ map. entry ( class_name) . or_default ( ) . definition = * s;
85119 }
86-
87- PluginItem :: ITraitImpl ( ITraitImpl {
88- virtual_method_docs,
89- ..
90- } ) => map. entry ( class_name) . or_default ( ) . virtual_methods = virtual_method_docs,
91-
92- PluginItem :: Struct ( Struct { docs, .. } ) => {
93- map. entry ( class_name) . or_default ( ) . definition = docs
120+ DocsItem :: InherentImpl ( trait_docs) => {
121+ let InherentImplDocs {
122+ methods,
123+ constants,
124+ signals,
125+ } = trait_docs;
126+ map. entry ( class_name) . or_default ( ) . methods . push ( methods) ;
127+ map. entry ( class_name)
128+ . and_modify ( |pieces| pieces. constants . push ( constants) ) ;
129+ map. entry ( class_name)
130+ . and_modify ( |pieces| pieces. signals . push ( signals) ) ;
131+ }
132+ DocsItem :: ITraitImpl ( methods) => {
133+ map. entry ( class_name) . or_default ( ) . methods . push ( methods) ;
94134 }
95-
96- _ => ( ) ,
97135 }
98136 } ) ;
99137
@@ -106,22 +144,31 @@ pub fn gather_xml_docs() -> impl Iterator<Item = String> {
106144 members,
107145 } = pieces. definition ;
108146
109- let InherentImplDocs {
110- methods,
111- signals_block,
112- constants_block,
113- } = pieces. inherent ;
114-
115- let virtual_methods = pieces. virtual_methods ;
116- let methods_block = ( virtual_methods. is_empty ( ) && methods. is_empty ( ) )
117- . then ( String :: new)
118- . unwrap_or_else ( || format ! ( "<methods>{methods}{virtual_methods}</methods>" ) ) ;
119147
148+ let method_docs = String :: from_iter ( pieces. methods ) ;
149+ let signal_docs = String :: from_iter ( pieces. signals ) ;
150+ let constant_docs = String :: from_iter ( pieces. constants ) ;
151+
152+ let methods_block = if method_docs. is_empty ( ) {
153+ String :: new ( )
154+ } else {
155+ format ! ( "<methods>{method_docs}</methods>" )
156+ } ;
157+ let signals_block = if signal_docs. is_empty ( ) {
158+ String :: new ( )
159+ } else {
160+ format ! ( "<signals>{signal_docs}</signals>" )
161+ } ;
162+ let constants_block = if constant_docs. is_empty ( ) {
163+ String :: new ( )
164+ } else {
165+ format ! ( "<constants>{constant_docs}</constants>" )
166+ } ;
120167 let ( brief, description) = match description
121168 . split_once ( "[br]" ) {
122- Some ( ( brief, description) ) => ( brief, description. trim_start_matches ( "[br]" ) ) ,
123- None => ( description, "" ) ,
124- } ;
169+ Some ( ( brief, description) ) => ( brief, description. trim_start_matches ( "[br]" ) ) ,
170+ None => ( description, "" ) ,
171+ } ;
125172
126173 format ! ( r#"<?xml version="1.0" encoding="UTF-8"?>
127174<class name="{class}" inherits="{base}"{deprecated}{experimental} xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
0 commit comments