Browse Source

[display] add expected types to CRPattern (#7324)

Jens Fischer 7 năm trước cách đây
mục cha
commit
6ec27f272a
2 tập tin đã thay đổi với 22 bổ sung18 xóa
  1. 15 13
      src/core/displayTypes.ml
  2. 7 5
      src/typing/typerDisplay.ml

+ 15 - 13
src/core/displayTypes.ml

@@ -76,11 +76,18 @@ module CompletionResultKind = struct
 		| CRImport
 		| CRUsing
 		| CRNew
-		| CRPattern of bool
+		| CRPattern of (CompletionItem.CompletionType.t * CompletionItem.CompletionType.t) option * bool
 		| CROverride
 		| CRTypeRelation
 
 	let to_json ctx kind =
+		let expected_type_fields t = match t with
+			| None -> []
+			| Some(ct1,ct2) -> [
+					"expectedType",CompletionItem.CompletionType.to_json ctx ct1;
+					"expectedTypeFollowed",CompletionItem.CompletionType.to_json ctx ct2;
+				]
+		in
 		let i,args = match kind with
 			| CRField(item,p) ->
 				let t = CompletionItem.get_type item in
@@ -105,15 +112,7 @@ module CompletionResultKind = struct
 				in
 				0,Some (jobject fields)
 			| CRStructureField -> 1,None
-			| CRToplevel t ->
-				let args = match t with
-					| None -> None
-					| Some(ct1,ct2) -> Some (jobject [
-						"expectedType",CompletionItem.CompletionType.to_json ctx ct1;
-						"expectedTypeFollowed",CompletionItem.CompletionType.to_json ctx ct2;
-					])
-				in
-				2,args
+			| CRToplevel t -> 2,Some (jobject (expected_type_fields t))
 			| CRMetadata -> 3,None
 			| CRTypeHint -> 4,None
 			| CRExtends -> 5,None
@@ -124,9 +123,12 @@ module CompletionResultKind = struct
 			| CRImport -> 8,None
 			| CRUsing -> 9,None
 			| CRNew -> 10,None
-			| CRPattern isOutermostPattern -> 11,Some (jobject [
-					"isOutermostPattern",jbool isOutermostPattern
-				])
+			| CRPattern (t,isOutermostPattern) ->
+				let fields =
+					("isOutermostPattern",jbool isOutermostPattern) ::
+					(expected_type_fields t)
+				in
+				11,Some (jobject fields)
 			| CROverride -> 12,None
 			| CRTypeRelation -> 13,None
 		in

+ 7 - 5
src/typing/typerDisplay.ml

@@ -125,16 +125,18 @@ let completion_item_of_expr ctx e =
 	in
 	loop e
 
-let raise_toplevel ctx dk with_type po p =
+let get_expected_type ctx with_type =
 	let t = match with_type with
 		| WithType t -> Some t
 		| _ -> None
 	in
-	let ct = match t with
+	match t with
 		| None -> None
 		| Some t -> Some (completion_type_of_type ctx t,completion_type_of_type ctx (follow t))
-	in
-	raise_fields (DisplayToplevel.collect ctx (match dk with DKPattern _ -> TKPattern p | _ -> TKExpr p) with_type) (CRToplevel ct) po
+
+let raise_toplevel ctx dk with_type po p =
+	let expected_type = get_expected_type ctx with_type in
+	raise_fields (DisplayToplevel.collect ctx (match dk with DKPattern _ -> TKPattern p | _ -> TKExpr p) with_type) (CRToplevel expected_type) po
 
 let rec handle_signature_display ctx e_ast with_type =
 	ctx.in_display <- true;
@@ -479,6 +481,6 @@ let handle_edisplay ctx e dk with_type =
 		begin try
 			handle_display ctx e dk with_type
 		with DisplayException(DisplayFields(l,CRToplevel _,p)) ->
-			raise_fields l (CRPattern outermost) p
+			raise_fields l (CRPattern ((get_expected_type ctx with_type),outermost)) p
 		end
 	| _ -> handle_display ctx e dk with_type