13
0
geforkt von Mirrors/Paper

Improve console completion with brig suggestions (#9251)

* Improve console completion with brig suggestions

* silence warning

* small fixes

* squashed
Dieser Commit ist enthalten in:
Jake Potrebic 2024-09-06 13:37:09 -07:00
Ursprung 5bf1879bed
Commit d668e93940

Datei anzeigen

@ -3,11 +3,21 @@ From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Tue, 30 Mar 2021 16:06:08 -0700 Date: Tue, 30 Mar 2021 16:06:08 -0700
Subject: [PATCH] Enhance console tab completions for brigadier commands Subject: [PATCH] Enhance console tab completions for brigadier commands
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
diff --git a/src/main/java/com/destroystokyo/paper/console/PaperConsole.java b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java diff --git a/src/main/java/com/destroystokyo/paper/console/PaperConsole.java b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/com/destroystokyo/paper/console/PaperConsole.java --- a/src/main/java/com/destroystokyo/paper/console/PaperConsole.java
+++ b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java +++ b/src/main/java/com/destroystokyo/paper/console/PaperConsole.java
@@ -0,0 +0,0 @@
package com.destroystokyo.paper.console;
+import io.papermc.paper.configuration.GlobalConfiguration;
+import io.papermc.paper.console.BrigadierCompletionMatcher;
+import io.papermc.paper.console.BrigadierConsoleParser;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecrell.terminalconsole.SimpleTerminalConsole;
import org.bukkit.craftbukkit.command.ConsoleCommandCompleter;
@@ -0,0 +0,0 @@ public final class PaperConsole extends SimpleTerminalConsole { @@ -0,0 +0,0 @@ public final class PaperConsole extends SimpleTerminalConsole {
@Override @Override
@ -22,6 +32,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ if (io.papermc.paper.configuration.GlobalConfiguration.get().console.enableBrigadierHighlighting) { + if (io.papermc.paper.configuration.GlobalConfiguration.get().console.enableBrigadierHighlighting) {
+ builder.highlighter(new io.papermc.paper.console.BrigadierCommandHighlighter(this.server)); + builder.highlighter(new io.papermc.paper.console.BrigadierCommandHighlighter(this.server));
+ } + }
+ if (GlobalConfiguration.get().console.enableBrigadierCompletions) {
+ System.setProperty("org.jline.reader.support.parsedline", "true"); // to hide a warning message about the parser not supporting
+ builder.parser(new BrigadierConsoleParser(this.server));
+ builder.completionMatcher(new BrigadierCompletionMatcher());
+ }
+ return super.buildReader(builder); + return super.buildReader(builder);
} }
@ -34,6 +49,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@ @@ -0,0 +0,0 @@
+package io.papermc.paper.console; +package io.papermc.paper.console;
+ +
+import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent;
+import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent.Completion; +import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent.Completion;
+import com.google.common.base.Suppliers; +import com.google.common.base.Suppliers;
+import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.CommandDispatcher;
@ -45,10 +61,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import java.util.Collections; +import java.util.Collections;
+import java.util.List; +import java.util.List;
+import java.util.function.Supplier; +import java.util.function.Supplier;
+import net.kyori.adventure.text.Component;
+import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.network.chat.ComponentUtils; +import net.minecraft.network.chat.ComponentUtils;
+import net.minecraft.server.dedicated.DedicatedServer; +import net.minecraft.server.dedicated.DedicatedServer;
+import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.jline.reader.Candidate; +import org.jline.reader.Candidate;
+import org.jline.reader.LineReader; +import org.jline.reader.LineReader;
+import org.jline.reader.ParsedLine; +import org.jline.reader.ParsedLine;
@ -69,7 +87,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ if (this.server.overworld() == null) { // check if overworld is null, as worlds haven't been loaded yet + if (this.server.overworld() == null) { // check if overworld is null, as worlds haven't been loaded yet
+ return; + return;
+ } else if (!io.papermc.paper.configuration.GlobalConfiguration.get().console.enableBrigadierCompletions) { + } else if (!io.papermc.paper.configuration.GlobalConfiguration.get().console.enableBrigadierCompletions) {
+ this.addCandidates(candidates, Collections.emptyList(), existing); + this.addCandidates(candidates, Collections.emptyList(), existing, new ParseContext(line.line(), 0));
+ return; + return;
+ } + }
+ final CommandDispatcher<CommandSourceStack> dispatcher = this.server.getCommands().getDispatcher(); + final CommandDispatcher<CommandSourceStack> dispatcher = this.server.getCommands().getDispatcher();
@ -77,41 +95,57 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ this.addCandidates( + this.addCandidates(
+ candidates, + candidates,
+ dispatcher.getCompletionSuggestions(results, line.cursor()).join().getList(), + dispatcher.getCompletionSuggestions(results, line.cursor()).join().getList(),
+ existing + existing,
+ new ParseContext(line.line(), results.getContext().findSuggestionContext(line.cursor()).startPos)
+ ); + );
+ } + }
+ +
+ private void addCandidates( + private void addCandidates(
+ final @NonNull List<Candidate> candidates, + final @NonNull List<Candidate> candidates,
+ final @NonNull List<Suggestion> brigSuggestions, + final @NonNull List<Suggestion> brigSuggestions,
+ final @NonNull List<Completion> existing + final @NonNull List<Completion> existing,
+ final @NonNull ParseContext context
+ ) { + ) {
+ final List<Completion> completions = new ArrayList<>(); + brigSuggestions.forEach(it -> {
+ brigSuggestions.forEach(it -> completions.add(toCompletion(it))); + if (it.getText().isEmpty()) return;
+ for (final Completion completion : existing) { + candidates.add(toCandidate(it, context));
+ });
+ for (final AsyncTabCompleteEvent.Completion completion : existing) {
+ if (completion.suggestion().isEmpty() || brigSuggestions.stream().anyMatch(it -> it.getText().equals(completion.suggestion()))) { + if (completion.suggestion().isEmpty() || brigSuggestions.stream().anyMatch(it -> it.getText().equals(completion.suggestion()))) {
+ continue; + continue;
+ } + }
+ completions.add(completion);
+ }
+ for (final Completion completion : completions) {
+ if (completion.suggestion().isEmpty()) {
+ continue;
+ }
+ candidates.add(toCandidate(completion)); + candidates.add(toCandidate(completion));
+ } + }
+ } + }
+ +
+ private static Candidate toCandidate(final Suggestion suggestion, final @NonNull ParseContext context) {
+ Component tooltip = null;
+ if (suggestion.getTooltip() != null) {
+ tooltip = PaperAdventure.asAdventure(ComponentUtils.fromMessage(suggestion.getTooltip()));
+ }
+ return toCandidate(context.line.substring(context.suggestionStart, suggestion.getRange().getStart()) + suggestion.getText(), tooltip);
+ }
+
+ private static @NonNull Candidate toCandidate(final @NonNull Completion completion) { + private static @NonNull Candidate toCandidate(final @NonNull Completion completion) {
+ final String suggestionText = completion.suggestion(); + return toCandidate(completion.suggestion(), completion.tooltip());
+ final String suggestionTooltip = PaperAdventure.ANSI_SERIALIZER.serializeOr(completion.tooltip(), null); + }
+ return new Candidate( +
+ private static @NonNull Candidate toCandidate(final @NonNull String suggestionText, final @Nullable Component tooltip) {
+ final String suggestionTooltip = PaperAdventure.ANSI_SERIALIZER.serializeOr(tooltip, null);
+ //noinspection SpellCheckingInspection
+ return new PaperCandidate(
+ suggestionText, + suggestionText,
+ suggestionText, + suggestionText,
+ null, + null,
+ suggestionTooltip, + suggestionTooltip,
+ null, + null,
+ null, + null,
+ /*
+ in an ideal world, this would sometimes be true if the suggestion represented the final possible value for a word.
+ Like for `/execute alig`, pressing enter on align would add a trailing space if this value was true. But not all
+ suggestions should add spaces after, like `/execute as @`, accepting any suggestion here would be valid, but its also
+ valid to have a `[` following the selector
+ */
+ false + false
+ ); + );
+ } + }
@ -130,6 +164,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ } + }
+ return stringReader; + return stringReader;
+ } + }
+
+ private record ParseContext(String line, int suggestionStart) {
+ }
+
+ public static final class PaperCandidate extends Candidate {
+ public PaperCandidate(final String value, final String display, final String group, final String descr, final String suffix, final String key, final boolean complete) {
+ super(value, display, group, descr, suffix, key, complete);
+ }
+ }
+} +}
diff --git a/src/main/java/io/papermc/paper/console/BrigadierCommandHighlighter.java b/src/main/java/io/papermc/paper/console/BrigadierCommandHighlighter.java diff --git a/src/main/java/io/papermc/paper/console/BrigadierCommandHighlighter.java b/src/main/java/io/papermc/paper/console/BrigadierCommandHighlighter.java
new file mode 100644 new file mode 100644
@ -207,6 +250,125 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Override + @Override
+ public void setErrorIndex(final int errorIndex) {} + public void setErrorIndex(final int errorIndex) {}
+} +}
diff --git a/src/main/java/io/papermc/paper/console/BrigadierCompletionMatcher.java b/src/main/java/io/papermc/paper/console/BrigadierCompletionMatcher.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/console/BrigadierCompletionMatcher.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.console;
+
+import com.google.common.collect.Iterables;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.jline.reader.Candidate;
+import org.jline.reader.CompletingParsedLine;
+import org.jline.reader.LineReader;
+import org.jline.reader.impl.CompletionMatcherImpl;
+
+public class BrigadierCompletionMatcher extends CompletionMatcherImpl {
+
+ @Override
+ protected void defaultMatchers(final Map<LineReader.Option, Boolean> options, final boolean prefix, final CompletingParsedLine line, final boolean caseInsensitive, final int errors, final String originalGroupName) {
+ super.defaultMatchers(options, prefix, line, caseInsensitive, errors, originalGroupName);
+ this.matchers.addFirst(m -> {
+ final Map<String, List<Candidate>> candidates = new HashMap<>();
+ for (final Map.Entry<String, List<Candidate>> entry : m.entrySet()) {
+ if (Iterables.all(entry.getValue(), BrigadierCommandCompleter.PaperCandidate.class::isInstance)) {
+ candidates.put(entry.getKey(), entry.getValue());
+ }
+ }
+ return candidates;
+ });
+ }
+}
diff --git a/src/main/java/io/papermc/paper/console/BrigadierConsoleParser.java b/src/main/java/io/papermc/paper/console/BrigadierConsoleParser.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/console/BrigadierConsoleParser.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.console;
+
+import com.mojang.brigadier.ImmutableStringReader;
+import com.mojang.brigadier.ParseResults;
+import com.mojang.brigadier.context.CommandContextBuilder;
+import com.mojang.brigadier.context.ParsedCommandNode;
+import com.mojang.brigadier.context.StringRange;
+import java.util.ArrayList;
+import java.util.List;
+import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.server.dedicated.DedicatedServer;
+import org.jline.reader.ParsedLine;
+import org.jline.reader.Parser;
+import org.jline.reader.SyntaxError;
+
+import static io.papermc.paper.console.BrigadierCommandCompleter.prepareStringReader;
+
+public class BrigadierConsoleParser implements Parser {
+
+ private final DedicatedServer server;
+
+ public BrigadierConsoleParser(DedicatedServer server) {
+ this.server = server;
+ }
+
+ @Override
+ public ParsedLine parse(final String line, final int cursor, final ParseContext context) throws SyntaxError {
+ final ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(prepareStringReader(line), this.server.createCommandSourceStack());
+ final ImmutableStringReader reader = results.getReader();
+ final List<String> words = new ArrayList<>();
+ CommandContextBuilder<CommandSourceStack> currentContext = results.getContext();
+ int currentWordIdx = -1;
+ int wordIdx = -1;
+ int inWordCursor = -1;
+ if (currentContext.getRange().getLength() > 0) {
+ do {
+ for (final ParsedCommandNode<CommandSourceStack> node : currentContext.getNodes()) {
+ final StringRange nodeRange = node.getRange();
+ String current = nodeRange.get(reader);
+ words.add(current);
+ currentWordIdx++;
+ if (wordIdx == -1 && nodeRange.getStart() <= cursor && nodeRange.getEnd() >= cursor) {
+ // if cursor is in the middle of a parsed word/node
+ wordIdx = currentWordIdx;
+ inWordCursor = cursor - nodeRange.getStart();
+ }
+ }
+ currentContext = currentContext.getChild();
+ } while (currentContext != null);
+ }
+ final String leftovers = reader.getRemaining();
+ if (!leftovers.isEmpty() && leftovers.isBlank()) {
+ // if brig didn't consume the whole line, and everything else is blank, add a new empty word
+ currentWordIdx++;
+ words.add("");
+ if (wordIdx == -1) {
+ wordIdx = currentWordIdx;
+ inWordCursor = 0;
+ }
+ } else if (!leftovers.isEmpty()) {
+ // if there are unparsed leftovers, add a new word with the remaining input
+ currentWordIdx++;
+ words.add(leftovers);
+ if (wordIdx == -1) {
+ wordIdx = currentWordIdx;
+ inWordCursor = cursor - reader.getCursor();
+ }
+ }
+ if (wordIdx == -1) {
+ currentWordIdx++;
+ words.add("");
+ wordIdx = currentWordIdx;
+ inWordCursor = 0;
+ }
+ return new BrigadierParsedLine(words.get(wordIdx), inWordCursor, wordIdx, words, line, cursor);
+ }
+
+ record BrigadierParsedLine(String word, int wordCursor, int wordIndex, List<String> words, String line, int cursor) implements ParsedLine {
+ }
+}
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java --- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java