Browse Source

[display] fixed parameter index in signature help for rest args (closes #10008)

Aleksandr Kuzmenko 4 years ago
parent
commit
16e44eccea

+ 1 - 1
src/compiler/displayOutput.ml

@@ -201,7 +201,7 @@ let print_signature tl display_arg =
 	) tl in
 	let jo = JObject [
 		"signatures",JArray siginf;
-		"activeParameter",JInt display_arg;
+		"activeParameter",JInt (arg_index tl 0 display_arg);
 		"activeSignature",JInt 0;
 	] in
 	string_of_json jo

+ 15 - 1
src/context/display/displayException.ml

@@ -143,6 +143,20 @@ let fields_to_json ctx fields kind subj =
 	in
 	jobject fl
 
+let arg_index signatures signature_index param_index =
+	try
+		let args,_ = fst (fst (List.nth signatures signature_index)) in
+		let rec loop args index =
+			match args with
+			| [] -> param_index
+			| [_,_,t] when index < param_index && ExtType.is_rest (follow t) -> index
+			| arg :: _ when index = param_index -> param_index
+			| _ :: args -> loop args (index + 1)
+		in
+		loop args 0
+	with Invalid_argument _ ->
+		param_index
+
 let to_json ctx de =
 	match de with
 	| Statistics _
@@ -166,7 +180,7 @@ let to_json ctx de =
 		in
 		jobject [
 			"activeSignature",jint isig;
-			"activeParameter",jint iarg;
+			"activeParameter",jint (arg_index sigs isig iarg);
 			"signatures",jlist fsig sigs;
 			"kind",jint sigkind;
 		]

+ 22 - 0
tests/server/src/cases/display/issues/Issue10008.hx

@@ -0,0 +1,22 @@
+package cases.display.issues;
+
+class Issue10008 extends DisplayTestCase {
+	/**
+		class Main {
+			static function main() {
+				foo(1, {-1-});
+			}
+
+			static function foo(...rest:Int) {}
+		}
+	**/
+	function test(_) {
+		runHaxeJson([], DisplayMethods.SignatureHelp, {
+			file: file,
+			offset: offset(1),
+			wasAutoTriggered: true
+		});
+		var result = parseSignatureHelp();
+		Assert.equals(0, result.result.activeParameter);
+	}
+}