diff --git a/src/Docs/Search/Declarations.purs b/src/Docs/Search/Declarations.purs index 1535daf..403bfbb 100644 --- a/src/Docs/Search/Declarations.purs +++ b/src/Docs/Search/Declarations.purs @@ -1,6 +1,6 @@ module Docs.Search.Declarations where -import Docs.Search.DocsJson (ChildDeclType(..), ChildDeclaration(..), DeclType(..), Declaration(..), DocsJson(..)) +import Docs.Search.DocsJson (ChildDeclType(..), ChildDeclaration(..), DeclType(..), Declaration(..), DocsJson(..), SourceSpan) import Docs.Search.PackageIndex (Scores) import Docs.Search.SearchResult (ResultInfo(..), SearchResult(..)) import Docs.Search.TypeDecoder (Constraint(..), QualifiedName(..), Type(..), Kind, joinForAlls) @@ -87,7 +87,7 @@ resultsForDeclaration resultsForDeclaration scores moduleName indexEntry@(Declaration entry) = let { info, title, sourceSpan, comments, children } = entry { name, declLevel } = getLevelAndName info.declType title - packageName = extractPackageName sourceSpan.name + packageName = extractPackageName moduleName sourceSpan in case mkInfo declLevel indexEntry of Nothing -> mempty Just info' -> @@ -95,7 +95,7 @@ resultsForDeclaration scores moduleName indexEntry@(Declaration entry) = , comments , hashAnchor: declLevelToHashAnchor declLevel , moduleName - , sourceSpan: Just sourceSpan + , sourceSpan , packageName , score: fromMaybe 0 $ Map.lookup packageName scores , info: info' @@ -189,24 +189,22 @@ getLevelAndName DeclExternKind name = { name, declLevel: KindLevel } -- | Extract package name from `sourceSpan.name`, which contains path to --- | the source file. -extractPackageName :: String -> String -extractPackageName name = - let chunks = String.split (Pattern "/") name in - fromMaybe "" $ - chunks !! 0 >>= \dir -> - if dir == ".spago" then - chunks !! 1 - else - let - bowerComponentsIndex = - Array.findIndex (_ == "bower_components") chunks - in - case bowerComponentsIndex of - Just n -> - chunks !! (n + 1) - Nothing -> - Just "" +-- | the source file. If `ModuleName` string starts with `Prim.`, it's a +-- | built-in (guaranteed by the compiler). +extractPackageName :: ModuleName -> Maybe SourceSpan -> String +extractPackageName moduleName _ + | String.split (Pattern ".") moduleName !! 0 == Just "Prim" = "" +extractPackageName _ Nothing = "" +extractPackageName _ (Just { name }) = + let dirs = String.split (Pattern "/") name + in + fromMaybe "" do + topLevelDir <- dirs !! 0 + if topLevelDir == ".spago" + then dirs !! 1 + else do + bowerDirIx <- Array.findIndex (_ == "bower_components") dirs + dirs !! (bowerDirIx + 1) -- | Extract `SearchResults` from a `ChildDeclaration`. diff --git a/src/Docs/Search/DocsJson.purs b/src/Docs/Search/DocsJson.purs index c605eca..680d98a 100644 --- a/src/Docs/Search/DocsJson.purs +++ b/src/Docs/Search/DocsJson.purs @@ -33,6 +33,10 @@ instance decodeJsonDocsJson :: DecodeJson DocsJson where instance encodeJsonDocsJson :: EncodeJson DocsJson where encodeJson = encodeJson <<< unwrap +type SourceSpan = { start :: Array Int + , end :: Array Int + , name :: String + } newtype Declaration = Declaration { title :: String @@ -46,10 +50,7 @@ newtype Declaration , arguments :: Maybe (Array TypeArgument) , fundeps :: Maybe FunDeps } - , sourceSpan :: { start :: Array Int - , end :: Array Int - , name :: String - } + , sourceSpan :: Maybe SourceSpan , children :: Array ChildDeclaration } diff --git a/test/Main.purs b/test/Main.purs index b38f824..ac2b506 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -5,6 +5,7 @@ import Prelude import Docs.Search.TypeDecoder (Constraint(..), FunDep(..), FunDeps(..), Kind(..), QualifiedName(..), Type(..)) import Test.TypeQuery as TypeQuery import Test.IndexBuilder as IndexBuilder +import Test.Declarations as Declarations import Test.Extra (assertRight) @@ -26,6 +27,7 @@ mainTest :: TestSuite mainTest = do TypeQuery.tests IndexBuilder.tests + Declarations.tests let mkJson x = unsafePartial $ fromRight $ jsonParser x suite "FunDeps decoder" do diff --git a/test/Test/Declarations.purs b/test/Test/Declarations.purs new file mode 100644 index 0000000..fe3d9a3 --- /dev/null +++ b/test/Test/Declarations.purs @@ -0,0 +1,39 @@ +module Test.Declarations where + +import Prelude + +import Data.Maybe (Maybe(..)) +import Docs.Search.Declarations (extractPackageName) + +import Test.Unit (TestSuite, suite, test) +import Test.Unit.Assert as Assert + +tests :: TestSuite +tests = do + suite "Declarations" do + test "extractPackageName" do + Assert.equal "" (extractPackageName "Prim" Nothing) + Assert.equal "" (extractPackageName "Prim.Foo" Nothing) + Assert.equal "" (extractPackageName "Prim.Foo.Bar" Nothing) + Assert.equal "" (extractPackageName "Primitive" Nothing) + Assert.equal "foo" + (extractPackageName "Foo" $ + Just { start: [] + , end: [] + , name: ".spago/foo/src/Foo.purs" + } + ) + Assert.equal "bar" + (extractPackageName "Bar" $ + Just { start: [] + , end: [] + , name: "/path/to/somewhere/bower_components/bar/src/Bar.purs" + } + ) + Assert.equal "" + (extractPackageName "Bar" $ + Just { start: [] + , end: [] + , name: "/path/to/somewhere/src/Bar.purs" + } + )