Carleslc / Simple-YAML

This Java API provides an easy-to-use way to store data and provide configurations using the YAML format.
https://carleslc.me/Simple-YAML
GNU General Public License v3.0
130 stars 38 forks source link

Multilined comments support? #39

Closed Osiris-Team closed 3 years ago

Carleslc commented 3 years ago

As specified in setComment method documentation, multiline comments can be provided using \n character.

A comment is identified by a path, so there can only be one comment for each path, this is the reason why the method is called setComment and not addComment, but in that comment you can provide multiple lines as you'd do in any String. This is only useful for BLOCK comments.

If you need to add a comment with several lines you have two equivalent approaches:

  1. Build a single String with several lines using \n character and then use setComment.
yamlFile.setComment("test", "Line one\nLine two");

Or using a StringBuilder:

StringBuilder commentBuilder = new StringBuilder();
commentBuilder.append("Line one");
commentBuilder.append('\n');
commentBuilder.append("Line two");
yamlFile.setComment("test", commentBuilder.build());
  1. Use setComment for the first line and then add the following lines prefixing the result ofgetComment.
yamlFile.setComment("test", "Line one");
yamlFile.setComment("test", yamlFile.getComment("test") + '\n' + "Line two");
Osiris-Team commented 3 years ago

@Carleslc Great thanks