-
Notifications
You must be signed in to change notification settings - Fork 16
/
Oracle.elm
88 lines (73 loc) · 2.54 KB
/
Oracle.elm
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
76
77
78
79
80
81
82
83
84
85
86
87
88
module Oracle where
import Json.Encode
import String
import Set
import Dict
import Import
import Package
search : String -> String -> List (String, String) -> String
search query source docs =
let infos = collate (Import.parse source) (Package.parse docs) query
in
List.map encodeInfo infos
|> Json.Encode.list
|> Json.Encode.encode 0
type alias Info =
{ name : String
, fullName : String
, href : String
, signature : String
, comment : String
}
encodeInfo : Info -> Json.Encode.Value
encodeInfo info =
Json.Encode.object
[ ("name", Json.Encode.string (info.name))
, ("fullName", Json.Encode.string (info.fullName))
, ("href", Json.Encode.string (info.href))
, ("signature", Json.Encode.string (info.signature))
, ("comment", Json.Encode.string (info.comment))
]
collate : Dict.Dict String Import.Import -> Package.Package -> String -> List Info
collate imports moduleList filterName =
let getInfo modul =
Maybe.map (moduleToDocs modul) (Dict.get modul.name imports)
accept (token, info) =
if String.startsWith filterName token
then Just info
else Nothing
in
List.filterMap getInfo moduleList
|> List.concat
|> List.filterMap accept
moduleToDocs : Package.Module -> Import.Import -> List (String, Info)
moduleToDocs modul { alias, exposed } =
let dotToHyphen string =
String.map (\c -> if c == '.' then '-' else c) string
urlTo name =
"http://package.elm-lang.org/packages/"
++ modul.packageName ++ "/latest/" ++ dotToHyphen modul.name ++ "#" ++ name
nameToPair vname =
let name = vname.name
fullName = modul.name ++ "." ++ name
info = Info name fullName (urlTo name) vname.signature vname.comment
localName = Maybe.withDefault modul.name alias ++ "." ++ name
pairs = [(localName, info)]
in
case exposed of
Import.None ->
pairs
Import.Some set ->
if Set.member name set then (name, info) :: pairs else pairs
Import.Every ->
(name, info) :: pairs
typeToPair type' tag =
let fullName = modul.name ++ "." ++ tag
info = Info tag fullName (urlTo type') "" ""
in
[ (tag, info)
, (fullName, info)
]
in
List.concatMap nameToPair (modul.values.aliases ++ modul.values.values)
++ List.concatMap (\(type', tags) -> List.concatMap (typeToPair type') tags) modul.values.types