astrale-sharp / typstfmt

Apache License 2.0
253 stars 25 forks source link

Whitespace is removed in strings in math mode #116

Closed arnaudgolfouse closed 11 months ago

arnaudgolfouse commented 1 year ago

Typstfmt version

version: 0.2.6 (b45cab03)

Description

Whitespace is compressed when using a string in math mode, but it should not.

Before

$ a "        x" $

TEST1

After

$ a " x" $

TEST2

I don't really know if this is intended behaviour from typst, but it is handy 🙂

astrale-sharp commented 1 year ago

Thanks a lot for your bug report! I suspect this might be a weird Typst parser behavior

monaqa commented 12 months ago

String literals in code mode also seems to be changed as well as in math mode.

$ typstfmt --version
version: 0.2.6 (6974e5d9)
$ cat input.typ
#raw("    foo")
$ cat input.typ | typstfmt
#raw(" foo")
monaqa commented 12 months ago

As far as I can tell from my investigation of the above example, the Typst parser is working, and the subsequent format_default function seems to be removing the adjacent whitespace.

[lib/src/lib.rs:40] parse(s) = Markup: 16 [
    Hashtag: "#",
    FuncCall: 14 [
        Ident: "raw",
        Args: 11 [
            LeftParen: "(",
            Str: "\"    foo\"",
            RightParen: ")",
        ],
    ],
    Space: "\n",
]

https://github.com/astrale-sharp/typstfmt/blob/6ab2cabb7054f4875e9fb70f9e3790d774b2ed2b/lib/src/lib.rs#L92-L108

if node.kind() is Str literal, it should be given special treatment in formatting.

astrale-sharp commented 12 months ago

True, I talked a bit fast, the solution would be to add string literals to the no_format rule (just like raw blocks)

I can try to do it soonish but it's really difficult for me to do that right now so I really appreciate PR