Explorar o código

[display] improve structure handling

also retire `call(` completion
Simon Krajewski %!s(int64=7) %!d(string=hai) anos
pai
achega
bb86776277
Modificáronse 31 ficheiros con 167 adicións e 125 borrados
  1. 1 2
      src/compiler/displayOutput.ml
  2. 11 8
      src/context/display.ml
  3. 0 1
      src/context/displayJson.ml
  4. 4 3
      src/syntax/grammar.mly
  5. 0 1
      src/syntax/parser.ml
  6. 18 16
      src/typing/typerDisplay.ml
  7. 17 1
      tests/display/src/DisplayTestCase.hx
  8. 1 1
      tests/display/src/DisplayTestContext.hx
  9. 1 5
      tests/display/src/cases/ArrowFunctions.hx
  10. 2 2
      tests/display/src/cases/Issue5141.hx
  11. 1 1
      tests/display/src/cases/Issue5988.hx
  12. 2 2
      tests/display/src/cases/Issue6434.hx
  13. 0 13
      tests/display/src/cases/Signature.hx
  14. 108 0
      tests/display/src/cases/StructureCompletion.hx
  15. 0 4
      tests/display/src/cases/Toplevel.hx
  16. 0 10
      tests/misc/projects/Issue3256/Main.hx
  17. 0 1
      tests/misc/projects/Issue3256/compile.hxml
  18. 0 4
      tests/misc/projects/Issue3256/compile.hxml.stderr
  19. 0 11
      tests/misc/projects/Issue3261/Main.hx
  20. 0 1
      tests/misc/projects/Issue3261/compile.hxml
  21. 0 4
      tests/misc/projects/Issue3261/compile.hxml.stderr
  22. 0 7
      tests/misc/projects/Issue3911/Main1.hx
  23. 0 7
      tests/misc/projects/Issue3911/Main2.hx
  24. 0 7
      tests/misc/projects/Issue3911/Main3.hx
  25. 0 1
      tests/misc/projects/Issue3911/compile1.hxml
  26. 0 3
      tests/misc/projects/Issue3911/compile1.hxml.stderr
  27. 0 1
      tests/misc/projects/Issue3911/compile2.hxml
  28. 0 3
      tests/misc/projects/Issue3911/compile2.hxml.stderr
  29. 0 1
      tests/misc/projects/Issue3911/compile3.hxml
  30. 0 3
      tests/misc/projects/Issue3911/compile3.hxml.stderr
  31. 1 1
      tests/misc/projects/Issue6005/Main.hx

+ 1 - 2
src/compiler/displayOutput.ml

@@ -682,7 +682,7 @@ let handle_display_argument com file_pos pre_compilation did_something =
 				Common.define com Define.NoCOpt;
 				DMHover
 			| "toplevel" ->
-				Common.define com Define.NoCOpt;
+				Parser.is_completion := true;
 				DMDefault
 			| "module-symbols" ->
 				Common.define com Define.NoCOpt;
@@ -716,7 +716,6 @@ let handle_display_argument com file_pos pre_compilation did_something =
 		Common.display_default := mode;
 		Common.define_value com Define.Display (if smode <> "" then smode else "1");
 		Parser.use_doc := true;
-		Parser.legacy_display := true;
 		Parser.resume_display := {
 			pfile = Path.unique_full_path file;
 			pmin = pos + !offset;

+ 11 - 8
src/context/display.ml

@@ -31,14 +31,15 @@ let is_display_position p =
 	encloses_position !Parser.resume_display p
 
 module ExprPreprocessing = struct
-	let find_before_pos com is_completion dk e =
+	let find_before_pos com is_completion e =
 		let display_pos = ref (!Parser.resume_display) in
 		let is_annotated p = p.pmin < !display_pos.pmin && p.pmax >= !display_pos.pmax in
-		let annotate e =
+		let annotate e dk =
 			display_pos := { pfile = ""; pmin = -2; pmax = -2 };
 			(EDisplay(e,dk),pos e)
 		in
-		let mk_null p = annotate ((EConst(Ident "null")),p) in
+		let annotate_marked e = annotate e DKMarked in
+		let mk_null p = annotate_marked ((EConst(Ident "null")),p) in
 		let loop_el el =
 			let pr = !Parser.resume_display in
 			let rec loop el = match el with
@@ -62,9 +63,11 @@ module ExprPreprocessing = struct
 			match fst e with
 			| EVars vl ->
 				if List.exists (fun ((_,p),_,_) -> is_annotated p) vl then
-					annotate e
+					annotate_marked e
 				else
 					e
+			| EBlock [] when is_annotated (pos e) ->
+				annotate e DKStructure
 			| EBlock el when is_annotated (pos e) && is_completion ->
 				let el = loop_el el in
 				EBlock el,(pos e)
@@ -73,7 +76,7 @@ module ExprPreprocessing = struct
 				ECall(e1,el),(pos e)
 			| ENew((tp,pp),el) when is_annotated (pos e) && is_completion ->
 				if is_annotated pp || pp.pmax >= !Parser.resume_display.pmax then
-					annotate e
+					annotate_marked e
 				else begin
 					let el = loop_el el in
 					ENew((tp,pp),el),(pos e)
@@ -85,7 +88,7 @@ module ExprPreprocessing = struct
 				raise Exit
 			| _ ->
 				if is_annotated (pos e) then
-					annotate e
+					annotate_marked e
 				else
 					e
 		in
@@ -114,8 +117,8 @@ module ExprPreprocessing = struct
 
 
 	let process_expr com e = match com.display.dms_kind with
-		| DMDefinition | DMUsage _ | DMHover -> find_before_pos com false DKMarked e
-		| DMDefault -> find_before_pos com true DKMarked e
+		| DMDefinition | DMUsage _ | DMHover -> find_before_pos com false e
+		| DMDefault -> find_before_pos com true e
 		| DMSignature -> find_display_call e
 		| _ -> e
 end

+ 0 - 1
src/context/displayJson.ml

@@ -77,7 +77,6 @@ let parse_input com input =
 			}
 		in
 		Parser.is_completion := false;
-		Parser.legacy_display := false;
 		begin match name with
 			| "initialize" ->
 				raise (DisplayOutput.Completion (f_result (JObject [

+ 4 - 3
src/syntax/grammar.mly

@@ -928,7 +928,6 @@ and expr = parser
 			display (make_meta name params e p)
 		end
 	| [< '(BrOpen,p1); s >] ->
-		check_resume p1 (fun() -> display (EDisplay ((EObjectDecl [],p1),DKStructure),p1)) (fun () -> ());
 		(match s with parser
 		| [< '(Binop OpOr,p2) when do_resume() >] ->
 			set_resume p1;
@@ -943,7 +942,10 @@ and expr = parser
 			let e = (b,punion p1 p2) in
 			(match b with
 			| EObjectDecl _ -> expr_next e s
-			| _ -> e))
+			| _ -> e)
+		| [< >] ->
+			check_resume p1 (fun() -> display (EDisplay ((EObjectDecl [],p1),DKStructure),p1)) serror;
+		)
 	| [< '(Kwd k,p) when !parsing_macro_cond; s >] ->
 		expr_next (EConst (Ident (s_keyword k)), p) s
 	| [< '(Kwd Macro,p); s >] ->
@@ -1172,7 +1174,6 @@ and parse_call_params f p1 s =
 		let e = f el p2 in
 		display (EDisplay(e,DKCall),pos e)
 	in
-	if !legacy_display then check_resume p1 (fun () -> make_display_call [] p1) (fun () -> ());
 	let rec parse_next_param acc p1 =
 		let e = try
 			expr s

+ 0 - 1
src/syntax/parser.ml

@@ -78,7 +78,6 @@ let last_doc : (string * int) option ref = ref None
 let use_doc = ref false
 let was_auto_triggered = ref false
 let is_completion = ref false
-let legacy_display = ref false
 let resume_display = ref null_pos
 let in_macro = ref false
 

+ 18 - 16
src/typing/typerDisplay.ml

@@ -231,24 +231,17 @@ and display_expr ctx e_ast e dk with_type p =
 		let fields = DisplayFields.collect ctx e_ast e dk with_type p in
 		raise (Display.DisplayFields(fields,false))
 
-let handle_structure_display ctx e with_type =
+let handle_structure_display ctx e fields =
 	let p = pos e in
 	match fst e with
 	| EObjectDecl fl ->
-		let fail () = [] in
-		let fields = match with_type with
-		| WithType t ->
-			begin match follow t with
-			| TAnon an ->
-				let fields = PMap.foldi (fun k cf acc ->
-					if Expr.field_mem_assoc k fl then acc
-					else (DisplayTypes.CompletionKind.ITClassMember cf) :: acc
-				) an.a_fields [] in
-				fields
-			| _ -> fail()
-			end
-		| _ -> fail()
-		in
+		let fields = PMap.foldi (fun k cf acc ->
+			if Expr.field_mem_assoc k fl then acc
+			else (DisplayTypes.CompletionKind.ITClassMember cf) :: acc
+		) fields [] in
+		raise (Display.DisplayFields(fields,false))
+	| EBlock [] ->
+		let fields = PMap.foldi (fun _ cf acc -> DisplayTypes.CompletionKind.ITClassMember cf :: acc) fields [] in
 		raise (Display.DisplayFields(fields,false))
 	| _ ->
 		error "Expected object expression" p
@@ -257,5 +250,14 @@ let handle_structure_display ctx e with_type =
 let handle_edisplay ctx e dk with_type =
 	match dk,ctx.com.display.dms_kind with
 	| DKCall,(DMSignature | DMDefault) -> handle_signature_display ctx e with_type
-	| DKStructure,DMDefault -> handle_structure_display ctx e with_type
+	| DKStructure,DMDefault ->
+		begin match with_type with
+			| WithType t ->
+				begin match follow t with
+					| TAnon an -> handle_structure_display ctx e an.a_fields
+					| _ -> handle_display ctx e dk with_type
+				end
+			| _ ->
+				handle_display ctx e dk with_type
+		end
 	| _ -> handle_display ctx e dk with_type

+ 17 - 1
tests/display/src/DisplayTestCase.hx

@@ -13,7 +13,6 @@ class DisplayTestCase {
 	// api
 	inline function pos(name) return ctx.pos(name);
 	inline function fields(pos) return ctx.fields(pos);
-	inline function signatures(pos) return ctx.signatures(pos);
 	inline function toplevel(pos) return ctx.toplevel(pos);
 	inline function type(pos) return ctx.type(pos);
 	inline function position(pos) return ctx.position(pos);
@@ -76,10 +75,27 @@ class DisplayTestCase {
 		return a.exists(function(t) return t.type == type && t.name == name && (kind == null || t.kind == kind));
 	}
 
+	function hasToplevel(a:Array<ToplevelElement>, kind:String, name:String):Bool {
+		return a.exists(function(t) return t.kind == kind && t.name == name);
+	}
+
 	function hasPath(a:Array<FieldElement>, name:String):Bool {
 		return a.exists(function(t) return t.name == name);
 	}
 
+	function sigEq(arg:Int, params:Array<Array<String>>, sig:SignatureHelp, ?pos:haxe.PosInfos) {
+		eq(arg, sig.activeParameter, pos);
+		eq(params.length, sig.signatures.length, pos);
+		for (i in 0...params.length) {
+			var sigInf = sig.signatures[i];
+			var args = params[i];
+			eq(sigInf.parameters.length, args.length, pos);
+			for (i in 0...args.length) {
+				eq(sigInf.parameters[i].label, args[i], pos);
+			}
+		}
+	}
+
 	function report(message, pos:haxe.PosInfos) {
 		haxe.Log.trace(message, pos);
 	}

+ 1 - 1
tests/display/src/DisplayTestContext.hx

@@ -109,7 +109,7 @@ class DisplayTestContext {
 		return StringTools.trim(xml.firstChild().nodeValue);
 	}
 
-	static function extractSignatures(result:String) {
+	static function  extractSignatures(result:String) {
 		var xml = Xml.parse('<x>$result</x>');
 		xml = xml.firstElement();
 		var ret = [];

+ 1 - 5
tests/display/src/cases/ArrowFunctions.hx

@@ -40,10 +40,6 @@ class ArrowFunctions extends DisplayTestCase {
     x -> { {-1-}
     **/
     @:funcCode function testTopLevel(){
-        eq(true, Toplevel.hasToplevel(toplevel(pos(1)), "local", "x"));
+        eq(true, hasToplevel(toplevel(pos(1)), "local", "x"));
     }
-
-    /*public static function hasToplevel(a:Array<ToplevelElement>, kind:String, name:String):Bool {
-		return a.exists(function(t) return t.kind == kind && t.name == name);
-    }*/
 }

+ 2 - 2
tests/display/src/cases/Issue5141.hx

@@ -14,7 +14,7 @@ class Issue5141 extends DisplayTestCase {
 	**/
 	function testTypedef() {
 		eq("cases.MyHandler", type(pos(1)));
-		arrayEq(["Int -> String -> Void"], signatures(pos(2)));
+		sigEq(0, [[":Int", ":String"]], signature(pos(2)));
 	}
 
 	/**
@@ -31,6 +31,6 @@ class Issue5141 extends DisplayTestCase {
 	**/
 	function testAbstract() {
 		eq("cases.MyCallable", type(pos(1)));
-		arrayEq(["Int -> String -> Void"], signatures(pos(2)));
+		sigEq(0, [[":Int", ":String"]], signature(pos(2)));
 	}
 }

+ 1 - 1
tests/display/src/cases/Issue5988.hx

@@ -11,6 +11,6 @@ class Issue5988 extends DisplayTestCase {
 	}
 	**/
 	function test() {
-		eq(true, Toplevel.hasToplevel(toplevel(pos(1)), "type", "Array"));
+		eq(true, hasToplevel(toplevel(pos(1)), "type", "Array"));
 	}
 }

+ 2 - 2
tests/display/src/cases/Issue6434.hx

@@ -12,9 +12,9 @@ class Issue6434 extends DisplayTestCase {
 	**/
 	function test() {
 		var toplevel = toplevel(pos(1));
-		eq(true, Toplevel.hasToplevel(toplevel, "type", "PublicClass"));
+		eq(true, hasToplevel(toplevel, "type", "PublicClass"));
 		// This doesn't really test what I want to test, but I don't know how
 		// to test what I want to test...
-		eq(false, Toplevel.hasToplevel(toplevel, "type", "PrivateClass"));
+		eq(false, hasToplevel(toplevel, "type", "PrivateClass"));
 	}
 }

+ 0 - 13
tests/display/src/cases/Signature.hx

@@ -172,17 +172,4 @@ class Signature extends DisplayTestCase {
 	function testNested() {
 		sigEq(0, [["a:String", "b:Int"]], signature(pos(1)));
 	}
-
-	function sigEq(arg:Int, params:Array<Array<String>>, sig:SignatureHelp, ?pos:haxe.PosInfos) {
-		eq(arg, sig.activeParameter, pos);
-		eq(params.length, sig.signatures.length, pos);
-		for (i in 0...params.length) {
-			var sigInf = sig.signatures[i];
-			var args = params[i];
-			eq(sigInf.parameters.length, args.length, pos);
-			for (i in 0...args.length) {
-				eq(sigInf.parameters[i].label, args[i], pos);
-			}
-		}
-	}
 }

+ 108 - 0
tests/display/src/cases/StructureCompletion.hx

@@ -53,4 +53,112 @@ class StructureCompletion extends DisplayTestCase {
 		eq(true, hasField(fields(pos(1)), "a", "Float"));
 		eq(true, hasField(fields(pos(1)), "b", "String"));
 	}
+
+	/**
+	typedef T = Dynamic;
+	class Main {
+		static function main () {{-1-}
+	**/
+	function testStructureVsToplevel1() {
+		eq(true, hasToplevel(toplevel(pos(1)), "type", "T"));
+	}
+
+	/**
+	typedef T = Dynamic;
+	class Main {
+		static function main () { {-1-}
+	**/
+	function testStructureVsToplevel2() {
+		eq(true, hasToplevel(toplevel(pos(1)), "type", "T"));
+	}
+
+	/**
+	typedef T = Dynamic;
+	class Main {
+		static function main () {{-1-}
+		}
+	**/
+	function testStructureVsToplevel3() {
+		eq(true, hasToplevel(toplevel(pos(1)), "type", "T"));
+	}
+
+	/**
+	typedef T = Dynamic;
+	class Main {
+		static function main () { {-1-}
+		}
+	**/
+	function testStructureVsToplevel4() {
+		eq(true, hasToplevel(toplevel(pos(1)), "type", "T"));
+	}
+
+	/**
+	typedef Foo = {
+		var a:Int;
+		var b:String;
+	}
+	class Main {
+		static function main () {
+			var foo:Foo = {{-1-}
+
+	**/
+	function testStructureVsToplevel5() {
+		var fields = fields(pos(1));
+		eq(false, hasField(fields, "type", "T"));
+		eq(true, hasField(fields, "a", "Int"));
+		eq(true, hasField(fields, "b", "String"));
+	}
+
+	/**
+	typedef Foo = {
+		var a:Int;
+		var b:String;
+	}
+	class Main {
+		static function main () {
+			var foo:Foo = { {-1-}
+
+	**/
+	function testStructureVsToplevel6() {
+		var fields = fields(pos(1));
+		eq(false, hasField(fields, "type", "T"));
+		eq(true, hasField(fields, "a", "Int"));
+		eq(true, hasField(fields, "b", "String"));
+	}
+
+	/**
+	typedef Foo = {
+		var a:Int;
+		var b:String;
+	}
+	class Main {
+		static function main () {
+			var foo:Foo = {{-1-}
+			}
+
+	**/
+	function testStructureVsToplevel7() {
+		var fields = fields(pos(1));
+		eq(false, hasField(fields, "type", "T"));
+		eq(true, hasField(fields, "a", "Int"));
+		eq(true, hasField(fields, "b", "String"));
+	}
+
+	/**
+	typedef Foo = {
+		var a:Int;
+		var b:String;
+	}
+	class Main {
+		static function main () {
+			var foo:Foo = { {-1-}
+			}
+
+	**/
+	function testStructureVsToplevel8() {
+		var fields = fields(pos(1));
+		eq(false, hasField(fields, "type", "T"));
+		eq(true, hasField(fields, "a", "Int"));
+		eq(true, hasField(fields, "b", "String"));
+	}
 }

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

@@ -314,8 +314,4 @@ class Toplevel extends DisplayTestCase {
 		var fields = toplevel(pos(1));
 		eq(true, hasToplevel(fields, "local", "foo"));
 	}
-
-	public static function hasToplevel(a:Array<ToplevelElement>, kind:String, name:String):Bool {
-		return a.exists(function(t) return t.kind == kind && t.name == name);
-	}
 }

+ 0 - 10
tests/misc/projects/Issue3256/Main.hx

@@ -1,10 +0,0 @@
-abstract A(Int) from Int {
-    public function addFloat(f:Float):Float return this + f;
-}
-
-class Main {
-    static function main() {
-        var a:A = 1;
-        a.addFloat(
-    }
-}

+ 0 - 1
tests/misc/projects/Issue3256/compile.hxml

@@ -1 +0,0 @@
---display Main.hx@173

+ 0 - 4
tests/misc/projects/Issue3256/compile.hxml.stderr

@@ -1,4 +0,0 @@
-<type>
-f : Float -&gt; Float
-</type>
-

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

@@ -1,11 +0,0 @@
-using Main.A;
-
-class A {
-    public static function f(v:String, b:Int):String return v;
-}
-
-class Main {
-    static function main() {
-        "a".f(
-    }
-}

+ 0 - 1
tests/misc/projects/Issue3261/compile.hxml

@@ -1 +0,0 @@
---display Main.hx@147

+ 0 - 4
tests/misc/projects/Issue3261/compile.hxml.stderr

@@ -1,4 +0,0 @@
-<type>
-b : Int -&gt; String
-</type>
-

+ 0 - 7
tests/misc/projects/Issue3911/Main1.hx

@@ -1,7 +0,0 @@
-class Main1 {
-    function new(a:Int) {}
-
-    static function main() {
-        var m = new Main1(
-    }
-}

+ 0 - 7
tests/misc/projects/Issue3911/Main2.hx

@@ -1,7 +0,0 @@
-class Main2 {
-    function new(a:Int) {}
-
-    static function main() {
-        var m = new Main2()
-    }
-}

+ 0 - 7
tests/misc/projects/Issue3911/Main3.hx

@@ -1,7 +0,0 @@
-class Main3 {
-    function new(a:Int) {}
-
-    static function main() {
-        var m = new Main3();
-    }
-}

+ 0 - 1
tests/misc/projects/Issue3911/compile1.hxml

@@ -1 +0,0 @@
---display Main1.hx@97

+ 0 - 3
tests/misc/projects/Issue3911/compile1.hxml.stderr

@@ -1,3 +0,0 @@
-<type>
-a : Int -&gt; Void
-</type>

+ 0 - 1
tests/misc/projects/Issue3911/compile2.hxml

@@ -1 +0,0 @@
---display Main2.hx@97

+ 0 - 3
tests/misc/projects/Issue3911/compile2.hxml.stderr

@@ -1,3 +0,0 @@
-<type>
-a : Int -&gt; Void
-</type>

+ 0 - 1
tests/misc/projects/Issue3911/compile3.hxml

@@ -1 +0,0 @@
---display Main3.hx@97

+ 0 - 3
tests/misc/projects/Issue3911/compile3.hxml.stderr

@@ -1,3 +0,0 @@
-<type>
-a : Int -&gt; Void
-</type>

+ 1 - 1
tests/misc/projects/Issue6005/Main.hx

@@ -1,6 +1,6 @@
 class Main {
 	public static function main() {
-		var foo = {|
+		var foo:Struct = {|
 
 		}
 	}