From a29475ba4a73841ad4838d7150483f0e5a4465ca Mon Sep 17 00:00:00 2001 From: Preben Ingvaldsen Date: Tue, 31 Mar 2015 16:05:58 -0700 Subject: [PATCH] Indent multi-line values on replacement Indent multi-line values when they replace a value in a ConfigDocument. --- .../com/typesafe/config/impl/ConfigNodeObject.java | 10 +++++++++- .../typesafe/config/impl/ConfigDocumentTest.scala | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java b/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java index cbb98562..17c5b291 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java @@ -69,7 +69,15 @@ final class ConfigNodeObject extends ConfigNodeComplexValue { } } else if (key.equals(desiredPath)) { seenNonMatching = true; - childrenCopy.set(i, node.replaceValue(value)); + AbstractConfigNodeValue indentedValue; + AbstractConfigNode before = i - 1 > 0 ? childrenCopy.get(i - 1) : null; + if (value instanceof ConfigNodeComplexValue && + before instanceof ConfigNodeSingleToken && + Tokens.isIgnoredWhitespace(((ConfigNodeSingleToken) before).token())) + indentedValue = ((ConfigNodeComplexValue) value).indentText(before); + else + indentedValue = value; + childrenCopy.set(i, node.replaceValue(indentedValue)); valueCopy = null; } else if (desiredPath.startsWith(key)) { seenNonMatching = true; diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala index 05d2d964..b4a571ba 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala @@ -413,4 +413,17 @@ class ConfigDocumentTest extends TestUtils { assertEquals("a { b {\n c: d\n}, e : f }", configDocument.setValue("a.e", "f").render()) } + @Test + def configDocumentIndentationReplacingWithMultiLineValue { + var origText = "a {\n b {\n c : 22\n }\n}" + var configDocument = ConfigDocumentFactory.parseString(origText) + + assertEquals("a {\n b {\n c : {\n d:e\n }\n }\n}", configDocument.setValue("a.b.c", "{\n d:e\n}").render()) + + origText = "a {\n b {\n f : 10\n c : 22\n }\n}" + configDocument = ConfigDocumentFactory.parseString(origText) + + assertEquals("a {\n b {\n f : 10\n c : {\n d:e\n }\n }\n}", configDocument.setValue("a.b.c", "{\n d:e\n}").render()) + } + }