Skip to content

Conversation

@willkroboth
Copy link

Right now, requirements are not checked when generating suggestions. This means that the following test fails:

// Added to `CommandSuggestionsTest`
@Test
public void getCompletionSuggestions_unmetRequirement() throws Exception {
    subject.register(
        literal("parent")
            .then(
                literal("invalid").requires(o -> false)
            )
            .then(
                literal("valid")
            )
    );

    final ParseResults<Object> parse = subject.parse("parent ", source);
    final Suggestions result = subject.getCompletionSuggestions(parse).join();

    // java.lang.AssertionError: 
    //  Expected: <[Suggestion{range=StringRange{start=7, end=7}, text='valid', tooltip='null'}]>
    //  but: was <[Suggestion{range=StringRange{start=7, end=7}, text='invalid', tooltip='null'}, 
    //  Suggestion{range=StringRange{start=7, end=7}, text='valid', tooltip='null'}]>
    assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(7), "valid"))));
}

Since the invalid literal was given the requirement o -> false, the source cannot execute that node or any of its children. However, invalid is still given as a suggestion. Since the source is not allowed to use this node, it should not have it suggested, since that confusingly suggests it could work.

This PR fixes this issue by ignoring a node's suggestions if the source does not meet its requirements.

@sakurawald
Copy link

Look good to me. This pull-request works perfect.

Currently, the results of /help <cmd> and client-side tab completion are in-consistent.
The /help command calls the ComamndNode#canUse, while the required argument with custom suggestions provider will not call the CommandNode#canUse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants