aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/command_dispatcher.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-10-12 22:01:15 -0500
committermat <git@matdoes.dev>2023-10-12 22:01:15 -0500
commit79ad1e93bf6ce2b7c2da6925a7c85b33bb76f154 (patch)
tree19004abf51cb19eb3b5cc13c61d670889cb228a0 /azalea-brigadier/src/command_dispatcher.rs
parentf505ace721d4c214cd32143febd0a066b6b95ce5 (diff)
downloadazalea-drasl-79ad1e93bf6ce2b7c2da6925a7c85b33bb76f154.tar.xz
brigadier suggestions
closes #109
Diffstat (limited to 'azalea-brigadier/src/command_dispatcher.rs')
-rwxr-xr-xazalea-brigadier/src/command_dispatcher.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs
index 0ec07a54..3b5987fc 100755
--- a/azalea-brigadier/src/command_dispatcher.rs
+++ b/azalea-brigadier/src/command_dispatcher.rs
@@ -6,6 +6,7 @@ use crate::{
exceptions::{BuiltInExceptions, CommandSyntaxException},
parse_results::ParseResults,
string_reader::StringReader,
+ suggestion::{Suggestions, SuggestionsBuilder},
tree::CommandNode,
};
use std::{
@@ -474,6 +475,41 @@ impl<S> CommandDispatcher<S> {
Some(this)
}
+
+ pub fn get_completion_suggestions(parse: ParseResults<S>) -> Suggestions {
+ let cursor = parse.reader.total_length();
+ Self::get_completion_suggestions_with_cursor(parse, cursor)
+ }
+
+ pub fn get_completion_suggestions_with_cursor(
+ parse: ParseResults<S>,
+ cursor: usize,
+ ) -> Suggestions {
+ let context = parse.context;
+
+ let node_before_cursor = context.find_suggestion_context(cursor);
+ let parent = node_before_cursor.parent;
+ let start = usize::min(node_before_cursor.start_pos, cursor);
+
+ let full_input = parse.reader.string();
+ let truncated_input = full_input[..cursor].to_string();
+ let truncated_input_lowercase = truncated_input.to_lowercase();
+
+ let mut all_suggestions = Vec::new();
+ for node in parent.read().children.values() {
+ let suggestions = node.read().list_suggestions(
+ context.build(&truncated_input),
+ SuggestionsBuilder::new_with_lowercase(
+ &truncated_input,
+ &truncated_input_lowercase,
+ start,
+ ),
+ );
+ all_suggestions.push(suggestions);
+ }
+
+ Suggestions::merge(full_input, &all_suggestions)
+ }
}
impl<S> Default for CommandDispatcher<S> {