@@ -1,6 +1,6 @@
11module Docs.Search.Declarations where
22
3−import Docs.Search.DocsJson (ChildDeclType(..), ChildDeclaration(..), DeclType(..), Declaration(..), DocsJson(..))
3+import Docs.Search.DocsJson (ChildDeclType(..), ChildDeclaration(..), DeclType(..), Declaration(..), DocsJson(..), SourceSpan)
44import Docs.Search.PackageIndex (Scores)
55import Docs.Search.SearchResult (ResultInfo(..), SearchResult(..))
66import Docs.Search.TypeDecoder (Constraint(..), QualifiedName(..), Type(..), Kind, joinForAlls)
@@ -87,7 +87,7 @@ resultsForDeclaration
8787resultsForDeclaration scores moduleName indexEntry@(Declaration entry) =
8888 let { info, title, sourceSpan, comments, children } = entry
8989 { name, declLevel } = getLevelAndName info.declType title
90− packageName = extractPackageName sourceSpan.name
90+ packageName = extractPackageName moduleName sourceSpan
9191 in case mkInfo declLevel indexEntry of
9292 Nothing -> mempty
9393 Just info' ->
@@ -95,7 +95,7 @@ resultsForDeclaration scores moduleName indexEntry@(Declaration entry) =
9595 , comments
9696 , hashAnchor: declLevelToHashAnchor declLevel
9797 , moduleName
98− , sourceSpan: Just sourceSpan
98+ , sourceSpan
9999 , packageName
100100 , score: fromMaybe 0 $ Map.lookup packageName scores
101101 , info: info'
@@ -189,24 +189,22 @@ getLevelAndName DeclExternKind name = { name, declLevel: KindLevel }
189189
190190
191191-- | Extract package name from `sourceSpan.name`, which contains path to
192−-- | the source file.
193−extractPackageName :: String -> String
194−extractPackageName name =
195− let chunks = String.split (Pattern "/") name in
196− fromMaybe "<unknown>" $
197− chunks !! 0 >>= \dir ->
198− if dir == ".spago" then
199− chunks !! 1
200− else
201− let
202− bowerComponentsIndex =
203− Array.findIndex (_ == "bower_components") chunks
204− in
205− case bowerComponentsIndex of
206− Just n ->
207− chunks !! (n + 1)
208− Nothing ->
209− Just "<local package>"
192+-- | the source file. If `ModuleName` string starts with `Prim.`, it's a
193+-- | built-in (guaranteed by the compiler).
194+extractPackageName :: ModuleName -> Maybe SourceSpan -> String
195+extractPackageName moduleName _
196+ | String.split (Pattern ".") moduleName !! 0 == Just "Prim" = "<builtin>"
197+extractPackageName _ Nothing = "<unknown>"
198+extractPackageName _ (Just { name }) =
199+ let dirs = String.split (Pattern "/") name
200+ in
201+ fromMaybe "<local package>" do
202+ topLevelDir <- dirs !! 0
203+ if topLevelDir == ".spago"
204+ then dirs !! 1
205+ else do
206+ bowerDirIx <- Array.findIndex (_ == "bower_components") dirs
207+ dirs !! (bowerDirIx + 1)
210208
211209
212210-- | Extract `SearchResults` from a `ChildDeclaration`.