Skip to main content

luaur_analysis/functions/
get_function_declaration_extents.rs

1use luaur_ast::records::ast_expr::AstExpr;
2use luaur_ast::records::ast_expr_function::AstExprFunction;
3use luaur_ast::records::ast_local::AstLocal;
4use luaur_ast::records::location::Location;
5
6pub fn get_function_declaration_extents(
7    expr_fn: *mut AstExprFunction,
8    expr_name: *mut AstExpr,
9    local_name: *mut AstLocal,
10) -> Location {
11    let fn_begin = unsafe { (*expr_fn).base.base.location.begin };
12    let mut fn_end = unsafe { (*expr_fn).base.base.location.end };
13
14    let return_annotation = unsafe { (*expr_fn).return_annotation };
15    if !return_annotation.is_null() {
16        fn_end = unsafe { (*return_annotation).base.location.end };
17    } else {
18        let args = unsafe { (*expr_fn).args };
19        if args.size != 0 {
20            let last = unsafe { *args.data.add(args.size as usize - 1) };
21            let annotation = unsafe { (*last).annotation };
22            if !annotation.is_null() {
23                fn_end = unsafe { (*annotation).base.location.end };
24            } else {
25                fn_end = unsafe { (*last).location.end };
26            }
27        } else {
28            let generic_packs = unsafe { (*expr_fn).generic_packs };
29            if generic_packs.size != 0 {
30                let last = unsafe { *generic_packs.data.add(generic_packs.size as usize - 1) };
31                fn_end = unsafe { (*last).base.location.end };
32            } else {
33                let generics = unsafe { (*expr_fn).generics };
34                if generics.size != 0 {
35                    let last = unsafe { *generics.data.add(generics.size as usize - 1) };
36                    fn_end = unsafe { (*last).base.location.end };
37                } else if !expr_name.is_null() {
38                    fn_end = unsafe { (*expr_name).base.location.end };
39                } else if !local_name.is_null() {
40                    fn_end = unsafe { (*local_name).location.end };
41                }
42            }
43        }
44    }
45
46    Location::new(fn_begin, fn_end)
47}