Skip to main content

luaur_analysis/records/
nearest_likely_block_finder.rs

1use luaur_ast::records::ast_stat_block::AstStatBlock;
2use luaur_ast::records::ast_visitor::AstVisitor;
3
4#[derive(Debug, Clone)]
5pub struct NearestLikelyBlockFinder {
6    pub stmt_block_recent_ast: *mut AstStatBlock,
7    pub found: Option<*mut AstStatBlock>,
8}
9
10impl NearestLikelyBlockFinder {
11    pub fn nearest_likely_block_finder(stmt_block_recent_ast: *mut AstStatBlock) -> Self {
12        Self {
13            stmt_block_recent_ast,
14            found: None,
15        }
16    }
17}
18
19// C++ `struct NearestLikelyBlockFinder : public AstVisitor` with a single
20// `bool visit(AstStatBlock* block)` override (FragmentAutocomplete.cpp). Driven
21// via `stale->visit(&lsf)` so the full AST is traversed and every nested block
22// is considered; all other node visits fall through to the default (recurse).
23impl AstVisitor for NearestLikelyBlockFinder {
24    fn visit_stat_block(&mut self, node: *mut core::ffi::c_void) -> bool {
25        let block = node as *mut AstStatBlock;
26        unsafe {
27            let block_location = (*block).base.base.location;
28            let recent_location = (*self.stmt_block_recent_ast).base.base.location;
29            if block_location.begin <= recent_location.begin {
30                if let Some(found) = self.found {
31                    let found_location = (*found).base.base.location;
32                    if found_location.begin < block_location.begin {
33                        self.found = Some(block);
34                    }
35                } else {
36                    self.found = Some(block);
37                }
38            }
39        }
40        true
41    }
42}