Skip to content

Commit 0340e46

Browse files
authored
fix #8 (#9)
1 parent c3d51d6 commit 0340e46

File tree

3 files changed

+58
-48
lines changed

3 files changed

+58
-48
lines changed

src/Docs/Search/IndexBuilder.purs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import Data.Argonaut.Encode (encodeJson)
1616
import Data.Argonaut.Parser (jsonParser)
1717
import Data.Array as Array
1818
import Data.Either (Either(..))
19+
import Data.Foldable (sum)
1920
import Data.List (List)
2021
import Data.List as List
2122
import Data.Map (Map)
2223
import Data.Map as Map
23-
import Data.Maybe (Maybe(..))
24+
import Data.Maybe (Maybe(..), fromMaybe)
2425
import Data.Newtype (unwrap)
2526
import Data.Search.Trie as Trie
2627
import Data.Set as Set
@@ -29,7 +30,7 @@ import Data.String.CodeUnits (singleton) as String
2930
import Data.String.Common (replace) as String
3031
import Data.String.Pattern (Pattern(..), Replacement(..))
3132
import Data.Traversable (for, for_)
32-
import Data.Tuple (Tuple(..), fst, snd)
33+
import Data.Tuple (Tuple(..), fst)
3334
import Effect (Effect)
3435
import Effect.Aff (Aff, launchAff_, parallel, sequential)
3536
import Effect.Class (liftEffect)
@@ -60,7 +61,7 @@ run' cfg = do
6061
log $ "Found " <> show (Array.length docsJsons) <> " modules."
6162

6263
let index = mkDeclarations docsJsons
63-
typeIndex = mkTypeIndex index
64+
typeIndex = mkTypeIndex docsJsons
6465

6566
createDirectories cfg
6667

@@ -70,12 +71,16 @@ run' cfg = do
7071
<*> parallel (patchDocs cfg)
7172
<*> parallel (copyAppFile cfg)
7273

74+
let countOfDefinitions = Trie.size $ unwrap index
75+
countOfTypeDefinitions =
76+
sum $ fromMaybe 0 <$> map Array.length <$> Map.values (unwrap typeIndex)
77+
7378
liftEffect do
7479
log $
7580
"Added " <>
76-
show (Trie.size $ unwrap index) <>
81+
show countOfDefinitions <>
7782
" definitions and " <>
78-
show (List.length $ join $ map snd $ Trie.entriesUnordered (unwrap index)) <>
83+
show countOfTypeDefinitions <>
7984
" type definitions to the search index."
8085

8186
where ignore _ _ _ _ = unit

src/Docs/Search/Interactive.purs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ import Docs.Search.IndexBuilder as IndexBuilder
1010
import Docs.Search.SearchResult (ResultInfo(..), SearchResult(..))
1111
import Docs.Search.Terminal (bold, cyan, green, yellow)
1212
import Docs.Search.TypeDecoder (Constraint, FunDeps, Kind, QualifiedName, Type, TypeArgument)
13-
import Docs.Search.TypeIndex (mkTypeIndex)
13+
import Docs.Search.TypeIndex (resultsWithTypes)
1414
import Docs.Search.TypePrinter (keyword, showConstraint, showFunDeps, showKind, showType, showTypeArgument, space, syntax)
1515
import Docs.Search.TypeQuery (parseTypeQuery)
1616

1717
import Data.Array as Array
1818
import Data.Either (hush)
1919
import Data.List as List
20-
import Data.Map as Map
2120
import Data.Maybe (Maybe(..), fromMaybe)
2221
import Data.Newtype (unwrap, wrap)
2322
import Data.Search.Trie as Trie
2423
import Data.String (length) as String
2524
import Data.String.CodeUnits (fromCharArray, toCharArray) as String
2625
import Data.String.Common (split, toLower, trim) as String
27-
import Data.Tuple (snd, fst)
26+
import Data.Tuple (fst)
2827
import Effect (Effect)
2928
import Effect.Aff (launchAff_)
3029
import Effect.Class (liftEffect)
@@ -42,17 +41,19 @@ run cfg = launchAff_ $ do
4241
docsJsons <- IndexBuilder.decodeDocsJsons cfg
4342

4443
let index = mkDeclarations docsJsons
45-
typeIndex = Array.concat $ Array.fromFoldable (
46-
Map.values (unwrap (mkTypeIndex index)) <#> fromMaybe []
47-
) :: Array SearchResult
44+
typeIndex = docsJsons >>= resultsWithTypes
45+
46+
let countOfDefinitions = Trie.size $ unwrap index
47+
countOfTypeDefinitions =
48+
Array.length typeIndex
4849

4950
liftEffect do
5051
log $
5152
"Loaded " <>
52-
show (Trie.size $ unwrap index) <>
53+
show countOfDefinitions <>
5354
" definitions and " <>
54-
show (List.length $ join $ map snd $ Trie.entriesUnordered (unwrap index)) <>
55-
" type definitions"
55+
show countOfTypeDefinitions <>
56+
" type definitions."
5657

5758
liftEffect do
5859
let

src/Docs/Search/TypeIndex.purs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module Docs.Search.TypeIndex where
33
import Prelude
44

55
import Docs.Search.Config (config)
6-
import Docs.Search.Declarations (Declarations(..))
6+
import Docs.Search.Declarations (resultsForEntry)
7+
import Docs.Search.DocsJson (DocsJson(..))
78
import Docs.Search.SearchResult (ResultInfo(..), SearchResult)
9+
import Docs.Search.TypeDecoder (Type)
810
import Docs.Search.TypeQuery (TypeQuery)
911
import Docs.Search.TypeShape (shapeOfType, shapeOfTypeQuery, stringifyShape)
1012

@@ -13,14 +15,10 @@ import Data.Argonaut.Core (Json)
1315
import Data.Argonaut.Decode (decodeJson)
1416
import Data.Array as Array
1517
import Data.Either (hush)
16-
import Data.List (List)
17-
import Data.List as List
1818
import Data.Map (Map)
1919
import Data.Map as Map
20-
import Data.Maybe (Maybe(..), fromMaybe')
20+
import Data.Maybe (Maybe(..), fromMaybe', isJust)
2121
import Data.Newtype (class Newtype, unwrap, wrap)
22-
import Data.Search.Trie as Trie
23-
import Data.Tuple (Tuple(..), snd)
2422
import Effect (Effect)
2523
import Effect.Aff (Aff, try)
2624

@@ -30,41 +28,39 @@ derive newtype instance semigroupTypeIndex :: Semigroup TypeIndex
3028
derive newtype instance monoidTypeIndex :: Monoid TypeIndex
3129
derive instance newtypeTypeIndex :: Newtype TypeIndex _
3230

33-
insert
34-
:: String
35-
-> Maybe (Array SearchResult)
36-
-> TypeIndex
37-
-> TypeIndex
38-
insert key value = unwrap >>> Map.insert key value >>> wrap
39-
40-
mkTypeIndex :: Declarations -> TypeIndex
41-
mkTypeIndex (Declarations trie) = TypeIndex $ map (Array.fromFoldable >>> Just) types
31+
mkTypeIndex :: Array DocsJson -> TypeIndex
32+
mkTypeIndex docsJsons =
33+
TypeIndex $ map Just $ Array.foldr insert mempty docsJsons
4234
where
43-
insertTypes
44-
:: Tuple String SearchResult
45-
-> Map String (List SearchResult)
46-
-> Map String (List SearchResult)
47-
insertTypes (Tuple shape result) =
48-
Map.insertWith append shape (List.singleton result)
35+
insert :: DocsJson -> Map String (Array SearchResult) -> Map String (Array SearchResult)
36+
insert docsJson mp =
37+
Array.foldr (\result ->
38+
case getType result of
39+
Just ty ->
40+
Map.insertWith append (stringifyShape $ shapeOfType ty) (pure result)
41+
Nothing -> identity
42+
) mp (allResults docsJson)
4943

50-
types = List.foldr insertTypes mempty do
44+
allResults :: DocsJson -> Array SearchResult
45+
allResults (DocsJson { name, declarations }) =
46+
declarations >>= (resultsForEntry name >>> map (_.result) >>> Array.fromFoldable)
5147

52-
results <- Trie.entriesUnordered trie >>= snd
48+
resultsWithTypes :: DocsJson -> Array SearchResult
49+
resultsWithTypes docsJson = Array.filter (getType >>> isJust) $ allResults docsJson
5350

54-
case (unwrap results).info of
55-
ValueResult dict ->
56-
insertTypeResultsFor dict.type results
51+
getType :: SearchResult -> Maybe Type
52+
getType sr =
53+
case (unwrap sr).info of
54+
ValueResult dict ->
55+
Just dict.type
5756

58-
TypeClassMemberResult dict ->
59-
insertTypeResultsFor dict.type results
57+
TypeClassMemberResult dict ->
58+
Just dict.type
6059

61-
TypeSynonymResult dict ->
62-
insertTypeResultsFor dict.type results
63-
_ -> mempty
60+
TypeSynonymResult dict ->
61+
Just dict.type
6462

65-
insertTypeResultsFor ty results =
66-
let path = stringifyShape (shapeOfType ty) in
67-
pure $ Tuple path results
63+
_ -> Nothing
6864

6965
lookup
7066
:: String
@@ -82,6 +78,14 @@ lookup key typeIndex@(TypeIndex map) =
8278
results <- hush (decodeJson json)
8379
pure { typeIndex: insert key (Just results) typeIndex, results }
8480

81+
where
82+
insert
83+
:: String
84+
-> Maybe (Array SearchResult)
85+
-> TypeIndex
86+
-> TypeIndex
87+
insert k v = unwrap >>> Map.insert k v >>> wrap
88+
8589
query
8690
:: TypeIndex
8791
-> TypeQuery

0 commit comments

Comments
 (0)