aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/context
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/context
parentf505ace721d4c214cd32143febd0a066b6b95ce5 (diff)
downloadazalea-drasl-79ad1e93bf6ce2b7c2da6925a7c85b33bb76f154.tar.xz
brigadier suggestions
closes #109
Diffstat (limited to 'azalea-brigadier/src/context')
-rwxr-xr-xazalea-brigadier/src/context/command_context_builder.rs39
-rwxr-xr-xazalea-brigadier/src/context/mod.rs1
-rw-r--r--azalea-brigadier/src/context/suggestion_context.rs11
3 files changed, 50 insertions, 1 deletions
diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs
index 99c40dac..75295825 100755
--- a/azalea-brigadier/src/context/command_context_builder.rs
+++ b/azalea-brigadier/src/context/command_context_builder.rs
@@ -2,7 +2,7 @@ use parking_lot::RwLock;
use super::{
command_context::CommandContext, parsed_command_node::ParsedCommandNode,
- string_range::StringRange, ParsedArgument,
+ string_range::StringRange, suggestion_context::SuggestionContext, ParsedArgument,
};
use crate::{
command_dispatcher::CommandDispatcher,
@@ -99,6 +99,43 @@ impl<'a, S> CommandContextBuilder<'a, S> {
input: input.to_string(),
}
}
+
+ pub fn find_suggestion_context(&self, cursor: usize) -> SuggestionContext<S> {
+ if self.range.start() > cursor {
+ panic!("Can't find node before cursor");
+ }
+
+ if self.range.end() < cursor {
+ if let Some(child) = &self.child {
+ child.find_suggestion_context(cursor)
+ } else if let Some(last) = self.nodes.last() {
+ SuggestionContext {
+ parent: Arc::clone(&last.node),
+ start_pos: last.range.end() + 1,
+ }
+ } else {
+ SuggestionContext {
+ parent: Arc::clone(&self.root),
+ start_pos: self.range.start(),
+ }
+ }
+ } else {
+ let mut prev = &self.root;
+ for node in &self.nodes {
+ if node.range.start() <= cursor && cursor <= node.range.end() {
+ return SuggestionContext {
+ parent: Arc::clone(prev),
+ start_pos: node.range.start(),
+ };
+ }
+ prev = &node.node;
+ }
+ SuggestionContext {
+ parent: Arc::clone(prev),
+ start_pos: self.range.start(),
+ }
+ }
+ }
}
impl<S> Debug for CommandContextBuilder<'_, S> {
diff --git a/azalea-brigadier/src/context/mod.rs b/azalea-brigadier/src/context/mod.rs
index d535602a..28e1a12e 100755
--- a/azalea-brigadier/src/context/mod.rs
+++ b/azalea-brigadier/src/context/mod.rs
@@ -3,6 +3,7 @@ mod command_context_builder;
mod parsed_argument;
mod parsed_command_node;
mod string_range;
+pub mod suggestion_context;
pub use command_context::CommandContext;
pub use command_context_builder::CommandContextBuilder;
diff --git a/azalea-brigadier/src/context/suggestion_context.rs b/azalea-brigadier/src/context/suggestion_context.rs
new file mode 100644
index 00000000..58a73fdb
--- /dev/null
+++ b/azalea-brigadier/src/context/suggestion_context.rs
@@ -0,0 +1,11 @@
+use std::sync::Arc;
+
+use parking_lot::RwLock;
+
+use crate::tree::CommandNode;
+
+#[derive(Debug)]
+pub struct SuggestionContext<S> {
+ pub parent: Arc<RwLock<CommandNode<S>>>,
+ pub start_pos: usize,
+}