@@ -1,10 +1,10 @@
11module Docs.Search.Declarations where
22
33import Docs.Search.DocsJson (ChildDeclType(..), ChildDeclaration(..), DeclType(..), Declaration(..), DocsJson(..), SourceSpan)
4−import Docs.Search.Score (Scores)
4+import Docs.Search.Score (Scores, getPackageScore, getPackageScoreForPackageName)
55import Docs.Search.SearchResult (ResultInfo(..), SearchResult(..))
66import Docs.Search.TypeDecoder (Constraint(..), QualifiedName(..), Type(..), Kind, joinForAlls)
7−import Docs.Search.Types (ModuleName, PackageName)
7+import Docs.Search.Types (ModuleName(..), PackageName(..), PackageInfo(..))
88
99import Prelude
1010
@@ -14,7 +14,6 @@ import Data.Array as Array
1414import Data.Foldable (foldr)
1515import Data.List (List, (:))
1616import Data.List as List
17−import Data.Map as Map
1817import Data.Maybe (Maybe(..), fromMaybe)
1918import Data.Newtype (class Newtype, unwrap, wrap)
2019import Data.Search.Trie (Trie, alter)
@@ -24,8 +23,7 @@ import Data.String.Common (toLower)
2423import Data.String.Pattern (Pattern(..))
2524
2625
27−newtype Declarations
28− = Declarations (Trie Char (List SearchResult))
26+newtype Declarations = Declarations (Trie Char (List SearchResult))
2927
3028derive instance newtypeDeclarations :: Newtype Declarations _
3129derive newtype instance semigroupDeclarations :: Semigroup Declarations
@@ -42,7 +40,7 @@ insertDocsJson
4240 -> Trie Char (List SearchResult)
4341 -> Trie Char (List SearchResult)
4442insertDocsJson scores (DocsJson { name, declarations }) trie
45− = foldr (insertDeclaration scores name) trie declarations
43+ = foldr (insertDeclaration scores $ ModuleName name) trie declarations
4644
4745
4846insertDeclaration
@@ -63,13 +61,13 @@ insertSearchResult
6361 -> Trie Char (List SearchResult)
6462insertSearchResult { path, result } trie =
6563 let path' = List.fromFoldable $ toCharArray $ toLower path in
66− alter path' (Just <<< updateResults) trie
67− where
68− updateResults mbOldResults
69− | Just oldResults <- mbOldResults =
70− result : oldResults
71− | otherwise =
72− List.singleton result
64+ alter path' (Just <<< updateResults) trie
65+ where
66+ updateResults mbOldResults
67+ | Just oldResults <- mbOldResults =
68+ result : oldResults
69+ | otherwise =
70+ List.singleton result
7371
7472
7573-- | For each declaration, extract its own `SearchResult` and `SearchResult`s
@@ -82,19 +80,17 @@ resultsForDeclaration
8280 , result :: SearchResult
8381 }
8482resultsForDeclaration scores moduleName indexEntry@(Declaration entry) =
85− let { info, title, sourceSpan, comments, children } = entry
86− { name, declLevel } = getLevelAndName info.declType title
87− packageName = extractPackageName moduleName sourceSpan
88− in case mkInfo declLevel indexEntry of
89− Nothing -> mempty
90− Just info' ->
83+ case mkInfo declLevel indexEntry of
84+ Nothing -> mempty
85+ Just info' ->
9186 let result = SearchResult { name: title
9287 , comments
9388 , hashAnchor: declLevelToHashAnchor declLevel
9489 , moduleName
9590 , sourceSpan
96− , packageName
97− , score: fromMaybe 0 $ Map.lookup packageName scores
91+ , packageInfo
92+ , score:
93+ fromMaybe 0 $ getPackageScoreForPackageName scores <$> mbPackageName
9894 , info: info'
9995 }
10096 in
@@ -104,8 +100,16 @@ resultsForDeclaration scores moduleName indexEntry@(Declaration entry) =
104100 }
105101 ) <>
106102 ( List.fromFoldable children >>=
107− resultsForChildDeclaration scores packageName moduleName result
103+ resultsForChildDeclaration scores packageInfo moduleName result
108104 )
105+ where
106+ { info, title, sourceSpan, comments, children } = entry
107+ { name, declLevel } = getLevelAndName info.declType title
108+ packageInfo = extractPackageName moduleName sourceSpan
109+ mbPackageName =
110+ case packageInfo of
111+ Package packageName -> Just packageName
112+ _ -> Nothing
109113
110114
111115mkInfo :: DeclLevel -> Declaration -> Maybe ResultInfo
@@ -188,31 +192,30 @@ getLevelAndName DeclExternKind name = { name, declLevel: KindLevel }
188192-- | Extract package name from `sourceSpan.name`, which contains path to
189193-- | the source file. If `ModuleName` string starts with `Prim.`, it's a
190194-- | built-in (guaranteed by the compiler).
191−extractPackageName :: ModuleName -> Maybe SourceSpan -> PackageName
192−extractPackageName moduleName _
193− | String.split (Pattern ".") moduleName !! 0 == Just "Prim" = "<builtin>"
194−extractPackageName _ Nothing = "<unknown>"
195+extractPackageName :: ModuleName -> Maybe SourceSpan -> PackageInfo
196+extractPackageName (ModuleName moduleName) _
197+ | String.split (Pattern ".") moduleName !! 0 == Just "Prim" = Builtin
198+extractPackageName _ Nothing = UnknownPackage
195199extractPackageName _ (Just { name }) =
196− let dirs = String.split (Pattern "/") name
197− in
198− fromMaybe "<local package>" do
199− topLevelDir <- dirs !! 0
200− if topLevelDir == ".spago"
201− then dirs !! 1
202− else do
203− bowerDirIx <- Array.findIndex (_ == "bower_components") dirs
204− dirs !! (bowerDirIx + 1)
200+ fromMaybe LocalPackage do
201+ topLevelDir <- dirs !! 0
202+ if topLevelDir == ".spago"
203+ then Package <<< PackageName <$> dirs !! 1
204+ else do
205+ bowerDirIx <- Array.findIndex (_ == "bower_components") dirs
206+ Package <<< PackageName <$> dirs !! (bowerDirIx + 1)
207+ where dirs = String.split (Pattern "/") name
205208
206209
207210-- | Extract `SearchResults` from a `ChildDeclaration`.
208211resultsForChildDeclaration
209212 :: Scores
210− -> PackageName
213+ -> PackageInfo
211214 -> ModuleName
212215 -> SearchResult
213216 -> ChildDeclaration
214217 -> List { path :: String, result :: SearchResult }
215−resultsForChildDeclaration scores packageName moduleName parentResult
218+resultsForChildDeclaration scores packageInfo moduleName parentResult
216219 child@(ChildDeclaration { title, info, comments, mbSourceSpan })
217220 | Just resultInfo <- mkChildInfo parentResult child =
218221 { path: title
@@ -225,8 +228,8 @@ resultsForChildDeclaration scores packageName moduleName parentResult
225228 , hashAnchor: "v"
226229 , moduleName
227230 , sourceSpan: mbSourceSpan
228− , packageName
229− , score: fromMaybe 0 $ Map.lookup packageName scores
231+ , packageInfo
232+ , score: getPackageScore scores packageInfo
230233 , info: resultInfo
231234 }
232235 } # List.singleton
@@ -261,7 +264,8 @@ mkChildInfo
261264
262265 -- Then we construct a qualified name of the type class.
263266 constraintClass =
264− QualifiedName { moduleName: String.split (wrap ".") moduleName
267+ QualifiedName { moduleNameParts:
268+ String.split (wrap ".") $ unwrap moduleName
265269 , name: resultName }
266270
267271 -- We concatenate two lists: