@@ -10,19 +10,24 @@ type stack_or_heap =
1010
1111type stack_or_heap_enclosings = (Location .t * stack_or_heap ) list
1212
13- let from_nodes ~pos ~path =
13+ let from_nodes ~lsp_compat ~ pos ~path =
1414 let [@ tail_mod_cons] rec tails = function
1515 | hd :: tl -> (hd, tl) :: tails tl
1616 | [] -> []
1717 in
18+ let cursor_is_inside ({ loc_start; loc_end; _ } : Location.t ) =
19+ Lexing. compare_pos pos loc_start > = 0 && Lexing. compare_pos pos loc_end < = 0
20+ in
1821 let aux node parent =
1922 let open Browse_raw in
20- let ret mode_result = Some (Mbrowse. node_loc node, mode_result) in
21- let ret_alloc alloc_mode = ret (Alloc_mode alloc_mode) in
22- let ret_no_alloc reason = ret (No_alloc { reason }) in
23- let ret_maybe_alloc reason = function
24- | Some alloc_mode -> ret_alloc alloc_mode
25- | None -> ret_no_alloc reason
23+ let ret ?(loc = Mbrowse. node_loc node) mode_result =
24+ Some (loc, mode_result)
25+ in
26+ let ret_alloc ?loc alloc_mode = ret ?loc (Alloc_mode alloc_mode) in
27+ let ret_no_alloc ?loc reason = ret ?loc (No_alloc { reason }) in
28+ let ret_maybe_alloc ?loc reason = function
29+ | Some alloc_mode -> ret_alloc ?loc alloc_mode
30+ | None -> ret_no_alloc ?loc reason
2631 in
2732 match (node, parent) with
2833 | ( Pattern { pat_desc = Tpat_var _; _ },
@@ -32,7 +37,7 @@ let from_nodes ~pos ~path =
3237 ) -> ret (Alloc_mode alloc_mode.mode)
3338 | Expression { exp_desc; _ } , _ -> (
3439 match exp_desc with
35- | Texp_function { alloc_mode; body; _ } ->
40+ | Texp_function { alloc_mode; body; _ } -> (
3641 let body_loc =
3742 (* A function expression is often in a non-obvious way the nearest enclosing
3843 allocating expression. To avoid confusion, we only consider a function
@@ -60,26 +65,26 @@ let from_nodes ~pos ~path =
6065 }
6166 | [] -> None )
6267 in
63- let cursor_is_inside_function_body =
64- match body_loc with
65- | None -> false
66- | Some { loc_start; loc_end; loc_ghost = _ } ->
67- Lexing. compare_pos pos loc_start > = 0
68- && Lexing. compare_pos pos loc_end < = 0
69- in
70- if cursor_is_inside_function_body then None
71- else ret (Alloc_mode alloc_mode.mode)
68+ match body_loc with
69+ | Some loc when cursor_is_inside loc -> None
70+ | _ -> ret (Alloc_mode alloc_mode.mode))
7271 | Texp_array (_ , _ , _ , alloc_mode ) -> ret (Alloc_mode alloc_mode.mode)
73- | Texp_construct (_ , { cstr_repr; _ } , args , maybe_alloc_mode ) -> (
72+ | Texp_construct
73+ ({ loc; txt = _lident }, { cstr_repr; _ }, args, maybe_alloc_mode)
74+ -> (
75+ let loc =
76+ if lsp_compat && cursor_is_inside loc then Some loc else None
77+ in
7478 match maybe_alloc_mode with
75- | Some alloc_mode -> ret (Alloc_mode alloc_mode.mode)
79+ | Some alloc_mode -> ret ?loc (Alloc_mode alloc_mode.mode)
7680 | None -> (
7781 match args with
78- | [] -> ret_no_alloc " constructor without arguments"
82+ | [] -> ret_no_alloc ?loc " constructor without arguments"
7983 | _ :: _ -> (
8084 match cstr_repr with
81- | Variant_unboxed -> ret_no_alloc " unboxed constructor"
82- | Variant_extensible | Variant_boxed _ -> ret Unexpected_no_alloc )))
85+ | Variant_unboxed -> ret_no_alloc ?loc " unboxed constructor"
86+ | Variant_extensible | Variant_boxed _ ->
87+ ret ?loc Unexpected_no_alloc )))
8388 | Texp_record { representation; alloc_mode = maybe_alloc_mode ; _ } -> (
8489 match (maybe_alloc_mode, representation) with
8590 | _ , Record_inlined _ -> None
0 commit comments