Skip to content

Make par.first-line-indent foldable - #7747

Merged
laurmaedje merged 10 commits into
typst:mainfrom
YDX-2147483647:fold-amount-all
Feb 16, 2026
Merged

laurmaedje merged 10 commits into
typst:mainfrom
YDX-2147483647:fold-amount-all

Conversation

@YDX-2147483647

@YDX-2147483647 YDX-2147483647 commented Jan 23, 2026 •

Copy link
Copy Markdown
Contributor

In Typst 0.14, the following typst code emits an error (Dictionary does not contain key "amount").
In this PR, it enables all without affecting the amount. It would be helpful for templates and also handy for one-off toggling.

#set par(first-line-indent: (all: true))

Resolves #7052
Continues #7123 (which was rejected because AI didn't fully implement it)

Further details

Values from context

GitHub search shows that people are assuming type(par.first-line-indent.amount) == length. (#3206 (comment) has a concrete example.)
Therefore, this PR keeps this assumption.

Note that this behaviour is not consistent with grid.stroke, which is also both foldable and smart.

// Before all set rules
#context par.first-line-indent // (amount: 0pt, all: false)

// After partial set rules
#set par(first-line-indent: 2em)
#context par.first-line-indent.all // false
// Before all set rules
#context grid.stroke // (:) — an empty dictionary

// After partial set rules
#set grid(stroke: red)
#context grid.stroke.thickness // auto, not 1pt
Outdated content

Repeated code

The ad hoc take function that cast! Dict -> FirstLineIndent is copied from stroke.rs.
I'm not sure if it's worth merging the two functions.

mut dict: Dict => {
// Get a value by key, accepting either Auto or something convertible to type T.
fn take<T: FromValue>(dict: &mut Dict, key: &str) -> HintedStrResult<Smart<T>> {
Ok(dict.take(key).ok().map(Smart::<T>::from_value)
.transpose()?.unwrap_or(Smart::Auto))
}
let paint = take::<Paint>(&mut dict, "paint")?;
let thickness = take::<Length>(&mut dict, "thickness")?;
let cap = take::<LineCap>(&mut dict, "cap")?;

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Jan 26, 2026
JuanHPassos and others added 3 commits January 27, 2026 15:08
feat(par): make first-line-indent foldable

style: apply rustfmt

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a slight issue with auto being accepted. This should not be the case.

Implementation-wise, an alternative would have been to have just one struct, but have accessor methods on FirstLineIndent that basically perform the unwrap or default action internally, then having the defaults specified in constants that are used there and in an impl Default for FirstLineIndent.

I'm not sure which is better. Folding is unfortunately a mess. I wanted to point to the Fold impl for Augment, but it's actually wrong... there, a hline config overwrites an existing vline and vice versa.

Comment thread tests/suite/model/par.typ Resolved
Comment thread crates/typst-library/src/model/par.rs Resolved
@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Jan 27, 2026
@YDX-2147483647

Copy link
Copy Markdown
Contributor Author

I reorganized it and replaced "FixedFirstLineIndent" with accessors. It's cleaner now.

@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Jan 28, 2026
Comment thread crates/typst-library/src/model/par.rs Outdated
Comment on lines +664 to +665
"amount" => indent.amount(),
"all" => indent.all(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These properties should not be included in the dictionary if they are None, rather than then using the default, since the None represents the strongly-typed version of an absent key.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained in the PR description, I want context par.first-line-indent to have defaults filled.
impl From<FirstLineIndent> for Dict was added in #5768, and it looks like the only usage was in cast!, so I implemented the logic here.

cast! {
    FirstLineIndent,
    self => Value::Dict(self.into()),

I've just removed impl From<FirstLineIndent> for Dict. How about now?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained in the PR description, I want context par.first-line-indent to have defaults filled.

That shouldn't be an issue, since context par.first-line-indent will observe the folded value and since the default is initialized with Some(_) fields, they cannot be None after folding in practice. My comment was only about having the correct semantical behavior of the code. The code for folding across the code base is already a bit all over the place and if we give up on the semantics, I fear at some point nobody will understand it anymore, including me.

If you reintroduce the impl From<FirstLineIndent> for Dict with the None handling, I think it should all work as you expect.

At some point, I might create some additional infrastructure to make foldable properties less confusing to implement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't be an issue

Indeed! I was misled by my initial implementation. (In that version, the struct FirstLineIndent and the parameter par.first-line-indent have different default values. Therefore, that didn't work.)

Now updated.

create some additional infrastructure to make foldable properties less confusing to implement

I'm looking forward to it. It did take me some time to figure out the relationship between Fold and AlternativeFold, what should be stored if a key is not given, …

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff now is unfortunately still not quite what I meant. 🫠

Now, None values would be turned into Typst none values in the dictionary rather than being absent.

What I meant is that they would not be inserted at all, like in IntoValue for Sides<Option<T>>.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I guess this version would match what you meant…

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Feb 9, 2026
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. waiting-on-author Pull request waits on author and removed waiting-on-author Pull request waits on author waiting-on-review This PR is waiting to be reviewed. labels Feb 10, 2026
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Feb 13, 2026
@laurmaedje
laurmaedje added this pull request to the merge queue Feb 16, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Thanks!

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Feb 16, 2026
Merged via the queue into typst:main with commit 7499e59 Feb 16, 2026
8 checks passed
@YDX-2147483647
YDX-2147483647 deleted the fold-amount-all branch February 16, 2026 17:10
jassielof pushed a commit to jassielof/typst that referenced this pull request Apr 7, 2026
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make par.first-line-indent fold

3 participants