typstfmt
typstfmt copied to clipboard
Raw blocks drifting right
[x] I have tested with the master version of typstfmt and not just typst-lsp
$ typstfmt --version
version: 0.2.7 (46b4ec34)
Describe the bug
So, uh, sorry to bring this up again, but:
- ```
my beautiful raw block !
```
To Reproduce
Run typstfmt repetitively on the file, and watch the code block drift further and further right... 😢
Ideas to fix ?
I looked at the code, and I think this function does not do things correctly:
fn format_list_enum(parent: &LinkedNode, children: &[String], ctx: &mut Ctx) -> String {
let mut res = String::new();
for (s, node) in children.iter().zip(parent.children()) {
match node.kind() {
_ if ctx.off => res.push_str(node.text()),
EnumMarker | ListMarker | TermMarker => {
ctx.push_raw_in(node.text(), &mut res);
}
_ => {
ctx.push_raw_indent(s, &mut res);
}
}
}
res
}
In this case, we enter the third branch, and call ctx.push_raw_indent(s, &mut res); BUT: here node.kind() is not, as one might expect, Raw (which would allow filtering it somehow), but Markup (which internally contains a Raw) !!
Overall, I think simply adding an offset as push_raw_indent does is not the right thing to do here. I don't really know how to fix that.
REMARK: To motivate against using a hack (like "look inside the markup block, and if it contains a Raw don't add indentation!"), let me use the following example:
- #{
let raw_block = ```
another beautiful block !
```
}
Note how the Raw element is now nested twice: it goes CodeBlock -> Markup -> Raw
Testing it further, there is also this weird thing:
A code block:
```rust
if b {
let x = 2;
}
```
Formats to
A code block: ```rust
if b {
let x = 2;
}
```
A code block:
rust if b { let x = 2; }
I just fixed this
REMARK: To motivate against using a hack (like "look inside the markup block, and if it contains a
Rawdon't add indentation!"), let me use the following example:
I agree since for this example we would need the hack to be in code blocks and list enum and ....
I discovered a similar issue:
text[
J'ai fait un snippet neovim qui crée des banners ! ```
//////////////////////
// This is a banner //
//////////////////////
```]
This is a combination of a few problems. First, the opening of the backticks gets shifted to the line above; secondly, the closing ] gets wrapped around the code block. Lastly, the raw block drifts to the right, as this issues suggests.
Thank you for your time spent on this project! It makes typst even more awesome. Have a nice day =)
Thank you for your kind words <3
I think disabling raw text formatting until this is fixed could be a good idea as it messes up real bad any raw block (if saving regularly), i know this can be done manually but as it is a known issue for many
Not against reviewing someone doing that, I'm less and less motivated to work on this to be honest
How do you turn off raw text formatting?
I don't think you can unfortunately, maybe take a look at typstyles https://github.com/Enter-tainer/typstyle/