common icon indicating copy to clipboard operation
common copied to clipboard

Create a static method to pretty print gherkin

Open nddipiazza opened this issue 4 years ago • 17 comments

I find myself wishing there was a simple SDK with the ability to pretty print (format) gherkin files.

Like how intellij cucumber plugin formats them is exactly what I want.

Is there some way to add a simple static method for this?

GherkinFormatter.prettyPrint(gherkinString)

I think there is a way to do this with the library, but it's not as simple.

nddipiazza avatar Jul 16 '21 17:07 nddipiazza

I've moved this to the common repo where gherkin is maintained.

mpkorstanje avatar Jul 16 '21 18:07 mpkorstanje

There is a pretty method implemented in javascript. Would that fit your needs?

There is also https://github.com/cucumber/common/pull/1555 which work in enhancing that pretty method.

aurelien-reeves avatar Jul 19 '21 12:07 aurelien-reeves

I had originally opened this ticket in the cucumber-jvm project. it's there that I really need this. this was moved to the generic cucumber project which loses that fact.

nddipiazza avatar Jul 19 '21 17:07 nddipiazza

I considered the fact that you opened it in cucumber-jvm first. Maybe the javascript version for gherkin-utils would had fit your needs

aurelien-reeves avatar Jul 20 '21 06:07 aurelien-reeves

Did you consider the possibility to use cucumber-jvm to get the messages in the form of ndjson to use those as an input in javascript implementation of gherkin and then get the gherkinDocument to use with gherkin-utils?

aurelien-reeves avatar Jul 20 '21 13:07 aurelien-reeves

@nddipiazza what are you trying to achieve. You've described a solution, but not your problem. What are you trying to achieve by pretty formatting gherkin files?

mpkorstanje avatar Jul 20 '21 15:07 mpkorstanje

@mpkorstanje I have a java program that processes gherkin files. A portion of my java program needs to be able to, given a gherkin string, source format that gherkin. Most importantly so that the Test Data table is formatted pretty. Can this be done simply?

nddipiazza avatar Jul 20 '21 18:07 nddipiazza

Is there a reason it has to be done in Java?

We currently don't have any such function in Java. We do have a function that does it in Javascript. If it absolutely has to be Java then I guess you could port it. The structure isn't too different.

https://github.com/cucumber/common/blob/main/gherkin-utils/javascript/src/pretty.ts

mpkorstanje avatar Jul 20 '21 18:07 mpkorstanje

Yeah unfortunately in my case java is a requirement because my app server is java and there is no js engine available.

nddipiazza avatar Jul 20 '21 18:07 nddipiazza

I think that could be nice to port gherkin-utils to other languages. That's why I've added the "Help wanted" badge :)

aurelien-reeves avatar Jul 21 '21 07:07 aurelien-reeves

I could take a stab at it. there's not much code there. But i'm super weak with ts. So it'll take me a while

nddipiazza avatar Jul 21 '21 17:07 nddipiazza

Would be helpfull for IDEs as well!

laeubi avatar Jul 21 '21 18:07 laeubi

Short reminder: if anyone would need any kind of help, feel free to join our community slack. You'll find plenty of developers ready to help.

If for any reason you would not or can not join slack, we will find other way to provide any kind of help. It can be as part of this issue, or a pull-request, or something else? Are you using some tools which you would keep using? We can certainly join you there. Really, feel free to ask! We're here to help :smiley:

aurelien-reeves avatar Jul 22 '21 09:07 aurelien-reeves

We now have a pretty(GherkinDocument) -> string function implemented in TypeScript.

For implementations in other languages, please make a gherkin-utils/[language] implementation based on the same design (using a walkGherkinDocument function).

aslakhellesoy avatar Sep 01 '21 17:09 aslakhellesoy

@aslakhellesoy here you go - https://github.com/cucumber/common/pull/1725 Is there someone who can help me put it in the CircleCI and cover producing a release artifact etc?

nddipiazza avatar Sep 03 '21 03:09 nddipiazza

Great stuff @nddipiazza - let's continue the discussion in #1725

aslakhellesoy avatar Sep 06 '21 14:09 aslakhellesoy

FYI we're making good progress with this, but there are still a few areas I think we should improve on before we can say this is done - see #2024 for some Gherkin we'd want to finalise the prety-printing for before saying this is "ready"

This is something that isn't solved by my own library https://gitlab.com/jamietanna/gherkin-formatter/ so this isn't me holding things up - I'd love for us to solve it in here :+1:

jamietanna avatar Jun 26 '22 11:06 jamietanna