Bläddra i källkod

[display] fix kinds of enum fields in field completion, closes #5623 (#7321)

* [display] refactor the TAnon section of displayFields.collect

* [display] fix enum abstract values in field completion

fixes part of #5623

* [display] report EnumField for regular enums as well

closes #5623

* [display] sort enum fields anove class fields

* Add TKField so toplevel sorting isn't affected
Jens Fischer 7 år sedan
förälder
incheckning
9522fc4fe0

+ 1 - 1
src/context/display/displayEmitter.ml

@@ -16,7 +16,7 @@ open DisplayPosition
 
 
 let sort_fields l with_type tk =
 let sort_fields l with_type tk =
 	let p = match tk with
 	let p = match tk with
-		| TKExpr p -> Some p
+		| TKExpr p | TKField p -> Some p
 		| _ -> None
 		| _ -> None
 	in
 	in
 	let l = List.map (fun ci ->
 	let l = List.map (fun ci ->

+ 22 - 12
src/context/display/displayFields.ml

@@ -164,28 +164,38 @@ let collect ctx e_ast e dk with_type p =
 						should_access c cf false &&
 						should_access c cf false &&
 						(not (Meta.has Meta.Impl cf.cf_meta) || Meta.has Meta.Enum cf.cf_meta)
 						(not (Meta.has Meta.Impl cf.cf_meta) || Meta.has Meta.Enum cf.cf_meta)
 					in
 					in
-					let origin,check = match !(an.a_status) with
+					let ct = DisplayEmitter.completion_type_of_type ctx ~values:(get_value_meta cf.cf_meta) cf.cf_type in
+					let add origin make_field =
+						PMap.add name (make_field (CompletionClassField.make cf CFSMember origin true) (cf.cf_type,ct)) acc
+					in
+					match !(an.a_status) with
 						| Statics ({cl_kind = KAbstractImpl a} as c) ->
 						| Statics ({cl_kind = KAbstractImpl a} as c) ->
-							Self (TAbstractDecl a),allow_static_abstract_access c cf
-						| Statics c -> Self (TClassDecl c),should_access c cf true
-						| EnumStatics en -> Self (TEnumDecl en),true
+							if allow_static_abstract_access c cf then
+								let make = if Meta.has Meta.Enum cf.cf_meta then
+										(make_ci_enum_abstract_field a)
+									else
+										make_ci_class_field
+								in
+								add (Self (TAbstractDecl a)) make
+							else
+								acc;
+						| Statics c ->
+							if should_access c cf true then add (Self (TClassDecl c)) make_ci_class_field else acc;
+						| EnumStatics en ->
+							let ef = PMap.find name en.e_constrs in
+							PMap.add name (make_ci_enum_field (CompletionEnumField.make ef (Self (TEnumDecl en)) true) (cf.cf_type,ct)) acc
 						| AbstractStatics a ->
 						| AbstractStatics a ->
 							let check = match a.a_impl with
 							let check = match a.a_impl with
 								| None -> true
 								| None -> true
 								| Some c -> allow_static_abstract_access c cf
 								| Some c -> allow_static_abstract_access c cf
 							in
 							in
-							Self (TAbstractDecl a),check
+							if check then add (Self (TAbstractDecl a)) make_ci_class_field else acc;
 						| _ ->
 						| _ ->
 							let origin = match t with
 							let origin = match t with
 								| TType(td,_) -> Self (TTypeDecl td)
 								| TType(td,_) -> Self (TTypeDecl td)
 								| _ -> AnonymousStructure an
 								| _ -> AnonymousStructure an
 							in
 							in
-							origin,true
-					in
-					if check then begin
-						let ct = DisplayEmitter.completion_type_of_type ctx ~values:(get_value_meta cf.cf_meta) cf.cf_type in
-						PMap.add name (make_ci_class_field (CompletionClassField.make cf CFSMember origin true) (cf.cf_type,ct)) acc
-					end else acc
+							add origin make_ci_class_field;
 				end else
 				end else
 					acc
 					acc
 			) an.a_fields items
 			) an.a_fields items
@@ -220,7 +230,7 @@ let collect ctx e_ast e dk with_type p =
 	(* Add static extensions *)
 	(* Add static extensions *)
 	let items = collect_static_extensions ctx items e p in
 	let items = collect_static_extensions ctx items e p in
 	let items = PMap.fold (fun item acc -> item :: acc) items [] in
 	let items = PMap.fold (fun item acc -> item :: acc) items [] in
-	let items = sort_fields items Value (TKExpr p) in
+	let items = sort_fields items Value (TKField p) in
 	try
 	try
 		let sl = string_list_of_expr_path_raise e_ast in
 		let sl = string_list_of_expr_path_raise e_ast in
 		(* Add submodule fields *)
 		(* Add submodule fields *)

+ 1 - 1
src/context/display/displayToplevel.ml

@@ -188,7 +188,7 @@ let collect ctx tk with_type =
 	in
 	in
 	begin match tk with
 	begin match tk with
 	| TKType | TKOverride -> ()
 	| TKType | TKOverride -> ()
-	| TKExpr p | TKPattern p ->
+	| TKExpr p | TKPattern p | TKField p ->
 		(* locals *)
 		(* locals *)
 		PMap.iter (fun _ v ->
 		PMap.iter (fun _ v ->
 			if not (is_gen_local v) then
 			if not (is_gen_local v) then

+ 3 - 2
src/core/display/completionItem.ml

@@ -8,6 +8,7 @@ type toplevel_kind =
 	| TKType
 	| TKType
 	| TKPattern of pos
 	| TKPattern of pos
 	| TKOverride
 	| TKOverride
+	| TKField of pos
 
 
 module CompletionModuleKind = struct
 module CompletionModuleKind = struct
 	type t =
 	type t =
@@ -447,9 +448,9 @@ let get_sort_index tk item p = match item.ci_kind with
 		in
 		in
 		i,ccf.field.cf_name
 		i,ccf.field.cf_name
 	| ITEnumField ef ->
 	| ITEnumField ef ->
-		(match tk with TKPattern _ -> -1 | _ -> 20),(Printf.sprintf "%04i" ef.efield.ef_index)
+		(match tk with TKPattern _ | TKField _ -> -1 | _ -> 20),(Printf.sprintf "%04i" ef.efield.ef_index)
 	| ITEnumAbstractField(_,ccf) ->
 	| ITEnumAbstractField(_,ccf) ->
-		(match tk with TKPattern _ -> -1 | _ -> 21),ccf.field.cf_name
+		(match tk with TKPattern _ | TKField _ -> -1 | _ -> 21),ccf.field.cf_name
 	| ITTypeParameter c ->
 	| ITTypeParameter c ->
 		30,snd c.cl_path
 		30,snd c.cl_path
 	| ITType(cmt,is) ->
 	| ITType(cmt,is) ->