Merge pull request #260 from typesafehub/newlines-in-description

In render, put comment char in front of every description line
This commit is contained in:
Havoc Pennington 2015-02-26 08:52:08 -05:00
commit 0e722a10cd
3 changed files with 39 additions and 8 deletions

View File

@ -211,10 +211,15 @@ final class SimpleConfigList extends AbstractConfigValue implements ConfigList,
sb.append('\n');
for (AbstractConfigValue v : value) {
if (options.getOriginComments()) {
indent(sb, indent + 1, options);
sb.append("# ");
sb.append(v.origin().description());
sb.append("\n");
String[] lines = v.origin().description().split("\n");
for (String l : lines) {
indent(sb, indent + 1, options);
sb.append('#');
if (!l.isEmpty())
sb.append(' ');
sb.append(l);
sb.append("\n");
}
}
if (options.getComments()) {
for (String comment : v.origin().comments()) {

View File

@ -444,10 +444,15 @@ final class SimpleConfigObject extends AbstractConfigObject implements Serializa
v = value.get(k);
if (options.getOriginComments()) {
indent(sb, innerIndent, options);
sb.append("# ");
sb.append(v.origin().description());
sb.append("\n");
String[] lines = v.origin().description().split("\n");
for (String l : lines) {
indent(sb, indent + 1, options);
sb.append('#');
if (!l.isEmpty())
sb.append(' ');
sb.append(l);
sb.append("\n");
}
}
if (options.getComments()) {
for (String comment : v.origin().comments()) {

View File

@ -938,4 +938,25 @@ class ConfigValueTest extends TestUtils {
assertEquals(bottom(v), bottom(deserialized))
}
}
@Test
def renderWithNewlinesInDescription(): Unit = {
val v = ConfigValueFactory.fromAnyRef(89, "this is a description\nwith some\nnewlines")
val list = new SimpleConfigList(SimpleConfigOrigin.newSimple("\n5\n6\n7\n"),
java.util.Collections.singletonList(v.asInstanceOf[AbstractConfigValue]))
val conf = ConfigFactory.empty().withValue("bar", list)
val rendered = conf.root.render()
def assertHas(s: String): Unit =
assertTrue(s"has ${s.replace("\n", "\\n")} in it", rendered.contains(s))
assertHas("is a description\n")
assertHas("with some\n")
assertHas("newlines\n")
assertHas("#\n")
assertHas("5\n")
assertHas("6\n")
assertHas("7\n")
val parsed = ConfigFactory.parseString(rendered)
assertEquals(conf, parsed)
}
}