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

Have --check output be in different formats (pretty/unified/json) #230

Closed
levouh opened this issue Jul 21, 2021 · 4 comments · Fixed by #411
Closed

Have --check output be in different formats (pretty/unified/json) #230

levouh opened this issue Jul 21, 2021 · 4 comments · Fixed by #411
Labels
enhancement New feature or request

Comments

@levouh
Copy link

levouh commented Jul 21, 2021

In summary, add an argument to dump the formatted file to STDOUT rather than doing things in-place, say, --stdout/-d mode.

An alternative that I've used with a number of different tools is to pass diff output to patch in order to reconstruct what the changed file would look like. For stylua, something like stylua --check /path/to/file.lua | patch -p0 -s -o -. However, the --check option does not output a patch-able format (to my knowledge) so doing something like this doesn't give the "final" output either.

Given the former option of adding a new argument or similar, discerning which STDOUT applies to which file can be difficult as well, so it seems absolutely viable to have stylua only accept a single file when being run in either of these modes.

Thoughts @JohnnyMorganz?

@JohnnyMorganz
Copy link
Owner

You can pass code into stdin, then its formatted output is dumped to stdout (cat /path/to/file.lua | stylua -). Is this reasonable for your use case, or is it necessary that we take in a path instead?

For --check outputing a patch-able diff, that might be nice to have. I assume patch takes in a git-style diff? If we do this, it also might help editor integrations aswell, where they can patch parts of the file rather than currently replacing the whole file buffer with the formatted output, losing some niceties.

@levouh
Copy link
Author

levouh commented Jul 21, 2021

You can pass code into stdin, then its formatted output is dumped to stdout (cat /path/to/file.lua | stylua -). Is this reasonable for your use case,

Awesome, this satisfies 99% of my use case, but it still seems like it'd be worth it to have an option to explicitly pass the output to STDOUT.

or is it necessary that we take in a path instead?

Its effectively the same thing; I know the path so I'm either just outputting the file or passing it to stylua directly.

I assume patch takes in a git-style diff?

Yes, exactly. I'm not familiar with it enough to decoratively say whether or not:

it also might help editor integrations aswell, where they can patch parts of the file

is something that is easily done, but I cannot imagine it hurts either way. Depends on how easy it is to do and the complexity that it adds to stylua itself. If it's easy, etc. then it might be worth it, but if you have the start/end state of the file, it isn't too hard to form a git-style diff via some light scripting (implying it is something users can easily add themselves at no cost to the complexity of this project.


So TLDR, either of the things you mentioned would be nice, but given it is possible to dump a file to STDOUT like you mentioned, that is completely fine for my use case (although perhaps you should document it somewhere [unless I just missed it while skimming]).

Feel free to close this if you want, or leave it open to track any changes you see fit.

@JohnnyMorganz
Copy link
Owner

I think I'll leave the formatting dumped to stdout when taking in a path for now, since it can be done externally with cat and piping.

Having --check output in a more patch-friendly/machine-friendly format seems like a reasonable addition though. Probably through something like --output pretty/unified/json where pretty is current, unified is patch-able, json is the diff output in json. Will probably add something like that when I have some time.

@JohnnyMorganz JohnnyMorganz added the enhancement New feature or request label Jul 22, 2021
@JohnnyMorganz JohnnyMorganz changed the title Request - Output changes to STDOUT, or present diff in patch-able format Have --check output be in different formats (pretty/unified/json) Jul 22, 2021
@JohnnyMorganz
Copy link
Owner

This is now supported in v0.13.0: https://github.com/JohnnyMorganz/StyLua/releases/tag/v0.13.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants