Skip to content

Commit

Permalink
cleanup namespaces and add flatten option
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Nov 8, 2023
1 parent be222ea commit 865cee9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn generate(
let current_symbols = Rc::new(get_current_symbols(&doc_nodes, vec![]));

let partitions_by_kind =
symbols::namespace::partition_nodes_by_kind_dedup_overloads(&doc_nodes);
symbols::namespace::partition_nodes_by_kind(&doc_nodes, true);
let sidepanel_ctx = sidepanel_render_ctx(&ctx, &partitions_by_kind);

// Index page (list of all symbols in all files)
Expand Down Expand Up @@ -197,7 +197,7 @@ pub fn generate(

let doc_nodes = doc_nodes_by_url.get(&main_entrypoint).cloned().unwrap();
let partitions_by_kind_for_main_entrypoint =
symbols::namespace::partition_nodes_by_kind_dedup_overloads(&doc_nodes);
symbols::namespace::partition_nodes_by_kind(&doc_nodes, true);

let version_index = render_version_index(
&ctx,
Expand Down
46 changes: 14 additions & 32 deletions src/html/symbols/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ pub(crate) fn get_namespace_render_ctx(
let mut sections = Vec::with_capacity(partitions.len());

for (kind, doc_nodes) in partitions {
let ns_section_ctx =
get_namespace_section_render_ctx(ctx, *kind, doc_nodes);
let ns_section_ctx = get_namespace_section_render_ctx(ctx, kind, doc_nodes);
sections.push(ns_section_ctx)
}

Expand All @@ -46,7 +45,7 @@ pub(crate) fn render_namespace(
) -> String {
let namespace_def = doc_node.namespace_def.as_ref().unwrap();

let partitions = partition_nodes_by_kind(&namespace_def.elements);
let partitions = partition_nodes_by_kind(&namespace_def.elements, false);
let namespace_ctx = get_namespace_render_ctx(ctx, &partitions);

let content_parts = namespace_ctx
Expand All @@ -58,9 +57,9 @@ pub(crate) fn render_namespace(
content_parts.join("")
}

fn partition_nodes_by_kind_inner(
pub fn partition_nodes_by_kind(
doc_nodes: &[DocNode],
dedup_overloads: bool,
flatten_namespaces: bool,
) -> IndexMap<DocNodeKind, Vec<DocNode>> {
let mut partitions: IndexMap<DocNodeKind, Vec<DocNode>> = IndexMap::default();

Expand All @@ -69,10 +68,14 @@ fn partition_nodes_by_kind_inner(
continue;
}

if flatten_namespaces && node.kind == DocNodeKind::Namespace {
let namespace_def = node.namespace_def.as_ref().unwrap();
partitions.extend(partition_nodes_by_kind(&namespace_def.elements, true));
}

let entry = partitions.entry(node.kind).or_insert(vec![]);

if !dedup_overloads || !entry.iter().any(|n: &DocNode| n.name == node.name)
{
if !entry.iter().any(|n: &DocNode| n.name == node.name) {
entry.push(node.clone());
}
}
Expand All @@ -90,33 +93,12 @@ fn partition_nodes_by_kind_inner(
.collect()
}

pub fn partition_nodes_by_kind_dedup_overloads(
doc_nodes: &[DocNode],
) -> IndexMap<DocNodeKind, Vec<DocNode>> {
partition_nodes_by_kind_inner(doc_nodes, true)
}

pub fn partition_nodes_by_kind(
doc_nodes: &[DocNode],
) -> IndexMap<DocNodeKind, Vec<DocNode>> {
partition_nodes_by_kind_inner(doc_nodes, true)
}

fn get_namespace_section_render_ctx(
ctx: &RenderContext,
kind: DocNodeKind,
kind: &DocNodeKind,
doc_nodes: &[DocNode],
) -> NamespaceSectionRenderCtx {
let title = match kind {
DocNodeKind::Function => "Functions",
DocNodeKind::Variable => "Variables",
DocNodeKind::Class => "Classes",
DocNodeKind::Enum => "Enums",
DocNodeKind::Interface => "Interfaces",
DocNodeKind::TypeAlias => "Type Aliases",
DocNodeKind::Namespace => "Namespaces",
DocNodeKind::ModuleDoc | DocNodeKind::Import => unimplemented!(),
};
let kind_ctx = super::super::util::DocNodeKindCtx::from(kind);

let nodes = doc_nodes
.iter()
Expand Down Expand Up @@ -148,8 +130,8 @@ fn get_namespace_section_render_ctx(
.collect::<Vec<_>>();

NamespaceSectionRenderCtx {
id: title_to_id(title),
title: title.to_string(),
id: title_to_id(&kind_ctx.kind),
title: kind_ctx.title_plural.to_string(),
nodes,
}
}
8 changes: 4 additions & 4 deletions src/html/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl<'ctx> RenderContext<'ctx> {
#[derive(Debug, serde::Serialize, Clone)]
pub struct DocNodeKindCtx {
pub kind: String,
char: char,
title: &"static str,
title_lowercase: &"static str,
title_plural: &"static str,
pub char: char,
pub title: &"static str,
pub title_lowercase: &"static str,
pub title_plural: &"static str,
}

impl From<&DocNodeKind> for DocNodeKindCtx {
Expand Down

0 comments on commit 865cee9

Please sign in to comment.