typst / typst

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

Hint for language-region pair where language is expected #4142

Closed laurmaedje closed 1 week ago

laurmaedje commented 2 weeks ago

Description

When writing #set text(lang: "en-gb"), we should have a hint that you should use the region field instead.

Use Case

Helps people fix the problem.

Coekjan commented 2 weeks ago

Hi, I wonder if this diagnostics shall be implemented in Lang::from_str. I see current diagnostics information "expected two or three letter language code (ISO 639-1/2/3)" comes from there.

laurmaedje commented 2 weeks ago

Unfortunately, the casting infrastructure doesn't support hints yet (FromValue always returns a StrResult). So, I think the only place we can do this at the moment is by augmenting the text.lang with a #[parse] annotation (you can search the codebase to see how it works).

Coekjan commented 2 weeks ago

I am thinking about when to hint something like "you should use region field". Here are some options I thought out: Op1: The arg contains - Op2: Op1 + the length of arg.split('-') is 2 Op3: Op2 + arg[0] can be parsed into Lang & arg[1] can be parsed into Region

Or does anyone have some other ideas?