typst / typst

A new markup-based typesetting system that is powerful and easy to learn.
https://typst.app
Apache License 2.0
29.88k stars 818 forks source link

Content `repr` isn't always valid syntax #2887

Open jbirnick opened 7 months ago

jbirnick commented 7 months ago

For #outline there is a fill argument. In the docs it says:

Content to fill the space between the title and the page number. Can be set to none to disable filling.

Default: repeat(body: [.])

However, repeat(body: [.]) appears to be not valid Typst syntax. (I get an error that the argument is positional.) I think it should be repeat[.].

PgBiel commented 7 months ago

Good catch, seems to be a bug with the docs generator.

Dherse commented 7 months ago

This is mostly an issue in the implementation of the Repr trait, it gets auto generated for every element and is... not great. We could certainly improve it, I might open a PR doing just that for these specific use cases.

astrale-sharp commented 7 months ago

If the goal is to have eval(repr(element)) == element in most case which I think would be pretty neat then we have a number of problems :

astrale-sharp commented 7 months ago

The simplest fix would be to modify

/// Creates the element's `Repr` implementation.
fn create_repr_impl(element: &Elem) -> TokenStream {
    let ident = &element.ident;
    let repr_format = format!("{}{{}}", element.name);
    quote! {
        impl #foundations::Repr for #ident {
            fn repr(&self) -> ::ecow::EcoString {
                let fields = #foundations::NativeElement::fields(self).into_iter()
                    .map(|(name, value)| ::ecow::eco_format!("{}: {}", name, value.repr()))
                    .collect::<Vec<_>>();
                ::ecow::eco_format!(#repr_format, #foundations::repr::pretty_array_like(&fields, false))
            }
        }
    }
}

So as to add an exception when the name of the field is body, and inline that directly instead. I don't really see a reason to not to that?

astrale-sharp commented 7 months ago

To fix the types, I would switch to using short_name instead of long name as the output of representation