Skip to content

Commit

Permalink
add k=var/method/type/package to field completion, sort fields first …
Browse files Browse the repository at this point in the history
…by kind and then by name (see HaxeFoundation#3287)
  • Loading branch information
nadako committed Sep 3, 2014
1 parent b52001c commit a37c199
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
29 changes: 20 additions & 9 deletions main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ let reserved_flags = [
let complete_fields fields =
let b = Buffer.create 0 in
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";
raise (Completion (Buffer.contents b))

Expand Down Expand Up @@ -1135,7 +1143,7 @@ try
| "classes" ->
pre_compilation := (fun() -> raise (Parser.TypePath (["."],None))) :: !pre_compilation;
| "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" ->
did_something := true;
(try display_memory ctx with e -> prerr_endline (Printexc.get_backtrace ()));
Expand Down Expand Up @@ -1516,15 +1524,15 @@ with
message ctx msg Ast.null_pos
| Typer.DisplayFields fields ->
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
close_times();
let tot = ref 0. in
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
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
else fields
end else
Expand Down Expand Up @@ -1575,7 +1583,10 @@ with
if packs = [] && classes = [] then
error ctx ("No classes found in " ^ String.concat "." p) Ast.null_pos
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) ->
try
let ctx = Typer.create com in
Expand All @@ -1591,7 +1602,7 @@ with
raise e
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 ->
raise (Completion c)
| _ ->
Expand Down
4 changes: 2 additions & 2 deletions tests/misc/projects/Issue3288/with-empty-type.hxml.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<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>

8 changes: 4 additions & 4 deletions tests/misc/projects/Issue3288/with-type.hxml.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<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>

4 changes: 2 additions & 2 deletions tests/misc/projects/Issue3288/without-type.hxml.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<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>

18 changes: 14 additions & 4 deletions typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ type identifier_type =
| ITType of module_type
| 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 WithTypeError of unify_error list * pos
Expand Down Expand Up @@ -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.map (fun mt ->
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
Expand Down Expand Up @@ -3643,7 +3650,10 @@ and handle_display ctx e_ast iscall p =
| _ -> t_dynamic
else
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
let fields = List.fold_left get_field [] fields in
let fields = try
Expand Down Expand Up @@ -4162,7 +4172,7 @@ let make_macro_api ctx p =
"NO COMPLETION"
with DisplayFields fields ->
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 ->
let pctx = print_context() in
String.concat "," (List.map (s_type pctx) tl)
Expand Down

0 comments on commit a37c199

Please sign in to comment.