Skip to content

Commit

Permalink
Use SpongeCommandDispatcher for JLine
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Oct 25, 2023
1 parent 22298c6 commit f1e5a61
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
*/
package org.spongepowered.vanilla.chat.console;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ImmutableStringReader;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.context.ParsedCommandNode;
import net.minecraft.commands.CommandSourceStack;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jline.reader.Highlighter;
import org.jline.reader.LineReader;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.command.brigadier.dispatcher.SpongeCommandDispatcher;

import java.util.function.Supplier;
import java.util.regex.Pattern;
Expand All @@ -53,29 +54,29 @@ public class BrigadierHighlighter<S> implements Highlighter {
.mapToObj(AttributedStyle.DEFAULT::foreground)
.toArray(AttributedStyle[]::new);

private final Supplier<@Nullable CommandDispatcher<S>> dispatcherProvider;
private final Supplier<S> commandSourceProvider;
private final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider;
private final Supplier<CommandSourceStack> commandSourceProvider;

public BrigadierHighlighter(final Supplier<@Nullable CommandDispatcher<S>> dispatcherProvider, final Supplier<S> commandSourceProvider) {
public BrigadierHighlighter(final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider, final Supplier<CommandSourceStack> commandSourceProvider) {
this.dispatcherProvider = dispatcherProvider;
this.commandSourceProvider = commandSourceProvider;
}

@Override
public AttributedString highlight(final LineReader lineReader, final String buffer) {
final CommandDispatcher<S> dispatcher = this.dispatcherProvider.get();
final SpongeCommandDispatcher dispatcher = this.dispatcherProvider.get();
if (dispatcher == null) {
return new AttributedString(buffer);
}

try {
final ParseResults<S> results = dispatcher.parse(buffer, this.commandSourceProvider.get());
final ParseResults<CommandSourceStack> results = dispatcher.parse(buffer, this.commandSourceProvider.get(), true);
final ImmutableStringReader reader = results.getReader();
final AttributedStringBuilder builder = new AttributedStringBuilder();

int lastPos = 0;
int argColorIdx = 0;
for (final ParsedCommandNode<S> node : results.getContext().getLastChild().getNodes()) {
for (final ParsedCommandNode<CommandSourceStack> node : results.getContext().getLastChild().getNodes()) {
// Sometimes Brigadier will spit out ranges that are invalid for the current input string????
final int start = Math.min(node.getRange().getStart(), reader.getTotalLength());
final int end = Math.min(node.getRange().getEnd(), reader.getTotalLength());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
*/
package org.spongepowered.vanilla.chat.console;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.suggestion.Suggestions;
import net.minecraft.commands.CommandSourceStack;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.command.brigadier.dispatcher.SpongeCommandDispatcher;

import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand All @@ -43,23 +44,23 @@

final class BrigadierJLineCompleter<S> implements Completer {

private final Supplier<@Nullable CommandDispatcher<S>> dispatcherProvider;
private final Supplier<S> commandSourceProvider;
private final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider;
private final Supplier<CommandSourceStack> commandSourceProvider;

public BrigadierJLineCompleter(final Supplier<@Nullable CommandDispatcher<S>> dispatcherProvider, final Supplier<S> commandSourceProvider) {
public BrigadierJLineCompleter(final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider, final Supplier<CommandSourceStack> commandSourceProvider) {
this.dispatcherProvider = dispatcherProvider;
this.commandSourceProvider = commandSourceProvider;
}

@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
final CommandDispatcher<S> dispatcher = this.dispatcherProvider.get();
final SpongeCommandDispatcher dispatcher = this.dispatcherProvider.get();
if (dispatcher == null) {
return;
}

final String input = line.line();
final ParseResults<S> parseResult = dispatcher.parse(input, this.commandSourceProvider.get());
final ParseResults<CommandSourceStack> parseResult = dispatcher.parse(input, this.commandSourceProvider.get(), true);
final CompletableFuture<Suggestions> suggestions = dispatcher.getCompletionSuggestions(
parseResult,
line.cursor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
*/
package org.spongepowered.vanilla.chat.console;

import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecrell.terminalconsole.SimpleTerminalConsole;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.spongepowered.common.command.brigadier.dispatcher.SpongeCommandDispatcher;
import org.spongepowered.common.command.manager.SpongeCommandManager;
import org.spongepowered.common.launch.Launch;

Expand All @@ -46,7 +46,7 @@ public VanillaConsole(DedicatedServer server) {

@Override
protected LineReader buildReader(LineReaderBuilder builder) {
final Supplier<@Nullable CommandDispatcher<CommandSourceStack>> dispatcherProvider = () -> {
final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider = () -> {
final SpongeCommandManager manager = SpongeCommandManager.get(this.server);
return manager == null ? null : manager.getDispatcher();
};
Expand Down

0 comments on commit f1e5a61

Please sign in to comment.