Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken, and incomprehensible formatting of struct initialization #571

Open
gdamore opened this issue Oct 21, 2022 · 1 comment
Open

Broken, and incomprehensible formatting of struct initialization #571

gdamore opened this issue Oct 21, 2022 · 1 comment

Comments

@gdamore
Copy link

gdamore commented Oct 21, 2022

Best explanation is by demonstration:

Here's the input:

unittest {
static immutable Element term = {
    aLongName1 = "\x1b\x1b,longName1",
    aLongName2 = "\x1b\x1b,longName1",
    aLongName3 = "\x1b\x1b,longName1",
    aLongName4 = "\x1b\x1b,longName1",
    aLongName5 = "\x1b\x1b,longName1",
    aLongName6 = "\x1b\x1b,longName1",
    aLongName7 = "\x1b\x1b,longName1",
    aLongName8 = "\x1b\x1b,longName1",
    aLongName9 = "\x1b\x1b,longName1",
    aLongName10 = "\x1b,\x1b[,longName1",
    aLongName11 = "\x1b,\x1b[,longName1",
    aLongName12 = "\x1b,\x1b[,longName1",
    aLongName13 = "\x1b,\x1b[,longName1",
    aLongName14 = "\x1b,\x1b[,longName1",
    aLongName15 = "\x1b,\x1b[,longName1",
    aLongName16 = "\x1b,\x1b[,longName1",
    aLongName17 = "\x1b,\x1b[,longName1",
    aLongName18 = "\x1b,\x1b[,longName1",
    aLongName19 = "\x1b,\x1b[,longName1",
    aLongName20 = "\x1b,\x1b[,longName1",
    aLongName21 = "\x1b,\x1b[,longName1",
    aLongName22 = "\x1b,\x1b[,longName1",
    aLongName23 = "\x1b,\x1b[,longName1",
    aLongName24 = "\x1b,\x1b[,longName1",
};
}

unittest {
    static immutable Termcap term = {
        name: "test-term",
        enterKeypad: "\x1b[?1h\x1b=",
        exitKeypad: "\x1b[?1l\x1b>",
        cursorBack1: "\x08",
        cursorUp1: "\x1b[A",
        keyBackspace: "\x08",
        keyF1: "\x1bOP",
        keyF2: "\x1bOQ",
        keyF3: "\x1bOR",
        keyInsert: "\x1b[2~",
        keyDelete: "\x1b[3~",
        keyHome: "\x1bOH",
        keyEnd: "\x1bOF",
        keyPgUp: "\x1b[5~",
        keyPgDn: "\x1b[6~",
        keyUp: "\x1bOA",
        keyDown: "\x1bOB",
        keyLeft: "\x1bOD",
        keyRight: "\x1bOC",
        keyBacktab: "\x1b[Z",
        keyShfRight: "\x1b[1;2C",
        mouse: "\x1b[M",
    };
}

Running with stock dfmt (no .editorconfig file, bultin defaults), here's the result:

unittest
{
    static immutable Element term = {
        aLongName1 = "\x1b\x1b,longName1", aLongName2 = "\x1b\x1b,longName1",
            aLongName3 = "\x1b\x1b,longName1", aLongName4 = "\x1b\x1b,longName1",
            aLongName5 = "\x1b\x1b,longName1",
            aLongName6 = "\x1b\x1b,longName1",
            aLongName7 = "\x1b\x1b,longName1", aLongName8 = "\x1b\x1b,longName1",
            aLongName9 = "\x1b\x1b,longName1", aLongName10 = "\x1b,\x1b[,longName1",
            aLongName11 = "\x1b,\x1b[,longName1",
            aLongName12 = "\x1b,\x1b[,longName1",
            aLongName13 = "\x1b,\x1b[,longName1",
            aLongName14 = "\x1b,\x1b[,longName1", aLongName15 = "\x1b,\x1b[,longName1",
            aLongName16 = "\x1b,\x1b[,longName1",
            aLongName17 = "\x1b,\x1b[,longName1", aLongName18 = "\x1b,\x1b[,longName1",
            aLongName19 = "\x1b,\x1b[,longName1",
            aLongName20 = "\x1b,\x1b[,longName1",
            aLongName21 = "\x1b,\x1b[,longName1", aLongName22 = "\x1b,\x1b[,longName1",
            aLongName23 = "\x1b,\x1b[,longName1", aLongName24 = "\x1b,\x1b[,longName1",
    };
}

unittest
{
    static immutable Termcap term = {
        name: "test-term", enterKeypad: "\x1b[?1h\x1b=", exitKeypad: "\x1b[?1l\x1b>", cursorBack1: "\x08", cursorUp1: "\x1b[A",
        keyBackspace: "\x08", keyF1: "\x1bOP", keyF2: "\x1bOQ", keyF3: "\x1bOR", keyInsert: "\x1b[2~",
        keyDelete: "\x1b[3~", keyHome: "\x1bOH", keyEnd: "\x1bOF", keyPgUp: "\x1b[5~", keyPgDn: "\x1b[6~",
        keyUp: "\x1bOA", keyDown: "\x1bOB", keyLeft: "\x1bOD", keyRight: "\x1bOC",
        keyBacktab: "\x1b[Z", keyShfRight: "\x1b[1;2C", mouse: "\x1b[M",
    };
}

The formatting seems stable, but some of those are over 120 characters line, and I can't reason intelligently about the line break choices that the program has made. Why does it join some lines that are almost identical, but then leave others all alone?

@WebFreak001
Copy link
Member

dfmt has a few different formatting procedures for long lines of things. I don't know what the top unittest code is supposed to be though, T x = { a = "foo", b = "bar" } doesn't seem to be any valid syntax (struct initializers use : like the bottom code)

In the unittest at the bottom it looks like valid struct initializer syntax. I'm not sure what special cases there were, but if it's the same as with associative arrays, the code parser first analyzes the whole tree, stores at which locations with [ (or here {) AAs were defined and in the formatter when it's at a [ token it checks that lookup table to see what exactly is following now. Depending on that it selects a formatter.

The long line splitting there works by testing in full if the whole current AST node (the full array) fits in one line, in which case it's formatted like so, but could still get wrapped because of weird edge cases or because in the middle it decided there is enough space to put more tokens in one line after all, after wrapping a line. It's kind of a mess I think, we should more consistently use full AST formatting e.g. to keep keys and values together, to consistently wrap all items inside an array or struct initializer, etc. (just need to make sure they don't oscillate back and forth on every format)

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

No branches or pull requests

2 participants