From 2176b1b77ce9a823e66a591bebdd1caf803a42cc Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Thu, 11 Jul 2024 20:53:12 -0300 Subject: [PATCH] improve template declaration on methods improves on #4, just missing classes now --- src/builder/shared.rs | 135 ++++++++++++++++++++++++------------------ src/builder/traits.rs | 28 +++++++++ templates/content.css | 21 ++++++- 3 files changed, 122 insertions(+), 62 deletions(-) diff --git a/src/builder/shared.rs b/src/builder/shared.rs index 1a6f0c4..2dc1763 100644 --- a/src/builder/shared.rs +++ b/src/builder/shared.rs @@ -163,27 +163,41 @@ fn fmt_param(param: &Entity, builder: &Builder) -> Html { } fn fmt_template_args(entity: &Entity, _builder: &Builder) -> Option { - 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::>() - .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 + .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::>() + .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 { @@ -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::>() - }) - .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::>() + }) + .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::( + HtmlList::new(vec![ + Html::span(&["space-before"], "="), + Html::span(&["space-before", "literal"], "0"), + ]) + .into(), + ) + ) ) - .with_child_opt( - fun.is_pure_virtual_method().then_some::( - HtmlList::new(vec![ - Html::span(&["space-before"], "="), - Html::span(&["space-before", "literal"], "0"), - ]) - .into(), - ), - ), ) .with_child( HtmlElement::new("div").with_child( diff --git a/src/builder/traits.rs b/src/builder/traits.rs index e7f309d..afbe973 100644 --- a/src/builder/traits.rs +++ b/src/builder/traits.rs @@ -45,6 +45,13 @@ pub trait EntityMethods<'e> { /// Gets the function arguments for this method, including templated ones fn get_function_arguments(&self) -> Option>>; + + /// 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; + + /// Same as extract_source_string, but removes new lines, double spaces, and leading/trailing whitespace + fn extract_source_string_cleaned(&self) -> Option; } impl<'e> EntityMethods<'e> for Entity<'e> { @@ -187,6 +194,27 @@ impl<'e> EntityMethods<'e> for Entity<'e> { }); Some(args) } + + fn extract_source_string(&self) -> Option { + 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 { + // 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)] diff --git a/templates/content.css b/templates/content.css index 86ecd63..ac2ac08 100644 --- a/templates/content.css +++ b/templates/content.css @@ -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;