Skip to main content

luaur_analysis/functions/
find_binding_local_statement.rs

1use crate::functions::find_ast_ancestry_of_position_ast_query::find_ast_ancestry_of_position_source_module_position_bool;
2use crate::records::binding::Binding;
3use crate::records::source_module::SourceModule;
4use luaur_ast::records::ast_node::AstNode;
5use luaur_ast::records::ast_stat_local::AstStatLocal;
6use luaur_ast::records::location::Location;
7use luaur_ast::records::position::Position;
8use luaur_ast::rtti::ast_node_as;
9
10pub fn find_binding_local_statement(
11    source: &SourceModule,
12    binding: &Binding,
13) -> Option<*mut AstStatLocal> {
14    if binding.location.begin == Position::new(0, 0) && binding.location.end == Position::new(0, 0)
15    {
16        return None;
17    }
18
19    let nodes = find_ast_ancestry_of_position_source_module_position_bool(
20        source,
21        binding.location.begin,
22        false,
23    );
24
25    let mut iter = nodes.iter().rev();
26    while let Some(&node) = iter.next() {
27        let stat_local = unsafe { ast_node_as::<AstStatLocal>(node as *mut AstNode) };
28        if !stat_local.is_null() {
29            return Some(stat_local);
30        }
31    }
32
33    None
34}