Skip to content

Commit

Permalink
improve template declaration on methods
Browse files Browse the repository at this point in the history
improves on #4, just missing classes now
  • Loading branch information
matcool committed Jul 11, 2024
1 parent 59f8eae commit 2176b1b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 62 deletions.
135 changes: 76 additions & 59 deletions src/builder/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,41 @@ fn fmt_param(param: &Entity, builder: &Builder) -> Html {
}

fn fmt_template_args(entity: &Entity, _builder: &Builder) -> Option<Html> {
let template_children = if entity.get_kind() == EntityKind::FunctionTemplate {
entity.get_children().into_iter().filter(|e| e.get_kind() == EntityKind::TemplateTypeParameter).collect()
} else {
entity.get_template()?.get_children()
};
Some(HtmlList::new(
template_children
.into_iter()
.map(|e|
HtmlText::new(e.get_name().unwrap_or("_".to_string())).into()
)
.collect::<Vec<_>>()
.insert_between(|| {
HtmlElement::new("span")
.with_class("comma")
.with_class("space-after")
.with_child(HtmlText::new(","))
.into()
})
.surround(HtmlText::new("<").into(), HtmlText::new(">").into()),
).into())
let template_children: Vec<Entity> = entity
.get_children()
.into_iter()
.filter(|e| e.get_kind() == EntityKind::TemplateTypeParameter)
.collect();
if template_children.is_empty() {
return None;
}
if entity.get_name().unwrap_or_default().contains("add") {
let first = template_children.first().unwrap();
println!("{:?}", first);
let source = first.extract_source_string_cleaned().unwrap();
println!("{:?}", source);
}
Some(HtmlElement::new("span")
.with_class("template-params")
.with_child(Html::span(&["keyword", "space-after"], "template"))
.with_children(
template_children
.into_iter()
.map(|e|
HtmlText::new(e.extract_source_string_cleaned().or_else(|| e.get_name().map(|x| format!("typename {x}"))).unwrap_or("_".into())).into()
)
.collect::<Vec<_>>()
.insert_between(|| {
HtmlElement::new("span")
.with_class("comma")
.with_class("space-after")
.with_child(HtmlText::new(","))
.into()
})
.surround(HtmlText::new("<").into(), HtmlText::new(">").into()),
)
.into()
)
}

pub fn fmt_field(field: &Entity, builder: &Builder) -> Html {
Expand Down Expand Up @@ -213,46 +227,49 @@ pub fn fmt_fun_decl(fun: &Entity, builder: &Builder) -> Html {
.with_child(
HtmlElement::new("summary")
.with_classes(&["entity", "fun"])
.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_opt(fmt_template_args(fun, builder))
.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(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(),
)
)
)
.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(),
),
),
)
.with_child(
HtmlElement::new("div").with_child(
Expand Down
28 changes: 28 additions & 0 deletions src/builder/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub trait EntityMethods<'e> {

/// Gets the function arguments for this method, including templated ones
fn get_function_arguments(&self) -> Option<Vec<Entity<'e>>>;

/// Extracts the source code that makes up this entity, according to its range.
/// This might have to read a file from disk, and also includes whitespace and all
fn extract_source_string(&self) -> Option<String>;

/// Same as extract_source_string, but removes new lines, double spaces, and leading/trailing whitespace
fn extract_source_string_cleaned(&self) -> Option<String>;
}

impl<'e> EntityMethods<'e> for Entity<'e> {
Expand Down Expand Up @@ -187,6 +194,27 @@ impl<'e> EntityMethods<'e> for Entity<'e> {
});
Some(args)
}

fn extract_source_string(&self) -> Option<String> {
let range = self.get_range()?;
let start = range.get_start().get_file_location();
let end = range.get_end().get_file_location();
let contents = start.file?.get_contents()?;
contents.get(start.offset as usize..end.offset as usize).map(|s| s.into())
}

fn extract_source_string_cleaned(&self) -> Option<String> {
// TODO: this code is stupid and should be improved
Some(self.extract_source_string()?
.trim()
.replace('\t', " ")
.replace('\r', "")
.replace('\n', "")
.split(' ')
.filter(|x| !x.is_empty())
.intersperse(" ")
.collect())
}
}

#[derive(Clone)]
Expand Down
21 changes: 18 additions & 3 deletions templates/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,29 @@ details.entity-desc[open] > summary .feather-chevron-right {
color: var(--flash-purple);
}

.entity.fun > .name {
color: var(--flash-blue);
}

.entity.var > .name {
color: var(--flash-white);
}

.entity.fun {
flex-direction: column;
}

.entity.fun .template-declaration {
display: flex;
flex-direction: row;
}

.entity.fun .function-signature {
display: flex;
flex-direction: row;
}

.entity.fun .function-signature > .name {
color: var(--flash-blue);
}

.entity.fun .params {
display: flex;
flex-direction: row;
Expand Down

0 comments on commit 2176b1b

Please sign in to comment.