Browse Source

add import completion for statics (see #2263)

Simon Krajewski 10 years ago
parent
commit
6b3befa912
4 changed files with 40 additions and 17 deletions
  1. 26 3
      main.ml
  2. 10 10
      parser.ml
  3. 1 1
      typeload.ml
  4. 3 3
      typer.ml

+ 26 - 3
main.ml

@@ -1212,7 +1212,7 @@ try
 		("--display", Arg.String (fun file_pos ->
 			match file_pos with
 			| "classes" ->
-				pre_compilation := (fun() -> raise (Parser.TypePath (["."],None))) :: !pre_compilation;
+				pre_compilation := (fun() -> raise (Parser.TypePath (["."],None,true))) :: !pre_compilation;
 			| "keywords" ->
 				complete_fields com (Hashtbl.fold (fun k _ acc -> (k,"",None,"") :: acc) Lexer.keywords [])
 			| "memory" ->
@@ -1673,7 +1673,7 @@ with
 		) il;
 		Buffer.add_string b "</il>";
 		raise (Completion (Buffer.contents b))
-	| Parser.TypePath (p,c) ->
+	| Parser.TypePath (p,c,is_import) ->
 		(match c with
 		| None ->
 			let packs, classes = read_type_path com p in
@@ -1699,7 +1699,30 @@ with
 							raise e
 				in
 				let m = lookup p in
-				complete_fields com (List.map (fun t -> snd (t_path t),"",Some Typer.FKType,"") (List.filter (fun t -> not (t_infos t).mt_private) m.m_types))
+				let statics = ref None in
+				let public_types = List.filter (fun t ->
+					let tinfos = t_infos t in
+					if snd tinfos.mt_path = c then begin match t with
+						| TClassDecl c ->
+							ignore(c.cl_build());
+							statics := Some c.cl_ordered_statics
+						| _ -> ()
+					end;
+					not tinfos.mt_private
+				) m.m_types in
+				let types = List.map (fun t -> snd (t_path t),"",Some Typer.FKType,"") public_types in
+				let ctx = print_context() in
+				let make_field_doc cf =
+					cf.cf_name,
+					s_type ctx cf.cf_type,
+					Some (match cf.cf_kind with Method _ -> Typer.FKMethod | Var _ -> Typer.FKVar),
+					(match cf.cf_doc with Some s -> s | None -> "")
+				in
+				let types = match !statics with
+					| None -> types
+					| Some cfl -> types @ (List.map make_field_doc (List.filter (fun cf -> cf.cf_public) cfl))
+				in
+				complete_fields com types
 			with Completion c ->
 				raise (Completion c)
 			| _ ->

+ 10 - 10
parser.ml

@@ -32,7 +32,7 @@ type error_msg =
 	| Custom of string
 
 exception Error of error_msg * pos
-exception TypePath of string list * (string * bool) option
+exception TypePath of string list * (string * bool) option * bool (* in import *)
 exception Display of expr
 
 let error_msg = function
@@ -566,17 +566,17 @@ and parse_type_decls pack acc s =
 		match s with parser
 		| [< v = parse_type_decl; l = parse_type_decls pack (v :: acc) >] -> l
 		| [< >] -> List.rev acc
-	with TypePath ([],Some (name,false)) ->
+	with TypePath ([],Some (name,false),b) ->
 		(* resolve imports *)
 		List.iter (fun d ->
 			match fst d with
 			| EImport (t,_) ->
 				(match List.rev t with
-				| (n,_) :: path when n = name && List.for_all (fun (i,_) -> is_lower_ident i) path -> raise (TypePath (List.map fst (List.rev path),Some (name,false)))
+				| (n,_) :: path when n = name && List.for_all (fun (i,_) -> is_lower_ident i) path -> raise (TypePath (List.map fst (List.rev path),Some (name,false),b))
 				| _ -> ())
 			| _ -> ()
 		) acc;
-		raise (TypePath (pack,Some(name,true)))
+		raise (TypePath (pack,Some(name,true),b))
 
 and parse_type_decl s =
 	match s with parser
@@ -645,8 +645,8 @@ and parse_import s p1 =
 		| [< '(Dot,p) >] ->
 			let resume() =
 				match acc with
-				| (n,_) :: l when n.[0] >= 'A' && n.[0] <= 'Z' -> raise (TypePath (List.rev (List.map fst l),Some (n,false)));
-				| _ -> raise (TypePath (List.rev (List.map fst acc),None));
+				| (n,_) :: l when n.[0] >= 'A' && n.[0] <= 'Z' -> raise (TypePath (List.rev (List.map fst l),Some (n,false),true));
+				| _ -> raise (TypePath (List.rev (List.map fst acc),None,true));
 			in
 			if is_resuming p then resume();
 			(match s with parser
@@ -835,7 +835,7 @@ and parse_type_path1 pack = parser
 			(match s with parser
 			| [< '(Dot,p) >] ->
 				if is_resuming p then
-					raise (TypePath (List.rev (name :: pack),None))
+					raise (TypePath (List.rev (name :: pack),None,false))
 				else
 					parse_type_path1 (name :: pack) s
 			| [< '(Semicolon,_) >] ->
@@ -845,12 +845,12 @@ and parse_type_path1 pack = parser
 			let sub = (match s with parser
 				| [< '(Dot,p); s >] ->
 					(if is_resuming p then
-						raise (TypePath (List.rev pack,Some (name,false)))
+						raise (TypePath (List.rev pack,Some (name,false),false))
 					else match s with parser
 						| [< '(Const (Ident name),_) when not (is_lower_ident name) >] -> Some name
 						| [< '(Binop OpOr,_) when do_resume() >] ->
 							set_resume p;
-							raise (TypePath (List.rev pack,Some (name,false)))
+							raise (TypePath (List.rev pack,Some (name,false),false))
 						| [< >] -> serror())
 				| [< >] -> None
 			) in
@@ -865,7 +865,7 @@ and parse_type_path1 pack = parser
 				tsub = sub;
 			}
 	| [< '(Binop OpOr,_) when do_resume() >] ->
-		raise (TypePath (List.rev pack,None))
+		raise (TypePath (List.rev pack,None,false))
 
 and type_name = parser
 	| [< '(Const (Ident name),p) >] ->

+ 1 - 1
typeload.ml

@@ -1350,7 +1350,7 @@ let type_function ctx args ret fmode f do_display p =
 			if Common.defined ctx.com Define.NoCOpt then raise Exit;
 			type_expr ctx (Optimizer.optimize_completion_expr e) NoValue
 		with
-		| Parser.TypePath (_,None) | Exit ->
+		| Parser.TypePath (_,None,_) | Exit ->
 			type_expr ctx e NoValue
 		| DisplayTypes [t] when (match follow t with TMono _ -> true | _ -> false) ->
 			type_expr ctx (if ctx.com.display = DMToplevel then find_enclosing ctx.com e else e) NoValue

+ 3 - 3
typer.ml

@@ -2537,7 +2537,7 @@ and type_access ctx e p mode =
 									raise (Error (Module_not_found (List.rev !path,name),p))
 								with
 									Not_found ->
-										if ctx.in_display then raise (Parser.TypePath (List.map (fun (n,_,_) -> n) (List.rev acc),None));
+										if ctx.in_display then raise (Parser.TypePath (List.map (fun (n,_,_) -> n) (List.rev acc),None,false));
 										raise e)
 				| (_,false,_) as x :: path ->
 					loop (x :: acc) path
@@ -3538,7 +3538,7 @@ and handle_display ctx e_ast iscall p =
 	let e = try
 		type_expr ctx e_ast Value
 	with Error (Unknown_ident n,_) when not iscall ->
-		raise (Parser.TypePath ([n],None))
+		raise (Parser.TypePath ([n],None,false))
 	| Error (Unknown_ident "trace",_) ->
 		raise (DisplayTypes [tfun [t_dynamic] ctx.com.basic.tvoid])
 	| Error (Type_not_found (path,_),_) as err ->
@@ -4267,7 +4267,7 @@ let make_macro_api ctx p =
 			| DisplayTypes tl ->
 				let pctx = print_context() in
 				String.concat "," (List.map (s_type pctx) tl)
-			| Parser.TypePath (p,sub) ->
+			| Parser.TypePath (p,sub,_) ->
 				(match sub with
 				| None ->
 					"path(" ^ String.concat "." p ^ ")"