@@ -65,14 +65,6 @@ public boolean registerArgumentTypeProcessor(Class<?> clazz, ArgumentTypeProcess
65
65
return true ;
66
66
}
67
67
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
-
76
68
public List <String > tabComplete (T commandSender , String cmd , String [] args ) {
77
69
List <String > list = new ArrayList <>();
78
70
CommandProcessor command = commandMap .get (cmd .toLowerCase ());
@@ -89,44 +81,51 @@ public List<String> tabComplete(T commandSender, String cmd, String[] args) {
89
81
}
90
82
91
83
SubCommand subCommand = method .getAnnotation (SubCommand .class );
84
+ extractSuggestions (commandSender , method , subCommand , argsTogether , list );
85
+ }
92
86
93
- for (String name : subCommand .names ()) {
87
+ return list ;
88
+ }
94
89
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 ()) {
96
92
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
+ }
99
99
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 (" " );
103
102
104
- if ( params < argsToCheck . length ) {
105
- continue ;
106
- }
103
+ long params = Arrays . stream ( method . getParameters ())
104
+ . filter ( parameter -> parameter . isAnnotationPresent ( Arg . class ))
105
+ . count ();
107
106
108
- Parameter parameter = method .getParameters ()[argsToCheck .length ];
107
+ if (params < argsToCheck .length ) {
108
+ continue ;
109
+ }
109
110
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 ];
122
112
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 ());
125
125
}
126
126
}
127
- }
128
127
129
- return list ;
128
+ }
130
129
}
131
130
132
131
public boolean onCommand (T commandSender , String cmd , String [] args ) {
@@ -257,43 +256,6 @@ private Method findMatchingSubCommand(CommandProcessor command, String[] args) {
257
256
return null ;
258
257
}
259
258
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
-
297
259
private String [] extractArguments (Method method , String [] args , String argsTogether ) {
298
260
if (args .length == 0 || argsTogether .isBlank ()) {
299
261
return new String [0 ];
@@ -356,18 +318,6 @@ private String namesCheck(String[] names, String[] args) {
356
318
return null ;
357
319
}
358
320
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
-
371
321
private void invokeMethod (Method method , CommandProcessor instance , T commandSender , Object ... args ) {
372
322
try {
373
323
if (args == null || args .length == 0 ) {
0 commit comments