@@ -65,14 +65,6 @@ public boolean registerArgumentTypeProcessor(Class<?> clazz, ArgumentTypeProcess
6565 return true ;
6666 }
6767
68- public void unregisterCommand (String name ) {
69- commandMap .remove (name .toLowerCase ());
70- }
71-
72- public void unregisterArgumentTypeProcessor (Class <?> clazz ) {
73- argumentTypeProcessorMap .remove (clazz );
74- }
75-
7668 public List <String > tabComplete (T commandSender , String cmd , String [] args ) {
7769 List <String > list = new ArrayList <>();
7870 CommandProcessor command = commandMap .get (cmd .toLowerCase ());
@@ -89,44 +81,51 @@ public List<String> tabComplete(T commandSender, String cmd, String[] args) {
8981 }
9082
9183 SubCommand subCommand = method .getAnnotation (SubCommand .class );
84+ extractSuggestions (commandSender , method , subCommand , argsTogether , list );
85+ }
9286
93- for (String name : subCommand .names ()) {
87+ return list ;
88+ }
9489
95- if (argsTogether .toLowerCase ().startsWith (name .toLowerCase ()) || argsTogether .equalsIgnoreCase (name )) {
90+ private void extractSuggestions (T commandSender , Method method , SubCommand subCommand , String argsTogether , List <String > list ) {
91+ for (String name : subCommand .names ()) {
9692
97- String subString = argsTogether .substring (name .length ()).trim ();
98- String [] argsToCheck = subString .split (" " );
93+ if (!argsTogether .toLowerCase ().startsWith (name .toLowerCase ()) && !argsTogether .equalsIgnoreCase (name )) {
94+ if (name .startsWith (argsTogether )) {
95+ list .add (name );
96+ }
97+ continue ;
98+ }
9999
100- long params = Arrays .stream (method .getParameters ())
101- .filter (parameter -> parameter .isAnnotationPresent (Arg .class ))
102- .count ();
100+ String subString = argsTogether .substring (name .length ()).trim ();
101+ String [] argsToCheck = subString .split (" " );
103102
104- if ( params < argsToCheck . length ) {
105- continue ;
106- }
103+ long params = Arrays . stream ( method . getParameters ())
104+ . filter ( parameter -> parameter . isAnnotationPresent ( Arg . class ))
105+ . count ();
107106
108- Parameter parameter = method .getParameters ()[argsToCheck .length ];
107+ if (params < argsToCheck .length ) {
108+ continue ;
109+ }
109110
110- if (parameter .isAnnotationPresent (Arg .class )) {
111- ArgumentTypeProcessor <?> processor = argumentTypeProcessorMap .get (parameter .getType ());
112- if (processor != null ) {
113- List <String > result = processor .tabComplete (commandSender , argsToCheck [argsToCheck .length - 1 ]);
114- if (result != null ) {
115- list .addAll (result );
116- } else {
117- Arg arg = parameter .getAnnotation (Arg .class );
118- list .add (arg .name ());
119- }
120- }
121- }
111+ Parameter parameter = method .getParameters ()[argsToCheck .length ];
122112
123- } else if (name .startsWith (argsTogether )) {
124- list .add (name );
113+ if (parameter .isAnnotationPresent (Arg .class )) {
114+ ArgumentTypeProcessor <?> processor = argumentTypeProcessorMap .get (parameter .getType ());
115+ if (processor == null ) {
116+ continue ;
117+ }
118+
119+ List <String > result = processor .tabComplete (commandSender , argsToCheck [argsToCheck .length - 1 ]);
120+ if (result != null ) {
121+ list .addAll (result );
122+ } else {
123+ Arg arg = parameter .getAnnotation (Arg .class );
124+ list .add (arg .name ());
125125 }
126126 }
127- }
128127
129- return list ;
128+ }
130129 }
131130
132131 public boolean onCommand (T commandSender , String cmd , String [] args ) {
@@ -257,43 +256,6 @@ private Method findMatchingSubCommand(CommandProcessor command, String[] args) {
257256 return null ;
258257 }
259258
260- private List <Method > findMatchingSubCommands (CommandProcessor commandProcessor , String [] args ) {
261- List <Method > methods = new ArrayList <>();
262- for (Method method : commandProcessor .getClass ().getDeclaredMethods ()) {
263- if (!method .isAnnotationPresent (SubCommand .class )) {
264- continue ;
265- }
266-
267- SubCommand subCommand = method .getAnnotation (SubCommand .class );
268- if (subCommand .names ().length == 0 ) {
269- continue ;
270- }
271-
272- String availablePrefix = namesCheckTabComplete (subCommand .names (), args );
273- if (availablePrefix == null ) {
274- continue ;
275- }
276-
277- long requiredParameters = Arrays .stream (method .getParameters ())
278- .filter (parameter -> {
279- if (parameter .isAnnotationPresent (Arg .class )) {
280- Arg arg = parameter .getAnnotation (Arg .class );
281- return arg .required ();
282- }
283- return false ;
284- }).count ();
285-
286- String [] extracted = extractArguments (method , args , String .join (" " , args ));
287-
288- if (requiredParameters > extracted .length ) {
289- continue ;
290- }
291-
292- methods .add (method );
293- }
294- return methods ;
295- }
296-
297259 private String [] extractArguments (Method method , String [] args , String argsTogether ) {
298260 if (args .length == 0 || argsTogether .isBlank ()) {
299261 return new String [0 ];
@@ -356,18 +318,6 @@ private String namesCheck(String[] names, String[] args) {
356318 return null ;
357319 }
358320
359- private String namesCheckTabComplete (String [] names , String [] args ) {
360- List <String > list = new ArrayList <>();
361- String mergedArgs = String .join (" " , args );
362-
363- for (String name : names ) {
364- if (name .toLowerCase ().startsWith (mergedArgs .toLowerCase ())) {
365- return mergedArgs ;
366- }
367- }
368- return null ;
369- }
370-
371321 private void invokeMethod (Method method , CommandProcessor instance , T commandSender , Object ... args ) {
372322 try {
373323 if (args == null || args .length == 0 ) {
0 commit comments