Skip to content

Commit 3fb6b51

Browse files
Ted Spencefacebook-github-bot
authored andcommitted
[hack] Move expand_namespace to Utils.ml
Summary: Let's standardize the behavior of expanding namespaces using the namespace alias map. * Take the existing function in serverSignatureHelp.ml and move it into Utils.ml * Redo the function so it no longer has a dependency on ServerEnv * Make an alias under ServerEnv so that it's easy to call as long as you have a ServerEnv available * Add a set of unit tests so we can prove it's working the way we want Reviewed By: jewelpit Differential Revision: D17084358 fbshipit-source-id: cfc89b9b35aa9129a8116c62ecce52f57ee1364a
1 parent c7062e9 commit 3fb6b51

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

hack/utils/core/utils.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,27 @@ let split_ns_from_name (s : string) : string * string =
186186

187187
let double_colon = Str.regexp_string "::"
188188

189+
(* Expands a namespace using the namespace map, a list of (string, string) tuples
190+
* Ensures the beginning backslash is present
191+
*
192+
* "Str\\join" -> "\\HH\\Lib\\Str\\join" (when "Str", "HH\\Lib\\Str" is present in map)
193+
* "HH\\Lib\\Str\\Join" -> "\\HH\\Lib\\Str\\join"
194+
* "\\HH\\Lib\\Str\\Join" -> "\\HH\\Lib\\Str\\join"
195+
* "just_plain_func" -> "\\just_plain_func"
196+
*)
197+
let expand_namespace (ns_map : (string * string) list) (s : string) : string =
198+
let (raw_ns, name) = split_ns_from_name s in
199+
(* Might need left backslash *)
200+
let ns = add_ns raw_ns in
201+
let matching_alias =
202+
List.find ns_map (fun (alias, _) ->
203+
let fixup = add_ns alias ^ "\\" in
204+
fixup = ns)
205+
in
206+
match matching_alias with
207+
| None -> add_ns s
208+
| Some (_, expanded) -> add_ns (expanded ^ "\\" ^ name)
209+
189210
(*
190211
* "A::B" -> Some "A" * "B"
191212
* "::B" "A::" "Abc" -> None

hack/utils/core/utils.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ val strip_all_ns : string -> string
8181

8282
val split_ns_from_name : string -> string * string
8383

84+
val expand_namespace : (string * string) list -> string -> string
85+
8486
val split_class_from_method : string -> (string * string) option
8587

8688
val iter2_shortest : ('a -> 'b -> 'c) -> 'a list -> 'b list -> unit

0 commit comments

Comments
 (0)