config icon indicating copy to clipboard operation
config copied to clipboard

Lightweight tool for formatting hocon files?

Open anilkumarmyla opened this issue 5 years ago • 4 comments

I'm aware of intellij's hocon plugin that does formatting in place, looking for another lightweight tool that does the same

anilkumarmyla avatar Jun 12 '20 21:06 anilkumarmyla

My understanding is that there is no way to safely format hocon currently.

I have attempted two approaches, but both failed.

val hoconConfig = ConfigFactory.parseString(input)
            hoconConfig.root().render(
                ConfigRenderOptions
                    .concise()
                    .setFormatted(true)
                    .setJson(false)
                    .setComments(true)
            )

This was the more promising option. But the major flaw is that this is destructive. If you have comments in the wrong place, they will be deleted.

This was supposedly "solved" by https://github.com/lightbend/config/issues/149 . My understanding is this new API was introduced:

val document = ConfigDocumentFactory.parseString(input)
document.render()

The document API seems to not destroy comments, but it seems to not have any rendering options and is hardcoded to render the the input without any format changes.

So the ConfigFactory and ConfigRenderOptions API demonstrates a capability for formatting, and the ConfigDocumentFactory API demonstrates an API that can preserve comments. It seems that all we are missing is a proper synthesis of the two. I haven't checked the source code of the library, but I'm wondering if under the hood these systems are similar enough that a true lightweight formatter can be assembled easily from them.

mgroth0 avatar Feb 11 '24 23:02 mgroth0

Easily I would say no. The Config object is meant to represent a configuration (like keys and values) at runtime, not preserve any formatting or specific file format. The ConfigDocument object is meant to represent a file, but mostly has not been worked on and has very few features, it's a very incomplete API.

havocp avatar Feb 11 '24 23:02 havocp

Easily I would say no. The Config object is meant to represent a configuration (like keys and values) at runtime, not preserve any formatting or specific file format. The ConfigDocument object is meant to represent a file, but mostly has not been worked on and has very few features, it's a very incomplete API.

havocp avatar Feb 11 '24 23:02 havocp