Skip to content

Commit 7ee2318

Browse files
Implement hints for function and command parameters
1 parent 972e80e commit 7ee2318

5 files changed

Lines changed: 74 additions & 6 deletions

File tree

src/main/java/ch/njol/skript/command/Argument.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public void set(final ScriptCommandEvent e, final Object[] o) {
130130
public T[] getCurrent(final Event e) {
131131
return current.get(e);
132132
}
133+
134+
public @Nullable String getName() {
135+
return name;
136+
}
133137

134138
public Class<T> getType() {
135139
return type.getC();

src/main/java/ch/njol/skript/command/ScriptCommand.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import ch.njol.skript.lang.Expression;
99
import ch.njol.skript.lang.SkriptParser;
1010
import ch.njol.skript.lang.Trigger;
11+
import ch.njol.skript.lang.Variable;
1112
import ch.njol.skript.lang.VariableString;
13+
import ch.njol.skript.lang.parser.ParserInstance;
1214
import ch.njol.skript.lang.util.SimpleEvent;
1315
import ch.njol.skript.lang.util.SimpleLiteral;
1416
import ch.njol.skript.localization.Language;
@@ -23,6 +25,7 @@
2325
import ch.njol.skript.util.Utils;
2426
import ch.njol.skript.util.chat.BungeeConverter;
2527
import ch.njol.skript.util.chat.MessageComponent;
28+
import ch.njol.skript.variables.HintManager;
2629
import ch.njol.skript.variables.Variables;
2730
import ch.njol.util.StringUtils;
2831
import com.google.common.base.Preconditions;
@@ -204,8 +207,21 @@ public ScriptCommand(
204207
this.pattern = pattern;
205208
this.arguments = arguments;
206209

207-
trigger = new Trigger(script, "command /" + name, new SimpleEvent(), ScriptLoader.loadItems(node));
208-
trigger.setLineNumber(node.getLine());
210+
HintManager hintManager = ParserInstance.get().getHintManager();
211+
try {
212+
hintManager.enterScope();
213+
for (Argument<?> argument : arguments) {
214+
String hintName = argument.getName();
215+
if (!argument.isSingle()) {
216+
hintName += Variable.SEPARATOR + "*";
217+
}
218+
hintManager.set(hintName, argument.getType());
219+
}
220+
trigger = new Trigger(script, "command /" + name, new SimpleEvent(), ScriptLoader.loadItems(node));
221+
trigger.setLineNumber(node.getLine());
222+
} finally {
223+
hintManager.exitScope();
224+
}
209225

210226
bukkitCommand = setupBukkitCommand();
211227
}

src/main/java/ch/njol/skript/lang/function/ScriptFunction.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import ch.njol.skript.lang.Expression;
66
import ch.njol.skript.lang.ReturnHandler;
77
import ch.njol.skript.lang.Trigger;
8+
import ch.njol.skript.lang.Variable;
9+
import ch.njol.skript.lang.parser.ParserInstance;
810
import ch.njol.skript.lang.util.SimpleEvent;
11+
import ch.njol.skript.variables.HintManager;
912
import ch.njol.skript.variables.Variables;
1013
import org.bukkit.event.Event;
1114
import org.jetbrains.annotations.ApiStatus;
@@ -31,9 +34,19 @@ public ScriptFunction(Signature<T> sign, SectionNode node) {
3134
super(sign);
3235

3336
Functions.currentFunction = this;
37+
HintManager hintManager = ParserInstance.get().getHintManager();
3438
try {
39+
hintManager.enterScope();
40+
for (Parameter<?> parameter : sign.getParameters()) {
41+
String hintName = parameter.getName();
42+
if (!parameter.isSingleValue()) {
43+
hintName += Variable.SEPARATOR + "*";
44+
}
45+
hintManager.set(hintName, parameter.getType().getC());
46+
}
3547
trigger = loadReturnableTrigger(node, "function " + sign.getName(), new SimpleEvent());
3648
} finally {
49+
hintManager.exitScope();
3750
Functions.currentFunction = null;
3851
}
3952
trigger.setLineNumber(node.getLine());

src/test/skript/tests/syntaxes/structures/StructCommand.sk

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
test "commands":
2-
execute command "skriptcommand taco"
3-
execute command "//somecommand burrito is tasty"
4-
51
options:
62
command: skriptcommand
73

@@ -24,3 +20,26 @@ command //somecommand [<text>]:
2420
command /commandtest <string="player">:
2521
trigger:
2622
stop
23+
24+
test "commands":
25+
execute command "skriptcommand taco"
26+
execute command "//somecommand burrito is tasty"
27+
28+
parse:
29+
results: {StructCommand::hints::1::*}
30+
code:
31+
command /hint_test_1 <x:number>:
32+
trigger:
33+
set {_a} to {_x} in lowercase
34+
35+
parse:
36+
results: {StructCommand::hints::2::*}
37+
code:
38+
command /hint_test_2 <x:numbers>:
39+
trigger:
40+
set {_a::*} to {_x::*} in lowercase
41+
42+
test "command structure type hints":
43+
assert {StructCommand::hints::1::*} contains "Expected variable '{_x}' to be a text, but it is a number" with "Hint failed (%{StructCommand::hints::1::*}%)"
44+
assert {StructCommand::hints::2::*} contains "Expected variable '{_x::*}' to be a text, but it is a number" with "Hint failed (%{StructCommand::hints::2::*}%)"
45+

src/test/skript/tests/syntaxes/structures/StructFunction.sk

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,19 @@ test "functions":
1717
assert foo() is true with "function return type failed"
1818
assert local() is not 1 with "global function parsed before local function"
1919
assert bar() is true with "local function didn't execute correctly"
20+
21+
parse:
22+
results: {StructFunction::hints::1::*}
23+
code:
24+
function hint_test_1(x: number):
25+
set {_a} to {_x} in lowercase
26+
27+
parse:
28+
results: {StructFunction::hints::2::*}
29+
code:
30+
function hint_test_2(x: numbers):
31+
set {_a::*} to {_x::*} in lowercase
32+
33+
test "function structure type hints":
34+
assert {StructFunction::hints::1::*} contains "Expected variable '{_x}' to be a text, but it is a number" with "Hint failed (%{StructFunction::hints::1::*}%)"
35+
assert {StructFunction::hints::2::*} contains "Expected variable '{_x::*}' to be a text, but it is a number" with "Hint failed (%{StructFunction::hints::2::*}%)"

0 commit comments

Comments
 (0)