-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathpartition.rs
More file actions
284 lines (256 loc) · 7.3 KB
/
Copy pathpartition.rs
File metadata and controls
284 lines (256 loc) · 7.3 KB
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
276
277
278
279
280
281
282
283
284
use super::DocNodeWithContext;
use super::GenerateCtx;
use crate::DeclarationDef;
use crate::Location;
use crate::js_doc::JsDocTag;
use indexmap::IndexMap;
use std::borrow::Cow;
use std::cell::RefCell;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::HashSet;
pub type Partitions<T> = IndexMap<T, Vec<DocNodeWithContext>>;
fn create_partitioner<'a, T, F>(
ctx: &GenerateCtx,
doc_nodes: impl Iterator<Item = Cow<'a, DocNodeWithContext>> + 'a,
flatten_namespaces: bool,
process: &F,
) -> Partitions<T>
where
F: Fn(&mut IndexMap<T, Vec<DocNodeWithContext>>, &DocNodeWithContext),
{
fn partitioner_inner<'a, T, F>(
ctx: &GenerateCtx,
partitions: &mut Partitions<T>,
parent_node: Option<&DocNodeWithContext>,
doc_nodes: Box<dyn Iterator<Item = Cow<'a, DocNodeWithContext>> + 'a>,
flatten_namespaces: bool,
process: &F,
visited_refs: &mut HashSet<Location>,
) where
F: Fn(&mut IndexMap<T, Vec<DocNodeWithContext>>, &DocNodeWithContext),
{
'outer: for node in doc_nodes {
for decl in &node.declarations {
if flatten_namespaces
&& matches!(decl.def, DeclarationDef::Namespace(..))
{
partitioner_inner(
ctx,
partitions,
Some(&node),
Box::new(
node
.namespace_children
.as_ref()
.unwrap()
.iter()
.map(Cow::Borrowed),
),
true,
process,
visited_refs,
);
}
if let Some(reference) = decl.reference_def() {
if visited_refs.insert(reference.target.clone()) {
partitioner_inner(
ctx,
partitions,
parent_node,
Box::new(ctx.resolve_reference(parent_node, &reference.target)),
flatten_namespaces,
process,
visited_refs,
);
visited_refs.remove(&reference.target);
}
// hack until reference nodes are separate from normal symbols
continue 'outer;
}
}
process(partitions, &node);
}
}
let mut partitions = IndexMap::default();
partitioner_inner(
ctx,
&mut partitions,
None,
Box::new(doc_nodes),
flatten_namespaces,
process,
&mut HashSet::new(),
);
partitions
}
pub fn partition_nodes_by_kind<'a>(
ctx: &GenerateCtx,
doc_nodes: impl Iterator<Item = Cow<'a, DocNodeWithContext>> + 'a,
flatten_namespaces: bool,
) -> Partitions<String> {
let name_to_kind =
RefCell::new(HashMap::<String, crate::node::DocNodeKind>::new());
let mut partitions = create_partitioner(
ctx,
doc_nodes,
flatten_namespaces,
&|partitions, node| {
let qname = node.get_qualified_name();
let mut index = name_to_kind.borrow_mut();
if let Some(&existing_kind) = index.get(qname) {
partitions
.get_mut(&existing_kind)
.unwrap()
.push(node.clone());
} else {
let kind = node.declarations[0].def.to_kind();
index.insert(qname.to_string(), kind);
partitions.entry(kind).or_default().push(node.clone());
}
},
);
sort_nodes(&mut partitions);
partitions
.sorted_by(|kind1, _nodes1, kind2, _nodes2| kind1.cmp(kind2))
.map(|(kind, nodes)| {
(
super::DocNodeKindCtx::from(kind).title_plural.to_string(),
nodes,
)
})
.collect()
}
pub fn partition_nodes_by_category<'a>(
ctx: &GenerateCtx,
doc_nodes: impl Iterator<Item = Cow<'a, DocNodeWithContext>> + 'a,
flatten_namespaces: bool,
) -> Partitions<String> {
let mut partitions = create_partitioner(
ctx,
doc_nodes,
flatten_namespaces,
&|partitions, node| {
let category = node
.declarations
.iter()
.flat_map(|decl| decl.js_doc.tags.iter())
.find_map(|tag| {
if let JsDocTag::Category { doc } = tag {
Some(doc.trim().to_owned())
} else {
None
}
})
.unwrap_or(String::from("Uncategorized"));
let entry = partitions.entry(category).or_default();
entry.push(node.clone());
},
);
sort_nodes(&mut partitions);
partitions
.sorted_by(|key1, _value1, key2, _value2| {
match (key1.as_str(), key2.as_str()) {
("Uncategorized", _) => Ordering::Greater,
(_, "Uncategorized") => Ordering::Less,
_ => match key1.cmp(key2) {
Ordering::Greater => Ordering::Greater,
Ordering::Less => Ordering::Less,
Ordering::Equal => unreachable!(),
},
}
})
.collect()
}
fn sort_nodes<T>(partitions: &mut Partitions<T>) {
for (_key, nodes) in partitions.iter_mut() {
nodes.sort_by_cached_key(|node| {
let mut tags =
node.declarations.iter().flat_map(|d| d.js_doc.tags.iter());
let is_deprecated = tags
.clone()
.any(|tag| matches!(tag, JsDocTag::Deprecated { .. }));
let priority = tags
.find_map(|tag| {
if let JsDocTag::Priority { priority } = tag {
Some(*priority)
} else {
None
}
})
.unwrap_or(0);
let qname_lower = node.get_qualified_name().to_ascii_lowercase();
let qname = node.get_qualified_name().to_string();
let kind = node.declarations.first().map(|d| d.def.to_kind());
(
is_deprecated,
std::cmp::Reverse(priority),
qname_lower,
qname,
kind,
)
});
}
}
pub fn flatten_namespace<'a>(
ctx: &GenerateCtx,
doc_nodes: impl Iterator<Item = Cow<'a, DocNodeWithContext>> + 'a,
) -> Vec<Cow<'a, DocNodeWithContext>> {
fn partitioner_inner<'a>(
ctx: &GenerateCtx,
out: &mut Vec<Cow<'a, DocNodeWithContext>>,
parent_node: Option<&DocNodeWithContext>,
doc_nodes: Box<dyn Iterator<Item = Cow<'a, DocNodeWithContext>> + 'a>,
visited_refs: &mut HashSet<Location>,
) {
let nodes: Vec<_> = doc_nodes.collect();
'outer: for node in &nodes {
for decl in &node.declarations {
if matches!(decl.def, DeclarationDef::Namespace(..)) {
let children: Vec<_> = node
.namespace_children
.as_ref()
.unwrap()
.iter()
.cloned()
.collect();
partitioner_inner(
ctx,
out,
Some(&**node),
Box::new(children.into_iter().map(Cow::Owned)),
visited_refs,
);
}
if let Some(reference) = decl.reference_def() {
if visited_refs.insert(reference.target.clone()) {
let resolved: Vec<_> = ctx
.resolve_reference(parent_node, &reference.target)
.map(|c| c.into_owned())
.collect();
partitioner_inner(
ctx,
out,
parent_node,
Box::new(resolved.into_iter().map(Cow::Owned)),
visited_refs,
);
visited_refs.remove(&reference.target);
}
// hack until reference nodes are separate from normal symbols
continue 'outer;
}
}
out.push((*node).clone());
}
}
let mut out = vec![];
partitioner_inner(
ctx,
&mut out,
None,
Box::new(doc_nodes),
&mut HashSet::new(),
);
out
}