brigadier icon indicating copy to clipboard operation
brigadier copied to clipboard

Don't suggest nodes which the source does not have requirements for

Open willkroboth opened this issue 9 months ago • 1 comments

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.

willkroboth avatar Mar 24 '25 23:03 willkroboth

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.

sakurawald avatar Aug 19 '25 06:08 sakurawald