@@ -715,7 +715,10 @@ end = struct
715715end
716716
717717let uid_from_longident ~config ~env nss ml_or_mli ident =
718- let str_ident = String. concat ~sep: " ." (Longident. flatten ident) in
718+ let str_ident =
719+ try String. concat ~sep: " ." (Longident. flatten ident)
720+ with _ -> " Not a flat longident"
721+ in
719722 match Env_lookup. in_namespaces nss ident env with
720723 | None -> `Not_in_env str_ident
721724 | Some (path , namespace , decl_uid , loc ) ->
@@ -746,51 +749,55 @@ let from_path ~config ~env ~namespace ml_or_mli path =
746749 | `Found (file , loc ) -> `Found (uid, file, loc)
747750 | `File_not_found _ as otherwise -> otherwise
748751
752+ let infer_namespace ?namespaces ~pos lid browse is_label =
753+ match namespaces with
754+ | Some nss ->
755+ if not is_label
756+ then `Ok (nss :> Namespace.inferred list )
757+ else if List. mem `Labels ~set: nss then (
758+ log ~title: " from_string" " restricting namespaces to labels" ;
759+ `Ok [ `Labels ]
760+ ) else (
761+ log ~title: " from_string"
762+ " input is clearly a label, but the given namespaces don't cover that" ;
763+ `Error `Missing_labels_namespace
764+ )
765+ | None ->
766+ match Context. inspect_browse_tree ~cursor: pos lid [browse], is_label with
767+ | None , _ ->
768+ log ~title: " from_string" " already at origin, doing nothing" ;
769+ `Error `At_origin
770+ | Some (Label _ as ctxt), true
771+ | Some ctxt , false ->
772+ log ~title: " from_string"
773+ " inferred context: %s" (Context. to_string ctxt);
774+ `Ok (Namespace. from_context ctxt)
775+ | _ , true ->
776+ log ~title: " from_string"
777+ " dropping inferred context, it is not precise enough" ;
778+ `Ok [ `Labels ]
779+
749780let from_string ~config ~env ~local_defs ~pos ?namespaces switch path =
750781 File_switching. reset () ;
751782 let browse = Mbrowse. of_typedtree local_defs in
752- let lid = Longident. parse path in
753- let ident, is_label = Longident. keep_suffix lid in
754- match
755- match namespaces with
756- | Some nss ->
757- if not is_label
758- then `Ok (nss :> Namespace.inferred list )
759- else if List. mem `Labels ~set: nss then (
760- log ~title: " from_string" " restricting namespaces to labels" ;
761- `Ok [ `Labels ]
762- ) else (
763- log ~title: " from_string"
764- " input is clearly a label, but the given namespaces don't cover that" ;
765- `Error `Missing_labels_namespace
766- )
767- | None ->
768- match Context. inspect_browse_tree ~cursor: pos lid [browse], is_label with
769- | None , _ ->
770- log ~title: " from_string" " already at origin, doing nothing" ;
771- `Error `At_origin
772- | Some (Label _ as ctxt), true
773- | Some ctxt , false ->
774- log ~title: " from_string"
775- " inferred context: %s" (Context. to_string ctxt);
776- `Ok (Namespace. from_context ctxt)
777- | _ , true ->
778- log ~title: " from_string"
779- " dropping inferred context, it is not precise enough" ;
780- `Ok [ `Labels ]
781- with
782- | `Error e -> e
783- | `Ok nss ->
784- log ~title: " from_string"
785- " looking for the source of '%s' (prioritizing %s files)"
786- path (match switch with `ML -> " .ml" | `MLI -> " .mli" );
787- match from_longident ~config ~env nss switch ident with
788- | `File_not_found _ | `Not_found _ | `Not_in_env _ as err -> err
789- | `Builtin -> `Builtin path
790- | `Found (uid , loc ) ->
791- match find_source ~config loc path with
792- | `Found (file , loc ) -> `Found (uid, file, loc)
793- | `File_not_found _ as otherwise -> otherwise
783+ let lid = Type_utils. parse_longident path in
784+ let from_lid lid =
785+ let ident, is_label = Longident. keep_suffix lid in
786+ match infer_namespace ?namespaces ~pos lid browse is_label with
787+ | `Error e -> e
788+ | `Ok nss ->
789+ log ~title: " from_string"
790+ " looking for the source of '%s' (prioritizing %s files)"
791+ path (match switch with `ML -> " .ml" | `MLI -> " .mli" );
792+ match from_longident ~config ~env nss switch ident with
793+ | `File_not_found _ | `Not_found _ | `Not_in_env _ as err -> err
794+ | `Builtin -> `Builtin path
795+ | `Found (uid , loc ) ->
796+ match find_source ~config loc path with
797+ | `Found (file , loc ) -> `Found (uid, file, loc)
798+ | `File_not_found _ as otherwise -> otherwise
799+ in
800+ Option. value_map ~f: from_lid ~default: (`Not_found (path, None )) lid
794801
795802(* * When we look for docstring in external compilation unit we can perform
796803 a uid-based search and return the attached comment in the attributes.
0 commit comments