Skip to content

Commit

Permalink
fix string stream not reflecting real changes in tab completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Dec 11, 2024
1 parent d60ef90 commit adf3b44
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class SingleCommandCompleter<A extends CommandActor> {
public SingleCommandCompleter(A actor, ExecutableCommand<A> command, MutableStringStream input) {
this.command = command;
this.input = input;
this.context = ExecutionContext.createMutable(command, actor, input.toImmutableCopy());
this.context = ExecutionContext.createMutable(command, actor, input.toImmutableView());
}

private void rememberPosition() {
Expand Down Expand Up @@ -84,7 +84,7 @@ private CompletionResult completeParameter(@NotNull ParameterNode<A, Object> par
context.addResolvedArgument(parameter.name(), value);
int positionAfterParsing = input.position();
String consumed = restorePosition();
Collection<String> parameterSuggestions = parameter.complete(context.actor(), input, context);
Collection<String> parameterSuggestions = parameter.complete(context);
input.setPosition(positionAfterParsing); // restore so that we can move forward

if (input.hasFinished()) {
Expand All @@ -96,7 +96,7 @@ private CompletionResult completeParameter(@NotNull ParameterNode<A, Object> par
return CompletionResult.CONTINUE;
} catch (Throwable t) {
String consumed = restorePosition();
filterSuggestions(consumed, parameter.complete(context.actor(), input, context));
filterSuggestions(consumed, parameter.complete(context));
return CompletionResult.HALT;
}
}
Expand All @@ -123,7 +123,7 @@ private void completeFlags(@NotNull Map<String, ParameterNode<A, Object>> remain
if (input.hasFinished())
return;
if (input.remaining() == 1 && input.peek() == ' ') {
Collection<String> parameterSuggestions = targetFlag.complete(context.actor(), input, context);
Collection<String> parameterSuggestions = targetFlag.complete(context);
suggestions.addAll(parameterSuggestions);
return;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ private void completeFlags(@NotNull Map<String, ParameterNode<A, Object>> remain
if (targetFlag.isSwitch())
continue;
if (input.remaining() == 1 && input.peek() == ' ') {
Collection<String> parameterSuggestions = targetFlag.complete(context.actor(), input, context);
Collection<String> parameterSuggestions = targetFlag.complete(context);
suggestions.addAll(parameterSuggestions);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,11 @@ default boolean isRequired() {
/**
* Provides suggestions for the given user input.
*
* @param actor The actor to generate for
* @param input The command input
* @param context The execution context.
* @return The
*/
@Contract(pure = true)
@NotNull Collection<String> complete(A actor, @NotNull StringStream input, @NotNull ExecutionContext<A> context);
@NotNull Collection<String> complete(@NotNull ExecutionContext<A> context);

/**
* Returns the parameter Java type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public boolean isOptional() {
}

@Override
public @NotNull Collection<String> complete(@NotNull A actor, @NotNull StringStream input, @NotNull ExecutionContext<A> context) {
public @NotNull Collection<String> complete(@NotNull ExecutionContext<A> context) {
return suggestions.getSuggestions(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@ public interface MutableStringStream extends StringStream {
@Contract(pure = true, value = "-> new")
MutableStringStream toMutableCopy();

/**
* Returns an immutable view of this {@link MutableStringStream}. A view is different
* from a copy, as it will reflect changes to the original string stream, but cannot
* be used to it.
*
* @return An immutable string stream view
*/
@NotNull
@Contract(pure = true)
@Unmodifiable
StringStream toImmutableView();

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import revxrsal.commands.exception.InputParseException;
import revxrsal.commands.exception.InvalidBooleanException;
import revxrsal.commands.exception.InvalidDecimalException;
Expand All @@ -34,6 +35,8 @@

public final class MutableStringStreamImpl extends BaseStringStream implements MutableStringStream {

private StringStreamView immutableView = null;

/**
* Creates a new {@link MutableStringStream} with its position at zero.
*
Expand Down Expand Up @@ -217,4 +220,81 @@ public boolean isMutable() {
return true;
}

@Override public @NotNull @Unmodifiable StringStream toImmutableView() {
if (immutableView == null)
immutableView = new StringStreamView();
return immutableView;
}

private class StringStreamView implements StringStream {

@Override public @NotNull String source() {
return MutableStringStreamImpl.this.source();
}

@Override public int totalSize() {
return MutableStringStreamImpl.this.totalSize();
}

@Override public int remaining() {
return MutableStringStreamImpl.this.remaining();
}

@Override public char peek() {
return MutableStringStreamImpl.this.peek();
}

@Override public String peek(int characters) {
return MutableStringStreamImpl.this.peek(characters);
}

@Override public char peekOffset(int offset) {
return MutableStringStreamImpl.this.peekOffset(offset);
}

@Override public boolean hasRemaining() {
return MutableStringStreamImpl.this.hasRemaining();
}

@Override public boolean hasFinished() {
return MutableStringStreamImpl.this.hasFinished();
}

@Override public boolean canRead(int characters) {
return MutableStringStreamImpl.this.canRead(characters);
}

@Override public int position() {
return MutableStringStreamImpl.this.position();
}

@Override public @NotNull String peekUnquotedString() {
return MutableStringStreamImpl.this.peekUnquotedString();
}

@Override public @NotNull String peekString() {
return MutableStringStreamImpl.this.peekString();
}

@Override public @NotNull String peekRemaining() {
return MutableStringStreamImpl.this.peekRemaining();
}

@Override public @NotNull @Unmodifiable StringStream toImmutableCopy() {
return MutableStringStreamImpl.this.toImmutableCopy();
}

@Override public @NotNull MutableStringStream toMutableCopy() {
return MutableStringStreamImpl.this.toMutableCopy();
}

@Override public boolean isMutable() {
return false;
}

@Override public boolean isEmpty() {
return MutableStringStreamImpl.this.isEmpty();
}
}

}

0 comments on commit adf3b44

Please sign in to comment.