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

fix #1076: Add support for palantir-java-format #1083

Merged
merged 15 commits into from
Jan 12, 2022

Conversation

carterkozak
Copy link
Contributor

@carterkozak carterkozak commented Jan 10, 2022

What

palantir-java-format is a modern, lambda-friendly, 120 character Java formatter based on the excellent google-java-format, and benefits from the work of all the original authors. palantir-java-format is available under the same (Apache 2.0 License)[https://github.com/palantir/palantir-java-format/blob/develop/LICENSE].

Examples from the readme

(1) before:

private static void configureResolvedVersionsWithVersionMapping(Project project) {
    project.getPluginManager()
            .withPlugin(
                    "maven-publish",
                    plugin -> {
                        project.getExtensions()
                                .getByType(PublishingExtension.class)
                                .getPublications()
                                .withType(MavenPublication.class)
                                .configureEach(
                                        publication ->
                                                publication.versionMapping(
                                                        mapping -> {
                                                            mapping.allVariants(
                                                                    VariantVersionMappingStrategy
                                                                            ::fromResolutionResult);
                                                        }));
                    });
}

(1) after:

private static void configureResolvedVersionsWithVersionMapping(Project project) {
    project.getPluginManager().withPlugin("maven-publish", plugin -> {
        project.getExtensions()
                .getByType(PublishingExtension.class)
                .getPublications()
                .withType(MavenPublication.class)
                .configureEach(publication -> publication.versionMapping(mapping -> {
                    mapping.allVariants(VariantVersionMappingStrategy::fromResolutionResult);
                }));
    });
}

(2) before:

private static GradleException notFound(
        String group, String name, Configuration configuration) {
    String actual =
            configuration.getIncoming().getResolutionResult().getAllComponents().stream()
                    .map(ResolvedComponentResult::getModuleVersion)
                    .map(
                            mvi ->
                                    String.format(
                                            "\t- %s:%s:%s",
                                            mvi.getGroup(), mvi.getName(), mvi.getVersion()))
                    .collect(Collectors.joining("\n"));
    // ...
}

(2) after:

private static GradleException notFound(String group, String name, Configuration configuration) {
    String actual = configuration.getIncoming().getResolutionResult().getAllComponents().stream()
            .map(ResolvedComponentResult::getModuleVersion)
            .map(mvi -> String.format("\t- %s:%s:%s", mvi.getGroup(), mvi.getName(), mvi.getVersion()))
            .collect(Collectors.joining("\n"));
    // ...
}

How

This implementation is nearly identical to the google-java-format implementation, given that palantir-java-format is a fork! I've avoided coupling the implementations, as that's more likely to cause harm than help, especially given the logic around version checks for supported features.

One difference between the implementations is that I have opted not to support reflowing strings. Currently the palantir-java-format library does not expose the reflowing method as public API, so it would have to be implemented using a different method such as Formatter.formatSourceAndFixImports.


PR metadata

  • Please DO NOT FORCE PUSH. Don't worry about messy history, it's easier to do code review if we can tell what happened after the review, and force pushing breaks that.
  • Please make sure that your PR allows edits from maintainers. Sometimes its faster for us to just fix something than it is to describe how to fix it.
  • After creating the PR, please add a commit that adds a bullet-point under the [Unreleased] section of CHANGES.md, plugin-gradle/CHANGES.md, and plugin-maven/CHANGES.md which includes:
    • a summary of the change
    • either
      • [] a link to the issue you are resolving (for small changes)
      • a link to the PR you just created (for big changes likely to have discussion)
    • If your change only affects a build plugin, and not the lib, then you only need to update the plugin-foo/CHANGES.md for that plugin. If your change affects lib in an end-user-visible way (fixing a bug, updating a version) then you need to update CHANGES.md for both the lib and all build plugins. Users of a build plugin shouldn't have to refer to lib to see changes that affect them.

@carterkozak
Copy link
Contributor Author

It looks like test_nomaven_11 Failed with java.lang.OutOfMemoryError: Metaspace. I don't believe this is related to my change, however I don't have permissions to 'rerun from failed' in circle. I can push an empty commit if that's preferred.

Copy link
Member

@nedtwigg nedtwigg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've gotta pick whether you're staying compatible with GJF or going your own way :)

@carterkozak
Copy link
Contributor Author

That's fair, I can remove the extra bits, and trim down the API :-)

Support for non-default styles and alternative artifacts have
been dropped in favor simplicity.
If requested, we can add support for reflowing strings in the
future.
Copy link
Member

@nedtwigg nedtwigg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@carterkozak
Copy link
Contributor Author

Thanks for the review!

@nedtwigg
Copy link
Member

nedtwigg commented Jan 11, 2022

I'm gonna give this ~24hrs in case anyone else wants to weigh-in and then I'll merge and release.

@nedtwigg
Copy link
Member

Welp, jcenter is down today (semi-scheduled), which has brought the Gradle plugin portal down. https://discuss.gradle.org/t/plugins-redirecting-to-jcenter/41909/4

This PR is confirmed to pass all tests before the Gradle outage, so I'm moving forward with merging.

@nedtwigg nedtwigg merged commit c66f83e into diffplug:main Jan 12, 2022
@nedtwigg
Copy link
Member

Released in plugin-gradle 6.2.0 and plugin-maven 2.20.0.

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

Successfully merging this pull request may close these issues.

2 participants