From fc70a1f325c715e7e9376313445cff09eb8f9148 Mon Sep 17 00:00:00 2001 From: Fabrizio Ferrai Date: Thu, 16 Jan 2020 12:08:37 +0100 Subject: [PATCH 1/5] Consider something a builtin when there's no sourceSpan --- src/Docs/Search/Declarations.purs | 4 ++-- src/Docs/Search/DocsJson.purs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Docs/Search/Declarations.purs b/src/Docs/Search/Declarations.purs index 1535daf..6ea8e2f 100644 --- a/src/Docs/Search/Declarations.purs +++ b/src/Docs/Search/Declarations.purs @@ -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 = fromMaybe "builtins" $ map (extractPackageName <<< _.name) 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' diff --git a/src/Docs/Search/DocsJson.purs b/src/Docs/Search/DocsJson.purs index c605eca..5137e2c 100644 --- a/src/Docs/Search/DocsJson.purs +++ b/src/Docs/Search/DocsJson.purs @@ -46,10 +46,10 @@ newtype Declaration , arguments :: Maybe (Array TypeArgument) , fundeps :: Maybe FunDeps } - , sourceSpan :: { start :: Array Int - , end :: Array Int - , name :: String - } + , sourceSpan :: Maybe { start :: Array Int + , end :: Array Int + , name :: String + } , children :: Array ChildDeclaration } From dd2fd66a68439121f41fe28812e2722444a71fa9 Mon Sep 17 00:00:00 2001 From: klntsky Date: Thu, 16 Jan 2020 17:37:10 +0300 Subject: [PATCH 2/5] Refactor `extractPackageName` --- src/Docs/Search/Declarations.purs | 33 +++++++++++++------------------ src/Docs/Search/DocsJson.purs | 9 +++++---- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Docs/Search/Declarations.purs b/src/Docs/Search/Declarations.purs index 6ea8e2f..60d3785 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 = fromMaybe "builtins" $ map (extractPackageName <<< _.name) sourceSpan + packageName = extractPackageName sourceSpan in case mkInfo declLevel indexEntry of Nothing -> mempty Just info' -> @@ -190,23 +190,18 @@ 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 "" +extractPackageName :: Maybe SourceSpan -> String +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 5137e2c..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 :: Maybe { start :: Array Int - , end :: Array Int - , name :: String - } + , sourceSpan :: Maybe SourceSpan , children :: Array ChildDeclaration } From 52e05e5740a91169c1149d3e8324bb6c4eab6e20 Mon Sep 17 00:00:00 2001 From: klntsky Date: Thu, 16 Jan 2020 21:40:38 +0300 Subject: [PATCH 3/5] Use module name to check if a declaration is built-in --- src/Docs/Search/Declarations.purs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Docs/Search/Declarations.purs b/src/Docs/Search/Declarations.purs index 60d3785..88c7a9f 100644 --- a/src/Docs/Search/Declarations.purs +++ b/src/Docs/Search/Declarations.purs @@ -16,7 +16,7 @@ import Data.List as List import Data.Maybe (Maybe(..), fromMaybe) import Data.Newtype (class Newtype, unwrap, wrap) import Data.Search.Trie (Trie, alter) -import Data.String.CodeUnits (stripPrefix, stripSuffix, toCharArray) +import Data.String.CodeUnits (stripPrefix, stripSuffix, toCharArray, indexOf) import Data.String.Common (split) as String import Data.String.Common (toLower) import Data.String.Pattern (Pattern(..)) @@ -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 + packageName = extractPackageName moduleName sourceSpan in case mkInfo declLevel indexEntry of Nothing -> mempty Just info' -> @@ -189,10 +189,13 @@ getLevelAndName DeclExternKind name = { name, declLevel: KindLevel } -- | Extract package name from `sourceSpan.name`, which contains path to --- | the source file. -extractPackageName :: Maybe SourceSpan -> String -extractPackageName Nothing = "" -extractPackageName (Just { name }) = +-- | 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 _ + | indexOf (Pattern "Prim.") moduleName == Just 0 = "" +extractPackageName _ Nothing = "" +extractPackageName _ (Just { name }) = let dirs = String.split (Pattern "/") name in fromMaybe "" do From 594fa0f3cfcfed05a2c242e3b728bf16c0318ccd Mon Sep 17 00:00:00 2001 From: klntsky Date: Fri, 17 Jan 2020 18:48:34 +0300 Subject: [PATCH 4/5] Recognize `Prim` module as built-in; add tests --- src/Docs/Search/Declarations.purs | 4 ++-- test/Main.purs | 2 ++ test/Test/Declarations.purs | 38 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 test/Test/Declarations.purs diff --git a/src/Docs/Search/Declarations.purs b/src/Docs/Search/Declarations.purs index 88c7a9f..403bfbb 100644 --- a/src/Docs/Search/Declarations.purs +++ b/src/Docs/Search/Declarations.purs @@ -16,7 +16,7 @@ import Data.List as List import Data.Maybe (Maybe(..), fromMaybe) import Data.Newtype (class Newtype, unwrap, wrap) import Data.Search.Trie (Trie, alter) -import Data.String.CodeUnits (stripPrefix, stripSuffix, toCharArray, indexOf) +import Data.String.CodeUnits (stripPrefix, stripSuffix, toCharArray) import Data.String.Common (split) as String import Data.String.Common (toLower) import Data.String.Pattern (Pattern(..)) @@ -193,7 +193,7 @@ getLevelAndName DeclExternKind name = { name, declLevel: KindLevel } -- | built-in (guaranteed by the compiler). extractPackageName :: ModuleName -> Maybe SourceSpan -> String extractPackageName moduleName _ - | indexOf (Pattern "Prim.") moduleName == Just 0 = "" + | String.split (Pattern ".") moduleName !! 0 == Just "Prim" = "" extractPackageName _ Nothing = "" extractPackageName _ (Just { name }) = let dirs = String.split (Pattern "/") name 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..8c7d35f --- /dev/null +++ b/test/Test/Declarations.purs @@ -0,0 +1,38 @@ +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" + } + ) From 969a3a2f5cb5ce9c3e35334ebef0089eb496cdb4 Mon Sep 17 00:00:00 2001 From: klntsky Date: Fri, 17 Jan 2020 18:51:14 +0300 Subject: [PATCH 5/5] Code style --- test/Test/Declarations.purs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/Test/Declarations.purs b/test/Test/Declarations.purs index 8c7d35f..fe3d9a3 100644 --- a/test/Test/Declarations.purs +++ b/test/Test/Declarations.purs @@ -16,12 +16,13 @@ tests = do 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 "foo" + (extractPackageName "Foo" $ + Just { start: [] + , end: [] + , name: ".spago/foo/src/Foo.purs" + } + ) Assert.equal "bar" (extractPackageName "Bar" $ Just { start: []