Skip to content

Commit ce87129

Browse files
authored
Properly handle multi-line comments split with the newline character (#17)
* Properly handle multi-line comments split with the newline character * Don't discard trailing newlines * Add unit tests
1 parent 26dfd4d commit ce87129

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

configlib-core/src/main/java/de/exlll/configlib/CommentNodeExtractor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ private Optional<CommentNode> createNodeIfCommentPresent(
9595
final Deque<String> elementNameStack
9696
) {
9797
if (element.isAnnotationPresent(Comment.class)) {
98-
final var comments = element.getAnnotation(Comment.class).value();
98+
final var comments = Arrays.stream(element.getAnnotation(Comment.class).value())
99+
.flatMap(s -> Arrays.stream(s.split("\n", -1)))
100+
.toList();
99101
final var formattedName = nameFormatter.format(elementName);
100102
final var elementNames = new ArrayList<>(elementNameStack);
101103
elementNames.add(formattedName);
102-
final var result = new CommentNode(Arrays.asList(comments), elementNames);
104+
final var result = new CommentNode(comments, elementNames);
103105
return Optional.of(result);
104106
}
105107
return Optional.empty();

configlib-core/src/test/java/de/exlll/configlib/CommentNodeExtractorTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,88 @@ record R(@Comment({"Hello", "World"}) int i) {}
7070
assertTrue(nodes.isEmpty());
7171
}
7272

73+
@Test
74+
void extractSingleCommentMultipleLines() {
75+
@Configuration
76+
class A {
77+
@Comment("""
78+
Hello
79+
World""")
80+
int i;
81+
}
82+
83+
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
84+
assertEquals(cn(List.of("Hello", "World"), "i"), nodes.poll());
85+
assertTrue(nodes.isEmpty());
86+
}
87+
88+
@Test
89+
void extractSingleCommentMultipleLinesTrailingNewlines() {
90+
@Configuration
91+
class A {
92+
93+
@Comment("""
94+
95+
96+
Hello
97+
98+
World
99+
100+
101+
""")
102+
int i;
103+
104+
}
105+
106+
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
107+
assertEquals(cn(List.of("", "", "Hello", "", "World", "", "", ""), "i"), nodes.poll());
108+
assertTrue(nodes.isEmpty());
109+
}
110+
111+
@Test
112+
void extractSingleCommentMultipleLinesInArrayAndNewlineSplit() {
113+
@Configuration
114+
class A {
115+
116+
@Comment({ """
117+
Hello
118+
World""", """
119+
Hi
120+
Again""" })
121+
int i;
122+
123+
}
124+
125+
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
126+
assertEquals(cn(List.of("Hello", "World", "Hi", "Again"), "i"), nodes.poll());
127+
assertTrue(nodes.isEmpty());
128+
}
129+
130+
@Test
131+
void extractSingleCommentMultipleLinesInArrayAndNewlineSplitWithTrailingNewlines() {
132+
@Configuration
133+
class A {
134+
135+
@Comment({ """
136+
137+
Hello
138+
World
139+
140+
""", """
141+
142+
Hi
143+
Again
144+
145+
""" })
146+
int i;
147+
148+
}
149+
150+
Queue<CommentNode> nodes = EXTRACTOR.extractCommentNodes(new A());
151+
assertEquals(cn(List.of("", "Hello", "World", "", "", "", "Hi", "Again", "", ""), "i"), nodes.poll());
152+
assertTrue(nodes.isEmpty());
153+
}
154+
73155
@Test
74156
void extractMultipleComments() {
75157
@Configuration

0 commit comments

Comments
 (0)