ducminh-phan / reformat-gherkin

Formatter for Gherkin language
MIT License
24 stars 13 forks source link

Add support for formatting stdin to stdout #45

Closed MageJohn closed 3 years ago

MageJohn commented 3 years ago

Description

Black, along with many other formatters, supports formatting code from stdin, writing it to stdout again. This is particularly useful when integrating the formatter with a code editor, where you often want to format the contents of a buffer without saving the file to disk.

The way Black implements this is through the Unix convention of accepting a '-' as one of the source paths, which is what I've done here.

I reorganised core.py a little to make it easier to redirect I/O to streams, rather than always to paths.

I have also tried to update the tests for the change. I added tests in test_cli.py, and while reading the tests and figuring out where best to make additions, I made a few tweaks to some existing tests. Check the individual commits for specifics. Let me know if you'd rather those changes weren't in this PR!

If you're happy with the code changes, I'll go ahead and update the documentation too.

Type of change

MageJohn commented 3 years ago

Thanks for the review! With regards to how it was meant to be used, I've been testing it with reformat-gherkin - < test.feature, which was working. I didn't realise that different methods of piping stdin would behave differently! I'll take a look.

MageJohn commented 3 years ago

There, those are the three suggestions implemented. I'll dig deeper into that bug later this evening.

MageJohn commented 3 years ago

So I ended up rafactoring quite a bit; turns out there was a cleaner way to handle the switching between streams and file paths. It wasn't necessary to solve the bug, but it reduces the number of times the input file is copied about in memory.

MageJohn commented 3 years ago

Hey @ducminh-phan! I encountered an issue because my editor was replacing the contents of the file with the contents of stdout even when stdout was empty, so now the tool will always print to stdout even if nothing changed. I also updated the README and bumped the patch version; hope that's all okay.