Browse Source

[display] prioritize enum ctors when sorting pattern completion

closes #7275
Simon Krajewski 7 năm trước cách đây
mục cha
commit
2b14b0ddcd

+ 7 - 3
src/context/display/displayEmitter.ml

@@ -14,9 +14,13 @@ open Common
 open Display
 open DisplayPosition
 
-let sort_fields l with_type p =
+let sort_fields l with_type tk =
+	let p = match tk with
+		| TKExpr p -> Some p
+		| _ -> None
+	in
 	let l = List.map (fun ci ->
-		let i = get_sort_index ci (Option.default Globals.null_pos p) in
+		let i = get_sort_index tk ci (Option.default Globals.null_pos p) in
 		ci,i
 	) l in
 	let sort l =
@@ -285,6 +289,6 @@ let check_field_modifiers ctx c cf override display_modifier =
 				let ct = completion_type_of_type ctx ~values:(get_value_meta cf.cf_meta) cf.cf_type in
 				make_ci_class_field (CompletionClassField.make cf CFSMember origin true) (cf.cf_type,ct) :: fields
 			) missing_fields [] in
-			let l = sort_fields l NoValue None in
+			let l = sort_fields l NoValue TKOverride in
 			raise_fields l CROverride None
 		| _ -> ()

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

@@ -220,7 +220,7 @@ let collect ctx e_ast e dk with_type p =
 	(* Add static extensions *)
 	let items = collect_static_extensions ctx items e p in
 	let items = PMap.fold (fun item acc -> item :: acc) items [] in
-	let items = sort_fields items Value (Some p) in
+	let items = sort_fields items Value (TKExpr p) in
 	try
 		let sl = string_list_of_expr_path_raise e_ast in
 		(* Add submodule fields *)

+ 6 - 4
src/context/display/displayToplevel.ml

@@ -127,7 +127,7 @@ let pack_similarity pack1 pack2 =
 	in
 	loop 0 pack1 pack2
 
-let collect ctx epos with_type =
+let collect ctx tk with_type =
 	let t = Timer.timer ["display";"toplevel"] in
 	let cctx = CollectionContext.create ctx in
 	let curpack = fst ctx.curclass.cl_path in
@@ -186,7 +186,9 @@ let collect ctx epos with_type =
 		let ct = DisplayEmitter.completion_type_of_type ctx ~values t in
 		(t,ct)
 	in
-	if epos <> None then begin
+	begin match tk with
+	| TKType | TKOverride -> ()
+	| TKExpr p | TKPattern p ->
 		(* locals *)
 		PMap.iter (fun _ v ->
 			if not (is_gen_local v) then
@@ -367,12 +369,12 @@ let collect ctx epos with_type =
 	) packages;
 	(* sorting *)
 	let l = DynArray.to_list cctx.items in
-	let l = sort_fields l with_type epos in
+	let l = sort_fields l with_type tk in
 	t();
 	l
 
 let handle_unresolved_identifier ctx i p only_types =
-	let l = collect ctx (if only_types then None else Some p) NoValue in
+	let l = collect ctx (if only_types then TKType else TKExpr p) NoValue in
 	let cl = List.map (fun it ->
 		let s = CompletionItem.get_name it in
 		let i = StringError.levenshtein i s in

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

@@ -3,6 +3,12 @@ open Ast
 open Type
 open Genjson
 
+type toplevel_kind =
+	| TKExpr of pos
+	| TKType
+	| TKPattern of pos
+	| TKOverride
+
 module CompletionModuleKind = struct
 	type t =
 		| Class
@@ -424,7 +430,7 @@ let get_index item = match item.ci_kind with
 	| ITExpression _ -> 12
 	| ITTypeParameter _ -> 13
 
-let get_sort_index item p = match item.ci_kind with
+let get_sort_index tk item p = match item.ci_kind with
 	| ITLocal v ->
 		let i = p.pmin - v.v_pos.pmin in
 		let i = if i < 0 then 0 else i in
@@ -441,9 +447,9 @@ let get_sort_index item p = match item.ci_kind with
 		in
 		i,ccf.field.cf_name
 	| ITEnumField ef ->
-		20,(Printf.sprintf "%04i" ef.efield.ef_index)
+		(match tk with TKPattern _ -> -1 | _ -> 20),(Printf.sprintf "%04i" ef.efield.ef_index)
 	| ITEnumAbstractField(_,ccf) ->
-		21,ccf.field.cf_name
+		(match tk with TKPattern _ -> -1 | _ -> 21),ccf.field.cf_name
 	| ITTypeParameter c ->
 		30,snd c.cl_path
 	| ITType(cmt,is) ->

+ 3 - 3
src/typing/typeload.ml

@@ -105,7 +105,7 @@ with Error((Module_not_found _ | Type_not_found _),p2) when p = p2 ->
 *)
 let load_type_def ctx p t =
 	let no_pack = t.tpackage = [] in
-	if t = Parser.magic_type_path then raise_fields (DisplayToplevel.collect ctx None NoValue) CRTypeHint None;
+	if t = Parser.magic_type_path then raise_fields (DisplayToplevel.collect ctx TKType NoValue) CRTypeHint None;
 	(* The type name is the module name or the module sub-type name *)
 	let tname = (match t.tsub with None -> t.tname | Some n -> n) in
 	try
@@ -315,7 +315,7 @@ and load_instance ctx ?(allow_display=false) (t,pn) allow_no_params =
 		t
 	with Error (Module_not_found path,_) when (ctx.com.display.dms_kind = DMDefault) && DisplayPosition.encloses_display_position pn ->
 		let s = s_type_path path in
-		raise_fields (DisplayToplevel.collect ctx None NoValue) CRTypeHint (Some {pn with pmin = pn.pmax - String.length s;});
+		raise_fields (DisplayToplevel.collect ctx TKType NoValue) CRTypeHint (Some {pn with pmin = pn.pmax - String.length s;});
 
 (*
 	build an instance from a complex type
@@ -824,7 +824,7 @@ let handle_path_display ctx path p =
 	in
 	match ImportHandling.convert_import_to_something_usable !DisplayPosition.display_position path,ctx.com.display.dms_kind with
 		| (IDKPackage [_],p),DMDefault ->
-			let fields = DisplayToplevel.collect ctx None Typecore.NoValue in
+			let fields = DisplayToplevel.collect ctx TKType Typecore.NoValue in
 			raise_fields fields CRImport (Some p)
 		| (IDKPackage sl,p),DMDefault ->
 			let sl = match List.rev sl with

+ 2 - 2
src/typing/typeloadModule.ml

@@ -364,7 +364,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 				ctx.m.wildcard_packages <- (List.map fst pack,p) :: ctx.m.wildcard_packages
 			| _ ->
 				(match List.rev path with
-				| [] -> DisplayException.raise_fields (DisplayToplevel.collect ctx None NoValue) CRImport None;
+				| [] -> DisplayException.raise_fields (DisplayToplevel.collect ctx TKType NoValue) CRImport None;
 				| (_,p) :: _ -> error "Module name must start with an uppercase letter" p))
 		| (tname,p2) :: rest ->
 			let p1 = (match pack with [] -> p2 | (_,p1) :: _ -> p1) in
@@ -476,7 +476,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 			| (s1,_) :: sl ->
 				{ tpackage = List.rev (List.map fst sl); tname = s1; tsub = None; tparams = [] }
 			| [] ->
-				DisplayException.raise_fields (DisplayToplevel.collect ctx None NoValue) CRUsing None;
+				DisplayException.raise_fields (DisplayToplevel.collect ctx TKType NoValue) CRUsing None;
 		in
 		(* do the import first *)
 		let types = (match t.tsub with

+ 1 - 1
src/typing/typer.ml

@@ -1344,7 +1344,7 @@ and handle_efield ctx e p mode =
 									(* if there was no module name part, last guess is that we're trying to get package completion *)
 									if ctx.in_display then begin
 										if ctx.com.json_out = None then raise (Parser.TypePath (List.map (fun (n,_,_) -> n) (List.rev acc),None,false,p))
-										else raise_fields (DisplayToplevel.collect ctx None NoValue) CRTypeHint (Some (Parser.cut_pos_at_display p0));
+										else raise_fields (DisplayToplevel.collect ctx TKType NoValue) CRTypeHint (Some (Parser.cut_pos_at_display p0));
 									end;
 									raise e)
 		in

+ 5 - 5
src/typing/typerDisplay.ml

@@ -125,7 +125,7 @@ let completion_item_of_expr ctx e =
 	in
 	loop e
 
-let raise_toplevel ctx with_type po p =
+let raise_toplevel ctx dk with_type po p =
 	let t = match with_type with
 		| WithType t -> Some t
 		| _ -> None
@@ -134,7 +134,7 @@ let raise_toplevel ctx with_type po p =
 		| None -> None
 		| Some t -> Some (completion_type_of_type ctx t,completion_type_of_type ctx (follow t))
 	in
-	raise_fields (DisplayToplevel.collect ctx (Some p) with_type) (CRToplevel ct) po
+	raise_fields (DisplayToplevel.collect ctx (match dk with DKPattern -> TKPattern p | _ -> TKExpr p) with_type) (CRToplevel ct) po
 
 let rec handle_signature_display ctx e_ast with_type =
 	ctx.in_display <- true;
@@ -321,7 +321,7 @@ and display_expr ctx e_ast e dk with_type p =
 				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;})
 			| _ ->
-				raise_toplevel ctx with_type (Some p) p
+				raise_toplevel ctx dk with_type (Some p) p
 		end
 	| DMDefault | DMNone | DMModuleSymbols _ | DMDiagnostics _ | DMStatistics ->
 		let fields = DisplayFields.collect ctx e_ast e dk with_type p in
@@ -388,14 +388,14 @@ let handle_display ctx e_ast dk with_type =
 		type_expr ctx e_ast with_type
 	with Error (Unknown_ident n,_) when ctx.com.display.dms_kind = DMDefault ->
         if dk = DKDot && ctx.com.json_out = None then raise (Parser.TypePath ([n],None,false,p))
-		else raise_toplevel ctx with_type (Some (Parser.cut_pos_at_display p)) p
+		else raise_toplevel ctx dk with_type (Some (Parser.cut_pos_at_display p)) p
 	| Error ((Type_not_found (path,_) | Module_not_found path),_) as err when ctx.com.display.dms_kind = DMDefault ->
 		if ctx.com.json_out = None then	begin try
 			raise_fields (DisplayFields.get_submodule_fields ctx path) (CRField((make_ci_module path),p)) None
 		with Not_found ->
 			raise err
 		end else
-			raise_toplevel ctx with_type (Some (Parser.cut_pos_at_display p)) p
+			raise_toplevel ctx dk with_type (Some (Parser.cut_pos_at_display p)) p
 	| DisplayException(DisplayFields(l,CRTypeHint,p)) when (match fst e_ast with ENew _ -> true | _ -> false) ->
 		let timer = Timer.timer ["display";"toplevel";"filter ctors"] in
 		ctx.pass <- PBuildClass;