Skip to content

Commit 2176b1b

Browse files
committed
improve template declaration on methods
improves on #4, just missing classes now
1 parent 59f8eae commit 2176b1b

File tree

3 files changed

+122
-62
lines changed

3 files changed

+122
-62
lines changed

src/builder/shared.rs

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -163,27 +163,41 @@ fn fmt_param(param: &Entity, builder: &Builder) -> Html {
163163
}
164164

165165
fn fmt_template_args(entity: &Entity, _builder: &Builder) -> Option<Html> {
166-
let template_children = if entity.get_kind() == EntityKind::FunctionTemplate {
167-
entity.get_children().into_iter().filter(|e| e.get_kind() == EntityKind::TemplateTypeParameter).collect()
168-
} else {
169-
entity.get_template()?.get_children()
170-
};
171-
Some(HtmlList::new(
172-
template_children
173-
.into_iter()
174-
.map(|e|
175-
HtmlText::new(e.get_name().unwrap_or("_".to_string())).into()
176-
)
177-
.collect::<Vec<_>>()
178-
.insert_between(|| {
179-
HtmlElement::new("span")
180-
.with_class("comma")
181-
.with_class("space-after")
182-
.with_child(HtmlText::new(","))
183-
.into()
184-
})
185-
.surround(HtmlText::new("<").into(), HtmlText::new(">").into()),
186-
).into())
166+
let template_children: Vec<Entity> = entity
167+
.get_children()
168+
.into_iter()
169+
.filter(|e| e.get_kind() == EntityKind::TemplateTypeParameter)
170+
.collect();
171+
if template_children.is_empty() {
172+
return None;
173+
}
174+
if entity.get_name().unwrap_or_default().contains("add") {
175+
let first = template_children.first().unwrap();
176+
println!("{:?}", first);
177+
let source = first.extract_source_string_cleaned().unwrap();
178+
println!("{:?}", source);
179+
}
180+
Some(HtmlElement::new("span")
181+
.with_class("template-params")
182+
.with_child(Html::span(&["keyword", "space-after"], "template"))
183+
.with_children(
184+
template_children
185+
.into_iter()
186+
.map(|e|
187+
HtmlText::new(e.extract_source_string_cleaned().or_else(|| e.get_name().map(|x| format!("typename {x}"))).unwrap_or("_".into())).into()
188+
)
189+
.collect::<Vec<_>>()
190+
.insert_between(|| {
191+
HtmlElement::new("span")
192+
.with_class("comma")
193+
.with_class("space-after")
194+
.with_child(HtmlText::new(","))
195+
.into()
196+
})
197+
.surround(HtmlText::new("<").into(), HtmlText::new(">").into()),
198+
)
199+
.into()
200+
)
187201
}
188202

189203
pub fn fmt_field(field: &Entity, builder: &Builder) -> Html {
@@ -213,46 +227,49 @@ pub fn fmt_fun_decl(fun: &Entity, builder: &Builder) -> Html {
213227
.with_child(
214228
HtmlElement::new("summary")
215229
.with_classes(&["entity", "fun"])
216-
.with_child_opt(
217-
fun.is_static_method()
218-
.then_some(Html::span(&["keyword", "space-after"], "static")),
219-
)
220-
.with_child_opt(
221-
fun.is_virtual_method()
222-
.then_some(Html::span(&["keyword", "space-after"], "virtual")),
223-
)
224-
.with_child_opt(fun.get_result_type().map(|t| fmt_type(&t, builder)))
225-
.with_child(Html::span(
226-
&["name", "space-before"],
227-
&fun.get_name().unwrap_or("_anon".into()),
228-
))
229230
.with_child_opt(fmt_template_args(fun, builder))
230-
.with_child(
231-
HtmlElement::new("span").with_class("params").with_children(
232-
fun.get_function_arguments()
233-
.map(|args| {
234-
args.iter()
235-
.map(|arg| fmt_param(arg, builder))
236-
.collect::<Vec<_>>()
237-
})
238-
.unwrap_or(Vec::new())
239-
.insert_between(|| Html::span(&["comma", "space-after"], ","))
240-
.surround(HtmlText::new("(").into(), HtmlText::new(")").into()),
241-
),
242-
)
243-
.with_child_opt(
244-
fun.is_const_method()
245-
.then_some(Html::span(&["keyword", "space-before"], "const")),
231+
.with_child(HtmlElement::new("span")
232+
.with_class("function-signature")
233+
.with_child_opt(
234+
fun.is_static_method()
235+
.then_some(Html::span(&["keyword", "space-after"], "static")),
236+
)
237+
.with_child_opt(
238+
fun.is_virtual_method()
239+
.then_some(Html::span(&["keyword", "space-after"], "virtual")),
240+
)
241+
.with_child_opt(fun.get_result_type().map(|t| fmt_type(&t, builder)))
242+
.with_child(Html::span(
243+
&["name", "space-before"],
244+
&fun.get_name().unwrap_or("_anon".into()),
245+
))
246+
.with_child(
247+
HtmlElement::new("span").with_class("params").with_children(
248+
fun.get_function_arguments()
249+
.map(|args| {
250+
args.iter()
251+
.map(|arg| fmt_param(arg, builder))
252+
.collect::<Vec<_>>()
253+
})
254+
.unwrap_or(Vec::new())
255+
.insert_between(|| Html::span(&["comma", "space-after"], ","))
256+
.surround(HtmlText::new("(").into(), HtmlText::new(")").into()),
257+
),
258+
)
259+
.with_child_opt(
260+
fun.is_const_method()
261+
.then_some(Html::span(&["keyword", "space-before"], "const")),
262+
)
263+
.with_child_opt(
264+
fun.is_pure_virtual_method().then_some::<Html>(
265+
HtmlList::new(vec![
266+
Html::span(&["space-before"], "="),
267+
Html::span(&["space-before", "literal"], "0"),
268+
])
269+
.into(),
270+
)
271+
)
246272
)
247-
.with_child_opt(
248-
fun.is_pure_virtual_method().then_some::<Html>(
249-
HtmlList::new(vec![
250-
Html::span(&["space-before"], "="),
251-
Html::span(&["space-before", "literal"], "0"),
252-
])
253-
.into(),
254-
),
255-
),
256273
)
257274
.with_child(
258275
HtmlElement::new("div").with_child(

src/builder/traits.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ pub trait EntityMethods<'e> {
4545

4646
/// Gets the function arguments for this method, including templated ones
4747
fn get_function_arguments(&self) -> Option<Vec<Entity<'e>>>;
48+
49+
/// Extracts the source code that makes up this entity, according to its range.
50+
/// This might have to read a file from disk, and also includes whitespace and all
51+
fn extract_source_string(&self) -> Option<String>;
52+
53+
/// Same as extract_source_string, but removes new lines, double spaces, and leading/trailing whitespace
54+
fn extract_source_string_cleaned(&self) -> Option<String>;
4855
}
4956

5057
impl<'e> EntityMethods<'e> for Entity<'e> {
@@ -187,6 +194,27 @@ impl<'e> EntityMethods<'e> for Entity<'e> {
187194
});
188195
Some(args)
189196
}
197+
198+
fn extract_source_string(&self) -> Option<String> {
199+
let range = self.get_range()?;
200+
let start = range.get_start().get_file_location();
201+
let end = range.get_end().get_file_location();
202+
let contents = start.file?.get_contents()?;
203+
contents.get(start.offset as usize..end.offset as usize).map(|s| s.into())
204+
}
205+
206+
fn extract_source_string_cleaned(&self) -> Option<String> {
207+
// TODO: this code is stupid and should be improved
208+
Some(self.extract_source_string()?
209+
.trim()
210+
.replace('\t', " ")
211+
.replace('\r', "")
212+
.replace('\n', "")
213+
.split(' ')
214+
.filter(|x| !x.is_empty())
215+
.intersperse(" ")
216+
.collect())
217+
}
190218
}
191219

192220
#[derive(Clone)]

templates/content.css

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,29 @@ details.entity-desc[open] > summary .feather-chevron-right {
571571
color: var(--flash-purple);
572572
}
573573

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

578575
.entity.var > .name {
579576
color: var(--flash-white);
580577
}
581578

579+
.entity.fun {
580+
flex-direction: column;
581+
}
582+
583+
.entity.fun .template-declaration {
584+
display: flex;
585+
flex-direction: row;
586+
}
587+
588+
.entity.fun .function-signature {
589+
display: flex;
590+
flex-direction: row;
591+
}
592+
593+
.entity.fun .function-signature > .name {
594+
color: var(--flash-blue);
595+
}
596+
582597
.entity.fun .params {
583598
display: flex;
584599
flex-direction: row;

0 commit comments

Comments
 (0)