astrale-sharp / typstfmt

Apache License 2.0
253 stars 25 forks source link

Raw blocks drifting right #136

Open arnaudgolfouse opened 11 months ago

arnaudgolfouse commented 11 months ago

[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:
```rust
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`
arnaudgolfouse commented 11 months ago

Testing it further, there is also this weird thing:

A code block:
```rust
if b {
    let x = 2;
}

Formats to
````typst
A code block: ```rust
if b {
 let x = 2;
}
astrale-sharp commented 11 months ago

A code block: rust if b { let x = 2; }

I just fixed this

astrale-sharp commented 11 months ago

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:

I agree since for this example we would need the hack to be in code blocks and list enum and ....

leana8959 commented 9 months ago

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 =)

astrale-sharp commented 8 months ago

Thank you for your kind words <3

Ciflire commented 6 months ago

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

astrale-sharp commented 6 months ago

Not against reviewing someone doing that, I'm less and less motivated to work on this to be honest

lukasbm commented 5 months ago

How do you turn off raw text formatting?

astrale-sharp commented 5 months ago

I don't think you can unfortunately, maybe take a look at typstyles https://github.com/Enter-tainer/typstyle/