Skip to content

Commit fb49dd9

Browse files
committed
Added an option to disable handling comments
1 parent 1d885c3 commit fb49dd9

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

format/yaml/src/main/java/org/spongepowered/configurate/yaml/YamlConfigurationLoader.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ public static Builder builder() {
7070
* <dl>
7171
* <dt>&lt;prefix&gt;.yaml.node-style</dt>
7272
* <dd>Equivalent to {@link #nodeStyle(NodeStyle)}</dd>
73+
* <dt>&lt;prefix&gt;.yaml.comments-enabled</dt>
74+
* <dd>Equivalent to {@link #commentsEnabled(boolean)}</dd>
7375
* </dl>
7476
*
7577
* @since 4.0.0
7678
*/
7779
public static final class Builder extends AbstractConfigurationLoader.Builder<Builder, YamlConfigurationLoader> {
7880
private final DumperOptions options = new DumperOptions();
7981
private @Nullable NodeStyle style;
82+
private boolean enableComments;
8083

8184
Builder() {
8285
this.indent(4);
@@ -90,6 +93,7 @@ protected void populate(final LoaderOptionSource options) {
9093
if (declared != null) {
9194
this.style = declared;
9295
}
96+
this.enableComments = options.getBoolean(true, "yaml", "comments-enabled");
9397
}
9498

9599
/**
@@ -156,6 +160,34 @@ public Builder nodeStyle(final @Nullable NodeStyle style) {
156160
return this.style;
157161
}
158162

163+
/**
164+
* Set whether comment handling is enabled on this loader.
165+
*
166+
* <p>When comment handling is enabled, comments will be read from files
167+
* and written back to files where possible.</p>
168+
*
169+
* <p>The default value is {@code true}</p>
170+
*
171+
* @param enableComments whether comment handling should be enabled
172+
* @return this builder (for chaining)
173+
* @since 4.2.0
174+
*/
175+
public Builder commentsEnabled(final boolean enableComments) {
176+
this.enableComments = enableComments;
177+
return this;
178+
}
179+
180+
/**
181+
* Get whether comment handling is enabled.
182+
*
183+
* @return whether comment handling is enabled
184+
* @see #commentsEnabled(boolean) for details on comment handling
185+
* @since 4.2.0
186+
*/
187+
public boolean commentsEnabled() {
188+
return this.enableComments;
189+
}
190+
159191
@Override
160192
public YamlConfigurationLoader build() {
161193
return new YamlConfigurationLoader(this);
@@ -169,12 +201,12 @@ private YamlConfigurationLoader(final Builder builder) {
169201
super(builder, new CommentHandler[] {CommentHandlers.HASH});
170202
final LoaderOptions loaderOpts = new LoaderOptions()
171203
.setAcceptTabs(true)
172-
.setProcessComments(true);
204+
.setProcessComments(builder.commentsEnabled());
173205
loaderOpts.setCodePointLimit(Integer.MAX_VALUE);
174206

175207
final DumperOptions opts = builder.options;
176208
opts.setDefaultFlowStyle(NodeStyle.asSnakeYaml(builder.style));
177-
opts.setProcessComments(true);
209+
opts.setProcessComments(builder.commentsEnabled());
178210
// the constructor needs ConfigurationOptions, which is only available when called (loadInternal)
179211
this.constructor = ThreadLocal.withInitial(() -> new YamlConstructor(loaderOpts));
180212
this.yaml = ThreadLocal.withInitial(() -> new Yaml(this.constructor.get(), new YamlRepresenter(opts), opts, loaderOpts));

0 commit comments

Comments
 (0)