Skip to main content

luaur_analysis/functions/
is_within_comment_module.rs

1use luaur_ast::records::comment::Comment;
2use luaur_ast::records::lexeme::Type;
3use luaur_ast::records::location::Location;
4use luaur_ast::records::position::Position;
5
6pub fn is_within_comment(comment_locations: &alloc::vec::Vec<Comment>, pos: Position) -> bool {
7    // Build a sentinel Comment to use with lower_bound
8    let sentinel = Comment {
9        r#type: Type::Comment,
10        location: Location::new(pos, pos),
11    };
12
13    // Find the first comment whose end is >= pos
14    let iter = comment_locations.iter().position(|c| {
15        if c.r#type == Type::Comment {
16            c.location.end.line >= pos.line
17        } else {
18            c.location.end >= pos
19        }
20    });
21
22    if let Some(idx) = iter {
23        if contains(pos, comment_locations[idx]) {
24            return true;
25        }
26
27        // Try the next comment, if it exists
28        let idx = idx + 1;
29        if idx < comment_locations.len() && contains(pos, comment_locations[idx]) {
30            return true;
31        }
32    }
33
34    false
35}
36
37fn contains(pos: Position, comment: Comment) -> bool {
38    if comment.location.contains(pos) {
39        return true;
40    } else if comment.r#type == Type::BrokenComment && comment.location.begin <= pos {
41        return true;
42    } else if comment.r#type == Type::Comment
43        && comment.location.end.line == pos.line
44        && comment.location.begin <= pos
45    {
46        return true;
47    } else {
48        return false;
49    }
50}
51
52// Pinned overload name advertised by the dependency cards.
53#[allow(unused_imports, non_snake_case)]
54pub use is_within_comment as is_within_comment_vector_comment_position;