Browse Source

support TNew in position/usage completion (closes #2995)

Simon Krajewski 11 years ago
parent
commit
19fb6404b3

+ 19 - 1
codegen.ml

@@ -913,6 +913,14 @@ let detect_usage com =
 	let usage = ref [] in
 	List.iter (fun t -> match t with
 		| TClassDecl c ->
+			let check_constructor c p =
+				try
+					let _,cf = get_constructor (fun cf -> cf.cf_type) c in
+					if Meta.has Meta.Usage cf.cf_meta then
+						usage := p :: !usage;
+				with Not_found ->
+					()
+			in
 			let rec expr e = match e.eexpr with
 				| TField(_,FEnum(_,ef)) when Meta.has Meta.Usage ef.ef_meta ->
 					let p = {e.epos with pmin = e.epos.pmax - (String.length ef.ef_name)} in
@@ -930,9 +938,19 @@ let detect_usage com =
 					raise (Typecore.DisplayPosition [e.epos])
 				| TTypeExpr mt when (Meta.has Meta.Usage (t_infos mt).mt_meta) ->
 					usage := e.epos :: !usage
+				| TNew (c,_,_) ->
+					check_constructor c e.epos;
+					Type.iter expr e;
+				| TCall({eexpr = TConst TSuper},_) ->
+					begin match c.cl_super with
+						| Some (c,_) ->
+							check_constructor c e.epos
+						| _ ->
+							()
+					end
 				| _ -> Type.iter expr e
 			in
-			let field cf = match cf.cf_expr with None -> () | Some e -> expr e in
+			let field cf = ignore(follow cf.cf_type); match cf.cf_expr with None -> () | Some e -> expr e in
 			(match c.cl_constructor with None -> () | Some cf -> field cf);
 			(match c.cl_init with None -> () | Some e -> expr e);
 			List.iter field c.cl_ordered_statics;

+ 11 - 0
tests/misc/projects/Issue2995/Main.hx

@@ -0,0 +1,11 @@
+class Main {
+    function new() {}
+
+    static function main() {
+        var m = new Main();
+    }
+
+	static function something() {
+        new Main();
+	}
+}

+ 1 - 0
tests/misc/projects/Issue2995/position.hxml

@@ -0,0 +1 @@
+--display Main.hx@92@position

+ 3 - 0
tests/misc/projects/Issue2995/position.hxml.stderr

@@ -0,0 +1,3 @@
+<list>
+<pos>$$normPath(::cwd::/Main.hx):2: characters 4-21</pos>
+</list>

+ 1 - 0
tests/misc/projects/Issue2995/usage.hxml

@@ -0,0 +1 @@
+--display Main.hx@92@usage

+ 4 - 0
tests/misc/projects/Issue2995/usage.hxml.stderr

@@ -0,0 +1,4 @@
+<list>
+<pos>$$normPath(::cwd::/Main.hx):5: characters 16-26</pos>
+<pos>$$normPath(::cwd::/Main.hx):9: characters 8-18</pos>
+</list>

+ 13 - 3
typer.ml

@@ -3358,6 +3358,11 @@ and handle_display ctx e iscall p =
 		raise (DisplayTypes [tfun [t_dynamic] ctx.com.basic.tvoid])
 	in
 	ctx.in_display <- old;
+	let handle_field cf =
+		if ctx.com.display = DMPosition then
+			raise (DisplayPosition [cf.cf_pos]);
+		cf.cf_meta <- (Meta.Usage,[],p) :: cf.cf_meta;
+	in
 	match ctx.com.display with
 	| DMNone ->
 		assert false
@@ -3369,9 +3374,7 @@ and handle_display ctx e iscall p =
 				raise (DisplayPosition [ef.ef_pos]);
 			ef.ef_meta <- (Meta.Usage,[],p) :: ef.ef_meta;
 		| TField(_,(FAnon cf | FInstance (_,cf) | FStatic (_,cf) | FClosure (_,cf))) ->
-			if ctx.com.display = DMPosition then
-				raise (DisplayPosition [cf.cf_pos]);
-			cf.cf_meta <- (Meta.Usage,[],p) :: cf.cf_meta;
+			handle_field cf;
 		| TLocal v ->
 			v.v_meta <- (Meta.Usage,[],p) :: v.v_meta;
 		| TTypeExpr mt ->
@@ -3379,6 +3382,13 @@ and handle_display ctx e iscall p =
 			if ctx.com.display = DMPosition then
 				raise (DisplayPosition [ti.mt_pos]);
 			ti.mt_meta <- (Meta.Usage,[],p) :: ti.mt_meta;
+		| TNew(c,tl,_) ->
+			begin try
+				let _,cf = get_constructor ctx c tl p in
+				handle_field cf;
+			with Not_found ->
+				()
+			end
 		| _ ->
 			()
 		end;