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

How to preserve commas? #116

Open
WillNilges opened this issue Jun 27, 2024 · 2 comments
Open

How to preserve commas? #116

WillNilges opened this issue Jun 27, 2024 · 2 comments

Comments

@WillNilges
Copy link

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?

@kssilveira
Copy link
Member

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.

@WillNilges
Copy link
Author

👋🏻 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!

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