Browse Source

[display] bring match `.match` on enum values

closes #7627
Simon Krajewski 6 years ago
parent
commit
a7e75b074c
2 changed files with 39 additions and 4 deletions
  1. 20 4
      src/context/display/displayFields.ml
  2. 19 0
      tests/display/src/cases/Issue7627.hx

+ 20 - 4
src/context/display/displayFields.ml

@@ -114,6 +114,10 @@ let collect ctx e_ast e dk with_type p =
 			(not stat || not (Meta.has Meta.Impl cf.cf_meta)) &&
 			can_access ctx c cf stat
 	in
+	let make_class_field origin cf =
+		let ct = DisplayEmitter.completion_type_of_type ctx ~values:(get_value_meta cf.cf_meta) cf.cf_type in
+		make_ci_class_field (CompletionClassField.make cf CFSMember origin true) (cf.cf_type,ct)
+	in
 	let rec loop items t =
 		let is_new_item items name = not (PMap.mem name items) in
 		match follow t with
@@ -127,12 +131,25 @@ let collect ctx e_ast e dk with_type p =
 			PMap.foldi (fun k (c,cf) acc ->
 				if should_access c cf false && is_new_item acc cf.cf_name then begin
 					let origin = if c == c0 then Self(TClassDecl c) else Parent(TClassDecl c) in
-					let ct = DisplayEmitter.completion_type_of_type ctx ~values:(get_value_meta cf.cf_meta) cf.cf_type in
-				 	let item = make_ci_class_field (CompletionClassField.make cf CFSMember origin true) (cf.cf_type,ct) in
+					let item = make_class_field origin cf in
 					PMap.add k item acc
 				end else
 					acc
 			) fields items
+		| TEnum _ ->
+			let t = ctx.g.do_load_type_def ctx p {tpackage=[];tname="EnumValue";tsub=None;tparams=[]} in
+			begin match t with
+			| TAbstractDecl ({a_impl = Some c} as a) ->
+				begin try
+					let cf = PMap.find "match" c.cl_statics in
+					let item = make_class_field (Self(TAbstractDecl a)) cf in
+					PMap.add "match" item items
+				with Not_found ->
+					items
+				end
+			| _ ->
+				items
+			end;
 		| TAbstract({a_impl = Some c} as a,tl) ->
 			merge_core_doc ctx (TAbstractDecl a);
 			(* Abstracts should show all their @:impl fields minus the constructor. *)
@@ -141,8 +158,7 @@ let collect ctx e_ast e dk with_type p =
 					let origin = Self(TAbstractDecl a) in
 					let cf = prepare_using_field cf in
 					let cf = if tl = [] then cf else {cf with cf_type = apply_params a.a_params tl cf.cf_type} in
-					let ct = DisplayEmitter.completion_type_of_type ctx ~values:(get_value_meta cf.cf_meta) cf.cf_type in
-					let item = make_ci_class_field (CompletionClassField.make cf CFSMember origin true) (cf.cf_type,ct) in
+					let item = make_class_field origin cf in
 					PMap.add cf.cf_name item acc
 				end else
 					acc

+ 19 - 0
tests/display/src/cases/Issue7627.hx

@@ -0,0 +1,19 @@
+package cases;
+
+class Issue7627 extends DisplayTestCase {
+	/**
+
+	import haxe.ds.Option;
+
+	class Main {
+		public static function main() {
+			var option = Some(1);
+			option.match(None);
+			option.{-1-}
+		}
+	}
+	**/
+	function test() {
+		eq(true, hasField(fields(pos(1)), "match", "(this : EnumValue, pattern : Dynamic) -> Bool"));
+	}
+}