Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom metadata and defines (documentation, completion) #10858

Merged
merged 31 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
17ad027
Add after_generation args context hook
kLabz Nov 19, 2022
171a299
Add support for user-defined custom metadata (doc + completion)
kLabz Nov 19, 2022
4f07bfc
Add support for user-defined custom defines (documentation)
kLabz Nov 19, 2022
de3a075
Remove dead code
kLabz Nov 19, 2022
e55b543
Add meta/define source
kLabz Nov 20, 2022
86b3325
A bit of cleanup
kLabz Nov 20, 2022
4483e6d
Further cleanup
kLabz Nov 20, 2022
b946832
Move parse_meta_usage to metaList (via prebuild)
kLabz Nov 20, 2022
2e4adb7
Sort metadatas before sending to displayEmitter
kLabz Nov 20, 2022
b11c863
Fix metadata/define source
kLabz Nov 22, 2022
2de5d1e
Display source (when available) in metadata/define documentation
kLabz Nov 22, 2022
613675a
Send source in completionItem too
kLabz Nov 22, 2022
d1b1992
Add from: in custom meta/define source
kLabz Nov 22, 2022
8496dab
Add tests for custom defines/metas
kLabz Nov 22, 2022
94179f3
Expose std path to misc tests
kLabz Nov 23, 2022
fff4b5d
Fix tests for windows
kLabz Nov 23, 2022
136e647
Revert "Expose std path to misc tests"
kLabz Nov 23, 2022
4118d69
Remove expected output for some failing tests
kLabz Nov 23, 2022
77bf3fe
Rename Compiler metadata/defines functions
kLabz Nov 23, 2022
50970d9
Update tests
kLabz Nov 23, 2022
8eb1278
Misc tests: check stdout against .hxml.stdout file if it exists
kLabz Nov 23, 2022
30d85ed
Update haxelib
kLabz Nov 23, 2022
dbd713f
Revert "Add after_generation args context hook"
kLabz Nov 24, 2022
85394fc
Execute --help-user-metas and --help-user-defines right after init ma…
kLabz Nov 24, 2022
6441b81
Add tests for --help-user-xxx commands vs failing compilation
kLabz Nov 24, 2022
ca74a67
Some cleanup
kLabz Nov 26, 2022
f7a0326
Use origin instead of source
kLabz Nov 26, 2022
edc93f7
Move user defines to common context
kLabz Nov 27, 2022
3adc702
Move user metas to common context
kLabz Nov 26, 2022
82814a9
Use Platform and MetadataTarget enum abstracts
kLabz Nov 27, 2022
693f80d
Add basic documentation for MetadataDescription / DefineDescription
kLabz Nov 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extra/haxelib_src
Submodule haxelib_src updated 37 files
+1 −1 .devcontainer/docker-compose.yml
+4 −13 .github/workflows/ci-dev.yml
+61 −27 Earthfile
+ run.n
+15 −0 schema.json
+2 −0 src/Package.hx
+71 −1 src/haxelib/Data.hx
+7 −5 src/haxelib/api/Connection.hx
+21 −3 src/haxelib/api/GlobalScope.hx
+36 −33 src/haxelib/api/Installer.hx
+25 −6 src/haxelib/api/LibFlagData.hx
+1 −1 src/haxelib/server/Repo.hx
+1 −1 terraform/.terraform-version
+96 −90 terraform/.terraform.lock.hcl
+35 −17 terraform/aws-s3.tf
+19 −37 terraform/do-spaces.tf
+13 −5 terraform/haxelib-server.tf
+10 −3 terraform/mysql.tf
+6 −6 terraform/providers.tf
+3 −3 test/RunCi.hx
+0 −0 test/libraries/InstallDeps/cpp.hxml
+4 −0 test/libraries/InstallDeps/target-lib-uppercase.hxml
+1 −1 test/libraries/InstallDeps/target-lib.hxml
+5 −0 test/libraries/libBadDefineJson/doc/defines.json
+13 −0 test/libraries/libBadDefineJson/haxelib.json
+1 −0 test/libraries/libBadMetaJson/doc/meta.json
+13 −0 test/libraries/libBadMetaJson/haxelib.json
+5 −0 test/libraries/libBadMetaJson2/doc/meta.json
+13 −0 test/libraries/libBadMetaJson2/haxelib.json
+13 −0 test/libraries/libDocumentationFiles/doc/defines.json
+14 −0 test/libraries/libDocumentationFiles/doc/meta.json
+14 −0 test/libraries/libDocumentationFiles/haxelib.json
+13 −0 test/libraries/libMissingMetaJson/haxelib.json
+21 −0 test/tests/TestData.hx
+15 −5 test/tests/TestInstaller.hx
+4 −2 test/tests/integration/TestUser.hx
+23 −20 www/documentation-files/using-haxelib.md
17 changes: 15 additions & 2 deletions src-prebuild/prebuild.ml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ type meta_usage =
| TTypeParameter
| TVariable

let parse_meta_usage = function
| \"TClass\" -> TClass
| \"TClassField\" -> TClassField
| \"TAbstract\" -> TAbstract
| \"TAbstractField\" -> TAbstractField
| \"TEnum\" -> TEnum
| \"TTypedef\" -> TTypedef
| \"TAnyField\" -> TAnyField
| \"TExpr\" -> TExpr
| \"TTypeParameter\" -> TTypeParameter
| \"TVariable\" -> TVariable
| t -> raise (failwith (\"invalid metadata target \" ^ t))

type meta_parameter =
| HasParam of string
| Platforms of platform list
Expand All @@ -276,10 +289,10 @@ match Array.to_list (Sys.argv) with
Printf.printf "%s" define_header;
Printf.printf "type strict_defined =\n";
Printf.printf "%s" (gen_define_type defines);
Printf.printf "\n\t| Last\n\n"; (* must be last *)
Printf.printf "\n\t| Last\n\t| Custom of string\n\n";
Printf.printf "let infos = function\n";
Printf.printf "%s" (gen_define_info defines);
Printf.printf "\n\t| Last -> die \"\" __LOC__\n"
Printf.printf "\n\t| Last -> die \"\" __LOC__\n\t| Custom s -> s,(\"\",[])\n"
| [_; "meta"; meta_path]->
let metas = parse_file_array meta_path parse_meta in
Printf.printf "%s" meta_header;
Expand Down
20 changes: 18 additions & 2 deletions src/compiler/args.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,33 @@ let parse_args com =
raise (Arg.Help "")
),"","show extended help information");
("Miscellaneous",["--help-defines"],[], Arg.Unit (fun() ->
let all,max_length = Define.get_documentation_list() in
let all,max_length = Define.get_documentation_list com.user_defines in
let all = List.map (fun (n,doc) -> Printf.sprintf " %-*s: %s" max_length n (limit_string doc (max_length + 3))) all in
List.iter (fun msg -> com.print (msg ^ "\n")) all;
actx.did_something <- true
),"","print help for all compiler specific defines");
("Miscellaneous",["--help-user-defines"],[], Arg.Unit (fun() ->
com.callbacks#add_after_init_macros (fun() ->
let all,max_length = Define.get_user_documentation_list com.user_defines in
let all = List.map (fun (n,doc) -> Printf.sprintf " %-*s: %s" max_length n (limit_string doc (max_length + 3))) all in
List.iter (fun msg -> com.print (msg ^ "\n")) all;
exit 0
)
),"","print help for all user defines");
("Miscellaneous",["--help-metas"],[], Arg.Unit (fun() ->
let all,max_length = Meta.get_documentation_list() in
let all,max_length = Meta.get_documentation_list com.user_metas in
let all = List.map (fun (n,doc) -> Printf.sprintf " %-*s: %s" max_length n (limit_string doc (max_length + 3))) all in
List.iter (fun msg -> com.print (msg ^ "\n")) all;
actx.did_something <- true
),"","print help for all compiler metadatas");
("Miscellaneous",["--help-user-metas"],[], Arg.Unit (fun() ->
com.callbacks#add_after_init_macros (fun() ->
let all,max_length = Meta.get_user_documentation_list com.user_metas in
let all = List.map (fun (n,doc) -> Printf.sprintf " %-*s: %s" max_length n (limit_string doc (max_length + 3))) all in
List.iter (fun msg -> com.print (msg ^ "\n")) all;
exit 0
)
),"","print help for all user metadatas");
] in
let adv_args_spec = [
("Optimization",["--dce"],["-dce"],Arg.String (fun mode ->
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,4 @@ module HighLevel = struct
in
let code = loop args in
comm.exit code
end
end
2 changes: 1 addition & 1 deletion src/compiler/displayOutput.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let print_fields fields =
| ITPackage(path,_) -> "package",snd path,"",None
| ITModule path -> "type",snd path,"",None
| ITMetadata meta ->
let s,(doc,_) = Meta.get_info meta in
let s,(doc,_),_ = Meta.get_info meta in
"metadata","@" ^ s,"",doc_from_string doc
| ITTimer(name,value) -> "timer",name,"",doc_from_string value
| ITLiteral s ->
Expand Down
6 changes: 5 additions & 1 deletion src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ type context = {
mutable load_extern_type : (string * (path -> pos -> Ast.package option)) list; (* allow finding types which are not in sources *)
callbacks : compiler_callbacks;
defines : Define.define;
mutable user_defines : (string, Define.user_define) Hashtbl.t;
mutable user_metas : (string, Meta.user_meta) Hashtbl.t;
mutable get_macros : unit -> context option;
(* typing state *)
shared : shared_context;
Expand Down Expand Up @@ -543,7 +545,7 @@ let default_config =
}

let get_config com =
let defined f = PMap.mem (fst (Define.infos f)) com.defines.values in
let defined f = PMap.mem (Define.get_define_key f) com.defines.values in
match com.platform with
| Cross ->
default_config
Expand Down Expand Up @@ -809,6 +811,8 @@ let create compilation_step cs version args =
defines_signature = None;
values = PMap.empty;
};
user_defines = Hashtbl.create 0;
user_metas = Hashtbl.create 0;
get_macros = (fun() -> None);
info = (fun _ _ -> die "" __LOC__);
warning = (fun _ _ _ -> die "" __LOC__);
Expand Down
4 changes: 2 additions & 2 deletions src/context/display/displayEmitter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ let display_meta com meta p = match com.display.dms_kind with
begin match meta with
| Meta.Custom _ | Meta.Dollar _ -> ()
| _ ->
if com.json_out = None then begin match Meta.get_documentation meta with
if com.json_out = None then begin match Meta.get_documentation com.user_metas meta with
| None -> ()
| Some (_,s) ->
raise_metadata ("<metadata>" ^ s ^ "</metadata>")
end else
raise_hover (make_ci_metadata meta) None p
end
| DMDefault ->
let all = Meta.get_all() in
let all = Meta.get_all com.user_metas in
let all = List.map make_ci_metadata all in
let subject = if meta = Meta.HxCompletion then None else Some (Meta.to_string meta) in
raise_fields all CRMetadata (make_subject subject p);
Expand Down
2 changes: 1 addition & 1 deletion src/core/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ module Expr = struct
add "ECheckType";
loop e1;
| EMeta((m,_,_),e1) ->
add ("EMeta " ^ fst (Meta.get_info m));
add ("EMeta " ^ (Meta.to_string m));
loop e1
in
loop' "" e;
Expand Down
76 changes: 59 additions & 17 deletions src/core/define.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,76 @@ type define = {
mutable defines_signature : string option;
}

type user_define = {
doc : string;
flags : define_parameter list;
source : string option;
}

let register_user_define user_defines s data =
Hashtbl.replace user_defines s data

type define_origin =
| Compiler
| UserDefined of string option

let infos ?user_defines d = match (user_defines,d) with
| (Some(user_defines), Custom(s)) when (Hashtbl.mem user_defines s) ->
let infos = Hashtbl.find user_defines s in
(s, (infos.doc, infos.flags), (UserDefined infos.source))
| (_, Custom(s)) ->
(s, ("", []), Compiler)
| _ ->
kLabz marked this conversation as resolved.
Show resolved Hide resolved
let def,infos = DefineList.infos d in
(def, infos, Compiler)

let get_define_key d =
fst (infos d)
match (infos d) with (s,_,_) -> s

let get_documentation_list() =
let get_documentation user_defines d =
let t, (doc,flags), origin = infos ~user_defines:user_defines d in
let params = ref [] and pfs = ref [] in
List.iter (function
| HasParam s -> params := s :: !params
| Platforms fl -> pfs := fl @ !pfs
| Link _ -> ()
) flags;
let params = (match List.rev !params with
| [] -> ""
| l -> "<" ^ String.concat ">, <" l ^ "> "
) in
let origin = match origin with
| UserDefined Some s -> " (from " ^ s ^ ")"
| Compiler | UserDefined None -> ""
in
let pfs = platform_list_help (List.rev !pfs) in
(String.concat "-" (ExtString.String.nsplit t "_")), params ^ doc ^ pfs ^ origin

let get_documentation_list user_defines =
let m = ref 0 in
let rec loop i =
let d = Obj.magic i in
if d <> Last then begin
let t, (doc,flags) = infos d in
let params = ref [] and pfs = ref [] in
List.iter (function
| HasParam s -> params := s :: !params
| Platforms fl -> pfs := fl @ !pfs
| Link _ -> ()
) flags;
let params = (match List.rev !params with
| [] -> ""
| l -> "<" ^ String.concat ">, <" l ^ "> "
) in
let pfs = platform_list_help (List.rev !pfs) in
if String.length t > !m then m := String.length t;
((String.concat "-" (ExtString.String.nsplit t "_")), params ^ doc ^ pfs) :: (loop (i + 1))
let (str,desc) = get_documentation user_defines d in
if String.length str > !m then m := String.length str;
(str,desc) :: loop (i + 1)
end else
[]
in
let all = List.sort (fun (s1,_) (s2,_) -> String.compare s1 s2) (loop 0) in
all,!m

let get_user_documentation_list user_defines =
let m = ref 0 in
let user_defines_list = (Hashtbl.fold (fun d _ acc ->
let (str,desc) = get_documentation user_defines (Custom d) in
if String.length str > !m then m := String.length str;
(str,desc) :: acc
) user_defines []) in

let all = List.sort (fun (s1,_) (s2,_) -> String.compare s1 s2) user_defines_list in
all,!m

let raw_defined ctx k =
PMap.mem k ctx.values

Expand All @@ -44,7 +86,7 @@ let raw_defined_value ctx k =
PMap.find k ctx.values

let defined_value ctx v =
raw_defined_value ctx (fst (infos v))
raw_defined_value ctx (get_define_key v)

let defined_value_safe ?default ctx v =
try defined_value ctx v
Expand Down
9 changes: 7 additions & 2 deletions src/core/display/completionItem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ let to_json ctx index item =
]
| ITMetadata meta ->
let open Meta in
let name,(doc,params) = Meta.get_info meta in
let name,(doc,params),origin = Meta.get_info meta in
let name = "@" ^ name in
let usage_to_string = function
| TClass -> "TClass"
Expand All @@ -773,6 +773,10 @@ let to_json ctx index item =
| TTypeParameter -> "TTypeParameter"
| TVariable -> "TVariable"
in
let origin = match origin with
| Compiler -> Some "haxe compiler"
| UserDefined s -> s
in
let rec loop internal params platforms targets links l = match l with
| HasParam s :: l -> loop internal (s :: params) platforms targets links l
| Platforms pls :: l -> loop internal params ((List.map platform_name pls) @ platforms) targets links l
Expand All @@ -790,6 +794,7 @@ let to_json ctx index item =
"targets",jlist jstring targets;
"internal",jbool internal;
"links",jlist jstring links;
"origin",jopt jstring origin;
]
| ITKeyword kwd ->"Keyword",jobject [
"name",jstring (s_keyword kwd)
Expand Down Expand Up @@ -828,4 +833,4 @@ let to_json ctx index item =
| Some t ->
("type",CompletionType.to_json ctx (snd t)) :: jindex
)
)
)
15 changes: 15 additions & 0 deletions src/core/globals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ let platform_name = function
| Hl -> "hl"
| Eval -> "eval"

let parse_platform = function
| "cross" -> Cross
| "js" -> Js
| "lua" -> Lua
| "neko" -> Neko
| "flash" -> Flash
| "php" -> Php
| "cpp" -> Cpp
| "cs" -> Cs
| "java" -> Java
| "python" -> Python
| "hl" -> Hl
| "eval" -> Eval
| p -> raise (failwith ("invalid platform " ^ p))

let platform_list_help = function
| [] -> ""
| [p] -> " (" ^ platform_name p ^ " only)"
Expand Down
2 changes: 1 addition & 1 deletion src/core/json/genjson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ and generate_metadata_entry ctx (m,el,p) =

and generate_metadata ctx ml =
let ml = List.filter (fun (m,_,_) ->
let (_,(_,flags)) = Meta.get_info m in
let (_,(_,flags),_) = Meta.get_info m in
not (List.mem UsedInternally flags)
) ml in
jlist (generate_metadata_entry ctx) ml
Expand Down
Loading