1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 18:48:20 +08:00

feat: Optimized comments & sections behavior

This commit is contained in:
2025-02-15 05:56:00 +08:00
parent 4cdd184fe6
commit 3a3533ce81
26 changed files with 402 additions and 183 deletions
@@ -19,7 +19,7 @@ import java.lang.annotation.Target;
*/
@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FooterComment {
public @interface FooterComments {
/**
* If the content of the note is 0, it will be treated as a blank line.
@@ -19,7 +19,7 @@ import java.lang.annotation.Target;
*/
@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HeaderComment {
public @interface HeaderComments {
/**
* If the content of the note is 0, it will be treated as a blank line.
@@ -2,10 +2,7 @@ package cc.carm.lib.configuration.annotation;
import org.jetbrains.annotations.NotNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;
/**
* Inline comments,
@@ -17,6 +14,7 @@ import java.lang.annotation.Target;
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(InlineComments.class)
public @interface InlineComment {
/**
@@ -35,8 +33,9 @@ public @interface InlineComment {
* If the regex is not empty, the comment will be added to
* all sub paths if the regex matches the value.
* If the regex is empty, the comment will be added to the current path.
* <p> e.g. <b>"^foo\\.*\\.bar"</b> will be set like
* <p> e.g. for section, set <b>{"^foo", "*", "bar"}</b> will be set like
* <blockquote><pre>
* section:
* foo:
* some:
* lover: "bar" <- not matched so no comments
@@ -0,0 +1,15 @@
package cc.carm.lib.configuration.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface InlineComments {
InlineComment[] value();
}
@@ -0,0 +1,67 @@
package cc.carm.lib.configuration.commentable;
import cc.carm.lib.configuration.annotation.FooterComments;
import cc.carm.lib.configuration.annotation.HeaderComments;
import cc.carm.lib.configuration.annotation.InlineComment;
import cc.carm.lib.configuration.annotation.InlineComments;
import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.source.loader.ConfigurationInitializer;
import cc.carm.lib.configuration.source.meta.ConfigurationMetadata;
import org.jetbrains.annotations.NotNull;
import java.util.*;
public interface CommentableMeta {
/**
* Configuration's {@link HeaderComments}
*
* @see HeaderComments
*/
ConfigurationMetadata<List<String>> HEADER = ConfigurationMetadata.of(Collections.emptyList());
/**
* Configuration's footer comments
*
* @see FooterComments
*/
ConfigurationMetadata<List<String>> FOOTER = ConfigurationMetadata.of(Collections.emptyList());
/**
* Configuration's {@link InlineComment}
* <p> Map< regex, comment > , regex is used to match the key, null for current path.
*
* @see InlineComment
*/
ConfigurationMetadata<Map<String, String>> INLINE = ConfigurationMetadata.of();
static void register(@NotNull ConfigurationHolder<?> provider) {
register(provider.initializer());
}
static void register(@NotNull ConfigurationInitializer initializer) {
initializer.registerAnnotation(
HeaderComments.class, HEADER,
a -> Arrays.asList(a.value())
);
initializer.registerAnnotation(
FooterComments.class, FOOTER,
a -> Arrays.asList(a.value())
);
initializer.registerAnnotation(InlineComments.class, INLINE, a -> {
Map<String, String> map = new HashMap<>();
for (InlineComment comment : a.value()) {
if (comment.regex().length == 0) { // for current path
map.put(null, comment.value());
continue;
}
for (String regex : comment.regex()) { // for specified path
map.put(regex, comment.value());
}
}
return map;
});
}
}
@@ -1,49 +0,0 @@
package cc.carm.lib.configuration.commentable;
import cc.carm.lib.configuration.annotation.FooterComment;
import cc.carm.lib.configuration.annotation.HeaderComment;
import cc.carm.lib.configuration.annotation.InlineComment;
import cc.carm.lib.configuration.source.ConfigurationHolder;
import cc.carm.lib.configuration.source.loader.ConfigurationInitializer;
import cc.carm.lib.configuration.source.meta.ConfigurationMetadata;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public interface CommentableMetaTypes {
/**
* Configuration's {@link HeaderComment}
*/
ConfigurationMetadata<List<String>> HEADER_COMMENTS = ConfigurationMetadata.of(Collections.emptyList());
/**
* Configuration's footer comments
*/
ConfigurationMetadata<List<String>> FOOTER_COMMENTS = ConfigurationMetadata.of(Collections.emptyList());
/**
* Configuration's {@link InlineComment}
*/
ConfigurationMetadata<String> INLINE_COMMENT = ConfigurationMetadata.of();
static void register(@NotNull ConfigurationHolder<?> provider) {
register(provider.initializer());
}
static void register(@NotNull ConfigurationInitializer initializer) {
initializer.registerAnnotation(
HeaderComment.class, HEADER_COMMENTS,
a -> Arrays.asList(a.value())
);
initializer.registerAnnotation(
FooterComment.class, FOOTER_COMMENTS,
a -> Arrays.asList(a.value())
);
initializer.registerAnnotation(InlineComment.class, INLINE_COMMENT, InlineComment::value);
}
}
@@ -8,7 +8,7 @@ public interface CommentableOptions {
// * Whether to keep modified comments in configuration,
// * that means we only set comments for values that are not exists in configuration.
// */
// ConfigurationOption<Boolean> KEEP_COMMENTS = ConfigurationOption.of(true);
// ConfigurationOption<Boolean> KEEP_COMMENTS = ConfigurationOption.of(true); // TODO: Implement this feature
/**
* Whether to comment values name that are not exists in configuration and no default value offered.
@@ -19,6 +19,6 @@ public interface CommentableOptions {
* # foo:
* </pre></blockquote>
*/
ConfigurationOption<Boolean> COMMENT_EMPTY_VALUE = ConfigurationOption.of(true);
ConfigurationOption<Boolean> COMMENT_EMPTY_VALUE = ConfigurationOption.of(false);
}