@@ -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}
0 commit comments