Add a way to control spacing around array brackets.

This commit is contained in:
Jonathan Wood 2015-06-28 11:26:49 -07:00
parent 72941c146d
commit ecbd9f048d
3 changed files with 46 additions and 2 deletions

View file

@ -14,7 +14,11 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
public void write(Object value, WriterContext context) { public void write(Object value, WriterContext context) {
Collection values = normalize(value); Collection values = normalize(value);
context.output.append("[ "); context.output.append('[');
if (context.getTomlWriter().wantSpaceyArrays()) {
context.output.append(' ');
}
boolean first = true; boolean first = true;
for (Object elem : values) { for (Object elem : values) {
if (!first) { if (!first) {
@ -23,7 +27,11 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
ValueWriters.WRITERS.write(elem, context); ValueWriters.WRITERS.write(elem, context);
first = false; first = false;
} }
context.output.append(" ]");
if (context.getTomlWriter().wantSpaceyArrays()) {
context.output.append(' ');
}
context.output.append(']');
} }
private PrimitiveArrayValueWriter() {} private PrimitiveArrayValueWriter() {}

View file

@ -26,6 +26,7 @@ import static com.moandjiezana.toml.ValueWriters.WRITERS;
public class TomlWriter { public class TomlWriter {
private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy(); private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy();
private boolean wantSpaceyArraysValue = true;
/** /**
* Creates a TomlWriter instance. * Creates a TomlWriter instance.
@ -93,4 +94,35 @@ public class TomlWriter {
this.indentationPolicy = indentationPolicy; this.indentationPolicy = indentationPolicy;
return this; return this;
} }
/**
* <p>Control spacing around array brackets in the TOML output.</p>
*
* <p>Spacey arrays = true (default):</p>
*
* <pre><code>
* a = [ 1, 2, 3 ]
* </code></pre>
*
* <p>Spacey arrays = false:</p>
*
* <pre><code>
* a = [1, 2, 3]
* </code></pre>
*
* @param value spacey arrays setting
* @return this TomlWriter instance
*/
public TomlWriter wantSpaceyArrays(boolean value) {
this.wantSpaceyArraysValue = value;
return this;
}
/**
* Get the current array whitespace policy
* @return the current policy
*/
public boolean wantSpaceyArrays() {
return wantSpaceyArraysValue;
}
} }

View file

@ -79,4 +79,8 @@ class WriterContext {
return new String(chars); return new String(chars);
} }
public TomlWriter getTomlWriter() {
return tomlWriter;
}
} }