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

Add support for 'palantir-java-format' #1076

Closed
carterkozak opened this issue Jan 10, 2022 · 3 comments
Closed

Add support for 'palantir-java-format' #1076

carterkozak opened this issue Jan 10, 2022 · 3 comments

Comments

@carterkozak
Copy link
Contributor

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"));
    // ...
}

We've leveraged the expanded configuration surface of the Gradle plugin to use a custom spotless format step with Gradle, however it's not quite as easy to wire in using the maven plugin. I'd love to use spotless with the Palantir java formatter in some maven projects, but it's nontrivial to wire together externally. Rather than attempting to rebuild your excellent wheel, I'd like to investigate if there's any interest in accepting it as a third java formatter option out of the box.

Thanks!

@nedtwigg
Copy link
Member

Tons of interest! The best way is probably to add a PalantirJavaFormatStep like this.

If your API is close enough to GoogleJavaFormat's, then another way to do it is to add a configuration option to the existing GoogleJavaFormatStep, but I imagine that even if your API's match today, they might diverge over time, so probably best to stay decoupled.

A recent PR which I would consider a canonical example of adding a new formatter is #1011.

@nedtwigg nedtwigg changed the title Would you accept a contribution to support 'palantir-java-format'? Add support for 'palantir-java-format' Jan 10, 2022
@carterkozak
Copy link
Contributor Author

Fantastic, thanks @nedtwigg! I'll put together a PR this evening :-)

@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
Projects
None yet
Development

No branches or pull requests

2 participants