Procházet zdrojové kódy

support display for variable/property fields

Simon Krajewski před 9 roky
rodič
revize
dbbfc81c14

+ 6 - 1
src/display/display.ml

@@ -323,4 +323,9 @@ let convert_import_to_something_usable path =
 		| [] ->
 			(IDK,null_pos)
 	in
-	loop [] None None path
+	loop [] None None path
+
+let process_expr com e = match com.display with
+	| DMToplevel -> find_enclosing com e
+	| DMPosition | DMUsage | DMType -> find_before_pos com e
+	| _ -> e

+ 4 - 7
src/typing/typeload.ml

@@ -272,8 +272,9 @@ let type_function_arg ctx t e opt p =
 		let t = match e with Some (EConst (Ident "null"),p) -> ctx.t.tnull t | _ -> t in
 		t, e
 
-let type_var_field ctx t e stat p =
+let type_var_field ctx t e stat do_display p =
 	if stat then ctx.curfun <- FunStatic else ctx.curfun <- FunMember;
+	let e = if do_display then Display.process_expr ctx.com e else e in
 	let e = type_expr ctx e (WithType t) in
 	let e = (!cast_or_unify_ref) ctx t e p in
 	match t with
@@ -1592,11 +1593,7 @@ let type_function ctx args ret fmode f do_display p =
 	let e = if not do_display then
 		type_expr ctx e NoValue
 	else begin
-		let e = match ctx.com.display with
-			| DMToplevel -> Display.find_enclosing ctx.com e
-			| DMPosition | DMUsage | DMType -> Display.find_before_pos ctx.com e
-			| _ -> e
-		in
+		let e = Display.process_expr ctx.com e in
 		try
 			if Common.defined ctx.com Define.NoCOpt then raise Exit;
 			type_expr ctx (Optimizer.optimize_completion_expr e) NoValue
@@ -2179,7 +2176,7 @@ module ClassInitializer = struct
 					r := (fun() -> t);
 					cctx.context_init();
 					if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ cf.cf_name);
-					let e = type_var_field ctx t e fctx.is_static p in
+					let e = type_var_field ctx t e fctx.is_static fctx.is_display_field p in
 					let maybe_run_analyzer e = match e.eexpr with
 						| TConst _ | TLocal _ | TFunction _ -> e
 						| _ -> !analyzer_run_on_expr_ref ctx.com e

+ 18 - 0
tests/display/src/cases/Basic.hx

@@ -57,4 +57,22 @@ class Basic extends DisplayTestCase {
 		eq("String", type(pos(1)));
 		eq("String", type(pos(2)));
 	}
+
+	/**
+	class Some {
+		var x1 = "fo{-1-}";
+		static var x2 = "fo{-2-}";
+		static inline var x3 = "fo{-3-}";
+
+		var x4(default, null) = "fo{-4-}";
+		static var x5(default, null) = "fo{-5-}";
+	}
+	**/
+	function testFieldDisplay() {
+		eq("String", type(pos(1)));
+		eq("String", type(pos(2)));
+		eq("String", type(pos(3)));
+		eq("String", type(pos(4)));
+		eq("String", type(pos(5)));
+	}
 }