-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathdocs.rs
275 lines (255 loc) · 8.21 KB
/
docs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (c) godot-rust; Bromeon and contributors.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
mod markdown_converter;
use crate::class::{ConstDefinition, Field, FuncDefinition, SignalDefinition};
use proc_macro2::{Ident, TokenStream};
use quote::{quote, ToTokens};
use venial::*;
pub fn make_definition_docs(
base: String,
description: &[Attribute],
members: &[Field],
) -> TokenStream {
(|| {
let base_escaped = xml_escape(base);
let desc_escaped = xml_escape(make_docs_from_attributes(description)?);
let members = members
.into_iter()
.filter(|x| x.var.is_some() | x.export.is_some())
.filter_map(member)
.collect::<String>();
Some(quote! {
docs: ::godot::docs::StructDocs {
base: #base_escaped,
description: #desc_escaped,
members: #members,
}.into()
})
})()
.unwrap_or(quote! { docs: None })
}
pub fn make_inherent_impl_docs(
functions: &[FuncDefinition],
constants: &[ConstDefinition],
signals: &[SignalDefinition],
) -> TokenStream {
/// Generates TokenStream containing field definitions for documented methods and documentation blocks for constants and signals.
fn pieces(
functions: &[FuncDefinition],
signals: &[SignalDefinition],
constants: &[ConstDefinition],
) -> TokenStream {
let to_tagged = |s: String, tag: &str| -> String {
if s.is_empty() {
s
} else {
format!("<{tag}>{s}</{tag}>")
}
};
let signals_block = to_tagged(
signals
.iter()
.filter_map(make_signal_docs)
.collect::<String>(),
"signals",
);
let constants_block = to_tagged(
constants
.iter()
.map(|ConstDefinition { raw_constant }| raw_constant)
.filter_map(make_constant_docs)
.collect::<String>(),
"constants",
);
let methods = functions
.iter()
.filter_map(make_method_docs)
.collect::<String>();
quote! {
docs: ::godot::docs::InherentImplDocs {
methods: #methods,
signals_block: #signals_block,
constants_block: #constants_block,
}
}
}
pieces(functions, signals, constants)
}
pub fn make_virtual_impl_docs(vmethods: &[ImplMember]) -> TokenStream {
let virtual_methods = vmethods
.iter()
.filter_map(|x| match x {
venial::ImplMember::AssocFunction(f) => Some(f.clone()),
_ => None,
})
.filter_map(make_virtual_method_docs)
.collect::<String>();
quote! { virtual_method_docs: #virtual_methods, }
}
/// `///` is expanded to `#[doc = "…"]`.
/// This function goes through and extracts the …
fn siphon_docs_from_attributes(doc: &[Attribute]) -> impl Iterator<Item = String> + '_ {
doc.iter()
// find #[doc]
.filter(|x| x.get_single_path_segment().is_some_and(|x| x == "doc"))
// #[doc = "…"]
.filter_map(|x| match &x.value {
AttributeValue::Equals(_, doc) => Some(doc),
_ => None,
})
.flat_map(|doc| {
doc.into_iter().map(|x| {
x.to_string()
.trim_start_matches('r')
.trim_start_matches('#')
.trim_start_matches('"')
.trim_end_matches('#')
.trim_end_matches('"')
.to_string()
})
})
}
fn xml_escape(value: String) -> String {
// Most strings have no special characters, so this check helps avoid unnecessary string copying
if !value.contains(&['&', '<', '>', '"', '\'']) {
return value;
}
let mut result = String::with_capacity(value.len());
for c in value.chars() {
match c {
'&' => result.push_str("&"),
'<' => result.push_str("<"),
'>' => result.push_str(">"),
'"' => result.push_str("""),
'\'' => result.push_str("'"),
c => result.push(c),
}
}
result
}
/// Calls [`siphon_docs_from_attributes`] and converts the result to BBCode
/// for Godot's consumption.
fn make_docs_from_attributes(doc: &[Attribute]) -> Option<String> {
let doc = siphon_docs_from_attributes(doc)
.collect::<Vec<_>>()
.join("\n");
(!doc.is_empty()).then(|| markdown_converter::to_bbcode(&doc))
}
fn make_signal_docs(signal: &SignalDefinition) -> Option<String> {
let name = &signal.signature.name;
let params = params(signal.signature.params.iter().filter_map(|(x, _)| match x {
FnParam::Receiver(_) => None,
FnParam::Typed(y) => Some((&y.name, &y.ty)),
}));
let desc = make_docs_from_attributes(&signal.external_attributes)?;
Some(format!(
r#"
<signal name="{name}">
{params}
<description>
{desc}
</description>
</signal>
"#,
name = xml_escape(name.to_string()),
desc = xml_escape(desc),
))
}
fn make_constant_docs(constant: &Constant) -> Option<String> {
let docs = make_docs_from_attributes(&constant.attributes)?;
let name = constant.name.to_string();
let value = constant
.initializer
.as_ref()
.map(|x| x.to_token_stream().to_string())
.unwrap_or("null".into());
Some(format!(
r#"<constant name="{name}" value="{value}">{docs}</constant>"#,
name = xml_escape(name),
value = xml_escape(value),
docs = xml_escape(docs),
))
}
pub fn member(member: &Field) -> Option<String> {
let docs = make_docs_from_attributes(&member.attributes)?;
let name = &member.name;
let ty = member.ty.to_token_stream().to_string();
let default = member.default_val.to_token_stream().to_string();
Some(format!(
r#"<member name="{name}" type="{ty}" default="{default}">{docs}</member>"#,
name = xml_escape(name.to_string()),
ty = xml_escape(ty),
default = xml_escape(default),
docs = xml_escape(docs),
))
}
fn params<'a, 'b>(params: impl Iterator<Item = (&'a Ident, &'b TypeExpr)>) -> String {
let mut output = String::new();
for (index, (name, ty)) in params.enumerate() {
output.push_str(&format!(
r#"<param index="{index}" name="{name}" type="{ty}" />"#,
name = xml_escape(name.to_string()),
ty = xml_escape(ty.to_token_stream().to_string()),
));
}
output
}
pub fn make_virtual_method_docs(method: Function) -> Option<String> {
let desc = make_docs_from_attributes(&method.attributes)?;
let name = method.name.to_string();
let ret = method
.return_ty
.map(|x| x.to_token_stream().to_string())
.unwrap_or("void".into());
let params = params(method.params.iter().filter_map(|(x, _)| match x {
FnParam::Receiver(_) => None,
FnParam::Typed(y) => Some((&y.name, &y.ty)),
}));
Some(format!(
r#"
<method name="_{name}">
<return type="{ret}" />
{params}
<description>
{desc}
</description>
</method>
"#,
name = xml_escape(name),
ret = xml_escape(ret),
desc = xml_escape(desc),
))
}
pub fn make_method_docs(method: &FuncDefinition) -> Option<String> {
let desc = make_docs_from_attributes(&method.external_attributes)?;
let name = method
.rename
.clone()
.unwrap_or_else(|| method.signature_info.method_name.to_string());
let ret = method.signature_info.ret_type.to_token_stream().to_string();
let params = params(
method
.signature_info
.param_idents
.iter()
.zip(&method.signature_info.param_types),
);
Some(format!(
r#"
<method name="{name}">
<return type="{ret}" />
{params}
<description>
{desc}
</description>
</method>
"#,
name = xml_escape(name),
ret = xml_escape(ret),
desc = xml_escape(desc),
))
}