Fix completion logic to only see words pertaining to args

This commit is contained in:
Eric Bottard
2017-06-01 17:16:23 +02:00
parent 109094c48c
commit 85a57018e8
2 changed files with 10 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.shell2;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -78,4 +79,11 @@ public class CompletionContext {
String currentWord = currentWord();
return currentWord != null ? currentWord.substring(0, getPosition()) : null;
}
/**
* Return a copy of this context, as if the first {@literal nbWords} were not present
*/
public CompletionContext drop(int nbWords) {
return new CompletionContext(new ArrayList<String>(words.subList(nbWords, words.size())), wordIndex-nbWords, position);
}
}

View File

@@ -154,12 +154,13 @@ public class Shell implements CommandRegistry {
return candidates;
}
CompletionContext argsContext = context.drop(best.split(" ").length);
// Try to complete arguments
MethodTarget methodTarget = methodTargets.get(best);
Method method = methodTarget.getMethod();
return Arrays.stream(method.getParameters())
.map(Utils::createMethodParameter)
.flatMap(mp -> findResolver(mp).complete(mp, context).stream())
.flatMap(mp -> findResolver(mp).complete(mp, argsContext).stream())
.collect(Collectors.toList());
}