Browse Source

[display] sort toplevel by compatible types

closes #6750
Simon Krajewski 7 years ago
parent
commit
f57538ad73
3 changed files with 50 additions and 13 deletions
  1. 1 1
      src/compiler/displayOutput.ml
  2. 33 8
      src/context/displayToplevel.ml
  3. 16 4
      src/context/displayTypes.ml

+ 1 - 1
src/compiler/displayOutput.ml

@@ -111,7 +111,7 @@ let print_toplevel il =
 			Buffer.add_string b (Printf.sprintf "<i k=\"type\" p=\"%s\"%s%s>%s</i>\n" (s_type_path infos.mt_path) import ("") name);
 		| ITPackage s ->
 			Buffer.add_string b (Printf.sprintf "<i k=\"package\">%s</i>\n" s)
-		| ITLiteral s ->
+		| ITLiteral(s,_) ->
 			Buffer.add_string b (Printf.sprintf "<i k=\"literal\">%s</i>\n" s)
 		| ITTimer(s,_) ->
 			Buffer.add_string b (Printf.sprintf "<i k=\"timer\">%s</i>\n" s)

+ 33 - 8
src/context/displayToplevel.ml

@@ -133,13 +133,20 @@ let collect ctx only_types with_type =
 		) ctx.m.module_globals;
 
 		(* literals *)
-		add (ITLiteral "null");
-		add (ITLiteral "true");
-		add (ITLiteral "false");
-		add (ITLiteral "this");
-		match ctx.curclass.cl_super with
-			| Some _ -> add (ITLiteral "super")
-			| None -> ()
+		add (ITLiteral("null",t_dynamic));
+		add (ITLiteral("true",ctx.com.basic.tbool));
+		add (ITLiteral("false",ctx.com.basic.tbool));
+		begin match ctx.curfun with
+			| FunMember | FunConstructor | FunMemberClassLocal ->
+				let t = TInst(ctx.curclass,List.map snd ctx.curclass.cl_params) in
+				add (ITLiteral("this",t));
+				begin match ctx.curclass.cl_super with
+					| Some(c,tl) -> add (ITLiteral("super",TInst(c,tl)))
+					| None -> ()
+				end
+			| _ ->
+				()
+		end
 	end;
 
 	let module_types = ref [] in
@@ -213,7 +220,25 @@ let collect ctx only_types with_type =
 		add (ITType (module_type_of_type t,RMTypeParameter))
 	) ctx.type_params;
 
-	DynArray.to_list acc
+	let l = DynArray.to_list acc in
+	match with_type with
+		| WithType t ->
+			let rec comp t' =
+				if type_iseq t' t then 0 (* equal types - perfect *)
+				else if t' == t_dynamic then 5 (* dynamic isn't good, but better than incompatible *)
+				else try Type.unify t' t; 1 (* assignable - great *)
+				with Unify_error _ -> match follow t' with
+					| TFun(_,tr) ->
+						if type_iseq tr t then 2 (* function returns our exact type - alright *)
+						else (try Type.unify tr t; 3 (* function returns compatible type - okay *)
+						with Unify_error _ -> 7) (* incompatible function - useless *)
+					| _ ->
+						6 (* incompatible type - probably useless *)
+			in
+			let l = List.map (fun ck -> ck,comp (DisplayTypes.CompletionKind.get_type ck)) l in
+			let l = List.sort (fun (_,i1) (_,i2) -> compare i1 i2) l in
+			List.map fst l
+		| _ -> l
 
 let handle_unresolved_identifier ctx i p only_types =
 	let l = collect ctx only_types NoValue in

+ 16 - 4
src/context/displayTypes.ml

@@ -82,7 +82,7 @@ module CompletionKind = struct
 		| ITType of module_type * resolution_mode
 		| ITPackage of string
 		| ITModule of string
-		| ITLiteral of string
+		| ITLiteral of string * Type.t
 		| ITTimer of string * string
 		| ITMetadata of string * documentation
 
@@ -105,7 +105,7 @@ module CompletionKind = struct
 		| ITTimer(s,_) -> 6,s
 		| ITLocal v -> 7,v.v_name
 		| ITGlobal(_,s,_) -> 8,s
-		| ITLiteral s -> 9,s
+		| ITLiteral(s,_) -> 9,s
 
 	let get_name = function
 		| ITLocal v -> v.v_name
@@ -115,10 +115,22 @@ module CompletionKind = struct
 		| ITType(mt,_) -> snd (t_infos mt).mt_path
 		| ITPackage s -> s
 		| ITModule s -> s
-		| ITLiteral s -> s
+		| ITLiteral(s,_) -> s
 		| ITTimer(s,_) -> s
 		| ITMetadata(s,_) -> s
 
+	let get_type = function
+		| ITLocal v -> v.v_type
+		| ITClassMember cf | ITClassStatic cf | ITEnumAbstractField(_,cf) -> cf.cf_type
+		| ITEnumField(_,ef) -> ef.ef_type
+		| ITGlobal(_,_,t) -> t
+		| ITType(mt,_) -> t_dynamic (* TODO: hmm *)
+		| ITPackage _ -> t_dynamic
+		| ITModule _ -> t_dynamic
+		| ITLiteral(_,t) -> t
+		| ITTimer(_,_) -> t_dynamic
+		| ITMetadata(_,_) -> t_dynamic
+
 	let to_json ctx ck =
 		let kind,data = match ck with
 			| ITLocal v -> "Local",generate_tvar ctx v
@@ -137,7 +149,7 @@ module CompletionKind = struct
 			]
 			| ITPackage s -> "Package",jstring s
 			| ITModule s -> "Module",jstring s
-			| ITLiteral s -> "Literal",jstring s
+			| ITLiteral(s,_) -> "Literal",jstring s
 			| ITTimer(s,value) -> "Timer",jobject [
 				"name",jstring s;
 				"value",jstring value;