-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Feature Request: Enable Option to Receive stdin and Output stdout #1153
Comments
@rogerluan so just to clarify - the request is to output the formatted file to stdout instead of modifying the file in-place with formatting? |
Yes @shashachu ! 😃 |
I'm interested in this as well. I maintain a project in both Swift and Kotlin. Thanks to |
Same/similar idea is requested in #1123 |
This functionality seems to be available since version 0.2 of ktlint. After some experimenting, I found that command below does exactly what you want:
results in output:
Important notes:
|
@paul-dingemans Thank you, it works perfectly. Here's my #!/bin/sh
git-format-staged --formatter "ktlint --stdin -F {} 2> /dev/null" "*.kt" |
🤩 🤩 Amazing!! I can't wait to start using this! Thank you so much for showing this to us @paul-dingemans @michael-menu 🚀 I'll report back with my findings once I get to test this 🙌 |
@rogerluan did you have any success running this? On my side, running this command seems to have no effect. @mickael-menu did you change anything else in your pre commit script? Thank you for answers in advance. |
Just tried this out now. I've made bittersweet observations: First, the precommit hook crashes (doesn't work and return an error) when the file has any
When committing from Git Tower, and:
When committing from Terminal. This is probably something misconfigured, or something that could be skipped, so I continued... Then I noticed that, for ktlint, it won't read only the modified lines. It reads and lints the entire file. So if I have a file with 100 lines, I add 23 lines at the end of it, all the 123 lines are going to be formatted by the linter. This is often not desired, and it's not the same behavior with other linters, even always using the same tool (https://github.com/hallettj/git-format-staged) 😬 Here's my pre-commit:
Here's the file I tested with:
I also tested with a real file from my project, and observed same results. |
Hey 👋
I've searched for this feature in existing issues and pull requests and couldn't find. I also read the documentation provided by
ktlint --help
.Expected Behavior
I use ktlint on Android projects, and use SwiftFormat on iOS projects. I've been using ktlint on my team's
precommit
git hook, where I created a custom script that basically "lints (and autoformat) git staged files". However, although we've been using this script for (probably) over 3 years now, it has some flaws. So I found out recently that SwiftFormat added a section that instructs users how to install a precommit hook pretty well: https://github.com/nicklockwood/SwiftFormat#git-pre-commit-hookIt uses https://github.com/hallettj/git-format-staged - which I found quite of an amazing strategy, take some time to read its README :). I believe it's the state of the art and most accurate way to perform such task of formatting staged files.
Current Behavior
Here comes the problem: ktlint doesn't work with
git-format-staged
out of the box because (AFAICT) there's no way to make it print the output to stdout, unlike SwiftFormat and other linters from other languages like ruby and javascript. In practical terms:SwiftFormat stdin option prints result to stdout
ktlint stdin option just analyzes the input stream and prints its analysis to stdout
Thus, this feature request consists of enabling us to request ktlint to print the output of a stdin, to stdout (same as
-F
or--format
options, but printing to stdout instead of in-place at the original file).Proposal
Additional information
Also, check out this excerpt taken from https://github.com/nicklockwood/SwiftFormat:
I don't know if this would make sense in ktlint as I don't know if there're rules that make use of the filepath (e.g. filepath linter, or detecting configuration files). May be something to keep in mind, though!
Thanks for reading! Looking forward to hearing your thoughts on this feature and its feasibility, as well as if there's any shortcut we can use to make this happen with zero code changes that I may not be aware of 🙏
The text was updated successfully, but these errors were encountered: