Skip to content

IntelliJ formatter #2020

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Gradle IDEA integration.
IDEA running using gradle
  • Loading branch information
IlyasYOY committed Jan 23, 2024
commit affd905be602b818e3084ffe186ae1149b829eb1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.diffplug.spotless.java.CleanthatJavaStep;
import com.diffplug.spotless.java.FormatAnnotationsStep;
import com.diffplug.spotless.java.GoogleJavaFormatStep;
import com.diffplug.spotless.java.IdeaStep;
import com.diffplug.spotless.java.ImportOrderStep;
import com.diffplug.spotless.java.PalantirJavaFormatStep;
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
Expand Down Expand Up @@ -313,6 +314,37 @@ public EclipseConfig withP2Mirrors(Map<String, String> mirrors) {

}

public IdeaConfig idea() {
return new IdeaConfig();
}

public class IdeaConfig {
String binaryPath;
boolean withDefaults = false;

IdeaConfig() {
addStep(createStep());
}

private FormatterStep createStep() {
return IdeaStep.create(withDefaults, binaryPath);
}

public IdeaConfig binaryPath(String binaryPath) {
Objects.requireNonNull(binaryPath);
this.binaryPath = binaryPath;
replaceStep(createStep());
return this;
}

public IdeaConfig withDefaults(Boolean withDefaults) {
Objects.requireNonNull(withDefaults);
this.withDefaults = withDefaults;
replaceStep(createStep());
return this;
}
}

/** Removes newlines between type annotations and types. */
public FormatAnnotationsConfig formatAnnotations() {
return new FormatAnnotationsConfig();
Expand Down Expand Up @@ -400,7 +432,7 @@ public CleanthatJavaConfig clearMutators() {
}

// An id of a mutator (see IMutator.getIds()) or
// tThe fully qualified name of a class implementing eu.solven.cleanthat.engine.java.refactorer.meta.IMutator
// The fully qualified name of a class implementing eu.solven.cleanthat.engine.java.refactorer.meta.IMutator
public CleanthatJavaConfig addMutator(String mutator) {
this.mutators.add(mutator);
replaceStep(createStep());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2020-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;

import java.io.IOException;

import org.junit.jupiter.api.Test;


class JavaIdeaTest extends GradleIntegrationHarness {
@Test
void idea() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"spotless {",
" java {",
" target file('test.java')",
" idea().binaryPath('idea').withDefaults(true)",
" }",
"}");

setFile("test.java").toResource("java/idea/full.dirty.java");
gradleRunner().withArguments("spotlessApply").build();
assertFile("test.java").notSameAsResource("java/idea/full.dirty.java");
}
}
12 changes: 12 additions & 0 deletions testlib/src/main/java/com/diffplug/spotless/ResourceHarness.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,18 @@ public void hasContent(String expected) {
hasContent(expected, StandardCharsets.UTF_8);
}

public void hasDifferentContent(String expected) {
hasDifferentContent(expected, StandardCharsets.UTF_8);
}

public void hasContent(String expected, Charset charset) {
assertThat(file).usingCharset(charset).hasContent(expected);
}

public void hasDifferentContent(String expected, Charset charset) {
assertThat(file).usingCharset(charset).isNotEqualTo(expected);
}

public void hasLines(String... lines) {
hasContent(String.join("\n", Arrays.asList(lines)));
}
Expand All @@ -163,6 +171,10 @@ public void sameAsResource(String resource) throws IOException {
hasContent(getTestResource(resource));
}

public void notSameAsResource(String resource) throws IOException {
hasDifferentContent(getTestResource(resource));
}

public void matches(Consumer<AbstractCharSequenceAssert<?, String>> conditions) throws IOException {
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
conditions.accept(assertThat(content));
Expand Down