Tomlyn icon indicating copy to clipboard operation
Tomlyn copied to clipboard

Indentation/whitespace not kept if an element is added at runtime

Open RivenSkaye opened this issue 9 months ago • 1 comments

When loading in a TOML file and making sure to have sane defaults in the code for any keys that are missing, Tomlyn will apply the whitespace of the first known element to the current element, and then not apply it to the element it was read from. This subsequently has funky effects on the remainder of the output.

# This is the input file
[db.maria]
    database     = "Foo"
    load_balance = "LeastConnections"
    password     = "ENV:SUPER_SEKRIT"
    port         = 3306
    protocol     = "Socket"
    server       = "db.local.net"
    user         = "Who knows"

    [db.maria.misc]
        cancellation_timeout    = 15
        connection_timeout      = 60

    [db.maria.ssl]
        ssl_mode = "Preferred"

    [db.maria.pooling]
        connection_idle_timeout = 100
        connection_life_time    = 0
        connection_reset        = true
        dns_check_interval      = 0
        maximum_pool_size       = 15
        minimum_pool_size       = 1
        pooling                 = true

Gets mangled into (verbatim copy-pasted from serializing and printing TOML)

[db.maria]
    database     = "Foo"
    load_balance = "LeastConnections"
    password     = "ENV:SUPER_SEKRIT"
    port         = 3306
    protocol     = "Socket"
    server       = "db.local.net"
    user         = "Who knows"

    [db.maria.misc]
        cancellation_timeout    = 15
        connection_timeout      = 60

[db.maria.pooling]

    connection_idle_timeout = 100
        connection_life_time = 0
        connection_reset = true
        dns_check_interval = 0
        maximum_pool_size = 15
        minimum_pool_size = 1
        pooling = true

    [db.maria.ssl]
        ssl_mode = "Preferred"

RivenSkaye avatar Mar 31 '25 09:03 RivenSkaye

Similarly to my comment https://github.com/xoofx/Tomlyn/issues/101#issuecomment-2765741698

Tomlyn runtime model was not designed to offer precise control of trivias (whitespaces, comment...etc.) and solving that is non-trivial.

Only DocumentSyntax can achieve this.

xoofx avatar Mar 31 '25 10:03 xoofx