Skip to content

Commit

Permalink
Cleanup signature of Locate.from_string
Browse files Browse the repository at this point in the history
  • Loading branch information
liam923 committed Sep 26, 2024
1 parent 7e2575a commit 2021712
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/analysis/locate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ type result = {
approximated: bool;
}

module Namespace_resolution = struct
type t =
| From_context of Query_protocol.Locate_context.t
| Explicit of Env_lookup.Namespace.inferred_basic list
| Inferred
end

module File : sig
type t = private
| ML of string
Expand Down Expand Up @@ -864,19 +871,20 @@ let infer_namespace ?let_pun_behavior ?namespaces ~pos lid browse is_label =
"dropping inferred context, it is not precise enough";
`Ok [ `Labels ]

let from_string ~config ~env ~local_defs ~pos ~context ?let_pun_behavior ?namespaces path =
let from_string ~config ~env ~local_defs ~pos ?let_pun_behavior ?(namespaces = Namespace_resolution.Inferred) path =
File_switching.reset ();
let browse = Mbrowse.of_typedtree local_defs in
let lid = Type_utils.parse_longident path in
let from_lid lid =
let ident, is_label = Longident.keep_suffix lid in
let namespaces =
match context with
| Some ctxt ->
match namespaces with
| From_context ctxt ->
let ctxt = Context.of_locate_context ctxt in
log ~title:"from_string" "overrode context: %s" (Context.to_string ctxt);
`Ok (Env_lookup.Namespace.from_context ctxt)
| None -> infer_namespace ?let_pun_behavior ?namespaces ~pos lid browse is_label
| Explicit namespaces -> infer_namespace ?let_pun_behavior ~namespaces ~pos lid browse is_label
| Inferred -> infer_namespace ?let_pun_behavior ?namespaces:None ~pos lid browse is_label
in
match namespaces with
| `Error e -> e
Expand Down Expand Up @@ -965,7 +973,7 @@ let get_doc ~config:mconfig ~env ~local_defs ~comments ~pos =
end
| `User_input path ->
log ~title:"get_doc" "looking for the doc of '%s'" path;
begin match from_string ~config ~env ~local_defs ~pos ~context:None path with
begin match from_string ~config ~env ~local_defs ~pos path with
| `Found { uid; location = loc; _ } ->
doc_from_uid ~config ~loc uid
| `At_origin ->
Expand Down
16 changes: 14 additions & 2 deletions src/analysis/locate.mli
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ type result = {
approximated: bool;
}

module Namespace_resolution : sig
type t =
| From_context of Query_protocol.Locate_context.t
(** Choose the namespaces based on a [Query_protocol.Locate_context.t] *)

| Explicit of Env_lookup.Namespace.inferred_basic list
(** Explicitly choose which namespaces to search in. The namespaces are prioritized
based on the list order (with the first element being highest priority) *)

| Inferred
(** Infer which namespaces to search in *)
end

val uid_of_result
: traverse_aliases:bool
-> Shape_reduce.result
Expand Down Expand Up @@ -79,9 +92,8 @@ val from_string
-> env:Env.t
-> local_defs:Mtyper.typedtree
-> pos:Lexing.position
-> context:Query_protocol.Locate_context.t option
-> ?let_pun_behavior:Mbrowse.Let_pun_behavior.t
-> ?namespaces:Env_lookup.Namespace.inferred_basic list
-> ?namespaces:Namespace_resolution.t
-> string
-> [> `File_not_found of string
| `Found of result
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/occurrences.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ let locs_of ~config ~env ~typer_result ~pos ~scope path =
let locate_result =
Locate.from_string
~config:{ mconfig = config; traverse_aliases=false; ml_or_mli = `ML}
~env ~local_defs ~pos ~context:None path
~env ~local_defs ~pos path
in
(* When we fail to find an exact definition we restrict scope to `Buffer *)
let def, scope =
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/query_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
traverse_aliases = true
}
in
begin match Locate.from_string ~config ~env ~local_defs ~pos ~context ~let_pun_behavior path with
let namespaces = Option.map context ~f:(fun ctx -> Locate.Namespace_resolution.From_context ctx) in
begin match Locate.from_string ~config ~env ~local_defs ~pos ?namespaces ~let_pun_behavior path with
| `Found { file; location; _ } ->
Locate.log ~title:"result"
"found: %s" file;
Expand Down

0 comments on commit 2021712

Please sign in to comment.