From 2814bb2b38f688b3a77fce61993a165b9879ea38 Mon Sep 17 00:00:00 2001 From: Ted Spence Date: Mon, 10 Jun 2019 12:46:32 -0700 Subject: [PATCH] Fix values at the beginning of a namespace Summary: During bug review, I discovered that namespace search does not produce a list of possible results if you have simply typed the name of a namespace. Reviewed By: 2BitSalute Differential Revision: D15716193 fbshipit-source-id: cccf1838aa14eb05a8f8a4cf2f6b67bec67191d7 --- hphp/hack/src/search/namespaceSearchService.ml | 23 ++++++++++++++--------- hphp/hack/test/integration/symbol_index_test.ml | 12 +++++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/hphp/hack/src/search/namespaceSearchService.ml b/hphp/hack/src/search/namespaceSearchService.ml index 8aaff55c8d4..a5e415db383 100644 --- a/hphp/hack/src/search/namespaceSearchService.ml +++ b/hphp/hack/src/search/namespaceSearchService.ml @@ -79,23 +79,26 @@ let find_exact_match (namespace: string): nss_node = ) end +(* Produce a list of results for the current node *) +let get_matches_for_node (node: nss_node): si_results = + Hashtbl.fold (fun _key value acc -> + { + si_name = value.nss_name; + si_kind = SI_Namespace; + si_filehash = 0L; + } :: acc + ) node.nss_children [] + (* Find all namespaces that match this prefix *) let find_matching_namespaces (query_text: string): si_results = (* Trivial case *) if query_text = "" then begin [] + (* Special case - just give root namespace only *) end else if query_text = "\\" then begin - - (* Play that fun _key music *) - Hashtbl.fold (fun _key value acc -> - { - si_name = value.nss_name; - si_kind = SI_Namespace; - si_filehash = 0L; - } :: acc - ) root_namespace.nss_children [] + get_matches_for_node root_namespace (* Normal case, search for matches *) end else begin @@ -116,7 +119,9 @@ let find_matching_namespaces (query_text: string): si_results = match Hashtbl.find_opt !current_node.nss_children leaf_name with | Some matching_leaf -> current_node := matching_leaf; + matches := get_matches_for_node matching_leaf; | None -> + matches := []; Hashtbl.iter (fun key _ -> if Core_kernel.String.is_substring key ~substring:leaf_name then begin let node = Hashtbl.find !current_node.nss_children key in diff --git a/hphp/hack/test/integration/symbol_index_test.ml b/hphp/hack/test/integration/symbol_index_test.ml index b234210c50c..751ce0e72a6 100644 --- a/hphp/hack/test/integration/symbol_index_test.ml +++ b/hphp/hack/test/integration/symbol_index_test.ml @@ -244,14 +244,20 @@ let test_namespace_map (harness: Test_harness.t): bool = let matches = find_matching_namespaces "" in IA.assert_equals 0 (List.length matches) "Empty string / zero matches"; - (* Special case: single backslash should show root namespaces *) + (* Special case: single backslash should show at least these root namespaces *) let matches = find_matching_namespaces "\\" in assert_ns_matches "Str" matches; assert_ns_matches "StrFb" matches; assert_ns_matches "HH" matches; - IA.assert_equals 3 (List.length matches) "Special case root namespace"; - true + (* Normal use case *) + Hh_logger.log "Reached the hh section"; + let matches = find_matching_namespaces "hh" in + assert_ns_matches "Lib" matches; + let matches = find_matching_namespaces "\\HH\\" in + assert_ns_matches "Lib" matches; + + true ;; (* Main test suite *) -- 2.11.4.GIT