txtpbfmt icon indicating copy to clipboard operation
txtpbfmt copied to clipboard

How to preserve commas?

Open WillNilges opened this issue 1 year ago • 2 comments

When I use this library to make a change in the AST, and then write it back to a file, I lose commas . This seems to be a standard part of the library, but I am wondering if there's a way to stop this?

WillNilges avatar Jun 27 '24 15:06 WillNilges

hi, do you have an example input and output to make sure we're talking about the same thing?

Part of the optional configuration is documented on https://github.com/protocolbuffers/txtpbfmt/blob/master/docs/config.md. You can find the full configuration on Config on https://github.com/protocolbuffers/txtpbfmt/blob/master/parser/parser.go#L24 and addToConfig on https://github.com/protocolbuffers/txtpbfmt/blob/master/parser/parser.go#L357.

It seems we don't currently have a configuration to preserve commas. The recommended way is to do one round of formatting first, which will unconditionally remove all the unnecessary commas, and then do the automated edits you want to do.

It is possible to add a new config to optionally preserve commas, let me know if you're interested in contributing it!

You would need to change consumeOptionalSeparator on https://github.com/protocolbuffers/txtpbfmt/blob/master/parser/parser.go#L542 to store it in the AST.

kssilveira avatar Jun 28 '24 14:06 kssilveira

👋🏻 Sure, here's a minimal example. I made a script that changes something in an unrelated part of a config via parsing into an AST, changing it within the AST, and then dumping back to the file, and that removes all commas from the file.

 object {
-  key: "hello",
-  value: { foo: "bar", baz: "bin" }
+  key: "hello"
+  value: { foo: "bar" baz: "bin" }
 }

I'd expect this object block not to change, but it does.

Unfortunately, I don't think I can put any time towards updating this in the near future. I will let you know if that changes!

WillNilges avatar Jul 08 '24 20:07 WillNilges