Skip to content

Commit

Permalink
display function signature on function pages
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
matcool committed Jul 12, 2024
1 parent 2323d45 commit 006aa63
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/builder/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::{
builder::Builder,
traits::{BuildResult, Entry, NavItem, OutputEntry, ASTEntry},
shared::{fmt_fun_decl, fmt_section, fmt_classlike_decl},
shared::{fmt_class_method, fmt_section, fmt_classlike_decl},
namespace::CppItemKind
};
use crate::{
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<'e> OutputEntry<'e> for File {
) && matcher(entry)
)
.into_iter()
.map(|fun| fmt_fun_decl(fun.entity(), builder))
.map(|fun| fmt_class_method(fun.entity(), builder))
.collect()
),
),
Expand Down
6 changes: 2 additions & 4 deletions src/builder/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use crate::{html::Html, url::UrlPath};
use clang::Entity;

use super::{
traits::{ASTEntry, BuildResult, EntityMethods, Entry, NavItem, OutputEntry},
builder::Builder,
shared::output_entity,
builder::Builder, shared::{output_entity, output_function}, traits::{ASTEntry, BuildResult, EntityMethods, Entry, NavItem, OutputEntry}
};

pub struct Function<'e> {
Expand Down Expand Up @@ -53,7 +51,7 @@ impl<'e> OutputEntry<'e> for Function<'e> {
fn output(&self, builder: &Builder<'e>) -> (Arc<String>, Vec<(&'static str, Html)>) {
(
builder.config.templates.function.clone(),
output_entity(self, builder),
output_function(self, builder),
)
}

Expand Down
117 changes: 68 additions & 49 deletions src/builder/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,56 +214,61 @@ pub fn fmt_field(field: &Entity, builder: &Builder) -> Html {
.into()
}

pub fn fmt_fun_decl(fun: &Entity, builder: &Builder) -> Html {
fn fmt_fun_signature(fun: &Entity, builder: &Builder) -> Html {
HtmlElement::new("summary")
.with_classes(&["entity", "fun"])
.with_child_opt(fmt_template_args(fun, builder))
.with_child(HtmlElement::new("span")
.with_class("function-signature")
.with_child_opt(
fun.is_static_method()
.then_some(Html::span(&["keyword", "space-after"], "static")),
)
.with_child_opt(
fun.is_virtual_method()
.then_some(Html::span(&["keyword", "space-after"], "virtual")),
)
.with_child_opt(fun.get_result_type().map(|t| fmt_type(&t, builder)))
.with_child(Html::span(
&["name", "space-before"],
&fun.get_name().unwrap_or("_anon".into()),
))
.with_child(
HtmlElement::new("span").with_class("params").with_children(
fun.get_function_arguments()
.map(|args| {
args.iter()
.map(|arg| fmt_param(arg, builder))
.collect::<Vec<_>>()
})
.unwrap_or(Vec::new())
.insert_between(|| Html::span(&["comma", "space-after"], ","))
.surround(HtmlText::new("(").into(), HtmlText::new(")").into()),
),
)
.with_child_opt(
fun.is_const_method()
.then_some(Html::span(&["keyword", "space-before"], "const")),
)
.with_child_opt(
fun.is_pure_virtual_method().then_some::<Html>(
HtmlList::new(vec![
Html::span(&["space-before"], "="),
Html::span(&["space-before", "literal"], "0"),
])
.into(),
)
)
)
.into()
}

pub fn fmt_class_method(fun: &Entity, builder: &Builder) -> Html {
HtmlElement::new("details")
.with_class("entity-desc")
.with_attr_opt("id", member_fun_link(fun))
.with_child(
HtmlElement::new("summary")
.with_classes(&["entity", "fun"])
.with_child_opt(fmt_template_args(fun, builder))
.with_child(HtmlElement::new("span")
.with_class("function-signature")
.with_child_opt(
fun.is_static_method()
.then_some(Html::span(&["keyword", "space-after"], "static")),
)
.with_child_opt(
fun.is_virtual_method()
.then_some(Html::span(&["keyword", "space-after"], "virtual")),
)
.with_child_opt(fun.get_result_type().map(|t| fmt_type(&t, builder)))
.with_child(Html::span(
&["name", "space-before"],
&fun.get_name().unwrap_or("_anon".into()),
))
.with_child(
HtmlElement::new("span").with_class("params").with_children(
fun.get_function_arguments()
.map(|args| {
args.iter()
.map(|arg| fmt_param(arg, builder))
.collect::<Vec<_>>()
})
.unwrap_or(Vec::new())
.insert_between(|| Html::span(&["comma", "space-after"], ","))
.surround(HtmlText::new("(").into(), HtmlText::new(")").into()),
),
)
.with_child_opt(
fun.is_const_method()
.then_some(Html::span(&["keyword", "space-before"], "const")),
)
.with_child_opt(
fun.is_pure_virtual_method().then_some::<Html>(
HtmlList::new(vec![
Html::span(&["space-before"], "="),
Html::span(&["space-before", "literal"], "0"),
])
.into(),
)
)
)
fmt_fun_signature(fun, builder)
)
.with_child(
HtmlElement::new("div").with_child(
Expand Down Expand Up @@ -430,7 +435,7 @@ pub fn output_classlike<'e, T: ASTEntry<'e>>(
"Public static methods",
entry.entity().get_member_functions(Access::Public, Include::Statics)
.into_iter()
.map(|e| fmt_fun_decl(&e, builder))
.map(|e| fmt_class_method(&e, builder))
.collect::<Vec<_>>(),
),
),
Expand All @@ -440,7 +445,7 @@ pub fn output_classlike<'e, T: ASTEntry<'e>>(
"Public member functions",
entry.entity().get_member_functions(Access::Public, Include::Members)
.into_iter()
.map(|e| fmt_fun_decl(&e, builder))
.map(|e| fmt_class_method(&e, builder))
.collect::<Vec<_>>(),
),
),
Expand All @@ -451,7 +456,7 @@ pub fn output_classlike<'e, T: ASTEntry<'e>>(
"Protected member functions",
entry.entity().get_member_functions(Access::Protected, Include::Members)
.into_iter()
.map(|e| fmt_fun_decl(&e, builder))
.map(|e| fmt_class_method(&e, builder))
.collect::<Vec<_>>(),
),
),
Expand Down Expand Up @@ -491,6 +496,20 @@ pub fn output_classlike<'e, T: ASTEntry<'e>>(
ent
}

pub fn output_function<'e, T: ASTEntry<'e>>(
entry: &T,
builder: &Builder,
) -> Vec<(&'static str, Html)> {
let mut ent = output_entity(entry, builder);
ent.extend(vec![
(
"function_signature",
fmt_fun_signature(entry.entity(), builder)
)
]);
ent
}

fn fmt_autolinks_recursive<'a>(
entity: &CppItem,
config: Arc<Config>,
Expand Down
1 change: 1 addition & 0 deletions templates/function.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<h1 class="entity-title">Function <i data-feather="code" class="icon"></i><a href="{page_url}">{name}</a></h1>
<div>
{header_link}
{function_signature}
</div>
<div>
{description}
Expand Down

0 comments on commit 006aa63

Please sign in to comment.