From 79ad1e93bf6ce2b7c2da6925a7c85b33bb76f154 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 12 Oct 2023 22:01:15 -0500 Subject: brigadier suggestions closes #109 --- .../src/context/command_context_builder.rs | 39 +++++++++++++++++++++- azalea-brigadier/src/context/mod.rs | 1 + azalea-brigadier/src/context/suggestion_context.rs | 11 ++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 azalea-brigadier/src/context/suggestion_context.rs (limited to 'azalea-brigadier/src/context') 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 { + 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 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 { + pub parent: Arc>>, + pub start_pos: usize, +} -- cgit v1.2.3