Skip to content

Commit 53bf8e8

Browse files
committed
v1.0.2
1 parent e5f38da commit 53bf8e8

File tree

11 files changed

+123
-21
lines changed

11 files changed

+123
-21
lines changed

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group = 'dev.manere.inscript'
6-
version = '1.0.1'
6+
version = '1.0.2'
77

88
repositories {
99
mavenCentral()
@@ -14,4 +14,10 @@ dependencies {
1414
annotationProcessor('org.jetbrains:annotations:24.0.0')
1515
implementation('com.google.errorprone:error_prone_annotations:2.36.0')
1616
annotationProcessor('com.google.errorprone:error_prone_annotations:2.36.0')
17+
}
18+
19+
jar {
20+
manifest {
21+
attributes('Implementation-Version': project.version)
22+
}
1723
}

src/main/java/dev/manere/inscript/Inscript.java

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ private void writeNode(final @NotNull BufferedWriter writer, final @NotNull Insc
9292
final String key = node.getKey();
9393

9494
if (node instanceof SectionNode section) {
95+
for (final String comment : section.getComments()) {
96+
writer.write(InscriptConstants.COMMENT_START.get() + " " + comment);
97+
writer.newLine();
98+
}
99+
95100
if (section.getChildren().isEmpty()) {
96101
writer.write(indent + key + " " + InscriptConstants.SECTION_START.get() + InscriptConstants.SECTION_END.get());
97102
return;
@@ -105,6 +110,11 @@ private void writeNode(final @NotNull BufferedWriter writer, final @NotNull Insc
105110

106111
writer.write(indent + InscriptConstants.SECTION_END.get() + "\n");
107112
} else if (node instanceof ScalarNode<?> scalar) {
113+
for (final String comment : scalar.getComments()) {
114+
writer.write(InscriptConstants.COMMENT_START.get() + " " + comment);
115+
writer.newLine();
116+
}
117+
108118
final Object objectValue = scalar.getValue();
109119
final Class<?> type = objectValue.getClass();
110120

@@ -159,12 +169,13 @@ public void loadFromDisk() {
159169
try (final BufferedReader reader = Files.newBufferedReader(getPath().get())) {
160170
getEditor().reset();
161171

172+
final Set<String> tempComments = new HashSet<>();
162173
String line;
163174
while ((line = reader.readLine()) != null) {
164175
line = line.trim();
165176
if (line.isEmpty()) continue;
166177

167-
final InscriptNode node = parseNode(line, reader, 0);
178+
final InscriptNode node = parseNode(line, reader, 0, tempComments);
168179
if (node != null) getEditor().getSection().getChildren().add(node);
169180
}
170181
} catch (final Exception e) {
@@ -176,27 +187,33 @@ public void loadFromString(final @NotNull String configString) {
176187
try (final BufferedReader reader = new BufferedReader(new StringReader(configString))) {
177188
getEditor().reset();
178189

190+
final Set<String> tempComments = new HashSet<>();
179191
String line;
180192
while ((line = reader.readLine()) != null) {
181193
line = line.trim();
182194
if (line.isEmpty()) continue;
183195

184-
final InscriptNode node = parseNode(line, reader, 0);
196+
final InscriptNode node = parseNode(line, reader, 0, tempComments);
185197
if (node != null) getEditor().getSection().getChildren().add(node);
186198
}
187199
} catch (final IOException e) {
188-
throw new RuntimeException(e);
200+
throw new InscriptError(e);
189201
}
190202
}
191203

192204
@Nullable
193205
@ApiStatus.Internal
194-
private InscriptNode parseNode(@NotNull String line, final @NotNull BufferedReader reader, final int depth) throws IOException {
206+
private InscriptNode parseNode(@NotNull String line, final @NotNull BufferedReader reader, final int depth, final @NotNull Set<String> tempComments) throws IOException {
195207
final String indent = InscriptConstants.INDENT.get().apply(depth);
196208
if (!line.startsWith(indent)) return null;
197209

198210
line = line.substring(indent.length());
199211

212+
if (line.startsWith(InscriptConstants.COMMENT_START.get())) {
213+
tempComments.add(line.substring(InscriptConstants.COMMENT_START.get().length()).trim());
214+
return null;
215+
}
216+
200217
final String[] parts = line.split("=", 2);
201218
final String name = parts[0].trim();
202219

@@ -225,15 +242,19 @@ public String getKey() {
225242

226243
String childLine;
227244
while ((childLine = reader.readLine()) != null && !childLine.trim().equals(InscriptConstants.SECTION_END.get())) {
228-
final InscriptNode childNode = parseNode(childLine, reader, depth + 1);
245+
final InscriptNode childNode = parseNode(childLine, reader, depth + 1, new HashSet<>());
229246
if (childNode != null) {
230247
section.getChildren().add(childNode);
231248
}
232249
}
233250

251+
System.out.println(1);
252+
section.getComments().addAll(tempComments);
253+
tempComments.clear();
254+
234255
return section;
235256
} else if (line.endsWith(InscriptConstants.SECTION_START.get() + InscriptConstants.SECTION_END.get())) {
236-
return new SectionNode() {
257+
final SectionNode node = new SectionNode() {
237258
private final Set<InscriptNode> nodes = Collections.newSetFromMap(new ConcurrentHashMap<>());
238259

239260
@NotNull
@@ -248,19 +269,24 @@ public String getKey() {
248269
return key;
249270
}
250271
};
272+
273+
System.out.println(2);
274+
node.getComments().addAll(tempComments);
275+
tempComments.clear();
276+
277+
return node;
251278
}
252279

253280
return null;
254281
}
255282

256283
final String value = parts[1].trim();
257284

258-
if (value.startsWith(InscriptConstants.SECTION_START.get())) {
285+
if (value.startsWith(InscriptConstants.LIST_START.get())) {
259286
final List<Object> list = new ArrayList<>();
260287
final StringBuilder listContent = new StringBuilder(value);
261288

262-
// Read until we find the closing bracket
263-
while (!listContent.toString().trim().endsWith(InscriptConstants.SECTION_END.get())) {
289+
while (!listContent.toString().trim().endsWith(InscriptConstants.LIST_END.get())) {
264290
String nextLine = reader.readLine();
265291
if (nextLine != null) {
266292
listContent.append(nextLine.trim());
@@ -290,7 +316,7 @@ public String getKey() {
290316
}
291317
}
292318

293-
return new ScalarNode<>() {
319+
final ScalarNode<?> node = new ScalarNode<>() {
294320
@Override
295321
public @NotNull String getKey() {
296322
return key;
@@ -301,6 +327,13 @@ public String getKey() {
301327
return Collections.synchronizedList(list);
302328
}
303329
};
330+
331+
332+
System.out.println(3);
333+
node.getComments().addAll(tempComments);
334+
tempComments.clear();
335+
336+
return node;
304337
}
305338

306339
InlineValue<?> inlineMatched = new StringValue();
@@ -317,7 +350,7 @@ public String getKey() {
317350
final Object o = inlineMatched.deserialize(value);
318351
if (o == null) return null;
319352

320-
return new ScalarNode<>() {
353+
final ScalarNode<?> node = new ScalarNode<>() {
321354
@Override
322355
public @NotNull String getKey() {
323356
return key;
@@ -328,5 +361,12 @@ public String getKey() {
328361
return o;
329362
}
330363
};
364+
365+
System.out.println(4);
366+
367+
node.getComments().addAll(tempComments);
368+
tempComments.clear();
369+
370+
return node;
331371
}
332372
}

src/main/java/dev/manere/inscript/InscriptConstant.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ public InscriptConstant<T> set(final @NotNull T value) {
2121
public T get() {
2222
return value;
2323
}
24+
25+
@NotNull
26+
@Override
27+
public String toString() {
28+
return get().toString();
29+
}
2430
}

src/main/java/dev/manere/inscript/InscriptConstants.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
import java.util.Optional;
56
import java.util.function.Function;
7+
import java.util.function.Supplier;
68

9+
// We don't care if VERSION or FILE_EXTENSION is not used.
10+
@SuppressWarnings("unused")
711
public interface InscriptConstants {
812
@NotNull
913
InscriptConstant<String> LIST_START = new InscriptConstant<>("[");
@@ -22,4 +26,13 @@ public interface InscriptConstants {
2226

2327
@NotNull
2428
InscriptConstant<String> ROOT_SECTION_KEY = new InscriptConstant<>("*");
29+
30+
@NotNull
31+
InscriptConstant<String> COMMENT_START = new InscriptConstant<>("//");
32+
33+
@NotNull
34+
InscriptConstant<Supplier<Optional<String>>> VERSION = new InscriptConstant<>(() -> Optional.ofNullable(InscriptConstants.class.getPackage().getImplementationVersion()));
35+
36+
@NotNull
37+
InscriptConstant<String> FILE_EXTENSION = new InscriptConstant<>(".is");
2538
}

src/main/java/dev/manere/inscript/InscriptEditor.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
public interface InscriptEditor {
1616
@NotNull
1717
@Unmodifiable
18-
Set<InscriptNode> getChildren();
18+
default Set<InscriptNode> getChildren() {
19+
return Set.copyOf(getSection().getChildren());
20+
}
1921

2022
@NotNull
2123
@Unmodifiable
@@ -128,4 +130,29 @@ default InscriptEditor forEach(final @NotNull Consumer<ScalarNode<?>> scalarCons
128130

129131
return this;
130132
}
133+
134+
@NotNull
135+
@CanIgnoreReturnValue
136+
default InscriptEditor setComments(final @NotNull String key, final @NotNull Collection<? extends String> comments) {
137+
getNode(key).ifPresent(node -> {
138+
node.getComments().clear();
139+
node.getComments().addAll(comments);
140+
});
141+
142+
return this;
143+
}
144+
145+
@NotNull
146+
default Collection<String> getComments(final @NotNull String key) {
147+
final InscriptNode node = getNode(key).orElse(null);
148+
if (node == null) return Set.of();
149+
150+
return Set.copyOf(node.getComments());
151+
}
152+
153+
@NotNull
154+
@CanIgnoreReturnValue
155+
default InscriptEditor setComments(final @NotNull String key, final @NotNull String @NotNull ... comments) {
156+
return setComments(key, Arrays.asList(comments));
157+
}
131158
}

src/main/java/dev/manere/inscript/SimpleInscriptEditor.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
import java.util.concurrent.ConcurrentHashMap;
1414

1515
public record SimpleInscriptEditor(@NotNull SectionNode sectionNode) implements InscriptEditor {
16-
@Override
17-
public @NotNull @Unmodifiable Set<InscriptNode> getChildren() {
18-
return Set.copyOf(sectionNode.getChildren());
19-
}
20-
2116
@Override
2217
public @NotNull SectionNode getSection() {
2318
return sectionNode;

src/main/java/dev/manere/inscript/node/InscriptNode.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
import java.util.HashSet;
6+
import java.util.Set;
7+
58
public abstract class InscriptNode {
9+
private final Set<String> comments = new HashSet<>();
10+
611
@NotNull
712
public abstract String getKey();
813

914
@NotNull
1015
@Override
1116
public String toString() {
12-
return getKey();
17+
return getKey() + "[comments = " + getComments() + "]";
18+
}
19+
20+
@NotNull
21+
public Set<String> getComments() {
22+
return comments;
1323
}
1424
}

src/main/java/dev/manere/inscript/node/RootSectionNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public String getKey() {
2121
public Set<InscriptNode> getChildren() {
2222
return nodes;
2323
}
24+
25+
@Override
26+
public @NotNull String toString() {
27+
return getKey() + "[children = " + getChildren() + "]";
28+
}
2429
}

src/main/java/dev/manere/inscript/node/ScalarNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public <T> Optional<T> getValueAs(final @NotNull Class<T> type) {
2020
@NotNull
2121
@Override
2222
public String toString() {
23-
return getKey() + "[value=" + getValue() + "]";
23+
return getKey() + "[value = " + getValue() + ", comments = " + getComments() + "]";
2424
}
2525
}

src/main/java/dev/manere/inscript/node/SectionNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public abstract class SectionNode extends InscriptNode {
1212
@NotNull
1313
@Override
1414
public String toString() {
15-
return getKey() + "[children=" + getChildren() + "]";
15+
return getKey() + "[comments = " + getComments() + ", children = " + getChildren() + "]";
1616
}
1717
}

0 commit comments

Comments
 (0)