Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Raw blocks drifting right #136

Open
arnaudgolfouse opened this issue Dec 7, 2023 · 9 comments
Open

Raw blocks drifting right #136

arnaudgolfouse opened this issue Dec 7, 2023 · 9 comments

Comments

@arnaudgolfouse
Copy link
Contributor

arnaudgolfouse commented Dec 7, 2023

[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

@arnaudgolfouse
Copy link
Contributor Author

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;
}
```

@astrale-sharp
Copy link
Owner

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

I just fixed this

@astrale-sharp
Copy link
Owner

astrale-sharp commented Dec 7, 2023

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
Copy link

leana8959 commented Feb 18, 2024

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
Copy link
Owner

Thank you for your kind words <3

@Ciflire
Copy link

Ciflire commented May 13, 2024

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
Copy link
Owner

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

@lukasbm
Copy link

lukasbm commented Jun 4, 2024

How do you turn off raw text formatting?

@astrale-sharp
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants