Selaa lähdekoodia

move toplevel identifier collector code into its own module

Dan Korostelev 7 vuotta sitten
vanhempi
commit
00f57ea645
4 muutettua tiedostoa jossa 190 lisäystä ja 190 poistoa
  1. 0 182
      src/context/display.ml
  2. 182 0
      src/context/displayToplevel.ml
  3. 5 5
      src/typing/typeload.ml
  4. 3 3
      src/typing/typer.ml

+ 0 - 182
src/context/display.ml

@@ -751,185 +751,3 @@ module Statistics = struct
 		if false then deal_with_imports ctx.com.shared.shared_display_information.import_positions;
 		symbols,relations
 end
-
-let explore_class_paths ctx class_paths recusive f_pack f_module f_type =
-	let rec loop dir pack =
-		try
-			let entries = Sys.readdir dir in
-			Array.iter (fun file ->
-				match file with
-					| "." | ".." ->
-						()
-					| _ when Sys.is_directory (dir ^ file) && file.[0] >= 'a' && file.[0] <= 'z' ->
-						f_pack file;
-						if recusive then loop (dir ^ file ^ "/") (file :: pack)
-					| _ ->
-						let l = String.length file in
-						if l > 3 && String.sub file (l - 3) 3 = ".hx" then begin
-							try
-								let name = String.sub file 0 (l - 3) in
-								let path = (List.rev pack,name) in
-								let md = ctx.g.do_load_module ctx path null_pos in
-								f_module md;
-								List.iter (fun mt -> f_type mt) md.m_types
-							with _ ->
-								()
-						end
-			) entries;
-		with Sys_error _ ->
-			()
-	in
-	List.iter (fun dir -> loop dir []) class_paths
-
-module ToplevelCollector = struct
-	open IdentifierType
-
-	let run ctx only_types =
-		let acc = DynArray.create () in
-		let add x = DynArray.add acc x in
-
-		if not only_types then begin
-			(* locals *)
-			PMap.iter (fun _ v ->
-				if not (is_gen_local v) then
-					add (ITLocal v)
-			) ctx.locals;
-
-			(* member vars *)
-			if ctx.curfun <> FunStatic then begin
-				let rec loop c =
-					List.iter (fun cf ->
-						if not (Meta.has Meta.NoCompletion cf.cf_meta) then add (ITMember(ctx.curclass,cf))
-					) c.cl_ordered_fields;
-					match c.cl_super with
-						| None ->
-							()
-						| Some (csup,tl) ->
-							loop csup; (* TODO: type parameters *)
-				in
-				loop ctx.curclass;
-				(* TODO: local using? *)
-			end;
-
-			(* statics *)
-			List.iter (fun cf ->
-				if not (Meta.has Meta.NoCompletion cf.cf_meta) then add (ITStatic(ctx.curclass,cf))
-			) ctx.curclass.cl_ordered_statics;
-
-			(* enum constructors *)
-			let rec enum_ctors t =
-				match t with
-				| TAbstractDecl ({a_impl = Some c} as a) when Meta.has Meta.Enum a.a_meta ->
-					List.iter (fun cf ->
-						if (Meta.has Meta.Enum cf.cf_meta) && not (Meta.has Meta.NoCompletion cf.cf_meta) then add (ITEnumAbstract(a,cf));
-					) c.cl_ordered_statics
-				| TClassDecl _ | TAbstractDecl _ ->
-					()
-				| TTypeDecl t ->
-					begin match follow t.t_type with
-						| TEnum (e,_) -> enum_ctors (TEnumDecl e)
-						| _ -> ()
-					end
-				| TEnumDecl e ->
-					PMap.iter (fun _ ef ->
-						add (ITEnum(e,ef))
-					) e.e_constrs;
-			in
-			List.iter enum_ctors ctx.m.curmod.m_types;
-			List.iter enum_ctors (List.map fst ctx.m.module_types);
-
-			(* imported globals *)
-			PMap.iter (fun _ (mt,s,_) ->
-				try
-					let t = match resolve_typedef mt with
-						| TClassDecl c -> (PMap.find s c.cl_statics).cf_type
-						| TEnumDecl en -> (PMap.find s en.e_constrs).ef_type
-						| TAbstractDecl {a_impl = Some c} -> (PMap.find s c.cl_statics).cf_type
-						| _ -> raise Not_found
-					in
-					add (ITGlobal(mt,s,t))
-				with Not_found ->
-					()
-			) ctx.m.module_globals;
-
-			(* literals *)
-			add (ITLiteral "null");
-			add (ITLiteral "true");
-			add (ITLiteral "false");
-		end;
-
-		let module_types = ref [] in
-
-		let add_type mt =
-			match mt with
-			| TClassDecl {cl_kind = KAbstractImpl _} -> ()
-			| _ ->
-				let path = (t_infos mt).mt_path in
-				if not (List.exists (fun mt2 -> (t_infos mt2).mt_path = path) !module_types) then begin
-					(match mt with
-					| TClassDecl c | TAbstractDecl { a_impl = Some c } when Meta.has Meta.CoreApi c.cl_meta ->
-						!merge_core_doc_ref ctx c
-					| _ -> ());
-					module_types := mt :: !module_types
-				end
-		in
-
-		(* module types *)
-		List.iter add_type ctx.m.curmod.m_types;
-
-		(* module imports *)
-		List.iter add_type (List.map fst ctx.m.module_types);
-
-		(* module using *)
-		List.iter (fun (c,_) ->
-			add_type (TClassDecl c)
-		) ctx.m.module_using;
-
-		(* TODO: wildcard packages. How? *)
-
-		(* packages and toplevel types *)
-		let class_paths = ctx.com.class_path in
-		let class_paths = List.filter (fun s -> s <> "") class_paths in
-
-		let packages = ref [] in
-		let add_package pack =
-			try
-				begin match PMap.find pack ctx.com.package_rules with
-					| Forbidden ->
-						()
-					| _ ->
-						raise Not_found
-				end
-			with Not_found ->
-				if not (List.mem pack !packages) then packages := pack :: !packages
-		in
-
-		let maybe_add_type mt = if not (t_infos mt).mt_private then add_type mt in
-
-		explore_class_paths ctx class_paths false add_package (fun _ -> ()) maybe_add_type;
-
-		List.iter (fun pack ->
-			add (ITPackage pack)
-		) !packages;
-
-		List.iter (fun mt ->
-			add (ITType mt)
-		) !module_types;
-
-		(* type params *)
-		List.iter (fun (_,t) ->
-			add (ITType (module_type_of_type t))
-		) ctx.type_params;
-
-		DynArray.to_list acc
-
-	let handle_unresolved_identifier ctx i p only_types =
-		let l = run ctx only_types in
-		let cl = List.map (fun it ->
-			let s = IdentifierType.get_name it in
-			(s,it),StringError.levenshtein i s
-		) l in
-		let cl = List.sort (fun (_,c1) (_,c2) -> compare c1 c2) cl in
-		let cl = StringError.filter_similar (fun (s,_) r -> r > 0 && r <= (min (String.length s) (String.length i)) / 3) cl in
-		ctx.com.display_information.unresolved_identifiers <- (i,p,cl) :: ctx.com.display_information.unresolved_identifiers
-end

+ 182 - 0
src/context/displayToplevel.ml

@@ -0,0 +1,182 @@
+open Common
+open Type
+open Typecore
+open Common.IdentifierType
+
+let explore_class_paths ctx class_paths recusive f_pack f_module f_type =
+	let rec loop dir pack =
+		try
+			let entries = Sys.readdir dir in
+			Array.iter (fun file ->
+				match file with
+					| "." | ".." ->
+						()
+					| _ when Sys.is_directory (dir ^ file) && file.[0] >= 'a' && file.[0] <= 'z' ->
+						f_pack file;
+						if recusive then loop (dir ^ file ^ "/") (file :: pack)
+					| _ ->
+						let l = String.length file in
+						if l > 3 && String.sub file (l - 3) 3 = ".hx" then begin
+							try
+								let name = String.sub file 0 (l - 3) in
+								let path = (List.rev pack,name) in
+								let md = ctx.g.do_load_module ctx path Globals.null_pos in
+								f_module md;
+								List.iter (fun mt -> f_type mt) md.m_types
+							with _ ->
+								()
+						end
+			) entries;
+		with Sys_error _ ->
+			()
+	in
+	List.iter (fun dir -> loop dir []) class_paths
+
+let collect ctx only_types =
+	let acc = DynArray.create () in
+	let add x = DynArray.add acc x in
+
+	if not only_types then begin
+		(* locals *)
+		PMap.iter (fun _ v ->
+			if not (is_gen_local v) then
+				add (ITLocal v)
+		) ctx.locals;
+
+		(* member vars *)
+		if ctx.curfun <> FunStatic then begin
+			let rec loop c =
+				List.iter (fun cf ->
+					if not (Meta.has Meta.NoCompletion cf.cf_meta) then add (ITMember(ctx.curclass,cf))
+				) c.cl_ordered_fields;
+				match c.cl_super with
+					| None ->
+						()
+					| Some (csup,tl) ->
+						loop csup; (* TODO: type parameters *)
+			in
+			loop ctx.curclass;
+			(* TODO: local using? *)
+		end;
+
+		(* statics *)
+		List.iter (fun cf ->
+			if not (Meta.has Meta.NoCompletion cf.cf_meta) then add (ITStatic(ctx.curclass,cf))
+		) ctx.curclass.cl_ordered_statics;
+
+		(* enum constructors *)
+		let rec enum_ctors t =
+			match t with
+			| TAbstractDecl ({a_impl = Some c} as a) when Meta.has Meta.Enum a.a_meta ->
+				List.iter (fun cf ->
+					if (Meta.has Meta.Enum cf.cf_meta) && not (Meta.has Meta.NoCompletion cf.cf_meta) then add (ITEnumAbstract(a,cf));
+				) c.cl_ordered_statics
+			| TClassDecl _ | TAbstractDecl _ ->
+				()
+			| TTypeDecl t ->
+				begin match follow t.t_type with
+					| TEnum (e,_) -> enum_ctors (TEnumDecl e)
+					| _ -> ()
+				end
+			| TEnumDecl e ->
+				PMap.iter (fun _ ef ->
+					add (ITEnum(e,ef))
+				) e.e_constrs;
+		in
+		List.iter enum_ctors ctx.m.curmod.m_types;
+		List.iter enum_ctors (List.map fst ctx.m.module_types);
+
+		(* imported globals *)
+		PMap.iter (fun _ (mt,s,_) ->
+			try
+				let t = match resolve_typedef mt with
+					| TClassDecl c -> (PMap.find s c.cl_statics).cf_type
+					| TEnumDecl en -> (PMap.find s en.e_constrs).ef_type
+					| TAbstractDecl {a_impl = Some c} -> (PMap.find s c.cl_statics).cf_type
+					| _ -> raise Not_found
+				in
+				add (ITGlobal(mt,s,t))
+			with Not_found ->
+				()
+		) ctx.m.module_globals;
+
+		(* literals *)
+		add (ITLiteral "null");
+		add (ITLiteral "true");
+		add (ITLiteral "false");
+	end;
+
+	let module_types = ref [] in
+
+	let add_type mt =
+		match mt with
+		| TClassDecl {cl_kind = KAbstractImpl _} -> ()
+		| _ ->
+			let path = (t_infos mt).mt_path in
+			if not (List.exists (fun mt2 -> (t_infos mt2).mt_path = path) !module_types) then begin
+				(match mt with
+				| TClassDecl c | TAbstractDecl { a_impl = Some c } when Meta.has Meta.CoreApi c.cl_meta ->
+					!merge_core_doc_ref ctx c
+				| _ -> ());
+				module_types := mt :: !module_types
+			end
+	in
+
+	(* module types *)
+	List.iter add_type ctx.m.curmod.m_types;
+
+	(* module imports *)
+	List.iter add_type (List.map fst ctx.m.module_types);
+
+	(* module using *)
+	List.iter (fun (c,_) ->
+		add_type (TClassDecl c)
+	) ctx.m.module_using;
+
+	(* TODO: wildcard packages. How? *)
+
+	(* packages and toplevel types *)
+	let class_paths = ctx.com.class_path in
+	let class_paths = List.filter (fun s -> s <> "") class_paths in
+
+	let packages = ref [] in
+	let add_package pack =
+		try
+			begin match PMap.find pack ctx.com.package_rules with
+				| Forbidden ->
+					()
+				| _ ->
+					raise Not_found
+			end
+		with Not_found ->
+			if not (List.mem pack !packages) then packages := pack :: !packages
+	in
+
+	let maybe_add_type mt = if not (t_infos mt).mt_private then add_type mt in
+
+	explore_class_paths ctx class_paths false add_package (fun _ -> ()) maybe_add_type;
+
+	List.iter (fun pack ->
+		add (ITPackage pack)
+	) !packages;
+
+	List.iter (fun mt ->
+		add (ITType mt)
+	) !module_types;
+
+	(* type params *)
+	List.iter (fun (_,t) ->
+		add (ITType (module_type_of_type t))
+	) ctx.type_params;
+
+	DynArray.to_list acc
+
+let handle_unresolved_identifier ctx i p only_types =
+	let l = collect ctx only_types in
+	let cl = List.map (fun it ->
+		let s = IdentifierType.get_name it in
+		(s,it),StringError.levenshtein i s
+	) l in
+	let cl = List.sort (fun (_,c1) (_,c2) -> compare c1 c2) cl in
+	let cl = StringError.filter_similar (fun (s,_) r -> r > 0 && r <= (min (String.length s) (String.length i)) / 3) cl in
+	ctx.com.display_information.unresolved_identifiers <- (i,p,cl) :: ctx.com.display_information.unresolved_identifiers

+ 5 - 5
src/typing/typeload.ml

@@ -314,7 +314,7 @@ let apply_macro ctx mode path el p =
 let rec load_type_def ctx p t =
 	let no_pack = t.tpackage = [] in
 	let tname = (match t.tsub with None -> t.tname | Some n -> n) in
-	if tname = "" then raise (Display.DisplayToplevel (Display.ToplevelCollector.run ctx true));
+	if tname = "" then raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true));
 	try
 		if t.tsub <> None then raise Not_found;
 		let path_matches t2 =
@@ -777,7 +777,7 @@ let load_type_hint ?(opt=false) ctx pcur t =
 			try
 				load_complex_type ctx true pcur (t,p)
 			with Error(Module_not_found(([],name)),p) as exc ->
-				if Display.Diagnostics.is_diagnostics_run ctx then Display.ToplevelCollector.handle_unresolved_identifier ctx name p true;
+				if Display.Diagnostics.is_diagnostics_run ctx then DisplayToplevel.handle_unresolved_identifier ctx name p true;
 				(* Default to Dynamic in display mode *)
 				if ctx.com.display.dms_display then t_dynamic else raise exc
 	in
@@ -1488,7 +1488,7 @@ module Inheritance = struct
 				let t = load_instance ~allow_display:true ctx t false p in
 				Some (check_herit t is_extends)
 			with Error(Module_not_found(([],name)),p) when ctx.com.display.dms_display ->
-				if Display.Diagnostics.is_diagnostics_run ctx then Display.ToplevelCollector.handle_unresolved_identifier ctx name p true;
+				if Display.Diagnostics.is_diagnostics_run ctx then DisplayToplevel.handle_unresolved_identifier ctx name p true;
 				None
 		) herits in
 		fl
@@ -2997,7 +2997,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 				ctx.m.wildcard_packages <- (List.map fst pack,p) :: ctx.m.wildcard_packages
 			| _ ->
 				(match List.rev path with
-				| [] -> raise (Display.DisplayToplevel (Display.ToplevelCollector.run ctx true));
+				| [] -> raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true));
 				| (_,p) :: _ -> error "Module name must start with an uppercase letter" p))
 		| (tname,p2) :: rest ->
 			let p1 = (match pack with [] -> p2 | (_,p1) :: _ -> p1) in
@@ -3106,7 +3106,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 			| (s1,_) :: sl ->
 				{ tpackage = List.rev (List.map fst sl); tname = s1; tsub = None; tparams = [] }
 			| [] ->
-				raise (Display.DisplayToplevel (Display.ToplevelCollector.run ctx true));
+				raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true));
 		in
 		(* do the import first *)
 		let types = (match t.tsub with

+ 3 - 3
src/typing/typer.ml

@@ -363,7 +363,7 @@ let merge_core_doc ctx c =
 
 let check_error ctx err p = match err with
 	| Module_not_found ([],name) when Display.Diagnostics.is_diagnostics_run ctx ->
-		Display.ToplevelCollector.handle_unresolved_identifier ctx name p true
+		DisplayToplevel.handle_unresolved_identifier ctx name p true
 	| _ ->
 		display_error ctx (error_msg err) p
 
@@ -2417,7 +2417,7 @@ and type_ident ctx i p mode =
 					| DMNone ->
 						raise (Error(err,p))
 					| DMDiagnostics b when b || ctx.is_display_file ->
-						Display.ToplevelCollector.handle_unresolved_identifier ctx i p false;
+						DisplayToplevel.handle_unresolved_identifier ctx i p false;
 						let t = mk_mono() in
 						AKExpr (mk (TIdent i) t p)
 					| _ ->
@@ -3883,7 +3883,7 @@ and display_expr ctx e_ast e with_type p =
 		let pl = loop e in
 		raise (Display.DisplayPosition pl);
 	| DMToplevel ->
-		raise (Display.DisplayToplevel (Display.ToplevelCollector.run ctx false))
+		raise (Display.DisplayToplevel (DisplayToplevel.collect ctx false))
 	| DMField | DMNone | DMModuleSymbols _ | DMDiagnostics _ | DMStatistics ->
 		let opt_args args ret = TFun(List.map(fun (n,o,t) -> n,true,t) args,ret) in
 		let e = match e.eexpr with