Skip to content

Commit 972e80e

Browse files
Implement type hint updates for more syntax
1 parent 5c66cf4 commit 972e80e

6 files changed

Lines changed: 70 additions & 12 deletions

File tree

src/main/java/ch/njol/skript/effects/EffCopy.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ch.njol.skript.lang.SkriptParser.ParseResult;
1414
import ch.njol.skript.lang.Variable;
1515
import ch.njol.skript.registrations.Classes;
16+
import ch.njol.skript.variables.HintManager;
1617
import ch.njol.skript.variables.Variables;
1718
import ch.njol.util.Kleenean;
1819
import org.bukkit.event.Event;
@@ -64,6 +65,16 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
6465
return false;
6566
}
6667
}
68+
69+
// set type hints for destination(s) if applicable
70+
Class<?>[] sourceHints = source.possibleReturnTypes();
71+
HintManager hintManager = getParser().getHintManager();
72+
for (Variable<?> destination : destinations) {
73+
if (HintManager.canUseHints(destination)) {
74+
hintManager.set(destination, sourceHints);
75+
}
76+
}
77+
6778
return true;
6879
}
6980

src/main/java/ch/njol/skript/effects/EffTransform.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import ch.njol.skript.lang.SkriptParser.ParseResult;
1515
import ch.njol.skript.lang.Variable;
1616
import ch.njol.skript.lang.parser.ParserInstance;
17+
import ch.njol.skript.variables.HintManager;
1718
import ch.njol.skript.variables.Variables;
1819
import ch.njol.util.Kleenean;
1920
import ch.njol.util.Pair;
@@ -69,19 +70,27 @@ public class EffTransform extends Effect implements InputSource {
6970

7071
@Override
7172
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
72-
if (expressions[0].isSingle() || !(expressions[0] instanceof Variable)) {
73+
if (parseResult.regexes.isEmpty()) {
74+
return false;
75+
}
76+
77+
if (expressions[0].isSingle() || !(expressions[0] instanceof Variable<?> variable)) {
7378
Skript.error("You can only transform list variables!");
7479
return false;
7580
}
76-
unmappedObjects = (Variable<?>) expressions[0];
77-
78-
//noinspection DuplicatedCode
79-
if (!parseResult.regexes.isEmpty()) {
80-
@Nullable String unparsedExpression = parseResult.regexes.get(0).group();
81-
assert unparsedExpression != null;
82-
mappingExpr = parseExpression(unparsedExpression, getParser(), SkriptParser.ALL_FLAGS);
83-
return mappingExpr != null;
81+
unmappedObjects = variable;
82+
83+
String unparsedExpression = parseResult.regexes.get(0).group();
84+
mappingExpr = parseExpression(unparsedExpression, getParser(), SkriptParser.ALL_FLAGS);
85+
if (mappingExpr == null) {
86+
return false;
87+
}
88+
89+
// type hints
90+
if (HintManager.canUseHints(variable)) {
91+
getParser().getHintManager().set(variable, mappingExpr.possibleReturnTypes());
8492
}
93+
8594
return true;
8695
}
8796

src/main/java/ch/njol/skript/variables/HintManager.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,12 @@ public void set(String variableName, Class<?>... hints) {
175175
hintSet.add(hint);
176176
}
177177
}
178-
//noinspection DataFlowIssue - verified by checkState
179-
typeHints.peek().put(variableName, hintSet);
178+
if (hintSet.isEmpty()) { // treat as delete
179+
delete(variableName);
180+
} else {
181+
//noinspection DataFlowIssue - verified by checkState
182+
typeHints.peek().put(variableName, hintSet);
183+
}
180184
}
181185

182186
/**
@@ -259,6 +263,9 @@ public void remove(String variableName, Class<?>... hints) {
259263
for (Class<?> hint : hints) {
260264
hintSet.remove(hint);
261265
}
266+
if (hintSet.isEmpty()) {
267+
delete(variableName);
268+
}
262269
}
263270
}
264271

src/test/skript/tests/syntaxes/effects/EffCopy.sk

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,26 @@ test "single copy":
2828

2929
copy {_none} into {_foo}
3030
assert {_foo} is not set with "Copying nothing didn't delete variable"
31+
32+
test "copy effect type hints":
33+
set {_var} to 10
34+
copy {_var} into {_x}
35+
parse:
36+
set {_a} to {_x} in lowercase
37+
assert last parse logs contains "Expected variable '{_x}' to be a text, but it is an integer" with "Hint failed (%last parse logs%)"
38+
delete {_var}
39+
delete {_x}
40+
41+
set {_var::*} to 10 and 20
42+
copy {_var::*} into {_x::*}
43+
parse:
44+
set {_a::*} to {_x::*} in lowercase
45+
assert last parse logs contains "Expected variable '{_x::*}' to be a text, but it is an integer" with "Hint failed (%last parse logs%)"
46+
delete {_var::*}
47+
delete {_x::*}
48+
49+
copy pi into {_x}
50+
parse:
51+
set {_a} to {_x} in lowercase
52+
assert last parse logs contains "Expected variable '{_x}' to be a text, but it is a number" with "Hint failed (%last parse logs%)"
53+
delete {_foo}

src/test/skript/tests/syntaxes/effects/EffTransform.sk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ test "transform effect":
2727
assert {_a::*} is 1, 2, 1, 2, 1 and 2 with "failed to transform with plural expression"
2828
assert indices of {_a::*} is "1", "2", "3", "4", "5" and "6" with "transform failed to re-set indices"
2929

30+
31+
test "transform effect type hints":
32+
set {_x::*} to true and false
33+
transform {_x::*} using 0
34+
parse:
35+
set {_a::*} to {_x::*} in lowercase
36+
assert last parse logs contains "Expected variable '{_x::*}' to be a text, but it is an integer" with "Hint failed (%last parse logs%)"

src/test/skript/tests/syntaxes/sections/SecFor.sk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ test "for section":
6464
delete {_key}
6565
delete {_value}
6666

67-
# type hints
67+
test "for section type hints":
68+
set {_list::*} to 1, 5, and 10
6869
parse:
6970
set {_value} to true
7071
loop {_key} and {_value} in {_list::*}:

0 commit comments

Comments
 (0)