Skip to content

Commit f3aca9b

Browse files
committed
Fix trailing comments causing value to be cut
Affects issues: - Fixed #2297
1 parent c8bafd8 commit f3aca9b

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

Plan/common/src/main/java/com/djrapitops/plan/settings/config/ConfigReader.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,16 @@
3333
*/
3434
public class ConfigReader implements Closeable {
3535

36-
private boolean closed;
37-
3836
private final Scanner scanner;
37+
private final List<String> unboundComment = new ArrayList<>();
38+
private boolean closed;
3939
private ConfigNode previousNode;
4040
private ConfigNode parent;
41-
4241
// Indent mode assumes the number of spaces used to indent the file.
4342
// If the first found indent is smaller, that will be used instead.
4443
private int indentMode = 4;
45-
private final List<String> unboundComment = new ArrayList<>();
46-
4744
private int lastDepth = -1;
45+
private String lineDanglingComment;
4846

4947
/**
5048
* Create a new ConfigReader for a Path.
@@ -131,11 +129,13 @@ private void handleLine(String line, String trimmed) {
131129

132130
private String readNewLine() {
133131
String line = scanner.nextLine();
132+
lineDanglingComment = null;
134133

135134
// Removing any dangling comments
136135
int danglingComment = line.trim().indexOf(" #");
137136
if (danglingComment != -1) {
138-
line = line.substring(0, danglingComment);
137+
lineDanglingComment = line.trim().substring(danglingComment + 1);
138+
line = line.substring(0, line.indexOf(" #"));
139139
}
140140
return line;
141141
}
@@ -165,8 +165,16 @@ private void handleCommentLine(String line) {
165165
}
166166

167167
private void handleUnboundComments() {
168+
if (lineDanglingComment != null) {
169+
unboundComment.add(lineDanglingComment.substring(1).trim());
170+
}
168171
if (!unboundComment.isEmpty()) {
169-
previousNode.setComment(new ArrayList<>(unboundComment));
172+
List<String> currentComment = previousNode.getComment();
173+
if (currentComment == null || currentComment.isEmpty()) {
174+
previousNode.setComment(new ArrayList<>(unboundComment));
175+
} else {
176+
currentComment.addAll(unboundComment);
177+
}
170178
unboundComment.clear();
171179
}
172180
}

Plan/common/src/main/java/com/djrapitops/plan/settings/config/ConfigWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private void dfsTreeTraverseLineResolve(ConfigNode writing, Collection<String> l
101101
// node is null: Inconsistent config node state
102102
// value is null: Has no value (empty)
103103
// nodeOrder is empty: Has no children
104-
if (node == null || node.value == null && node.nodeOrder.isEmpty()) {
104+
if (node == null || (node.value == null && node.nodeOrder.isEmpty())) {
105105
continue;
106106
}
107107

Plan/common/src/test/java/com/djrapitops/plan/settings/config/ConfigWriterTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.file.Path;
2929
import java.util.Arrays;
3030
import java.util.List;
31+
import java.util.Scanner;
3132

3233
import static org.junit.jupiter.api.Assertions.assertEquals;
3334
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -169,4 +170,42 @@ void valueAfterAList() throws IOException {
169170
assertEquals(expected, writtenLines);
170171
}
171172

173+
@Test
174+
void danglingCommentIsPreserved() throws IOException {
175+
String read = """
176+
Node:
177+
Example: example value 123 # comment
178+
OnlyKey: # comment on key
179+
ChildNode: value
180+
List: # comment on list
181+
- First # comment on list item
182+
- Second""";
183+
184+
Config readConfig;
185+
try (ConfigReader reader = new ConfigReader(new Scanner(read))) {
186+
readConfig = reader.read();
187+
}
188+
189+
Path out = tempFolder.resolve("dangling.yml");
190+
new ConfigWriter(out).write(readConfig);
191+
192+
List<String> writtenLines = FileResource.lines(out.toFile());
193+
194+
List<String> expected = Arrays.asList(
195+
"""
196+
Node:
197+
# comment
198+
Example: example value 123
199+
# comment on key
200+
OnlyKey:
201+
ChildNode: value
202+
# comment on list
203+
# comment on list item
204+
List:
205+
- First
206+
- Second""".split("\n")
207+
);
208+
assertEquals(expected, writtenLines);
209+
}
210+
172211
}

0 commit comments

Comments
 (0)