-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathProcessAttributes.ml
75 lines (71 loc) · 2.01 KB
/
ProcessAttributes.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
open SharedTypes
(* TODO should I hang on to location? *)
let rec findDocAttribute attributes =
let open Parsetree in
match attributes with
| [] -> None
| ( {Asttypes.txt = "ocaml.doc" | "ocaml.text" | "ns.doc" | "res.doc"},
PStr
[
{
pstr_desc =
Pstr_eval ({pexp_desc = Pexp_constant (Pconst_string (doc, _))}, _);
};
] )
:: _ ->
Some doc
| _ :: rest -> findDocAttribute rest
let rec findDeprecatedAttribute attributes =
let open Parsetree in
match attributes with
| [] -> None
| ( {Asttypes.txt = "deprecated"},
PStr
[
{
pstr_desc =
Pstr_eval ({pexp_desc = Pexp_constant (Pconst_string (msg, _))}, _);
};
] )
:: _ ->
Some msg
| ({Asttypes.txt = "deprecated"}, _) :: _ -> Some ""
| _ :: rest -> findDeprecatedAttribute rest
let newDeclared ~item ~extent ~name ~stamp ~modulePath isExported attributes =
{
Declared.name;
stamp;
extentLoc = extent;
isExported;
modulePath;
deprecated = findDeprecatedAttribute attributes;
docstring =
(match findDocAttribute attributes with
| None -> []
| Some d -> [d]);
item;
}
let rec findEditorCompleteFromAttribute ?(modulePaths = []) attributes =
let open Parsetree in
match attributes with
| [] -> modulePaths
| ( {Asttypes.txt = "editor.completeFrom"},
PStr [{pstr_desc = Pstr_eval (payloadExpr, _)}] )
:: rest ->
let items =
match payloadExpr with
| {pexp_desc = Pexp_array items} -> items
| p -> [p]
in
let modulePathsFromArray =
items
|> List.filter_map (fun item ->
match item.Parsetree.pexp_desc with
| Pexp_construct ({txt = path}, None) ->
Some (Utils.flattenLongIdent path)
| _ -> None)
in
findEditorCompleteFromAttribute
~modulePaths:(modulePathsFromArray @ modulePaths)
rest
| _ :: rest -> findEditorCompleteFromAttribute ~modulePaths rest