-
Notifications
You must be signed in to change notification settings - Fork 459
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
IDE hook #568
IDE hook #568
Conversation
Moving a twitter convo here: the VS Code extension (#567) could work better with a Here's the relevant issues: https://gist.github.com/badsyntax/d6b3c0676d9f99a9784327e5004d889d |
This new API looks great and will make my consuming code a whole lot simpler. Regarding better VS Code integration, getting spotless to write to |
I'm retargeting this PR to be a prototype for an IDE hook. The API currently implemented is this:
Here are testcases for each of these: spotless/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java Lines 79 to 112 in 842bacc
@badsyntax you can integration-test against any commit in this PR using jitpack with these instructions (use 'diffplug' for 'USER_OR_ORG') Let's iterate in this PR until we have an API that makes it easy for your plugin to provide a good user experience. |
57b6e19
to
842bacc
Compare
…e 5, which has a stderr bug in the tooling API)
I've had some difficulties consuming the jitpack package. settlings.gradle
Gradle output
Seems like it's trying to download packages that require authentication, eg https://jitpack.io/com/github/diffplug/spotless-lib/1.29.0-SNAPSHOT/maven-metadata.xml I've tried a bunch of things, including checking out the branch locally and publishing to local maven but got a bit confused on how the snapshots work. |
@badsyntax Maybe we can chat in realtime on gitter: https://gitter.im/diffplug/spotless |
Solution is to add a metadataSources block like this:
I will update our instructions and also file a bug with JitPack. |
Fumble-fingers |
Cool that change is working for me. Will have a play and give some feedback 👍 |
After integrating this change into vscode I've discovered something else that would be required. Apologies for not realising this beforehand. |
I see, you'll use stdin to send the possibly-dirty editor state. Sounds like a good plan! Taking a look... |
Hmm.. that's too bad there's a race condition when you ask the editor to save.
But we'll see! Taking a quick hack at |
The gradle testkit doesn't have a "set stdin" that I could see, so I didn't do anything to fix the tests. But if the naive "read all bytes on System.in" approach works, then the commit above should work for you. |
About the race conditions, these are some good points. At the moment the formatting is restricted to files that have already been saved to disk, as I assumed spotless would need to know various things based on the file location in order to format correctly. This is a disclaimer I can add to the README. It'd be great if we could accommodate this scenario, but I see it as a "nice to have" rather than a real issue.
I see the package is in jitpack now. Am testing this change. |
So this seems to be working correctly, which is great news. I'm able to set the file contents via stdin with the Gradle Tooling API, and am able to get the formatted contents back via stdout. The working stdin approach is also demonstrated by this command using the gradle wrapper: ./gradlew spotlessApply \
-PspotlessIdeHook=/Users/richardwillis/Projects/badsyntax/vscode-spotless-gradle/test-fixtures/gradle-project/src/main/java/gradle/project/App.java \
--quiet \
<<< 'package gradle.project;public class App {public static void main(String[] args) {System.out.println("app");}}' \
2> /dev/null I'm however having some difficulties suppressing the gradle progress logs being written to stdout with tooling API, even with passing the
I had a look at the testkit API before suggesting stdin and couldn't see any API to allow you to test this, and so was hesitant to suggest this approach. I hope you can find a way to test this! I'll try debug the quiet issue tomorrow and let you know. |
Ignore my previous comment about issues with the You'll see in the gif below that i'm using the input & output streams instead of saving to disk, and it correctly formats a "dirty" state, as well as formatting "on save", and there's no race conditions. Super excited about this! I'll write some vscode integration tests tomorrow, but the new spotless API is looking good 👍 |
…ut` properties to configure where the I/O comes from. This makes it easier to test and more flexible.
@badsyntax Just FYI, you will need to add Also, I added a questionable commit 13700cb. When you send a file using the hook, it has to be tested against every single I don't know of a way to "end a build" cleanly from within a task, but I can close |
@nedtwigg No problem about the changes, I'll update my code. At the moment, I stream stdout/sterr to the client, but I wait for the gradle process/build to end, using BuildLauncher.run(), before applying the changes collected from stdout. I don't check for stream close. I'll see I can adapt my code to format on stream close, but I worry a bit about the effects of not waiting for the build to finish. I'll need to do some more testing to fully understand the effects. I've added an early-stage PR (badsyntax/vscode-spotless-gradle#10) if you want to have an initial look at how the vscode extension deals with the streams. (Keep in mind there's still lots I need to do before it's ready for release.) |
Yep, this is coming along slowly. I have a rough implementation and all of the tests passing. I expect to add some polish and have something to share next week. Thanks for the offer, but unless the merge conflicts are severe, I'll likely just absorb them as part of my rebase/cleanup work. |
This is probably premature optimization on my part. If it's trivial then take a look, if it's not then I'll just revert that last commit. There are other ways to speed it up if we need to in the future. |
@nedtwigg it seems that closing the system streams has no effect on the custom On the consuming side I need to explicitly close the custom So I guess it's fine to leave that code as is, as it might be providing performance improvements for those running this task without setting custom output streams (eg via the gradle wrapper). |
Btw badsyntax/vscode-spotless-gradle#10 is now ready, but depends on these changes being released. |
Now published in |
Announcement tweet |
Hey @nedtwigg, sorry for the delay in responding, it's been a busy week for me! I've just given this a cursory glance, but from what I've seen it LGTM. 👍 |
We currently have a flag which allows formatting specific files,
-PspotlessFiles
, documented hereThere are two problems with this:
String.matches(String regex)
is a bummer of an API for paths, because you have to regex-escape dots and backslashes. We've wasted a lot of user time on this surprising behavior (Maven flag -DspotlessFiles= ineffective #462, Problems with SpecificFilesTest #528, Relative Path Support for spotlessFiles #531, Custom Spotless Task #518).FileCollection
javadoc:This PR won't get merged until after #560 makes it in, too many merge conflicts between them and 560 is more important. But this PR shows how we can easily be much faster with this API. Main goal is to get feedback on the documentation, and alert interested parties to better API on the way.
The stimulus to make me work on this is @badsyntax's VS Code extension. Now our "format one file" performance matters, and it's silly that he has to do a regex-escape just to pass a single file.