diff options
Diffstat (limited to 'azalea-brigadier/src/context/command_context_builder.rs')
| -rwxr-xr-x | azalea-brigadier/src/context/command_context_builder.rs | 39 |
1 files changed, 38 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> { |
