Skip to content

Commit 3ceee85

Browse files
committed
[fix] Merge comments attached to the compilation unit
1 parent 48c8e78 commit 3ceee85

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/main/java/se/kth/spork/cli/Cli.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ public static String prettyPrint(CtModule spoonRoot) {
5656
.collect(Collectors.toList());
5757
new PrinterPreprocessor(importNames, activePackage.getQualifiedName()).scan(spoonRoot);
5858

59-
6059
StringBuilder sb = new StringBuilder();
60+
61+
String cuComment = (String) spoonRoot.getMetadata(Parser.COMPILATION_UNIT_COMMENT);
62+
if (!cuComment.isEmpty()) {
63+
sb.append(cuComment).append("\n");
64+
}
65+
6166
if (!activePackage.isUnnamedPackage()) {
6267
sb.append("package ").append(activePackage.getQualifiedName()).append(";").append("\n\n");
6368
}

src/main/java/se/kth/spork/spoon/Parser.java

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import spoon.Launcher;
88
import spoon.compiler.Environment;
99
import spoon.reflect.CtModel;
10+
import spoon.reflect.code.CtComment;
1011
import spoon.reflect.declaration.*;
1112
import spoon.support.compiler.FileSystemFile;
1213
import spoon.support.compiler.VirtualFile;
@@ -24,6 +25,7 @@
2425
*/
2526
public class Parser {
2627
public static final String IMPORT_STATEMENTS = "spork_import_statements";
28+
public static final String COMPILATION_UNIT_COMMENT = "spork_cu_comment";
2729

2830
private static final LazyLogger LOGGER = new LazyLogger(Parser.class);
2931

@@ -87,6 +89,11 @@ private static CtModule parse(Consumer<Launcher> addResource) {
8789

8890
CtModule module = model.getUnnamedModule();
8991

92+
// FIXME This is an ugly workaround for merging compliation unit comments
93+
List<CtComment> cuComments = module.getFactory().CompilationUnit().getMap().values().iterator().next().getComments();
94+
String cuComment = cuComments.isEmpty() ? "" : cuComments.get(0).getRawContent();
95+
module.putMetadata(COMPILATION_UNIT_COMMENT, cuComment);
96+
9097
// TODO preserve order of import statements
9198
List<CtImport> imports = new ArrayList<>(parseImportStatements(model));
9299
imports.sort(Comparator.comparing(CtElement::prettyprint));

src/main/java/se/kth/spork/spoon/Spoon3dmMerge.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import se.kth.spork.spoon.wrappers.RoledValues;
1313
import se.kth.spork.spoon.wrappers.SpoonNode;
1414
import se.kth.spork.util.LazyLogger;
15+
import se.kth.spork.util.LineBasedMerge;
1516
import se.kth.spork.util.Pair;
1617
import spoon.reflect.declaration.*;
1718

@@ -129,9 +130,31 @@ public static <T extends CtElement> Pair<T, Integer> merge(T base, T left, T rig
129130
List<CtImport> mergedImports = mergeImportStatements(base, left, right);
130131
mergeTree.putMetadata(Parser.IMPORT_STATEMENTS, mergedImports);
131132

133+
LOGGER.info(() -> "Merging compilation unit comments");
134+
Pair<String, Integer> cuCommentMerge = mergeCuComments(base, left, right);
135+
int cuCommentConflicts = cuCommentMerge.second;
136+
mergeTree.putMetadata(Parser.COMPILATION_UNIT_COMMENT, cuCommentMerge.first);
137+
132138
LOGGER.info(() -> "Merged in " + (double) (System.nanoTime() - start) / 1e9 + " seconds");
133139

134-
return Pair.of(mergeTree, numConflicts);
140+
return Pair.of(mergeTree, numConflicts + cuCommentConflicts);
141+
}
142+
143+
/**
144+
* Perform a line-based merge of the compilation unit comments.
145+
*
146+
* @return A pair with the merge and the amount of conflicts.
147+
*/
148+
private static Pair<String, Integer> mergeCuComments(CtElement base, CtElement left, CtElement right) {
149+
String baseComment = getCuComment(base);
150+
String leftComment = getCuComment(left);
151+
String rightComment = getCuComment(right);
152+
return LineBasedMerge.merge(baseComment, leftComment, rightComment);
153+
}
154+
155+
private static String getCuComment(CtElement mod) {
156+
String comment = (String) mod.getMetadata(Parser.COMPILATION_UNIT_COMMENT);
157+
return comment == null ? "" : comment;
135158
}
136159

137160
/**

src/test/java/se/kth/spork/spoon/Spoon3dmMergeTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,24 @@ void merge_shouldThrow_onUnhandledInconsistencies(Util.TestSources sources) {
5252
private static void runTestMerge(Util.TestSources sources) {
5353
CtModule expected = Parser.parse(sources.expected);
5454
Object expectedImports = expected.getMetadata(Parser.IMPORT_STATEMENTS);
55+
Object expectedCuComment = expected.getMetadata(Parser.COMPILATION_UNIT_COMMENT);
5556
assert expectedImports != null;
57+
assert expectedCuComment != null;
5658

5759
Pair<CtModule, Integer> merged = Spoon3dmMerge.merge(sources.base, sources.left, sources.right);
5860
CtModule mergeTree = merged.first;
5961
Object mergedImports = mergeTree.getMetadata(Parser.IMPORT_STATEMENTS);
62+
Object mergedCuComment = mergeTree.getMetadata(Parser.COMPILATION_UNIT_COMMENT);
6063

6164
// this assert is just to give a better overview of obvious errors, but it relies on the pretty printer's
6265
// correctness
6366
// assertEquals(Cli.prettyPrint(expected), Cli.prettyPrint(mergeTree));
67+
System.out.println(Cli.prettyPrint(mergeTree));
6468

6569
// these asserts are what actually matters
6670
assertEquals(expected, mergeTree);
6771
assertEquals(expectedImports, mergedImports);
72+
assertEquals(expectedCuComment, mergedCuComment);
6873
}
6974
}
7075

0 commit comments

Comments
 (0)