Skip to content

Commit 3c53d7c

Browse files
committed
Introduce scala, scalafmt and scalafix
1 parent d420aa0 commit 3c53d7c

File tree

35 files changed

+646
-34
lines changed

35 files changed

+646
-34
lines changed

code-providers/src/main/java/eu/solven/cleanthat/formatter/CodeProviderFormatter.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import cormoran.pepper.thread.PepperExecutorsHelper;
2828
import eu.solven.cleanthat.codeprovider.CodeProviderHelpers;
2929
import eu.solven.cleanthat.codeprovider.ICodeProvider;
30-
import eu.solven.cleanthat.codeprovider.ICodeProviderFile;
3130
import eu.solven.cleanthat.codeprovider.ICodeProviderWriter;
3231
import eu.solven.cleanthat.codeprovider.IListOnlyModifiedFiles;
3332
import eu.solven.cleanthat.config.ConfigHelpers;
@@ -192,7 +191,7 @@ protected AtomicLongMap<String> processFiles(ICodeProvider pr,
192191
if (matchingExclude.isEmpty()) {
193192
cs.submit(() -> {
194193
try {
195-
return doFormat(pr, pathToMutatedContent, languageP, file, filePath);
194+
return doFormat(pr, pathToMutatedContent, languageP, filePath);
196195
} catch (IOException e) {
197196
throw new UncheckedIOException("Issue with file: " + filePath, e);
198197
} catch (RuntimeException e) {
@@ -247,7 +246,6 @@ protected AtomicLongMap<String> processFiles(ICodeProvider pr,
247246
private boolean doFormat(ICodeProvider codeProvider,
248247
Map<String, String> pathToMutatedContent,
249248
ILanguageProperties languageP,
250-
ICodeProviderFile file,
251249
String filePath) throws IOException {
252250
Optional<String> optAlreadyMutated = Optional.ofNullable(pathToMutatedContent.get(filePath));
253251
String code = optAlreadyMutated.orElseGet(() -> {

java/pom.xml

+3-20
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@
3838
<artifactId>config</artifactId>
3939
<version>${project.version}</version>
4040
</dependency>
41-
4241
<dependency>
43-
<groupId>com.github.cormoran-io.pepper</groupId>
44-
<artifactId>pepper</artifactId>
45-
<version>${pepper.version}</version>
42+
<groupId>com.github.cormoran-io.cleanthat</groupId>
43+
<artifactId>any-language</artifactId>
44+
<version>${project.version}</version>
4645
</dependency>
4746

4847
<dependency>
@@ -84,15 +83,6 @@
8483
<scope>provided</scope>
8584
</dependency>
8685

87-
88-
<!-- This hacks many Eclipse classes -->
89-
<!-- <dependency> -->
90-
<!-- https://github.com/spring-io/spring-javaformat -->
91-
<!-- <groupId>io.spring.javaformat</groupId> -->
92-
<!-- <artifactId>spring-javaformat-formatter-eclipse-runtime</artifactId> -->
93-
<!-- <version>0.0.22</version> -->
94-
<!-- </dependency> -->
95-
9686
<dependency>
9787
<groupId>net.revelc.code</groupId>
9888
<artifactId>impsort-maven-plugin</artifactId>
@@ -133,12 +123,5 @@
133123
<version>${project.version}</version>
134124
<scope>test</scope>
135125
</dependency>
136-
137-
<dependency>
138-
<!-- https://www.baeldung.com/java-difference-between-two-strings -->
139-
<groupId>org.bitbucket.cowwoc</groupId>
140-
<artifactId>diff-match-patch</artifactId>
141-
<version>1.2</version>
142-
</dependency>
143126
</dependencies>
144127
</project>

java/src/test/java/io/cormoran/cleanthat/rules/it/ITTestLocalFile.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.List;
77

88
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;
9-
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch.Diff;
109
import org.junit.Test;
1110
import org.slf4j.Logger;
1211
import org.slf4j.LoggerFactory;
@@ -54,8 +53,7 @@ public void testCleanLocalFile() throws IOException {
5453
String newAsString = node.toString();
5554

5655
// TODO We may need to reformat to have a nice diff
57-
List<Diff> diff = dmp.diffMain(pathAsString, newAsString, false);
58-
LOGGER.info("Diff:");
56+
List<DiffMatchPatch.Diff> diff = dmp.diffMain(pathAsString, newAsString, false);
5957
diff.forEach(d -> LOGGER.info("{}", d));
6058
}
6159

json/pom.xml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>com.github.cormoran-io.cleanthat</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
<artifactId>aggregator-cleanthat</artifactId>
10+
</parent>
11+
12+
<artifactId>json</artifactId>
13+
14+
<properties>
15+
<scalafmt-core.version>3.0.0-RC6</scalafmt-core.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.github.cormoran-io.cleanthat</groupId>
21+
<artifactId>any-language</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>com.github.cormoran-io.cleanthat</groupId>
27+
<artifactId>test-helpers</artifactId>
28+
<version>${project.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
</project>

json/src/main/resources/empty

Whitespace-only changes.

java/src/test/java/eu/solven/cleanthat/language/json/TestJsonFormatter.java renamed to json/src/test/java/eu/solven/cleanthat/language/json/TestJsonFormatter.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44

5+
import org.junit.Assert;
56
import org.junit.Test;
67

78
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -15,6 +16,8 @@ public class TestJsonFormatter {
1516
@Test
1617
public void testFormatJson() throws IOException {
1718
ILanguageProperties languageProperties = new CleanthatLanguageProperties();
18-
formatter.format(languageProperties, "/somePath", "{ }");
19+
String formatted = formatter.format(languageProperties, "/somePath", "{ }");
20+
21+
Assert.assertEquals("{}", formatted);
1922
}
2023
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<configuration scan="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="logback.xsd">
2+
<!-- scan=true will reload this configuration file regularly -->
3+
<!-- debug="true" adds useless logs for each surefire JVM: add it only for investigation -->
4+
5+
<!-- http://logback.qos.ch/manual/jmxConfig.html -->
6+
<!-- TODO: stop in a ServletContextListener . See logback doc -->
7+
<contextName>CleanthatJsonTests</contextName>
8+
<jmxConfigurator />
9+
10+
<!-- http://logback.qos.ch/manual/configuration.html -->
11+
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator" />
12+
13+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
14+
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
15+
<encoder>
16+
<!-- http://stackoverflow.com/questions/2005929/howto-prevent-eclipse-from-line-wrapping-in-xml-and-html-files -->
17+
<!-- '%highlight' works under unix even if withJansi==false -->
18+
<pattern><![CDATA[%date [%thread] \(%-5level\) %logger{36}.%method\(%line\) - %msg%n]]></pattern>
19+
</encoder>
20+
21+
<!-- https://logback.qos.ch/manual/appenders.html#conAppWithJansi -->
22+
<!-- ANSI fails in our Windows env: https://jira.qos.ch/browse/LOGBACK-762 -->
23+
<withJansi>false</withJansi>
24+
</appender>
25+
26+
<!-- Spring is quite interesting, but quite verbose too: INFO but easily switchable to DEBUG -->
27+
<logger name="org.springframework" level="INFO" />
28+
<logger name="org.springframework.security" level="INFO" />
29+
30+
<!-- Debug by default -->
31+
<root level="INFO">
32+
<appender-ref ref="STDOUT" />
33+
</root>
34+
</configuration>

lambda/src/test/java/eu/solven/cleanthat/it/ITCheckNotModified.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public void testUnmodifiedString() throws IOException {
5959
LOGGER.info("----NEW---------END--------------");
6060

6161
// TODO We may need to reformat to have a nice diff
62-
List<Diff> diff = dmp.diffMain(pathAsString, newAsString, false);
63-
LOGGER.info("Diff:");
64-
// diff.forEach(d -> LOGGER.info("{}", d));
62+
List<DiffMatchPatch.Diff> diff = dmp.diffMain(pathAsString, newAsString, false);
63+
diff.forEach(d -> LOGGER.info("{}", d));
6564
}
6665
}

pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<module>config</module>
2828

2929
<module>java</module>
30+
<module>scala</module>
31+
<module>json</module>
3032
<!-- <module>python</module> -->
3133

3234
<module>runnable</module>

runnable/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@
5050
<artifactId>java</artifactId>
5151
<version>${project.version}</version>
5252
</dependency>
53+
<dependency>
54+
<groupId>com.github.cormoran-io.cleanthat</groupId>
55+
<artifactId>scala</artifactId>
56+
<version>${project.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.github.cormoran-io.cleanthat</groupId>
60+
<artifactId>json</artifactId>
61+
<version>${project.version}</version>
62+
</dependency>
5363

5464
<!-- <dependency> -->
5565
<!-- https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-aop-spring.html -->

runnable/src/main/java/eu/solven/cleanthat/lambda/AllLanguagesSpringConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.context.annotation.Import;
55

66
import eu.solven.cleanthat.language.java.JavaFormatter;
7-
import eu.solven.cleanthat.language.json.JsonFormatter;
7+
import eu.solven.cleanthat.language.scala.ScalaFormatter;
88

99
/**
1010
* Spring configuration wrapping all available languages
@@ -13,7 +13,7 @@
1313
*
1414
*/
1515
@Configuration
16-
@Import({ JavaFormatter.class, JsonFormatter.class })
16+
@Import({ JavaFormatter.class, ScalaFormatter.class })
1717
public class AllLanguagesSpringConfig {
1818

1919
}

scala/pom.xml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>com.github.cormoran-io.cleanthat</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
<artifactId>aggregator-cleanthat</artifactId>
10+
</parent>
11+
12+
<artifactId>scala</artifactId>
13+
14+
<properties>
15+
<scala.version>2.13</scala.version>
16+
17+
<scalafmt.version>3.0.0-RC6</scalafmt.version>
18+
<scalafix.version>0.9.29</scalafix.version>
19+
<scalafix-scala.version>${scala.version}.6</scalafix-scala.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.github.cormoran-io.cleanthat</groupId>
25+
<artifactId>any-language</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
29+
<!-- https://mvnrepository.com/artifact/org.scalameta/scalafmt-core -->
30+
<dependency>
31+
<groupId>org.scalameta</groupId>
32+
<artifactId>scalafmt-core_${scala.version}</artifactId>
33+
<version>${scalafmt.version}</version>
34+
</dependency>
35+
<!-- https://mvnrepository.com/artifact/org.scalameta/scalafmt-interfaces -->
36+
<dependency>
37+
<groupId>org.scalameta</groupId>
38+
<artifactId>scalafmt-interfaces</artifactId>
39+
<version>${scalafmt.version}</version>
40+
</dependency>
41+
<!-- https://mvnrepository.com/artifact/org.scalameta/scalafmt-dynamic -->
42+
<dependency>
43+
<groupId>org.scalameta</groupId>
44+
<artifactId>scalafmt-dynamic_${scala.version}</artifactId>
45+
<version>${scalafmt.version}</version>
46+
</dependency>
47+
48+
<!-- https://mvnrepository.com/artifact/ch.epfl.scala/scalafix-interfaces -->
49+
<dependency>
50+
<groupId>ch.epfl.scala</groupId>
51+
<artifactId>scalafix-interfaces</artifactId>
52+
<version>${scalafix.version}</version>
53+
</dependency>
54+
<!-- https://mvnrepository.com/artifact/ch.epfl.scala/scalafix-core -->
55+
<dependency>
56+
<groupId>ch.epfl.scala</groupId>
57+
<artifactId>scalafix-core_${scala.version}</artifactId>
58+
<version>${scalafix.version}</version>
59+
</dependency>
60+
<!-- https://mvnrepository.com/artifact/ch.epfl.scala/scalafix-reflect -->
61+
<dependency>
62+
<groupId>ch.epfl.scala</groupId>
63+
<artifactId>scalafix-reflect_${scalafix-scala.version}</artifactId>
64+
<version>${scalafix.version}</version>
65+
</dependency>
66+
<!-- https://mvnrepository.com/artifact/ch.epfl.scala/scalafix-cli -->
67+
<dependency>
68+
<groupId>ch.epfl.scala</groupId>
69+
<artifactId>scalafix-cli_${scalafix-scala.version}</artifactId>
70+
<version>${scalafix.version}</version>
71+
</dependency>
72+
73+
<!-- https://mvnrepository.com/artifact/com.thesamet.scalapb/scalapb-runtime -->
74+
<dependency>
75+
<!-- Unclear why Scalafix does not bring this transitively -->
76+
<groupId>com.thesamet.scalapb</groupId>
77+
<artifactId>scalapb-runtime_${scala.version}</artifactId>
78+
<version>0.11.4</version>
79+
</dependency>
80+
81+
82+
<dependency>
83+
<groupId>com.github.cormoran-io.cleanthat</groupId>
84+
<artifactId>test-helpers</artifactId>
85+
<version>${project.version}</version>
86+
<scope>test</scope>
87+
</dependency>
88+
</dependencies>
89+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package eu.solven.cleanthat.language.scala;
2+
3+
import java.util.Map;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
import cormoran.pepper.collection.PepperMapHelper;
11+
import eu.solven.cleanthat.formatter.ALanguageFormatter;
12+
import eu.solven.cleanthat.formatter.ISourceCodeFormatter;
13+
import eu.solven.cleanthat.language.ILanguageProperties;
14+
import eu.solven.cleanthat.language.IStringFormatter;
15+
import eu.solven.cleanthat.language.scala.scalafix.ScalafixFormatter;
16+
import eu.solven.cleanthat.language.scala.scalafix.ScalafixProperties;
17+
import eu.solven.cleanthat.language.scala.scalafmt.ScalafmtFormatter;
18+
import eu.solven.cleanthat.language.scala.scalafmt.ScalafmtProperties;
19+
20+
/**
21+
* Formatter for Scala
22+
*
23+
* @author Benoit Lacelle
24+
*/
25+
public class ScalaFormatter extends ALanguageFormatter implements IStringFormatter {
26+
private static final Logger LOGGER = LoggerFactory.getLogger(ScalaFormatter.class);
27+
28+
public ScalaFormatter(ObjectMapper objectMapper) {
29+
super(objectMapper);
30+
}
31+
32+
@Override
33+
public String getLanguage() {
34+
return "scala";
35+
}
36+
37+
@SuppressWarnings("PMD.TooFewBranchesForASwitchStatement")
38+
@Override
39+
protected ISourceCodeFormatter makeFormatter(Map<String, ?> rawProcessor, ILanguageProperties languageProperties) {
40+
String engine = PepperMapHelper.getRequiredString(rawProcessor, "engine");
41+
Map<String, Object> parameters = PepperMapHelper.getAs(rawProcessor, "parameters");
42+
if (parameters == null) {
43+
// Some engine takes no parameter
44+
parameters = Map.of();
45+
}
46+
LOGGER.info("Processing: {}", engine);
47+
48+
ISourceCodeFormatter processor;
49+
switch (engine) {
50+
case "scalafmt":
51+
ScalafmtProperties scalafmtConfig = getObjectMapper().convertValue(parameters, ScalafmtProperties.class);
52+
processor = new ScalafmtFormatter(languageProperties.getSourceCodeProperties(), scalafmtConfig);
53+
54+
break;
55+
case "scalafix":
56+
ScalafixProperties scalafixConfig = getObjectMapper().convertValue(parameters, ScalafixProperties.class);
57+
processor = new ScalafixFormatter(languageProperties.getSourceCodeProperties(), scalafixConfig);
58+
59+
break;
60+
61+
default:
62+
throw new IllegalArgumentException("Unknown engine: " + engine);
63+
}
64+
65+
return processor;
66+
}
67+
}

0 commit comments

Comments
 (0)