Skip to content

Commit cc3b051

Browse files
committed
feat: introduce file type inferring to eclipse wtp
1 parent f2eac77 commit cc3b051

File tree

14 files changed

+137
-4
lines changed

14 files changed

+137
-4
lines changed

app/src/main/java/com/diffplug/spotless/cli/steps/EclipseWtp.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717

1818
import java.nio.file.Path;
1919
import java.util.List;
20+
import java.util.Locale;
21+
import java.util.function.Supplier;
2022

2123
import org.jetbrains.annotations.NotNull;
24+
import org.jetbrains.annotations.Nullable;
2225

2326
import com.diffplug.spotless.FormatterStep;
2427
import com.diffplug.spotless.cli.core.SpotlessActionContext;
28+
import com.diffplug.spotless.cli.core.TargetFileTypeInferer;
2529
import com.diffplug.spotless.cli.help.OptionConstants;
2630
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2731
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
@@ -45,9 +49,10 @@ public class EclipseWtp extends SpotlessFormatterStep {
4549

4650
@CommandLine.Option(
4751
names = {"-t", "--type"},
48-
description = "The type of the Eclipse WTP formatter." + OptionConstants.VALID_VALUES_SUFFIX,
49-
required = true)
50-
Type type; // TODO: might trying inferring type
52+
description =
53+
"The type of the Eclipse WTP formatter. If not provided, the type will be guessed based on the first few files we find. If that does not work, we fail the formatting run."
54+
+ OptionConstants.VALID_VALUES_SUFFIX)
55+
Type type;
5156

5257
public enum Type {
5358
CSS(EclipseWtpFormatterStep.CSS),
@@ -66,11 +71,23 @@ public enum Type {
6671
public @NotNull EclipseWtpFormatterStep toEclipseWtpType() {
6772
return this.backendEclipseWtpType;
6873
}
74+
75+
public static @Nullable Type fromTargetFileType(@NotNull TargetFileTypeInferer.TargetFileType targetFileType) {
76+
return switch (targetFileType.fileExtension().toLowerCase(Locale.getDefault())) {
77+
case "css" -> CSS;
78+
case "html", "htm" -> HTML;
79+
case "js" -> JS;
80+
case "json" -> JSON;
81+
case "xml" -> XML;
82+
case "xhtml" -> XHTML;
83+
default -> null;
84+
};
85+
}
6986
}
7087

7188
@Override
7289
public @NotNull List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) {
73-
EclipseWtpFormatterStep wtpType = type.toEclipseWtpType();
90+
EclipseWtpFormatterStep wtpType = type(context::targetFileType).toEclipseWtpType();
7491
EclipseBasedStepBuilder builder = wtpType.createBuilder(context.provisioner());
7592
builder.setVersion(ECLIPSE_WTP_VERSION);
7693
if (configFiles != null && !configFiles.isEmpty()) {
@@ -81,4 +98,19 @@ public enum Type {
8198
}
8299
return List.of(builder.build());
83100
}
101+
102+
private Type type(Supplier<TargetFileTypeInferer.TargetFileType> targetFileTypeSupplier) {
103+
if (type != null) {
104+
return type;
105+
}
106+
// try type inferring
107+
TargetFileTypeInferer.TargetFileType targetFileType = targetFileTypeSupplier.get();
108+
Type inferredType = Type.fromTargetFileType(targetFileType);
109+
if (inferredType != null) {
110+
return inferredType;
111+
} else {
112+
throw new IllegalArgumentException("Could not infer Eclipse WTP type from target file type: "
113+
+ targetFileType.fileExtension() + " - workaround by specifying the --type option.");
114+
}
115+
}
84116
}

app/src/test/java/com/diffplug/spotless/cli/steps/EclipseWtpCssTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ void itSupportsFormattingCssFileType() {
3333
String fileName = runEclipseWtpWithType(EclipseWtp.Type.CSS, "body {\n" + "a: v; b: \n" + "v;\n" + "} \n");
3434
selfie().expectResource(fileName).toMatchDisk();
3535
}
36+
37+
@Test
38+
void itInfersCssFileTypeFromFileExtension() {
39+
String fileName = runEclipseWtpWithTypeInferred("css", "body {\n" + "a: v; b: \n" + "v;\n" + "} \n");
40+
selfie().expectResource(fileName).toMatchDisk();
41+
}
3642
}

app/src/test/java/com/diffplug/spotless/cli/steps/EclipseWtpHtmlTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ void itSupportsFormattingHtmlFileType() {
3535
"<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n" + "</html> ");
3636
selfie().expectResource(fileName).toMatchDisk();
3737
}
38+
39+
@Test
40+
void itInfersHtmlFileTypeFromFileExtension() {
41+
String fileName = runEclipseWtpWithTypeInferred(
42+
"html", "<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n" + "</html> ");
43+
selfie().expectResource(fileName).toMatchDisk();
44+
}
45+
46+
@Test
47+
void itInfersHtmlFileTypeFromShortFileExtension() {
48+
String fileName = runEclipseWtpWithTypeInferred(
49+
"htm", "<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n" + "</html> ");
50+
selfie().expectResource(fileName).toMatchDisk();
51+
}
3852
}

app/src/test/java/com/diffplug/spotless/cli/steps/EclipseWtpJsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ void itSupportsFormattingJsFileType() {
3333
String fileName = runEclipseWtpWithType(EclipseWtp.Type.JS, "function f( ) {\n" + "a.b(1,\n" + "2);}");
3434
selfie().expectResource(fileName).toMatchDisk();
3535
}
36+
37+
@Test
38+
void itInfersJsFileTypeFromFileExtension() {
39+
String fileName = runEclipseWtpWithTypeInferred("js", "function f( ) {\n" + "a.b(1,\n" + "2);}");
40+
selfie().expectResource(fileName).toMatchDisk();
41+
}
3642
}

app/src/test/java/com/diffplug/spotless/cli/steps/EclipseWtpJsonTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ void itSupportsFormattingJsonFileType() {
3434
runEclipseWtpWithType(EclipseWtp.Type.JSON, "{\"a\": \"b\",\t\"c\": { \"d\": \"e\",\"f\": \"g\"}}");
3535
selfie().expectResource(fileName).toMatchDisk();
3636
}
37+
38+
@Test
39+
void itInfersJsonFileTypeFromFileExtension() {
40+
String fileName =
41+
runEclipseWtpWithTypeInferred("json", "{\"a\": \"b\",\t\"c\": { \"d\": \"e\",\"f\": \"g\"}}");
42+
selfie().expectResource(fileName).toMatchDisk();
43+
}
3744
}

app/src/test/java/com/diffplug/spotless/cli/steps/EclipseWtpTestBase.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ protected String runEclipseWtpWithType(EclipseWtp.Type type, String unformatted)
3232

3333
return fileName;
3434
}
35+
36+
protected String runEclipseWtpWithTypeInferred(String fileExtension, String unformatted) {
37+
String fileName = "test." + fileExtension;
38+
setFile(fileName).toContent(unformatted);
39+
40+
SpotlessCLIRunner.Result result =
41+
cliRunner().withTargets(fileName).withStep(EclipseWtp.class).run();
42+
43+
return fileName;
44+
}
3545
}

app/src/test/java/com/diffplug/spotless/cli/steps/EclipseWtpXhtmlTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ void itSupportsFormattingXhtmlFileType() {
3535
"<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n" + "</html> ");
3636
selfie().expectResource(fileName).toMatchDisk();
3737
}
38+
39+
@Test
40+
void itInfersXhtmlFileTypeFromFileExtension() {
41+
String fileName = runEclipseWtpWithTypeInferred(
42+
"xhtml", "<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n" + "</html> ");
43+
selfie().expectResource(fileName).toMatchDisk();
44+
}
3845
}

app/src/test/java/com/diffplug/spotless/cli/steps/EclipseWtpXmlTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ void itSupportsFormattingXmlFileType() {
3333
String fileName = runEclipseWtpWithType(EclipseWtp.Type.XML, "<a><b> c</b></a>");
3434
selfie().expectResource(fileName).toMatchDisk();
3535
}
36+
37+
@Test
38+
void itInfersXmlFileTypeFromFileExtension() {
39+
String fileName = runEclipseWtpWithTypeInferred("xml", "<a><b> c</b></a>");
40+
selfie().expectResource(fileName).toMatchDisk();
41+
}
3642
}

app/src/test/resources/com/diffplug/spotless/cli/steps/EclipseWtpCssTest.ss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
╔═ itInfersCssFileTypeFromFileExtension ═╗
2+
body {
3+
a: v;
4+
b: v;
5+
}
16
╔═ itSupportsFormattingCssFileType ═╗
27
body {
38
a: v;

app/src/test/resources/com/diffplug/spotless/cli/steps/EclipseWtpHtmlTest.ss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
╔═ itInfersHtmlFileTypeFromFileExtension ═╗
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
</head>
7+
</html>
8+
9+
╔═ itInfersHtmlFileTypeFromShortFileExtension ═╗
10+
<!DOCTYPE html>
11+
<html>
12+
<head>
13+
<meta charset="UTF-8">
14+
</head>
15+
</html>
16+
117
╔═ itSupportsFormattingHtmlFileType ═╗
218
<!DOCTYPE html>
319
<html>

0 commit comments

Comments
 (0)