Skip to content

Commit adecd82

Browse files
committed
Fix suggestion argument splitting
1 parent 7a17857 commit adecd82

7 files changed

Lines changed: 65 additions & 20 deletions

File tree

bukkit/src/main/java/me/vaperion/blade/bukkit/container/BukkitContainer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,10 @@ public void tabComplete(@NotNull CommandSender sender,
394394
if (!node.isStub()) {
395395
// Found exact command, we can suggest arguments here.
396396

397-
String[] args = removePrefix(
397+
String[] args = splitSuggestionArguments(removePrefix(
398398
removeCommandQualifier(commandLine),
399399
node.matchedLabelOr("")
400-
).split(" ");
400+
));
401401

402402
if (!platformTypes.contains(SuggestionType.ARGUMENTS)) {
403403
// Platform doesn't support argument suggestions.
@@ -428,7 +428,7 @@ public void tabComplete(@NotNull CommandSender sender,
428428
return;
429429
}
430430

431-
String[] args = removeCommandQualifier(commandLine).split(" ");
431+
String[] args = splitSuggestionArguments(removeCommandQualifier(commandLine));
432432

433433
Context context = new Context(
434434
blade,

core/src/main/java/me/vaperion/blade/util/BladeHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ static String mergeLabelWithArgs(@NotNull String label,
119119
: label + " " + args;
120120
}
121121

122+
@NotNull
123+
static String[] splitSuggestionArguments(@NotNull String input) {
124+
int start = 0;
125+
126+
while (start < input.length() && input.charAt(start) == ' ') {
127+
start++;
128+
}
129+
130+
input = input.substring(start);
131+
132+
if (input.isEmpty()) {
133+
return new String[]{ "" };
134+
}
135+
136+
return input.split(" ", -1);
137+
}
138+
122139
@NotNull
123140
static String removePrefix(@NotNull String input,
124141
@NotNull String prefix) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package me.vaperion.blade;
2+
3+
import me.vaperion.blade.util.BladeHelper;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class BladeHelperTest {
8+
9+
@Test
10+
public void suggestionArgumentsPreserveTrailingEmptyArgument() {
11+
Assertions.assertArrayEquals(
12+
new String[]{ "server", "" },
13+
BladeHelper.splitSuggestionArguments("server ")
14+
);
15+
}
16+
17+
@Test
18+
public void suggestionArgumentsPreserveEmptyArgument() {
19+
Assertions.assertArrayEquals(
20+
new String[]{ "" },
21+
BladeHelper.splitSuggestionArguments("")
22+
);
23+
24+
Assertions.assertArrayEquals(
25+
new String[]{ "" },
26+
BladeHelper.splitSuggestionArguments(" ")
27+
);
28+
}
29+
30+
@Test
31+
public void suggestionArgumentsIgnoreLeadingSeparatorFromRemovedLabel() {
32+
Assertions.assertArrayEquals(
33+
new String[]{ "remove" },
34+
BladeHelper.splitSuggestionArguments(" remove")
35+
);
36+
}
37+
38+
}

fabric-legacy/src/main/java/me/vaperion/blade/fabric/container/FabricContainer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ public void suggest(@NotNull CommandContext<CommandSourceStack> ctx,
249249
if (!node.isStub()) {
250250
// Found exact command, we can suggest arguments here.
251251

252-
String[] args = removePrefix(
252+
String[] args = splitSuggestionArguments(removePrefix(
253253
removeCommandQualifier(ctx.getInput()),
254254
node.matchedLabelOr("")
255-
).split(" ");
255+
));
256256

257257
Context context = new Context(
258258
blade,
@@ -273,7 +273,7 @@ public void suggest(@NotNull CommandContext<CommandSourceStack> ctx,
273273

274274
// Only found command stub - suggest subcommands.
275275

276-
String[] args = removeCommandQualifier(ctx.getInput()).split(" ");
276+
String[] args = splitSuggestionArguments(removeCommandQualifier(ctx.getInput()));
277277

278278
Context context = new Context(
279279
blade,

fabric/src/main/java/me/vaperion/blade/fabric/container/FabricContainer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ public void suggest(@NotNull CommandContext<CommandSourceStack> ctx,
249249
if (!node.isStub()) {
250250
// Found exact command, we can suggest arguments here.
251251

252-
String[] args = removePrefix(
252+
String[] args = splitSuggestionArguments(removePrefix(
253253
removeCommandQualifier(ctx.getInput()),
254254
node.matchedLabelOr("")
255-
).split(" ");
255+
));
256256

257257
Context context = new Context(
258258
blade,
@@ -273,7 +273,7 @@ public void suggest(@NotNull CommandContext<CommandSourceStack> ctx,
273273

274274
// Only found command stub - suggest subcommands.
275275

276-
String[] args = removeCommandQualifier(ctx.getInput()).split(" ");
276+
String[] args = splitSuggestionArguments(removeCommandQualifier(ctx.getInput()));
277277

278278
Context context = new Context(
279279
blade,

minestom/src/main/java/me/vaperion/blade/minestom/container/MinestomContainer.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,4 @@ private String normalizeCompletionInput(@NotNull String input) {
372372
return input.replace(MINESTOM_COMPLETION_PLACEHOLDER, ' ');
373373
}
374374

375-
@NotNull
376-
private String[] splitSuggestionArguments(@NotNull String input) {
377-
input = input.stripLeading();
378-
379-
if (input.isEmpty()) {
380-
return new String[0];
381-
}
382-
383-
return input.split(" ", -1);
384-
}
385375
}

velocity/src/main/java/me/vaperion/blade/velocity/container/VelocityContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void execute(Invocation invocation) {
232232
@Override
233233
public List<String> suggest(Invocation invocation) {
234234
CommandSource sender = invocation.source();
235-
String[] args = invocation.arguments().split(" ");
235+
String[] args = splitSuggestionArguments(invocation.arguments());
236236
String label = invocation.alias();
237237

238238
if (!blade.configuration().tabCompleter().isDefault())

0 commit comments

Comments
 (0)