Tomlyn icon indicating copy to clipboard operation
Tomlyn copied to clipboard

Comment always being force coupled to a node, even with extraneous whitespace

Open RivenSkaye opened this issue 9 months ago • 1 comments

Feature request: It would be nice if Tomlyn could keep the ordering of nodes from the input, or if it could decouple comments from nodes if there's blank lines separating them.

Problem leading me here:
I have a TOML file for configuration purposes and I'm using JSON schema for validation as per the current only stable option. This means that my TOML starts with a comment on line 1, column 1. This also means that the comment is tacked onto the first node found after, and it cannot be written back in the same place unless that node happens to be the alphabetical first node.
Small example, using 2 newlines for demonstrative purposes (I tried a dozen in my local file before realizing it's a Tomlyn limitation):

#:schema ./config.schema.json


[db.postgres]
    key = "val"
    valid = true

    [some.node.child]
        foo = "bar"

[db.maria]
    key = "otherval"
    valid = false

This works, parses fine, no complaints. Until you try to write back a changed config. which gets reordered automagically into:

[db.maria]
    key = "otherval"
    valid = false

#:schema ./config.schema.json


[db.postgres]
    key = "val"
    valid = true

    [some.node.child]
        foo = "bar"

Adding in a buffer comment doesn't impact this either - there's two comments and a blank line in between them tacked onto [db.postrgres].

RivenSkaye avatar Mar 31 '25 09:03 RivenSkaye

Comments are only roundtrip-able if you are using DocumentSyntax and Toml.Parse(string).

Keeping comments on a runtime model that can be declared quite differently from how it is serialized is what makes it challenging with TOML. There is some support of it via TomlPropertiesMetadata but as you discovered some stuffs are not working well.

Things could be improved, but as I'm not using heavily this library, you would have to bring that better support if you want it. PR welcome.

xoofx avatar Mar 31 '25 09:03 xoofx