forked from Mwexim/skript-parser
-
Notifications
You must be signed in to change notification settings - Fork 2
add: SyntaxStringBuilder #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+103
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package io.github.syst3ms.skriptparser.lang; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.StringJoiner; | ||
|
|
||
| /** | ||
| * Utility class to build syntax strings, primarily intended for use | ||
| * in {@link SyntaxElement#toString(TriggerContext, boolean)} implementations. | ||
| * Spaces are automatically added between the provided objects. | ||
| */ | ||
| public class SyntaxStringBuilder { | ||
|
|
||
| private final boolean debug; | ||
| private final TriggerContext context; | ||
| private final StringJoiner joiner = new StringJoiner(" "); | ||
|
|
||
| /** | ||
| * Creates a new SyntaxStringBuilder. | ||
| * | ||
| * @param context The context to get information from. | ||
| * @param debug If true this should print more information, if false this should print what is shown to the end user | ||
| */ | ||
| public SyntaxStringBuilder(TriggerContext context, boolean debug) { | ||
| this.context = context; | ||
| this.debug = debug; | ||
| } | ||
|
|
||
| /** | ||
| * Adds an object to the string and returns the builder. | ||
| * Spaces are automatically added between the provided objects. | ||
| * If the object is a {@link SyntaxElement} it will be formatted using | ||
| * {@link SyntaxElement#toString(TriggerContext, boolean)}. | ||
| * | ||
| * @param object The object to add. | ||
| * @return The builder. | ||
| * @see #appendIf(boolean, Object) | ||
| */ | ||
| public SyntaxStringBuilder append(@NotNull Object object) { | ||
| if (object instanceof SyntaxElement debuggable) { | ||
| joiner.add(debuggable.toString(context, debug)); | ||
| } else { | ||
| joiner.add(object.toString()); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds multiple objects to the string and returns the builder. | ||
| * Spaces are automatically added between the provided objects. | ||
| * | ||
| * @param objects The objects to add. | ||
| * @return The builder. | ||
| * @see #appendIf(boolean, Object...) | ||
| */ | ||
| public SyntaxStringBuilder append(@NotNull Object... objects) { | ||
| for (Object object : objects) { | ||
| append(object); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds an object to the string and returns the builder, if the given condition is true. | ||
| * Spaces are automatically added between the provided objects. | ||
| * If the object is a {@link SyntaxElement} it will be formatted using | ||
| * {@link SyntaxElement#toString(TriggerContext, boolean)}. | ||
| * | ||
| * @param condition The condition. | ||
| * @param object The object to add. Ensure this is not null. | ||
| * @return The builder. | ||
| * @see #append(Object) | ||
| */ | ||
| public SyntaxStringBuilder appendIf(boolean condition, Object object) { | ||
| if (condition) { | ||
| append(object); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds multiple objects to the string and returns the builder, if the given condition is true. | ||
| * Spaces are automatically added between the provided objects. | ||
| * | ||
| * @param condition The condition. | ||
| * @param objects The objects to add. Ensure this is not null. | ||
| * @return The builder. | ||
| * @see #append(Object...) | ||
| */ | ||
| public SyntaxStringBuilder appendIf(boolean condition, Object... objects) { | ||
| if (condition) { | ||
| append(objects); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return joiner.toString(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.