Explorar o código

[display] fix structure completion between colon and expression

closes #10414
Simon Krajewski %!s(int64=3) %!d(string=hai) anos
pai
achega
c2a74c77d2

+ 15 - 9
src/typing/typerDisplay.ml

@@ -651,18 +651,24 @@ let handle_structure_display ctx e fields origin =
 		let rec loop subj fl = match fl with
 			| [] -> subj
 			| ((n,p,_),e) :: fl ->
+				let wt () =
+					try
+						let cf = List.find (fun { cf_name = name } -> name = n) !fields in
+						WithType.with_type cf.cf_type
+					with Not_found -> WithType.value
+				in
 				let subj = if DisplayPosition.display_position#enclosed_in p then
 					Some(n,p)
 				else begin
-					if DisplayPosition.display_position#enclosed_in ({ (pos e) with pmin = p.pmax + 1 }) then begin
-						let e = fst e, { (pos e) with pmin = p.pmax + 1 } in
-						let wt =
-							try
-								let cf = List.find (fun { cf_name = name } -> name = n) !fields in
-								WithType.with_type cf.cf_type
-							with Not_found -> WithType.value
-						in
-						ignore(handle_display ctx e DKMarked MGet wt)
+					if DisplayPosition.display_position#enclosed_in (pos e) then
+						ignore(handle_display ctx e DKMarked MGet (wt()))
+					else begin
+						(* If we are between the : and the expression, we don't want to use the actual expression as a filter string (issue #10414) *)
+						let p_between = { p with pmin = p.pmax + 1; pmax = (pos e).pmin - 1} in
+						if DisplayPosition.display_position#enclosed_in p_between then begin
+							let e = (EConst(Ident "null"),p_between) in
+							ignore(handle_display ctx e DKMarked MGet (wt()))
+						end;
 					end;
 					fields := List.filter (fun cf -> cf.cf_name <> n) !fields;
 					subj

+ 1 - 1
tests/server/build.hxml

@@ -10,4 +10,4 @@
 # or set UTEST_PATTERN environment variable
 
 #Temporary. To find out what's wrong with random CI failures
---macro addMetadata('@:build(utils.macro.DebugFailures.patchAssert())', 'utest.Assert')
+#--macro addMetadata('@:build(utils.macro.DebugFailures.patchAssert())', 'utest.Assert')

+ 137 - 1
tests/server/src/cases/display/issues/Issue10414.hx

@@ -21,6 +21,142 @@ class Issue10414 extends DisplayTestCase {
 			offset: offset(1),
 			wasAutoTriggered: true
 		});
-		assertHasCompletion(parseCompletion(), item -> item.args.name == 'a');
+		final completion = parseCompletion();
+		assertHasCompletion(completion, item -> item.args.name == 'a');
+		Assert.equals("", completion.result.filterString);
+	}
+
+	/**
+		class Main {
+			static function main() {
+				var abc = 1;
+				var obj:{
+					foo:Int,
+					bar:Bool
+				} = {
+					foo:{-1-}a{-2-}b{-3-}
+					bar: true
+				}
+			}
+		}
+	**/
+	function test2(_) {
+		runHaxeJson([], DisplayMethods.Completion, {
+			file: file,
+			offset: offset(1),
+			wasAutoTriggered: true
+		});
+		final completion = parseCompletion();
+		assertHasCompletion(completion, item -> item.args.name == 'abc');
+		Assert.equals("ab", completion.result.filterString);
+
+		runHaxeJson([], DisplayMethods.Completion, {
+			file: file,
+			offset: offset(2),
+			wasAutoTriggered: true
+		});
+		final completion = parseCompletion();
+		assertHasCompletion(completion, item -> item.args.name == 'abc');
+		Assert.equals("a", completion.result.filterString);
+
+		runHaxeJson([], DisplayMethods.Completion, {
+			file: file,
+			offset: offset(3),
+			wasAutoTriggered: true
+		});
+		final completion = parseCompletion();
+		assertHasCompletion(completion, item -> item.args.name == 'abc');
+		Assert.equals("ab", completion.result.filterString);
+	}
+
+	/**
+		class Container {
+			public function new() {};
+		}
+		class Main {
+			static function main() {
+				final rockC = addContainer();
+				addSprite({
+					container: {-1-}
+					frame: ""
+				});
+			}
+			static function addContainer():Container return new Container();
+			static function addSprite(obj:{
+				container:Container,
+				frame:String
+			}):Void {}
+		}
+	**/
+	function test3(_) {
+		runHaxeJson([], DisplayMethods.Completion, {
+			file: file,
+			offset: offset(1),
+			wasAutoTriggered: true
+		});
+		final completion = parseCompletion();
+		assertHasCompletion(completion, item -> item.args.name == 'rockC');
+		Assert.equals("", completion.result.filterString);
+	}
+
+	/**
+		class Container {
+			public function new() {};
+		}
+		class Main {
+			static function main() {
+				final rockC = addContainer();
+				addSprite({
+					container: {-1-},
+					frame: ""
+				});
+			}
+			static function addContainer():Container return new Container();
+			static function addSprite(obj:{
+				container:Container,
+				frame:String
+			}):Void {}
+		}
+	**/
+	function test4(_) {
+		runHaxeJson([], DisplayMethods.Completion, {
+			file: file,
+			offset: offset(1),
+			wasAutoTriggered: true
+		});
+		final completion = parseCompletion();
+		assertHasCompletion(completion, item -> item.args.name == 'rockC');
+		Assert.equals("", completion.result.filterString);
+	}
+
+	/**
+		class Container {
+			public function new() {};
+		}
+		class Main {
+			static function main() {
+				final rockC = addContainer();
+				addSprite({
+					container: rockC,
+					container: {-1-}
+					frame: ""
+				});
+			}
+			static function addContainer():Container return new Container();
+			static function addSprite(obj:{
+				container:Container,
+				frame:String
+			}):Void {}
+		}
+	**/
+	function test5(_) {
+		runHaxeJson([], DisplayMethods.Completion, {
+			file: file,
+			offset: offset(1),
+			wasAutoTriggered: true
+		});
+		final completion = parseCompletion();
+		assertHasCompletion(completion, item -> item.args.name == 'rockC');
+		Assert.equals("", completion.result.filterString);
 	}
 }