#3226: Add console command name tab completion

This commit is contained in:
Outfluencer 2022-01-02 10:13:48 +11:00 committed by md_5
parent 6cbd7404f4
commit 9c95d4ba43
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11

View File

@ -2,6 +2,9 @@ package net.md_5.bungee.command;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import jline.console.completer.Completer; import jline.console.completer.Completer;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
@ -14,12 +17,22 @@ public class ConsoleCommandCompleter implements Completer
@Override @Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) public int complete(String buffer, int cursor, List<CharSequence> candidates)
{
int lastSpace = buffer.lastIndexOf( ' ' );
if ( lastSpace == -1 )
{
String lowerCase = buffer.toLowerCase( Locale.ROOT );
candidates.addAll( proxy.getPluginManager().getCommands().stream()
.map( Map.Entry::getKey )
.filter( (name) -> name.toLowerCase( Locale.ROOT ).startsWith( lowerCase ) )
.collect( Collectors.toList() ) );
} else
{ {
List<String> suggestions = new ArrayList<>(); List<String> suggestions = new ArrayList<>();
proxy.getPluginManager().dispatchCommand( proxy.getConsole(), buffer, suggestions ); proxy.getPluginManager().dispatchCommand( proxy.getConsole(), buffer, suggestions );
candidates.addAll( suggestions ); candidates.addAll( suggestions );
}
int lastSpace = buffer.lastIndexOf( ' ' );
return ( lastSpace == -1 ) ? cursor - buffer.length() : cursor - ( buffer.length() - lastSpace - 1 ); return ( lastSpace == -1 ) ? cursor - buffer.length() : cursor - ( buffer.length() - lastSpace - 1 );
} }
} }