Browse Source

[display] patch constructor types on hover

closes #7022
Simon Krajewski 7 years ago
parent
commit
7657b06ab3

+ 7 - 3
src/context/display.ml

@@ -205,7 +205,7 @@ module DisplayEmitter = struct
 		| DMHover -> raise_type v.v_type p None
 		| _ -> ()
 
-	let display_field dm cf p = match dm.dms_kind with
+	let display_field dm c cf p = match dm.dms_kind with
 		| DMDefinition -> raise_position [cf.cf_name_pos]
 		| DMUsage _ -> reference_position := cf.cf_name_pos
 		| DMHover ->
@@ -214,11 +214,15 @@ module DisplayEmitter = struct
 			else
 				cf.cf_type
 			in
+			let t = match c,follow t with
+				| Some c,TFun(tl,_) when cf.cf_name = "new" -> TFun(tl,TInst(c,List.map snd c.cl_params))
+				| _ -> t
+			in
 			raise_type t p cf.cf_doc
 		| _ -> ()
 
-	let maybe_display_field ctx p cf =
-		if is_display_position p then display_field ctx.com.display cf p
+	let maybe_display_field ctx c cf p =
+		if is_display_position p then display_field ctx.com.display c cf p
 
 	let display_enum_field dm ef p = match dm.dms_kind with
 		| DMDefinition -> raise_position [ef.ef_name_pos]

+ 2 - 2
src/typing/typeload.ml

@@ -402,7 +402,7 @@ and load_complex_type ctx allow_display p (t,pn) =
 			init_meta_overloads ctx None cf;
 			if ctx.is_display_file then begin
 				Display.DisplayEmitter.check_display_metadata ctx cf.cf_meta;
-				Display.DisplayEmitter.maybe_display_field ctx (cf.cf_name_pos) cf;
+				Display.DisplayEmitter.maybe_display_field ctx None cf cf.cf_name_pos;
 			end;
 			PMap.add n cf acc
 		in
@@ -724,7 +724,7 @@ let handle_path_display ctx path p =
 				| TClassDecl c when snd c.cl_path = st ->
 					ignore(c.cl_build());
 					let cf = PMap.find sf c.cl_statics in
-					Display.DisplayEmitter.display_field ctx.com.display cf p
+					Display.DisplayEmitter.display_field ctx.com.display (Some c) cf p
 				| _ ->
 					()
 			) m.m_types;

+ 8 - 7
src/typing/typeloadFields.ml

@@ -587,7 +587,7 @@ let bind_var (ctx,cctx,fctx) cf e =
 
 	match e with
 	| None ->
-		if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (cf.cf_name_pos) cf;
+		if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (Some c) cf cf.cf_name_pos;
 	| Some e ->
 		if requires_value_meta ctx.com (Some c) then cf.cf_meta <- ((Meta.Value,[e],null_pos) :: cf.cf_meta);
 		let check_cast e =
@@ -674,7 +674,7 @@ let bind_var (ctx,cctx,fctx) cf e =
 				let e = check_cast e in
 				cf.cf_expr <- Some e;
 				cf.cf_type <- t;
-				if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (cf.cf_name_pos) cf;
+				if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (Some c) cf cf.cf_name_pos;
 			end;
 			t
 		) "bind_var" in
@@ -968,7 +968,7 @@ let create_method (ctx,cctx,fctx) c f fd p =
 				| None ->
 					if fctx.field_kind = FKConstructor then FunConstructor else if fctx.is_static then FunStatic else FunMember
 			) in
-			(match ctx.com.platform with
+			begin match ctx.com.platform with
 				| Java when is_java_native_function cf.cf_meta ->
 					if fd.f_expr <> None then
 						ctx.com.warning "@:native function definitions shouldn't include an expression. This behaviour is deprecated." cf.cf_pos;
@@ -980,7 +980,7 @@ let create_method (ctx,cctx,fctx) c f fd p =
 							begin match c.cl_super with
 							| Some(c,tl) ->
 								let _,_,cf = raw_class_field (fun cf -> cf.cf_type) c tl cf.cf_name in
-								Display.DisplayEmitter.display_field ctx.com.display cf p
+								Display.DisplayEmitter.display_field ctx.com.display (Some c) cf p
 							| _ ->
 								()
 							end
@@ -1006,12 +1006,13 @@ let create_method (ctx,cctx,fctx) c f fd p =
 						| _ -> c.cl_init <- Some e);
 					cf.cf_expr <- Some (mk (TFunction tf) t p);
 					cf.cf_type <- t;
-				if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (cf.cf_name_pos) cf);
+				if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (Some c) cf cf.cf_name_pos
+			end;
 		end;
 		t
 	) "type_fun" in
 	if fctx.do_bind then bind_type (ctx,cctx,fctx) cf r (match fd.f_expr with Some e -> snd e | None -> f.cff_pos)
-	else if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (cf.cf_name_pos) cf;
+	else if fctx.is_display_field then Display.DisplayEmitter.maybe_display_field ctx (Some c) cf cf.cf_name_pos;
 	cf
 
 let create_property (ctx,cctx,fctx) c f (get,set,t,eo) p =
@@ -1105,7 +1106,7 @@ let create_property (ctx,cctx,fctx) c f (get,set,t,eo) p =
 	let display_accessor m p =
 		try
 			let cf = match find_accessor m with [_,cf] -> cf | _ -> raise Not_found in
-			Display.DisplayEmitter.display_field ctx.com.display cf p
+			Display.DisplayEmitter.display_field ctx.com.display (Some c) cf p
 		with Not_found ->
 			()
 	in

+ 2 - 2
src/typing/typer.ml

@@ -1651,7 +1651,7 @@ and type_object_decl ctx fl with_type p =
 					| Some t -> t
 					| None ->
 						let cf = PMap.find n field_map in
-						if ctx.in_display && Display.is_display_position pn then Display.DisplayEmitter.display_field ctx.com.display cf pn;
+						if ctx.in_display && Display.is_display_position pn then Display.DisplayEmitter.display_field ctx.com.display None cf pn;
 						cf.cf_type
 				in
 				let e = type_expr ctx e (WithType t) in
@@ -1689,7 +1689,7 @@ and type_object_decl ctx fl with_type p =
 			let e = type_expr ctx e Value in
 			(match follow e.etype with TAbstract({a_path=[],"Void"},_) -> error "Fields of type Void are not allowed in structures" e.epos | _ -> ());
 			let cf = mk_field f e.etype (punion pf e.epos) pf in
-			if ctx.in_display && Display.is_display_position pf then Display.DisplayEmitter.display_field ctx.com.display cf pf;
+			if ctx.in_display && Display.is_display_position pf then Display.DisplayEmitter.display_field ctx.com.display None cf pf;
 			(((f,pf,qs),e) :: l, if is_valid then begin
 				if String.length f > 0 && f.[0] = '$' then error "Field names starting with a dollar are not allowed" p;
 				PMap.add f cf acc

+ 16 - 0
tests/display/src/cases/Issue7022.hx

@@ -0,0 +1,16 @@
+package cases;
+
+class Issue7022 extends DisplayTestCase {
+	/**
+	class Main {
+		public static function main() {}
+
+		public function ne{-1-}w() {
+			new Main();
+		}
+	}
+	**/
+	function test() {
+		eq("Void -> cases.Main", type(pos(1)));
+	}
+}