Browse Source

[display] support completion for map keys in general

see #9133
Simon Krajewski 5 years ago
parent
commit
609c19e153
2 changed files with 24 additions and 0 deletions
  1. 3 0
      src/typing/typer.ml
  2. 21 0
      tests/display/src/cases/Issue9133.hx

+ 3 - 0
src/typing/typer.ml

@@ -2001,6 +2001,9 @@ and type_map_declaration ctx e1 el with_type p =
 	let el = e1 :: el in
 	let el_kv = List.map (fun e -> match fst e with
 		| EBinop(OpArrow,e1,e2) -> e1,e2
+		| EDisplay _ ->
+			ignore(type_expr ctx e (WithType.with_type tkey));
+			error "Expected a => b" (pos e)
 		| _ -> error "Expected a => b" (pos e)
 	) el in
 	let el_k,el_v,tkey,tval = if has_type then begin

+ 21 - 0
tests/display/src/cases/Issue9133.hx

@@ -21,4 +21,25 @@ class Issue9133 extends DisplayTestCase {
 		Assert.isTrue(i1 < i2);
 		Assert.isTrue(i1 != -1);
 	}
+
+	/**
+		class Main {
+		static function main() {
+			var i = 1;
+			var s = "";
+
+			var map:Map<Int, Int> = [
+				i => 1,
+				{-1-}
+	**/
+	function test2() {
+		// Note: One could argue if `i` should really be suggested here because it currently
+		// causes a `Duplicate key` error. See https://github.com/HaxeFoundation/haxe/issues/9144
+		// for more context.
+		var fields = toplevel(pos(1));
+		var i1 = fields.findIndex(item -> item.kind == "local" && item.name == "i");
+		var i2 = fields.findIndex(item -> item.kind == "local" && item.name == "s");
+		Assert.isTrue(i1 < i2);
+		Assert.isTrue(i1 != -1);
+	}
 }