Sfoglia il codice sorgente

[display] use expected type in toplevel display

closes #6454
Simon Krajewski 7 anni fa
parent
commit
8ac4d6d7ca

+ 8 - 2
src/context/displayToplevel.ml

@@ -58,7 +58,7 @@ let explore_class_paths ctx class_paths recusive f_pack f_module f_type =
 	in
 	List.iter (fun dir -> loop dir []) class_paths
 
-let collect ctx only_types =
+let collect ctx only_types with_type =
 	let acc = DynArray.create () in
 	let add x = DynArray.add acc x in
 
@@ -112,6 +112,12 @@ let collect ctx only_types =
 		List.iter enum_ctors ctx.m.curmod.m_types;
 		List.iter enum_ctors (List.map fst ctx.m.module_types);
 
+		begin match with_type with
+			| WithType t ->
+				(try enum_ctors (module_type_of_type t) with Exit -> ())
+			| _ -> ()
+		end;
+
 		(* imported globals *)
 		PMap.iter (fun _ (mt,s,_) ->
 			try
@@ -210,7 +216,7 @@ let collect ctx only_types =
 	DynArray.to_list acc
 
 let handle_unresolved_identifier ctx i p only_types =
-	let l = collect ctx only_types in
+	let l = collect ctx only_types NoValue in
 	let cl = List.map (fun it ->
 		let s = DisplayTypes.CompletionKind.get_name it in
 		(s,it),StringError.levenshtein i s

+ 1 - 1
src/typing/typeload.ml

@@ -39,7 +39,7 @@ let type_function_params_rec = ref (fun _ _ _ _ -> assert false)
 let rec load_type_def ctx p t =
 	let no_pack = t.tpackage = [] in
 	let tname = (match t.tsub with None -> t.tname | Some n -> n) in
-	if tname = "" then raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true));
+	if tname = "" then raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true NoValue));
 	try
 		if t.tsub <> None then raise Not_found;
 		let path_matches t2 =

+ 2 - 2
src/typing/typeloadModule.ml

@@ -360,7 +360,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 				ctx.m.wildcard_packages <- (List.map fst pack,p) :: ctx.m.wildcard_packages
 			| _ ->
 				(match List.rev path with
-				| [] -> raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true));
+				| [] -> raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true NoValue));
 				| (_,p) :: _ -> error "Module name must start with an uppercase letter" p))
 		| (tname,p2) :: rest ->
 			let p1 = (match pack with [] -> p2 | (_,p1) :: _ -> p1) in
@@ -469,7 +469,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 			| (s1,_) :: sl ->
 				{ tpackage = List.rev (List.map fst sl); tname = s1; tsub = None; tparams = [] }
 			| [] ->
-				raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true));
+				raise (Display.DisplayToplevel (DisplayToplevel.collect ctx true NoValue));
 		in
 		(* do the import first *)
 		let types = (match t.tsub with

+ 1 - 1
src/typing/typerDisplay.ml

@@ -226,7 +226,7 @@ and display_expr ctx e_ast e dk with_type p =
 		let pl = loop e in
 		raise (Display.DisplayPosition pl);
 	| DMDefault when not (!Parser.had_resume)->
-		raise (Display.DisplayToplevel (DisplayToplevel.collect ctx false))
+		raise (Display.DisplayToplevel (DisplayToplevel.collect ctx false with_type))
 	| DMDefault | DMNone | DMModuleSymbols _ | DMDiagnostics _ | DMStatistics ->
 		let fields = DisplayFields.collect ctx e_ast e dk with_type p in
 		raise (Display.DisplayFields fields)

+ 83 - 0
tests/display/src/cases/Toplevel.hx

@@ -219,6 +219,89 @@ class Toplevel extends DisplayTestCase {
 		eq(true, hasToplevel(toplevel(pos(2)), "literal", "super"));
 	}
 
+	/**
+	class Main {
+    	static function f(t:Type.ValueType) {}
+
+    	public static function main() {
+    	    f({-1-}
+	**/
+	function testExpectedType1() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
+	/**
+	class Main {
+    	public static function main() {
+    	    var x:Type.ValueType = {-1-}
+	**/
+	function testExpectedType2() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
+	/**
+	class Main {
+    	public static function main() {
+    	    var x:Type.ValueType;
+			x = {-1-}
+	**/
+	function testExpectedType3() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
+	/**
+	class Main {
+    	public static function main() {
+    	    var x:{v:Type.ValueType} = {v: {-1-}};
+	**/
+	function testExpectedType4() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
+	/**
+	class Main {
+    	public static function main() {
+    	    var x:Array<Type.ValueType> = [{-1-}];
+	**/
+	function testExpectedType5() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
+	/**
+	class Main {
+    	public static function main() {
+    	    var x:Array<Type.ValueType> = [{-1-}
+	**/
+	function testExpectedType6() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
+	/**
+	class Main {
+    	public static function main() {
+    	    var x:Array<Type.ValueType> = [null, {-1-}
+	**/
+	function testExpectedType7() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
+	/**
+	class Main {
+    	public static function main() {
+    	    var x:Array<Type.ValueType> = [null, {-1-} ]
+	**/
+	function testExpectedType8() {
+		var fields = toplevel(pos(1));
+		eq(true, hasToplevel(fields, "enum", "TNull"));
+	}
+
 	public static function hasToplevel(a:Array<ToplevelElement>, kind:String, name:String):Bool {
 		return a.exists(function(t) return t.kind == kind && t.name == name);
 	}