diff --git a/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java b/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java
index 6fee3e0..f9c9ba7 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/ConfigInitializer.java
@@ -2,6 +2,7 @@
import cc.carm.lib.configuration.core.annotation.ConfigComment;
import cc.carm.lib.configuration.core.annotation.ConfigPath;
+import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.NotNull;
@@ -62,7 +63,7 @@ protected void initializeClass(@NotNull Class> clazz,
@Nullable ConfigPath fieldPath, @Nullable ConfigComment filedComments,
boolean saveDefaults, boolean loadSubClasses) {
String path = getClassPath(clazz, parentPath, fieldName, fieldPath);
- if (path != null) setComments(path, getClassComments(clazz, filedComments));
+ this.provider.setComment(path, getClassComments(clazz, filedComments));
for (Field field : clazz.getDeclaredFields()) {
initializeField(clazz, field, path, saveDefaults, loadSubClasses);
}
@@ -82,21 +83,21 @@ protected void initializeClass(@NotNull Class> clazz,
protected void initializeValue(@NotNull ConfigValue> value, @NotNull String path,
- @NotNull String[] comments, boolean saveDefaults) {
+ @Nullable ConfigCommentInfo comments, boolean saveDefaults) {
value.initialize(provider, saveDefaults, path, comments);
}
private void initializeField(@NotNull Class> source, @NotNull Field field, @Nullable String parent,
boolean saveDefaults, boolean loadSubClasses) {
-
try {
field.setAccessible(true);
Object object = field.get(source);
if (object instanceof ConfigValue>) {
- String path = getFieldPath(field, parent);
- String[] comments = readComments(field.getAnnotation(ConfigComment.class));
- initializeValue((ConfigValue>) object, path, comments, saveDefaults);
- setComments(path, comments);
+ initializeValue(
+ (ConfigValue>) object, getFieldPath(field, parent),
+ ConfigCommentInfo.fromAnnotation(field.getAnnotation(ConfigComment.class)),
+ saveDefaults
+ );
} else if (object instanceof Class>) {
initializeClass(
(Class>) object, parent, field.getName(),
@@ -109,26 +110,15 @@ private void initializeField(@NotNull Class> source, @NotNull Field field, @Nu
}
}
- protected void setComments(@NotNull String path, @Nullable ConfigComment filedComments) {
- setComments(path, readComments(filedComments));
- }
-
- protected void setComments(@NotNull String path, @NotNull String[] comments) {
- if (comments.length <= 0) return;
- this.provider.setComments(path, comments);
- }
-
- protected static @NotNull String[] readComments(@Nullable ConfigComment filedComments) {
- if (filedComments == null) return new String[0];
- if (String.join("", filedComments.value()).length() <= 0) return new String[0];
- return filedComments.value();
+ protected void setComments(@Nullable String path, @Nullable ConfigCommentInfo comments) {
+ if (comments != null) this.provider.setComment(path, comments);
}
- protected static @NotNull String[] getClassComments(@NotNull Class> clazz,
- @Nullable ConfigComment fieldAnnotation) {
- String[] clazzComments = readComments(clazz.getAnnotation(ConfigComment.class));
- if (clazzComments.length > 0) return clazzComments;
- else return readComments(fieldAnnotation);
+ protected static @Nullable ConfigCommentInfo getClassComments(@NotNull Class> clazz,
+ @Nullable ConfigComment fieldAnnotation) {
+ ConfigCommentInfo classComments = ConfigCommentInfo.fromAnnotation(clazz.getAnnotation(ConfigComment.class));
+ if (classComments != null) return classComments;
+ return ConfigCommentInfo.fromAnnotation(fieldAnnotation);
}
protected static @Nullable String getClassPath(@NotNull Class> clazz,
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java b/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java
index b24e874..5874024 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/annotation/ConfigComment.java
@@ -14,4 +14,32 @@
@NotNull
String[] value() default "";
+ /**
+ * 首行换行,即会在注释开始前进行一次换行,与上方配置分离,优化观感。
+ * 如:
+ *
+ * some-key: "SomeValue"
+ *
+ * # 注释第一行
+ * # 注释第二行
+ * startWrap: true
+ *
+ *
+ * @return 是否在结尾添加换行符
+ */
+ boolean startWrap() default true;
+
+ /**
+ * 末尾换行,即会在注释结束后进行一次换行,如:
+ *
+ * # 注释第一行
+ * # 注释第二行
+ *
+ * endWrap: true
+ *
+ * 该功能可用于编写配置文件的顶部注释。
+ *
+ * @return 是否在结尾添加换行符
+ */
+ boolean endWrap() default false;
}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java
index 2b6b275..0be89fd 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/AbstractConfigBuilder.java
@@ -1,5 +1,6 @@
package cc.carm.lib.configuration.core.builder;
+import cc.carm.lib.configuration.core.source.ConfigCommentInfo;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
import cc.carm.lib.configuration.core.value.ConfigValue;
import org.jetbrains.annotations.NotNull;
@@ -13,6 +14,9 @@ public abstract class AbstractConfigBuilder providerClass) {
@@ -38,10 +42,34 @@ public AbstractConfigBuilder(Class super P> providerClass) {
return getThis();
}
+ public @NotNull B setStartWarp(boolean enable) {
+ this.startWrap = enable;
+ return getThis();
+ }
+
+ public @NotNull B startWarp() {
+ return setStartWarp(true);
+ }
+
+ public @NotNull B setEndWarp(boolean enable) {
+ this.endWrap = enable;
+ return getThis();
+ }
+
+ public @NotNull B endWarp() {
+ return setEndWarp(true);
+ }
+
public @NotNull B defaults(@Nullable T defaultValue) {
this.defaultValue = defaultValue;
return getThis();
}
+ protected @Nullable ConfigCommentInfo buildComments() {
+ ConfigCommentInfo info = ConfigCommentInfo.of(this.comments, this.startWrap, this.endWrap);
+ if (info.equals(ConfigCommentInfo.defaults())) return null;
+ else return info;
+ }
+
}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/CommonConfigBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/CommonConfigBuilder.java
index 72b395c..427340d 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/CommonConfigBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/CommonConfigBuilder.java
@@ -1,46 +1,12 @@
package cc.carm.lib.configuration.core.builder;
import cc.carm.lib.configuration.core.source.ConfigurationProvider;
-import cc.carm.lib.configuration.core.value.ConfigValue;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
public abstract class CommonConfigBuilder>
extends AbstractConfigBuilder> {
- protected @Nullable ConfigurationProvider> provider;
- protected @Nullable String path;
-
- protected @NotNull String[] comments = new String[0];
- protected @Nullable T defaultValue;
-
public CommonConfigBuilder() {
super(ConfigurationProvider.class);
}
- protected abstract @NotNull B getThis();
-
- public abstract @NotNull ConfigValue> build();
-
- public @NotNull B from(@Nullable ConfigurationProvider> provider) {
- this.provider = provider;
- return getThis();
- }
-
- public @NotNull B path(@Nullable String path) {
- this.path = path;
- return getThis();
- }
-
- public @NotNull B comments(@NotNull String... comments) {
- this.comments = comments;
- return getThis();
- }
-
- public @NotNull B defaults(@Nullable T defaultValue) {
- this.defaultValue = defaultValue;
- return getThis();
- }
-
-
}
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java
index 3ede77f..3a09cda 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/list/SourceListBuilder.java
@@ -65,7 +65,7 @@ public SourceListBuilder(@NotNull Class sourceClass, @NotNull ConfigDataFunct
@Override
public @NotNull ConfiguredList build() {
return new ConfiguredList<>(
- this.provider, this.path, this.comments,
+ this.provider, this.path, this.buildComments(),
this.valueClass, this.defaultValue,
this.sourceParser.andThen(this.valueParser),
this.valueSerializer.andThen(sourceSerializer)
diff --git a/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java b/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java
index 1667bd0..3345c6d 100644
--- a/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java
+++ b/core/src/main/java/cc/carm/lib/configuration/core/builder/map/SourceMapBuilder.java
@@ -89,7 +89,7 @@ public