Skip to content

Commit

Permalink
skip extra whitespace in flag autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Sep 22, 2024
1 parent b3401a2 commit 936d343
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private List<String> complete(ExecutableCommand<A> possible, MutableStringStream
List<ParameterNode<A, Object>> flags = filter(possible.parameters().values(), c -> c.isFlag() || c.isSwitch());
while (input.hasRemaining()) {
if (input.peek() == ' ')
input.moveForward();
input.skipWhitespace();
String next = input.peekUnquotedString();
if (next.startsWith(LONG_FORMAT_PREFIX)) {
String flagName = next.substring(LONG_FORMAT_PREFIX.length());
Expand All @@ -185,7 +185,7 @@ private List<String> complete(ExecutableCommand<A> possible, MutableStringStream
if (input.hasFinished())
return List.of();
if (input.hasRemaining() && input.peek() == ' ') {
input.moveForward();
input.skipWhitespace();
}
if (input.hasFinished() && parameter != null) {
return List.copyOf(parameter.suggestions().getSuggestions(context));
Expand All @@ -208,7 +208,7 @@ private List<String> complete(ExecutableCommand<A> possible, MutableStringStream
continue;
tryParseFlag(parameter, input, context);
if (input.hasRemaining() && input.peek() == ' ') {
input.moveForward();
input.skipWhitespace();
return List.copyOf(parameter.suggestions().getSuggestions(context));
} else if (input.hasFinished()) {
return flags.stream().map(f -> {
Expand Down Expand Up @@ -256,8 +256,7 @@ else if (child instanceof ParameterNode<A, ?> p)
return List.of();
}

private @Nullable ParameterNode<A, Object> removeParameterWithShorthand
(List<ParameterNode<A, Object>> parametersLeft, char c) {
private @Nullable ParameterNode<A, Object> removeParameterWithShorthand(List<ParameterNode<A, Object>> parametersLeft, char c) {
for (Iterator<ParameterNode<A, Object>> iterator = parametersLeft.iterator(); iterator.hasNext(); ) {
ParameterNode<A, Object> value = iterator.next();
Character shorthand = value.shorthand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public interface MutableStringStream extends StringStream {
*/
void moveForward();

/**
* Skips all the upcoming whitespace
*/
void skipWhitespace();

/**
* Moves the cursor 1 step backward
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public void moveForward() {
moveForward(1);
}

@Override public void skipWhitespace() {
while (hasRemaining() && peek() == ' ')
moveForward();
}

public void moveBackward() {
moveBackward(1);
}
Expand Down

0 comments on commit 936d343

Please sign in to comment.