Indent multi-line values on replacement

Indent multi-line values when they replace a value in a
ConfigDocument.
This commit is contained in:
Preben Ingvaldsen 2015-03-31 16:05:58 -07:00
parent 7ec3777c25
commit a29475ba4a
2 changed files with 22 additions and 1 deletions

View File

@ -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;

View File

@ -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())
}
}