Skip to content

Commit 4b0e1fb

Browse files
committed
Update EclipseCdtFormatterStep to Equo.
1 parent 96a9017 commit 4b0e1fb

File tree

4 files changed

+97
-24
lines changed

4 files changed

+97
-24
lines changed

lib-extra/build.gradle

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ tasks.withType(Test).configureEach {
4141

4242
def NEEDS_P2_DEPS = [
4343
'jdt',
44-
'groovy'
44+
'groovy',
45+
'cdt'
4546
]
4647
for (needsP2 in NEEDS_P2_DEPS) {
4748
sourceSets.register(needsP2) {
@@ -72,13 +73,18 @@ p2deps {
7273
install 'org.eclipse.jdt.core'
7374
}
7475
into 'groovyCompileOnly', {
75-
p2repo 'https://download.eclipse.org/eclipse/updates/4.23/'
76-
p2repo 'https://groovy.jfrog.io/artifactory/plugins-release/org/codehaus/groovy/groovy-eclipse-integration/4.8.0/e4.23/'
76+
p2repo 'https://download.eclipse.org/eclipse/updates/4.26/'
77+
p2repo 'https://groovy.jfrog.io/artifactory/plugins-release/org/codehaus/groovy/groovy-eclipse-integration/4.8.0/e4.26/'
7778
install 'org.codehaus.groovy.eclipse.refactoring'
7879
install 'org.codehaus.groovy.eclipse.core'
7980
install 'org.eclipse.jdt.groovy.core'
8081
install 'org.codehaus.groovy'
8182
}
83+
into 'cdtCompileOnly', {
84+
p2repo 'https://download.eclipse.org/eclipse/updates/4.26/'
85+
p2repo 'https://download.eclipse.org/tools/cdt/releases/11.0/'
86+
install 'org.eclipse.cdt.core'
87+
}
8288
}
8389

8490
// we'll hold the core lib to a high standard
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2016-2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra.glue.cdt;
17+
18+
import java.util.Map;
19+
import java.util.Map.Entry;
20+
import java.util.Properties;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
23+
24+
import org.eclipse.cdt.core.formatter.CodeFormatter;
25+
import org.eclipse.jface.text.Document;
26+
import org.eclipse.jface.text.IDocument;
27+
import org.eclipse.text.edits.TextEdit;
28+
29+
/** Formatter step which calls out to the Eclipse CDT formatter. */
30+
public class EclipseCdtFormatterStepImpl {
31+
private final CodeFormatter codeFormatter;
32+
33+
public EclipseCdtFormatterStepImpl(Properties settings) throws Exception {
34+
Stream<Entry<Object, Object>> stream = settings.entrySet().stream();
35+
Map<String, String> settingsMap = stream.collect(Collectors.toMap(
36+
e -> String.valueOf(e.getKey()),
37+
e -> String.valueOf(e.getValue())));
38+
codeFormatter = org.eclipse.cdt.core.ToolFactory.createDefaultCodeFormatter(settingsMap);
39+
}
40+
41+
/** Formatting C/C++ string */
42+
public String format(String raw) throws Exception {
43+
//The 'kind' can be set to CodeFormatter.K_UNKNOWN, since it is anyway ignored by the internal formatter
44+
TextEdit edit = codeFormatter.format(CodeFormatter.K_UNKNOWN, raw, 0, raw.length(), 0, "\n");
45+
if (edit == null) {
46+
throw new IllegalArgumentException("Invalid C/C++ syntax for formatting.");
47+
} else {
48+
IDocument doc = new Document(raw);
49+
edit.apply(doc);
50+
return doc.get();
51+
}
52+
}
53+
}
Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,14 +15,15 @@
1515
*/
1616
package com.diffplug.spotless.extra.cpp;
1717

18-
import java.lang.reflect.Method;
18+
import java.lang.reflect.InvocationTargetException;
1919
import java.util.Properties;
2020

2121
import com.diffplug.spotless.FormatterFunc;
2222
import com.diffplug.spotless.Jvm;
2323
import com.diffplug.spotless.Provisioner;
24-
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
25-
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
24+
import com.diffplug.spotless.extra.EquoBasedStepBuilder;
25+
26+
import dev.equo.solstice.p2.P2Model;
2627

2728
/**
2829
* Formatter step which calls out to the Eclipse CDT formatter.
@@ -36,25 +37,40 @@ public final class EclipseCdtFormatterStep {
3637
private EclipseCdtFormatterStep() {}
3738

3839
private static final String NAME = "eclipse cdt formatter";
39-
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.cdt.EclipseCdtFormatterStepImpl";
40-
private static final String FORMATTER_METHOD = "format";
41-
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.16.0").add(11, "4.21.0");
40+
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(11, "11.0");
4241

4342
public static String defaultVersion() {
4443
return JVM_SUPPORT.getRecommendedFormatterVersion();
4544
}
4645

4746
/** Provides default configuration */
48-
public static EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
49-
return new EclipseBasedStepBuilder(NAME, provisioner, EclipseCdtFormatterStep::apply);
47+
public static EquoBasedStepBuilder createBuilder(Provisioner provisioner) {
48+
return new EquoBasedStepBuilder(NAME, provisioner, EclipseCdtFormatterStep::apply) {
49+
@Override
50+
protected P2Model model(String version) {
51+
var model = new P2Model();
52+
addPlatformRepo(model, "4.26");
53+
model.addP2Repo("https://download.eclipse.org/tools/cdt/releases/" + version + "/");
54+
model.getInstall().add("org.eclipse.cdt.core");
55+
return model;
56+
}
57+
};
5058
}
5159

52-
private static FormatterFunc apply(State state) throws Exception {
60+
private static FormatterFunc apply(EquoBasedStepBuilder.State state) throws Exception {
5361
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
54-
Class<?> formatterClazz = state.loadClass(FORMATTER_CLASS);
55-
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
56-
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
57-
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(), input -> (String) method.invoke(formatter, input));
62+
Class<?> formatterClazz = state.getJarState().getClassLoader().loadClass("com.diffplug.spotless.extra.glue.cdt.EclipseCdtFormatterStepImpl");
63+
var formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
64+
var method = formatterClazz.getMethod("format", String.class);
65+
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(),
66+
input -> {
67+
try {
68+
return (String) method.invoke(formatter, input);
69+
} catch (InvocationTargetException exceptionWrapper) {
70+
Throwable throwable = exceptionWrapper.getTargetException();
71+
Exception exception = (throwable instanceof Exception) ? (Exception) throwable : null;
72+
throw (null == exception) ? exceptionWrapper : exception;
73+
}
74+
});
5875
}
59-
6076
}

lib-extra/src/test/java/com/diffplug/spotless/extra/cpp/EclipseCdtFormatterStepTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,12 +20,10 @@
2020
import org.junit.jupiter.params.ParameterizedTest;
2121
import org.junit.jupiter.params.provider.MethodSource;
2222

23-
import com.diffplug.spotless.Jvm;
2423
import com.diffplug.spotless.TestProvisioner;
25-
import com.diffplug.spotless.extra.eclipse.EclipseResourceHarness;
24+
import com.diffplug.spotless.extra.eclipse.EquoResourceHarness;
2625

27-
class EclipseCdtFormatterStepTest extends EclipseResourceHarness {
28-
private final static Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support("Oldest Version").add(8, "4.11.0");
26+
class EclipseCdtFormatterStepTest extends EquoResourceHarness {
2927
private final static String INPUT = "#include <a.h>;\nint main(int argc, \nchar *argv[]) {}";
3028
private final static String EXPECTED = "#include <a.h>;\nint main(int argc, char *argv[]) {\n}\n";
3129

@@ -40,6 +38,6 @@ void formatWithVersion(String version) throws Exception {
4038
}
4139

4240
private static Stream<String> formatWithVersion() {
43-
return Stream.of(JVM_SUPPORT.getRecommendedFormatterVersion(), EclipseCdtFormatterStep.defaultVersion());
41+
return Stream.of("10.6", "10.7", EclipseCdtFormatterStep.defaultVersion());
4442
}
4543
}

0 commit comments

Comments
 (0)