@@ -487,30 +487,42 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
487
487
Browse_tree. all_occurrences_of_prefix ~strict_prefix: true path node in
488
488
let paths = List. concat_map ~f: snd paths in
489
489
let leftmost_ident = Longident. flatten longident |> List. hd in
490
- let rec path_to_string acc (p : Path.t ) =
491
- match p with
492
- | Pident ident ->
493
- String. concat ~sep: " ." (Ident. name ident :: acc)
494
- | Pdot (path', s) when
495
- mode = `Unqualify && Path. same path path' ->
496
- String. concat ~sep: " ." (s :: acc)
497
- | Pdot (path', s) when
498
- mode = `Qualify && s = leftmost_ident ->
499
- String. concat ~sep: " ." (s :: acc)
500
- | Pdot (path' , s ) ->
501
- path_to_string (s :: acc) path'
502
- | _ -> raise Not_found
490
+ let qual_or_unqual_path p =
491
+ let rec aux acc (p : Path.t ) =
492
+ match p with
493
+ | Pident ident ->
494
+ Ident. name ident :: acc
495
+ | Pdot (path', s) when
496
+ mode = `Unqualify && Path. same path path' ->
497
+ s :: acc
498
+ | Pdot (path', s) when
499
+ mode = `Qualify && s = leftmost_ident ->
500
+ s :: acc
501
+ | Pdot (path' , s ) ->
502
+ aux (s :: acc) path'
503
+ | _ -> raise Not_found
504
+ in
505
+ aux [] p |> String. concat ~sep: " ."
506
+ in
507
+ (* checks if the (un)qualified longident has a different length, i.e., has changed
508
+
509
+ XXX(Ulugbek): computes longident length using [loc_start] and [loc_end], hence
510
+ it doesn't work for multiline longidents because we can't compute their length *)
511
+ let same_longident new_lident { Location. loc_start; loc_end; _ } =
512
+ let old_longident_len = Lexing. column loc_end - Lexing. column loc_start in
513
+ loc_start.Lexing. pos_lnum = loc_end.Lexing. pos_lnum &&
514
+ String. length new_lident = old_longident_len
503
515
in
504
516
List. filter_map paths ~f: (fun {Location. txt = path ; loc} ->
505
517
if not loc.Location. loc_ghost &&
506
518
Location_aux. compare_pos pos loc < = 0 then
507
- try Some (path_to_string [] path, loc)
508
- with Not_found -> None
519
+ match qual_or_unqual_path path with
520
+ | s when same_longident s loc -> None
521
+ | s -> Some (s, loc)
522
+ | exception Not_found -> None
509
523
else None
510
524
)
511
- |> List. sort_uniq
512
- ~cmp: (fun (_ ,l1 ) (_ ,l2 ) ->
513
- Lexing. compare_pos l1.Location. loc_start l2.Location. loc_start)
525
+ |> List. sort_uniq ~cmp: (fun (_ ,l1 ) (_ ,l2 ) -> Location_aux. compare l1 l2)
514
526
end
515
527
516
528
| Document (patho , pos ) ->
@@ -815,24 +827,22 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
815
827
let typer = Mpipeline. typer_result pipeline in
816
828
let str = Mbrowse. of_typedtree (Mtyper. get_typedtree typer) in
817
829
let pos = Mpipeline. get_lexing_pos pipeline pos in
818
- let tnode =
819
- let should_ignore_tnode = function
830
+ let enclosing = Mbrowse. enclosing pos [str] in
831
+ let curr_node =
832
+ let is_wildcard_pat = function
820
833
| Browse_raw. Pattern {pat_desc = Typedtree. Tpat_any ; _} -> true
821
834
| _ -> false
822
835
in
823
- let rec find = function
824
- | [] -> Browse_tree. dummy
825
- | (env , node )::rest ->
826
- if should_ignore_tnode node
827
- then find rest
828
- else Browse_tree. of_node ~env node
829
- in
830
- find (Mbrowse. enclosing pos [str])
836
+ List. find_some enclosing ~f: (fun (_ , node ) ->
837
+ (* it doesn't make sense to find occurrences of a wildcard pattern *)
838
+ not (is_wildcard_pat node))
839
+ |> Option. map ~f: (fun (env , node ) -> Browse_tree. of_node ~env node)
840
+ |> Option. value ~default: Browse_tree. dummy
831
841
in
832
842
let str = Browse_tree. of_browse str in
833
843
let get_loc {Location. txt = _ ; loc} = loc in
834
844
let ident_occurrence () =
835
- let paths = Browse_raw. node_paths tnode .Browse_tree. t_node in
845
+ let paths = Browse_raw. node_paths curr_node .Browse_tree. t_node in
836
846
let under_cursor p = Location_aux. compare_pos pos (get_loc p) = 0 in
837
847
Logger. log ~section: " occurrences" ~title: " Occurrences paths" " %a"
838
848
Logger. json (fun () ->
@@ -855,13 +865,14 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
855
865
let loc (_t ,paths ) = List. map ~f: get_loc paths in
856
866
List. concat_map ~f: loc ts
857
867
858
- and constructor_occurrence d =
859
- let ts = Browse_tree. all_constructor_occurrences (tnode,d) str in
868
+ in
869
+ let constructor_occurrence d =
870
+ let ts = Browse_tree. all_constructor_occurrences (curr_node,d) str in
860
871
List. map ~f: get_loc ts
861
872
862
873
in
863
874
let locs =
864
- match Browse_raw. node_is_constructor tnode .Browse_tree. t_node with
875
+ match Browse_raw. node_is_constructor curr_node .Browse_tree. t_node with
865
876
| Some d -> constructor_occurrence d.Location. txt
866
877
| None -> ident_occurrence ()
867
878
in
0 commit comments