-
Notifications
You must be signed in to change notification settings - Fork 473
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
IlyasYOY
wants to merge
17
commits into
diffplug:main
Choose a base branch
from
IlyasYOY:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
IntelliJ formatter #2020
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6419de1
Basic IDEA usage locally.
IlyasYOY b84d243
Parameterize IDEA usage.
IlyasYOY 1d8d9b2
Verify IDEA binary.
IlyasYOY 9d09760
Merge branch 'main' of github.com:diffplug/spotless
IlyasYOY affd905
Gradle IDEA integration.
IlyasYOY effe2fc
Maven IDEA integration.
IlyasYOY 8e30175
Make IDEA tests special
IlyasYOY 1554dd6
docs: CHANGES.md
IlyasYOY 763eaac
Config file for IDEA step
IlyasYOY fdb73b7
CI Fixes
IlyasYOY e58c9f8
Merge branch 'main' of github.com:diffplug/spotless
IlyasYOY c6c6cce
docs: fixed structure of CHANGES.md
IlyasYOY bf5918c
Merge branch 'main' of github.com:diffplug/spotless
IlyasYOY 9123527
Create State object
IlyasYOY 7d6eed4
Merge branch 'main' of github.com:diffplug/spotless
IlyasYOY e03e715
Merge branch 'main' into main-IlyasYOY
nedtwigg a0bde46
spotlessApply
nedtwigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ def special = [ | |
'Buf', | ||
'Clang', | ||
'gofmt', | ||
'idea', | ||
'Npm', | ||
'Shfmt' | ||
] | ||
|
93 changes: 93 additions & 0 deletions
93
lib/src/main/java/com/diffplug/spotless/java/IdeaFormatterFunc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.diffplug.spotless.java; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
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 org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import com.diffplug.spotless.ForeignExe; | ||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.ProcessRunner; | ||
|
||
public final class IdeaFormatterFunc implements FormatterFunc.NeedsFile { | ||
|
||
private static final Logger LOGGER = | ||
LoggerFactory.getLogger(IdeaStep.class); | ||
|
||
private static final String DEFAULT_IDEA = "idea"; | ||
|
||
private String binaryPath; | ||
private String configPath; | ||
private boolean withDefaults; | ||
|
||
private IdeaFormatterFunc(boolean withDefaults, String binaryPath, | ||
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); | ||
} | ||
nedtwigg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static IdeaFormatterFunc allowingDefaultsWithCustomBinary( | ||
String binaryPath, String configPath) { | ||
return new IdeaFormatterFunc(true, binaryPath, configPath); | ||
} | ||
|
||
public static IdeaFormatterFunc noDefaultsWithCustomBinary( | ||
String binaryPath, String configPath) { | ||
return new IdeaFormatterFunc(false, binaryPath, configPath); | ||
} | ||
|
||
@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()); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
lib/src/main/java/com/diffplug/spotless/java/IdeaStep.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.diffplug.spotless.java; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
|
||
public final class IdeaStep { | ||
|
||
private IdeaStep() {} | ||
|
||
public static FormatterStep create() { | ||
return create(true); | ||
} | ||
|
||
public static FormatterStep create(boolean withDefaults) { | ||
return create(true, null); | ||
} | ||
|
||
public static FormatterStep create(boolean withDefaults, | ||
String binaryPath) { | ||
return create(withDefaults, binaryPath, null); | ||
} | ||
|
||
public static FormatterStep create(boolean withDefaults, | ||
String binaryPath, String configPath) { | ||
IdeaFormatterFunc formatterFunc = | ||
getFormatterFunc(withDefaults, binaryPath, configPath); | ||
// TODO: make it lazy | ||
return FormatterStep.createNeverUpToDate("IDEA", formatterFunc); | ||
} | ||
|
||
private static IdeaFormatterFunc getFormatterFunc(boolean withDefaults, | ||
String binaryPath, String configPath) { | ||
if (withDefaults) { | ||
return IdeaFormatterFunc | ||
.allowingDefaultsWithCustomBinary(binaryPath, configPath); | ||
} | ||
return IdeaFormatterFunc.noDefaultsWithCustomBinary(binaryPath, configPath); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaIdeaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Idea.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
plugin-maven/src/test/java/com/diffplug/spotless/maven/java/IdeaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2016-2021 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"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.