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

IntelliJ formatter #2020

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* `FileSignature.Promised` and `JarState.Promised` to facilitate round-trip serialization for the Gradle configuration cache. ([#1945](https://github.com/diffplug/spotless/pull/1945))
* Support for `idea` ([#2020](https://github.com/diffplug/spotless/pull/2020))
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))

### Removed
Expand Down
1 change: 1 addition & 0 deletions gradle/special-tests.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def special = [
'buf',
'clang',
'gofmt',
'idea',
'npm',
'shfmt'
]
Expand Down
137 changes: 137 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/java/IdeaStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 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.spotless.java;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.ForeignExe;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.ProcessRunner;

public final class IdeaStep {

private static final Logger LOGGER = LoggerFactory.getLogger(IdeaStep.class);

private IdeaStep() {}

public static FormatterStep create() {
return create(true);
}

public static FormatterStep create(boolean withDefaults) {
return create(withDefaults, null);
}

public static FormatterStep create(boolean withDefaults,
@Nullable String binaryPath) {
return create(withDefaults, binaryPath, null);
}

public static FormatterStep create(boolean withDefaults,
@Nullable String binaryPath, @Nullable String configPath) {
return FormatterStep.createLazy("IDEA",
() -> createState(withDefaults, binaryPath, configPath),
state -> state);
}

private static State createState(boolean withDefaults,
@Nullable String binaryPath, @Nullable String configPath) {
return new State(withDefaults, binaryPath, configPath);
}

private static class State
implements FormatterFunc.NeedsFile, Serializable {

private static final long serialVersionUID = -1825662355363926318L;
private static final String DEFAULT_IDEA = "idea";

private String binaryPath;
@Nullable
private String configPath;
private boolean withDefaults;

private State(boolean withDefaults, @Nullable String binaryPath,
@Nullable String configPath) {
this.withDefaults = withDefaults;
this.configPath = configPath;
this.binaryPath = Objects.requireNonNullElse(binaryPath, DEFAULT_IDEA);
resolveFullBinaryPathAndCheckVersion();
}

private void resolveFullBinaryPathAndCheckVersion() {
var exe = ForeignExe
.nameAndVersion(this.binaryPath, "IntelliJ IDEA")
.versionRegex(Pattern.compile("(IntelliJ IDEA) .*"))
.fixCantFind(
"IDEA executable cannot be found on your machine, "
+ "please install it and put idea binary to PATH; or report the problem")
.fixWrongVersion("Provided binary is not IDEA, "
+ "please check it and fix the problem; or report the problem");
try {
this.binaryPath = exe.confirmVersionAndGetAbsolutePath();
} catch (IOException e) {
throw new IllegalArgumentException("binary cannot be found", e);
} catch (InterruptedException e) {
throw new IllegalArgumentException(
"binary cannot be found, process was interrupted", e);
}
}

@Override
public String applyWithFile(String unix, File file) throws Exception {
List<String> params = getParams(file);

try (ProcessRunner runner = new ProcessRunner()) {
var result = runner.exec(params);

LOGGER.debug("command finished with stdout: {}",
result.assertExitZero(StandardCharsets.UTF_8));

return Files.readString(file.toPath());
}
}

private List<String> getParams(File file) {
var builder = Stream.<String> builder();
builder.add(binaryPath);
builder.add("format");
if (withDefaults) {
builder.add("-allowDefaults");
}
if (configPath != null) {
builder.add("-s");
builder.add(configPath);
}
builder.add(file.toString());
return builder.build().collect(Collectors.toList());
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Added
* Support for `idea` ([#2020](https://github.com/diffplug/spotless/pull/2020))
### Fixed
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))

Expand Down
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,45 @@ public EclipseConfig withP2Mirrors(Map<String, String> mirrors) {

}

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

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

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

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

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

public IdeaConfig configPath(String configPath) {
Objects.requireNonNull(configPath);
this.configPath = configPath;
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 +440,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,43 @@
/*
* 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;

import com.diffplug.spotless.tag.IdeaTest;

@IdeaTest
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");
}
}
6 changes: 3 additions & 3 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))

### Added
* Support for `idea` ([#2020](https://github.com/diffplug/spotless/pull/2020))
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
### Fixed
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))

## [2.43.0] - 2024-01-23
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016-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.spotless.maven.java;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.java.IdeaStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class Idea implements FormatterStepFactory {

@Parameter
private String binaryPath;

@Parameter
private String configPath;

@Parameter
private Boolean withDefaults = false;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
return IdeaStep.create(withDefaults, binaryPath, configPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,6 +68,10 @@ public void addImportOrder(ImportOrder importOrder) {
addStepFactory(importOrder);
}

public void addIdea(Idea idea) {
addStepFactory(idea);
}

public void addPalantirJavaFormat(PalantirJavaFormat palantirJavaFormat) {
addStepFactory(palantirJavaFormat);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2016-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.spotless.maven.java;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

@com.diffplug.spotless.tag.IdeaTest
class IdeaTest extends MavenIntegrationHarness {
@Test
void idea() throws Exception {
setFile("test.java").toResource("java/cleanthat/MultipleMutators.dirty.test");
writePomWithJavaSteps(
"<idea>",
" <binaryPath>idea</binaryPath>",
" <withDefaults>true</withDefaults>",
"</idea>");

mavenRunner().withArguments("spotless:apply").runNoError();

assertFile("test.java").notSameAsResource("java/cleanthat/MultipleMutators.dirty.test");
}
}
Loading
Loading