Browse Source

[display] add expected type to toplevel results

Simon Krajewski 7 years ago
parent
commit
d857678523
3 changed files with 22 additions and 7 deletions
  1. 1 1
      src/compiler/main.ml
  2. 10 2
      src/core/displayTypes.ml
  3. 11 4
      src/typing/typerDisplay.ml

+ 1 - 1
src/compiler/main.ml

@@ -976,7 +976,7 @@ with
 			fields
 			fields
 		in
 		in
 		let s = match cr with
 		let s = match cr with
-			| CRToplevel
+			| CRToplevel _
 			| CRTypeHint
 			| CRTypeHint
 			| CRExtends
 			| CRExtends
 			| CRImplements
 			| CRImplements

+ 10 - 2
src/core/displayTypes.ml

@@ -67,7 +67,7 @@ module CompletionResultKind = struct
 	type t =
 	type t =
 		| CRField of CompletionItem.t * pos
 		| CRField of CompletionItem.t * pos
 		| CRStructureField
 		| CRStructureField
-		| CRToplevel
+		| CRToplevel of Type.t option
 		| CRMetadata
 		| CRMetadata
 		| CRTypeHint
 		| CRTypeHint
 		| CRExtends
 		| CRExtends
@@ -88,7 +88,15 @@ module CompletionResultKind = struct
 				"type",jopt (generate_type ctx) (CompletionItem.get_type item);
 				"type",jopt (generate_type ctx) (CompletionItem.get_type item);
 			])
 			])
 			| CRStructureField -> 1,None
 			| CRStructureField -> 1,None
-			| CRToplevel -> 2,None
+			| CRToplevel t ->
+				let args = match t with
+					| None -> None
+					| Some t -> Some (jobject [
+						"expectedType",generate_type ctx t;
+						"expectedTypeFollowed",generate_type ctx (follow t)
+					])
+				in
+				2,args
 			| CRMetadata -> 3,None
 			| CRMetadata -> 3,None
 			| CRTypeHint -> 4,None
 			| CRTypeHint -> 4,None
 			| CRExtends -> 5,None
 			| CRExtends -> 5,None

+ 11 - 4
src/typing/typerDisplay.ml

@@ -98,6 +98,13 @@ let completion_item_of_expr ctx e =
 	in
 	in
 	loop e
 	loop e
 
 
+let raise_toplevel ctx with_type po =
+	let sorted,t = match with_type with
+		| WithType t -> true,Some t
+		| _ -> false,None
+	in
+	raise_fields (DisplayToplevel.collect ctx false with_type) (CRToplevel t) po sorted
+
 let rec handle_signature_display ctx e_ast with_type =
 let rec handle_signature_display ctx e_ast with_type =
 	ctx.in_display <- true;
 	ctx.in_display <- true;
 	let p = pos e_ast in
 	let p = pos e_ast in
@@ -270,7 +277,7 @@ and display_expr ctx e_ast e dk with_type p =
 				let item = completion_item_of_expr ctx e2 in
 				let item = completion_item_of_expr ctx e2 in
 				raise_fields fields (CRField(item,e2.epos)) (Some {e.epos with pmin = e.epos.pmax - String.length s;}) false
 				raise_fields fields (CRField(item,e2.epos)) (Some {e.epos with pmin = e.epos.pmax - String.length s;}) false
 			| _ ->
 			| _ ->
-				raise_fields (DisplayToplevel.collect ctx false with_type) CRToplevel None (match with_type with WithType _ -> true | _ -> false)
+				raise_toplevel ctx with_type None
 		end
 		end
 	| DMDefault | DMNone | DMModuleSymbols _ | DMDiagnostics _ | DMStatistics ->
 	| DMDefault | DMNone | DMModuleSymbols _ | DMDiagnostics _ | DMStatistics ->
 		let fields = DisplayFields.collect ctx e_ast e dk with_type p in
 		let fields = DisplayFields.collect ctx e_ast e dk with_type p in
@@ -327,7 +334,7 @@ let handle_display ctx e_ast dk with_type =
 		type_expr ctx e_ast with_type
 		type_expr ctx e_ast with_type
 	with Error (Unknown_ident n,_) ->
 	with Error (Unknown_ident n,_) ->
         if dk = DKDot && ctx.com.json_out = None then raise (Parser.TypePath ([n],None,false,p))
         if dk = DKDot && ctx.com.json_out = None then raise (Parser.TypePath ([n],None,false,p))
-		else raise_fields (DisplayToplevel.collect ctx false with_type) CRToplevel (Some (Parser.cut_pos_at_display p)) (match with_type with WithType _ -> true | _ -> false)
+		else raise_toplevel ctx with_type (Some (Parser.cut_pos_at_display p))
 	| Error ((Type_not_found (path,_) | Module_not_found path),_) as err ->
 	| Error ((Type_not_found (path,_) | Module_not_found path),_) as err ->
 		if ctx.com.json_out = None then	begin try
 		if ctx.com.json_out = None then	begin try
 			let s = s_type_path path in
 			let s = s_type_path path in
@@ -335,7 +342,7 @@ let handle_display ctx e_ast dk with_type =
 		with Not_found ->
 		with Not_found ->
 			raise err
 			raise err
 		end else
 		end else
-			raise_fields (DisplayToplevel.collect ctx false with_type) CRToplevel (Some (Parser.cut_pos_at_display p)) (match with_type with WithType _ -> true | _ -> false)
+			raise_toplevel ctx with_type (Some (Parser.cut_pos_at_display p))
 	| DisplayException(DisplayFields(l,CRTypeHint,p,b)) when (match fst e_ast with ENew _ -> true | _ -> false) ->
 	| DisplayException(DisplayFields(l,CRTypeHint,p,b)) when (match fst e_ast with ENew _ -> true | _ -> false) ->
 		let timer = Timer.timer ["display";"toplevel";"filter ctors"] in
 		let timer = Timer.timer ["display";"toplevel";"filter ctors"] in
 		ctx.pass <- PBuildClass;
 		ctx.pass <- PBuildClass;
@@ -398,7 +405,7 @@ let handle_edisplay ctx e dk with_type =
 	| DKPattern,DMDefault ->
 	| DKPattern,DMDefault ->
 		begin try
 		begin try
 			handle_display ctx e dk with_type
 			handle_display ctx e dk with_type
-		with DisplayException(DisplayFields(l,CRToplevel,p,b)) ->
+		with DisplayException(DisplayFields(l,CRToplevel _,p,b)) ->
 			raise_fields l CRPattern p b
 			raise_fields l CRPattern p b
 		end
 		end
 	| _ -> handle_display ctx e dk with_type
 	| _ -> handle_display ctx e dk with_type