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

adding gradle and groovy auto detection #4561

Merged
merged 5 commits into from
Oct 8, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.gradle.style;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.style.Style;

import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;

import static java.util.Collections.emptySet;

public class Autodetect extends NamedStyles {
@JsonCreator
public Autodetect(UUID id, Collection<Style> styles) {
super(id,
"org.openrewrite.gradle.Autodetect",
"Auto-detected",
"Automatically detect styles from a repository's existing code.",
emptySet(), styles);
}

public static Detector detector() {
return new Detector();
}

public static class Detector {
org.openrewrite.java.style.Autodetect.Detector javaDetector = new org.openrewrite.java.style.Autodetect.Detector();

public void sample(SourceFile cu) {
if (cu instanceof G.CompilationUnit) {
javaDetector.sample(cu);
}
}

public Autodetect build() {
return new Autodetect(Tree.randomId(), Arrays.asList(
javaDetector.getTabsAndIndentsStyle(),
javaDetector.getImportLayoutStyle(),
javaDetector.getSpacesStyle(),
javaDetector.getWrappingAndBracesStyle(),
javaDetector.getFormatStyle()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.gradle.style;
lkerford marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.jupiter.api.Test;
import org.openrewrite.SourceFile;
import org.openrewrite.groovy.GroovyParser;
import org.openrewrite.java.style.TabsAndIndentsStyle;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.test.RewriteTest;

import java.util.stream.Stream;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings({"ConstantConditions"})
class AutodetectTest implements RewriteTest {
@Test
void gradleTabsAndIndents() {
Stream<SourceFile> parse = GroovyParser.builder().build()
.parse(
"""
plugins {
id 'groovy-gradle-plugin'
}
repositories {
gradlePluginPortal() // so that external plugins can be resolved in dependencies section
}

dependencies {
implementation 'io.freefair.gradle:lombok-plugin:8.4'
implementation 'com.github.spotbugs.snom:spotbugs-gradle-plugin:5.0.13'
}
"""
);
var detector = Autodetect.detector();
parse.forEach(detector::sample);
var styles = detector.build();
Copy link
Member

@sambsnyd sambsnyd Oct 8, 2024

Choose a reason for hiding this comment

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

Is the type of styles org.openrewrite.java.style.Autodetect or org.openrewrite.gradle.style.Autodetect ? 🙂

Might influence whether your other PRs work as intended or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are 100% correct, I have pushed an update which will fix this issue

assertThat(styles.getName()).isEqualTo("org.openrewrite.gradle.Autodetect");

var tabsAndIndents = NamedStyles.merge(TabsAndIndentsStyle.class, singletonList(styles));
assertThat(tabsAndIndents.getUseTabCharacter()).isFalse();
assertThat(tabsAndIndents.getTabSize()).isEqualTo(2);
assertThat(tabsAndIndents.getIndentSize()).isEqualTo(2);
assertThat(tabsAndIndents.getContinuationIndent()).isEqualTo(4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.groovy.style;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.style.Style;

import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;

import static java.util.Collections.emptySet;

public class Autodetect extends NamedStyles {
@JsonCreator
public Autodetect(UUID id, Collection<Style> styles) {
super(id,
"org.openrewrite.groovy.Autodetect",
"Auto-detected",
"Automatically detect styles from a repository's existing code.",
emptySet(), styles);
}


public static Detector detector() {
return new Detector();
}

public static class Detector {
org.openrewrite.java.style.Autodetect.Detector javaDetector = new org.openrewrite.java.style.Autodetect.Detector();

public void sample(SourceFile cu) {
if (cu instanceof G.CompilationUnit) {
javaDetector.sample(cu);
}
}

public Autodetect build() {
return new Autodetect(Tree.randomId(), Arrays.asList(
javaDetector.getTabsAndIndentsStyle(),
javaDetector.getImportLayoutStyle(),
javaDetector.getSpacesStyle(),
javaDetector.getWrappingAndBracesStyle(),
javaDetector.getFormatStyle()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.groovy.style;
lkerford marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.jupiter.api.Test;
import org.openrewrite.SourceFile;
import org.openrewrite.groovy.GroovyParser;
import org.openrewrite.java.style.TabsAndIndentsStyle;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.test.RewriteTest;

import java.util.stream.Stream;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings({"ConstantConditions"})
class AutodetectTest implements RewriteTest {
@Test
void groovyTabsAndIndents() {
Stream<SourceFile> parse = GroovyParser.builder().build()
.parse(
"""
class Example {
static void main(String[] args) {
String name = "Joe"
int ID = 1
println(name + ID )
}
}
"""
);
var detector = Autodetect.detector();
parse.forEach(detector::sample);
var styles = detector.build();
assertThat(styles.getName()).isEqualTo("org.openrewrite.groovy.Autodetect");

var tabsAndIndents = NamedStyles.merge(TabsAndIndentsStyle.class, singletonList(styles));
assertThat(tabsAndIndents.getUseTabCharacter()).isFalse();
assertThat(tabsAndIndents.getTabSize()).isEqualTo(4);
assertThat(tabsAndIndents.getIndentSize()).isEqualTo(4);
assertThat(tabsAndIndents.getContinuationIndent()).isEqualTo(8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public static class Detector {
private final FindLineFormatJavaVisitor findLineFormat = new FindLineFormatJavaVisitor();

public void sample(SourceFile cu) {
// only sample Java sources (extending languages need their own auto-detection)
if (cu instanceof J.CompilationUnit) {
if (cu instanceof JavaSourceFile) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@sambsnyd It seems to me that we shouldn't have to change this now. What am I missing?

findImportLayout.visitNonNull(cu, 0);
findIndent.visitNonNull(cu, indentStatistics);
findSpaces.visitNonNull(cu, spacesStatistics);
Expand All @@ -83,6 +82,23 @@ public Autodetect build() {
wrappingAndBracesStatistics.getWrappingAndBracesStyle(),
generalFormatStatistics.getFormatStyle()));
}

public TabsAndIndentsStyle getTabsAndIndentsStyle() {
return indentStatistics.getTabsAndIndentsStyle();
}

public ImportLayoutStyle getImportLayoutStyle(){
return findImportLayout.aggregate().getImportLayoutStyle();
}
public SpacesStyle getSpacesStyle(){
return spacesStatistics.getSpacesStyle();
}
public WrappingAndBracesStyle getWrappingAndBracesStyle(){
return wrappingAndBracesStatistics.getWrappingAndBracesStyle();
}
public GeneralFormatStyle getFormatStyle(){
return generalFormatStatistics.getFormatStyle();
}
}

private static class GeneralFormatStatistics {
Expand Down Expand Up @@ -428,6 +444,12 @@ public Expression visitExpression(Expression expression, IndentStatistics stats)
@SuppressWarnings("CommentedOutCode")
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation m, IndentStatistics stats) {
for (Expression argument : m.getArguments()) {
if (argument instanceof J.Lambda) {
visit((((J.Lambda) argument).getBody()), stats);
}
}

Set<Expression> statementExpressions = getCursor().getNearestMessage("STATEMENT_EXPRESSION", emptySet());
if (statementExpressions.contains(m)) {
visitStatement(m, stats);
Expand Down