Skip to content

Commit 30edc5d

Browse files
PatternElement Combinations + Conflicts Test (#8138)
* Create PatternConflictsTest.java * Draft * Update * Update PatternParserTest.java * Revert * Update PatternParserTest.java * Update PatternParserTest.java * Filtering + Exclusions * PatternElement Usage * Fix TypePatternElement * Partial Changes * Update PatternConflictsTest.java * Update PatternConflictsTest.java * Update PatternConflictsTest.java * Update PatternConflictsTest.java * Requested Changes * Pickles Changes
1 parent 2b4b50d commit 30edc5d

File tree

10 files changed

+688
-1
lines changed

10 files changed

+688
-1
lines changed

src/main/java/ch/njol/skript/patterns/ChoicePatternElement.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.jetbrains.annotations.Nullable;
44

55
import java.util.ArrayList;
6+
import java.util.HashSet;
67
import java.util.List;
8+
import java.util.Set;
79
import java.util.stream.Collectors;
810

911
/**
@@ -55,4 +57,12 @@ public String toString() {
5557
.map(PatternElement::toFullString)
5658
.collect(Collectors.joining("|"));
5759
}
60+
61+
@Override
62+
public Set<String> getCombinations(boolean clean) {
63+
Set<String> combinations = new HashSet<>();
64+
patternElements.forEach(patternElement -> combinations.addAll(patternElement.getAllCombinations(clean)));
65+
return combinations;
66+
}
67+
5868
}

src/main/java/ch/njol/skript/patterns/GroupPatternElement.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.jetbrains.annotations.Nullable;
44

5+
import java.util.Set;
6+
57
/**
68
* A {@link PatternElement} that represents a group, for example {@code (test)}.
79
*/
@@ -34,4 +36,9 @@ public String toString() {
3436
return "(" + patternElement + ")";
3537
}
3638

39+
@Override
40+
public Set<String> getCombinations(boolean clean) {
41+
return patternElement.getAllCombinations(clean);
42+
}
43+
3744
}

src/main/java/ch/njol/skript/patterns/LiteralPatternElement.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.jetbrains.annotations.Nullable;
44

5+
import java.util.HashSet;
56
import java.util.Locale;
7+
import java.util.Set;
68

79
/**
810
* A {@link PatternElement} that contains a literal string to be matched, for example {@code hello world}.
@@ -52,4 +54,9 @@ public String toString() {
5254
return new String(literal);
5355
}
5456

57+
@Override
58+
public Set<String> getCombinations(boolean clean) {
59+
return new HashSet<>(Set.of(toString()));
60+
}
61+
5562
}

src/main/java/ch/njol/skript/patterns/OptionalPatternElement.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.jetbrains.annotations.Nullable;
44

5+
import java.util.Set;
6+
57
/**
68
* A {@link PatternElement} that contains an optional part, for example {@code [hello world]}.
79
*/
@@ -37,4 +39,11 @@ public String toString() {
3739
return "[" + patternElement.toFullString() + "]";
3840
}
3941

42+
@Override
43+
public Set<String> getCombinations(boolean clean) {
44+
Set<String> combinations = patternElement.getAllCombinations(clean);
45+
combinations.add("");
46+
return combinations;
47+
}
48+
4049
}

src/main/java/ch/njol/skript/patterns/ParseTagPatternElement.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.jetbrains.annotations.Nullable;
44

5+
import java.util.HashSet;
56
import java.util.List;
7+
import java.util.Set;
68

79
/**
810
* A {@link PatternElement} that applies a parse mark when matched.
@@ -82,4 +84,16 @@ public String toString() {
8284
}
8385
}
8486

87+
/**
88+
* {@inheritDoc}
89+
* @param clean Whether the parse mark/tag should be excluded.
90+
*/
91+
@Override
92+
public Set<String> getCombinations(boolean clean) {
93+
Set<String> combinations = new HashSet<>();
94+
if (!clean)
95+
combinations.add(toString());
96+
return combinations;
97+
}
98+
8599
}

src/main/java/ch/njol/skript/patterns/PatternCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static SkriptPattern compile(String pattern) throws MalformedPatternExcep
4545
* {@link TypePatternElement} should be initiated with.
4646
* @return The first link of the {@link PatternElement} chain
4747
*/
48-
private static PatternElement compile(String pattern, AtomicInteger expressionOffset) {
48+
static PatternElement compile(String pattern, AtomicInteger expressionOffset) {
4949
StringBuilder literalBuilder = new StringBuilder();
5050
PatternElement first = null;
5151

src/main/java/ch/njol/skript/patterns/PatternElement.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.jetbrains.annotations.Nullable;
44

5+
import java.util.HashSet;
6+
import java.util.Set;
7+
58
public abstract class PatternElement {
69

710
@Nullable
@@ -48,4 +51,51 @@ public String toFullString() {
4851
return stringBuilder.toString();
4952
}
5053

54+
/**
55+
* Gets the combinations available to this {@link PatternElement}.
56+
* @param clean Whether unnecessary data, determined by each implementation, should be excluded from the combinations.
57+
* @return The combinations.
58+
*/
59+
public abstract Set<String> getCombinations(boolean clean);
60+
61+
/**
62+
* Gets all combinations available to this {@link PatternElement} and linked {@link PatternElement}s.
63+
* @param clean Whether unnecessary data, determined by each implementation, should be excluded from the combinations.
64+
* @return The combinations.
65+
*/
66+
public final Set<String> getAllCombinations(boolean clean) {
67+
Set<String> combinations = getCombinations(clean);
68+
if (combinations.isEmpty())
69+
combinations.add("");
70+
PatternElement next = this;
71+
while ((next = next.originalNext) != null) {
72+
Set<String> newCombinations = new HashSet<>();
73+
Set<String> nextCombinations = next.getCombinations(clean);
74+
if (nextCombinations.isEmpty())
75+
continue;
76+
for (String base : combinations) {
77+
for (String add : nextCombinations) {
78+
newCombinations.add(combineCombination(base, add));
79+
}
80+
}
81+
combinations = newCombinations;
82+
}
83+
return combinations;
84+
}
85+
86+
/**
87+
* Helper method for appropriately combining two strings together.
88+
* @return The resulting string.
89+
*/
90+
private static String combineCombination(String first, String second) {
91+
if (first.isBlank()) {
92+
return second.stripLeading();
93+
} else if (second.isEmpty()) {
94+
return first.stripTrailing();
95+
} else if (first.endsWith(" ") && second.startsWith(" ")) {
96+
return first + second.stripLeading();
97+
}
98+
return first + second;
99+
}
100+
51101
}

src/main/java/ch/njol/skript/patterns/RegexPatternElement.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import ch.njol.skript.log.SkriptLogger;
66
import org.jetbrains.annotations.Nullable;
77

8+
import java.util.HashSet;
9+
import java.util.Set;
810
import java.util.regex.Matcher;
911
import java.util.regex.Pattern;
1012

@@ -58,4 +60,9 @@ public String toString() {
5860
return "<" + pattern + ">";
5961
}
6062

63+
@Override
64+
public Set<String> getCombinations(boolean clean) {
65+
return new HashSet<>(Set.of(toString()));
66+
}
67+
6168
}

src/main/java/ch/njol/skript/patterns/TypePatternElement.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import ch.njol.util.NonNullPair;
1818
import org.jetbrains.annotations.Nullable;
1919

20+
import java.util.HashSet;
21+
import java.util.Set;
22+
2023
/**
2124
* A {@link PatternElement} that contains a type to be matched with an expressions, for example {@code %number%}.
2225
*/
@@ -263,4 +266,19 @@ public ExprInfo getExprInfo() {
263266
return exprInfo;
264267
}
265268

269+
/**
270+
* {@inheritDoc}
271+
* @param clean Whether this type should be replaced with {@code %*%} if it's not literal.
272+
*/
273+
@Override
274+
public Set<String> getCombinations(boolean clean) {
275+
Set<String> combinations = new HashSet<>();
276+
if (!clean || flagMask == 2) {
277+
combinations.add(toString());
278+
} else {
279+
combinations.add("%*%");
280+
}
281+
return combinations;
282+
}
283+
266284
}

0 commit comments

Comments
 (0)