Browse Source

add k=var/method/type/package to field completion, sort fields first by kind and then by name (see #3287)

Dan Korostelev 11 years ago
parent
commit
a37c199276

+ 20 - 9
main.ml

@@ -150,9 +150,17 @@ let reserved_flags = [
 let complete_fields fields =
 let complete_fields fields =
 	let b = Buffer.create 0 in
 	let b = Buffer.create 0 in
 	Buffer.add_string b "<list>\n";
 	Buffer.add_string b "<list>\n";
-	List.iter (fun (n,t,d) ->
-		Buffer.add_string b (Printf.sprintf "<i n=\"%s\"><t>%s</t><d>%s</d></i>\n" n (htmlescape t) (htmlescape d))
-	) (List.sort (fun (a,_,_) (b,_,_) -> compare a b) fields);
+	List.iter (fun (n,t,k,d) ->
+		let s_kind = match k with
+			| Some k -> (match k with
+				| Typer.FKVar -> "var"
+				| Typer.FKMethod -> "method"
+				| Typer.FKType -> "type"
+				| Typer.FKPackage -> "package")
+			| None -> ""
+		in
+		Buffer.add_string b (Printf.sprintf "<i n=\"%s\" k=\"%s\"><t>%s</t><d>%s</d></i>\n" n s_kind (htmlescape t) (htmlescape d))
+	) (List.sort (fun (a,_,ak,_) (b,_,bk,_) -> compare (ak,a) (bk,b)) fields);
 	Buffer.add_string b "</list>\n";
 	Buffer.add_string b "</list>\n";
 	raise (Completion (Buffer.contents b))
 	raise (Completion (Buffer.contents b))
 
 
@@ -1135,7 +1143,7 @@ try
 			| "classes" ->
 			| "classes" ->
 				pre_compilation := (fun() -> raise (Parser.TypePath (["."],None))) :: !pre_compilation;
 				pre_compilation := (fun() -> raise (Parser.TypePath (["."],None))) :: !pre_compilation;
 			| "keywords" ->
 			| "keywords" ->
-				complete_fields (Hashtbl.fold (fun k _ acc -> (k,"","") :: acc) Lexer.keywords [])
+				complete_fields (Hashtbl.fold (fun k _ acc -> (k,"",None,"") :: acc) Lexer.keywords [])
 			| "memory" ->
 			| "memory" ->
 				did_something := true;
 				did_something := true;
 				(try display_memory ctx with e -> prerr_endline (Printexc.get_backtrace ()));
 				(try display_memory ctx with e -> prerr_endline (Printexc.get_backtrace ()));
@@ -1516,15 +1524,15 @@ with
 		message ctx msg Ast.null_pos
 		message ctx msg Ast.null_pos
 	| Typer.DisplayFields fields ->
 	| Typer.DisplayFields fields ->
 		let ctx = print_context() in
 		let ctx = print_context() in
-		let fields = List.map (fun (name,t,doc) -> name, s_type ctx t, (match doc with None -> "" | Some d -> d)) fields in
+		let fields = List.map (fun (name,t,kind,doc) -> name, s_type ctx t, kind, (match doc with None -> "" | Some d -> d)) fields in
 		let fields = if !measure_times then begin
 		let fields = if !measure_times then begin
 			close_times();
 			close_times();
 			let tot = ref 0. in
 			let tot = ref 0. in
 			Hashtbl.iter (fun _ t -> tot := !tot +. t.total) Common.htimers;
 			Hashtbl.iter (fun _ t -> tot := !tot +. t.total) Common.htimers;
-			let fields = ("@TOTAL", Printf.sprintf "%.3fs" (get_time() -. !start_time), "") :: fields in
+			let fields = ("@TOTAL", Printf.sprintf "%.3fs" (get_time() -. !start_time), None, "") :: fields in
 			if !tot > 0. then
 			if !tot > 0. then
 				Hashtbl.fold (fun _ t acc ->
 				Hashtbl.fold (fun _ t acc ->
-					("@TIME " ^ t.name, Printf.sprintf "%.3fs (%.0f%%)" t.total (t.total *. 100. /. !tot), "") :: acc
+					("@TIME " ^ t.name, Printf.sprintf "%.3fs (%.0f%%)" t.total (t.total *. 100. /. !tot), None, "") :: acc
 				) Common.htimers fields
 				) Common.htimers fields
 			else fields
 			else fields
 		end else
 		end else
@@ -1575,7 +1583,10 @@ with
 			if packs = [] && classes = [] then
 			if packs = [] && classes = [] then
 				error ctx ("No classes found in " ^ String.concat "." p) Ast.null_pos
 				error ctx ("No classes found in " ^ String.concat "." p) Ast.null_pos
 			else
 			else
-				complete_fields (List.map (fun f -> f,"","") (packs @ classes))
+				complete_fields (
+					let convert k f = (f,"",Some k,"") in
+					(List.map (convert Typer.FKPackage) packs) @ (List.map (convert Typer.FKType) classes)
+				)
 		| Some (c,cur_package) ->
 		| Some (c,cur_package) ->
 			try
 			try
 				let ctx = Typer.create com in
 				let ctx = Typer.create com in
@@ -1591,7 +1602,7 @@ with
 							raise e
 							raise e
 				in
 				in
 				let m = lookup p in
 				let m = lookup p in
-				complete_fields (List.map (fun t -> snd (t_path t),"","") (List.filter (fun t -> not (t_infos t).mt_private) m.m_types))
+				complete_fields (List.map (fun t -> snd (t_path t),"",Some Typer.FKType,"") (List.filter (fun t -> not (t_infos t).mt_private) m.m_types))
 			with Completion c ->
 			with Completion c ->
 				raise (Completion c)
 				raise (Completion c)
 			| _ ->
 			| _ ->

+ 2 - 2
tests/misc/projects/Issue3288/with-empty-type.hxml.stderr

@@ -1,5 +1,5 @@
 <list>
 <list>
-<i n="AnotherOne"><t>AnotherOne</t><d></d></i>
-<i n="OtherType"><t>OtherType</t><d></d></i>
+<i n="AnotherOne" k="type"><t>AnotherOne</t><d></d></i>
+<i n="OtherType" k="type"><t>OtherType</t><d></d></i>
 </list>
 </list>
 
 

+ 4 - 4
tests/misc/projects/Issue3288/with-type.hxml.stderr

@@ -1,7 +1,7 @@
 <list>
 <list>
-<i n="AnotherOne"><t>AnotherOne</t><d></d></i>
-<i n="OtherType"><t>OtherType</t><d></d></i>
-<i n="f1"><t>Int</t><d></d></i>
-<i n="f2"><t>Void -&gt; Void</t><d></d></i>
+<i n="f1" k="var"><t>Int</t><d></d></i>
+<i n="f2" k="method"><t>Void -&gt; Void</t><d></d></i>
+<i n="AnotherOne" k="type"><t>AnotherOne</t><d></d></i>
+<i n="OtherType" k="type"><t>OtherType</t><d></d></i>
 </list>
 </list>
 
 

+ 2 - 2
tests/misc/projects/Issue3288/without-type.hxml.stderr

@@ -1,5 +1,5 @@
 <list>
 <list>
-<i n="OtherType"><t>OtherType</t><d></d></i>
-<i n="SomeType"><t>SomeType</t><d></d></i>
+<i n="OtherType" k="type"><t>OtherType</t><d></d></i>
+<i n="SomeType" k="type"><t>SomeType</t><d></d></i>
 </list>
 </list>
 
 

+ 14 - 4
typer.ml

@@ -46,7 +46,14 @@ type identifier_type =
 	| ITType of module_type
 	| ITType of module_type
 	| ITPackage of string
 	| ITPackage of string
 
 
-exception DisplayFields of (string * t * documentation) list
+(* order of these variants affects output sorting *)
+type display_field_kind =
+	| FKVar
+	| FKMethod
+	| FKType
+	| FKPackage
+
+exception DisplayFields of (string * t * display_field_kind option * documentation) list
 exception DisplayToplevel of identifier_type list
 exception DisplayToplevel of identifier_type list
 
 
 exception WithTypeError of unify_error list * pos
 exception WithTypeError of unify_error list * pos
@@ -3441,7 +3448,7 @@ and handle_display ctx e_ast iscall p =
 		let tl = List.filter (fun t -> path <> (t_infos t).mt_path && not (t_infos t).mt_private) m.m_types in
 		let tl = List.filter (fun t -> path <> (t_infos t).mt_path && not (t_infos t).mt_private) m.m_types in
 		let tl = List.map (fun mt ->
 		let tl = List.map (fun mt ->
 			let infos = t_infos mt in
 			let infos = t_infos mt in
-			(snd infos.mt_path),type_of_module_type mt,infos.mt_doc
+			(snd infos.mt_path),type_of_module_type mt,Some FKType,infos.mt_doc
 		) tl in
 		) tl in
 		tl
 		tl
 	in
 	in
@@ -3643,7 +3650,10 @@ and handle_display ctx e_ast iscall p =
 			| _ -> t_dynamic
 			| _ -> t_dynamic
 		else
 		else
 			let get_field acc f =
 			let get_field acc f =
-				List.fold_left (fun acc f -> if f.cf_public then (f.cf_name,f.cf_type,f.cf_doc) :: acc else acc) acc (f :: f.cf_overloads)
+				List.fold_left (fun acc f ->
+					let kind = match f.cf_kind with Method _ -> FKMethod | Var _ -> FKVar in
+					if f.cf_public then (f.cf_name,f.cf_type,Some kind,f.cf_doc) :: acc else acc
+				) acc (f :: f.cf_overloads)
 			in
 			in
 			let fields = List.fold_left get_field [] fields in
 			let fields = List.fold_left get_field [] fields in
 			let fields = try
 			let fields = try
@@ -4162,7 +4172,7 @@ let make_macro_api ctx p =
 				"NO COMPLETION"
 				"NO COMPLETION"
 			with DisplayFields fields ->
 			with DisplayFields fields ->
 				let pctx = print_context() in
 				let pctx = print_context() in
-				String.concat "," (List.map (fun (f,t,_) -> f ^ ":" ^ s_type pctx t) fields)
+				String.concat "," (List.map (fun (f,t,_,_) -> f ^ ":" ^ s_type pctx t) fields)
 			| DisplayTypes tl ->
 			| DisplayTypes tl ->
 				let pctx = print_context() in
 				let pctx = print_context() in
 				String.concat "," (List.map (s_type pctx) tl)
 				String.concat "," (List.map (s_type pctx) tl)