Skip to content

Commit

Permalink
Consider something a builtin when there's no sourceSpan (#32)
Browse files Browse the repository at this point in the history
* Consider something a builtin when there's no sourceSpan

* Refactor `extractPackageName`

* Use module name to check if a declaration is built-in

* Recognize `Prim` module as built-in; add tests

* Code style

Fix #31

Co-authored-by: Vladimir Kalnitsky <[email protected]>
  • Loading branch information
f-f and klntsky authored Jan 18, 2020
1 parent 1e531e2 commit 0b26229
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
40 changes: 19 additions & 21 deletions src/Docs/Search/Declarations.purs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -87,15 +87,15 @@ 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' ->
let result = SearchResult { name: title
, comments
, hashAnchor: declLevelToHashAnchor declLevel
, moduleName
, sourceSpan: Just sourceSpan
, sourceSpan
, packageName
, score: fromMaybe 0 $ Map.lookup packageName scores
, info: info'
Expand Down Expand Up @@ -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 "<unknown>" $
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 "<local package>"
-- | 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" = "<builtin>"
extractPackageName _ Nothing = "<unknown>"
extractPackageName _ (Just { name }) =
let dirs = String.split (Pattern "/") name
in
fromMaybe "<local package>" 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`.
Expand Down
9 changes: 5 additions & 4 deletions src/Docs/Search/DocsJson.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/Test/Declarations.purs
Original file line number Diff line number Diff line change
@@ -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 "<builtin>" (extractPackageName "Prim" Nothing)
Assert.equal "<builtin>" (extractPackageName "Prim.Foo" Nothing)
Assert.equal "<builtin>" (extractPackageName "Prim.Foo.Bar" Nothing)
Assert.equal "<unknown>" (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 "<local package>"
(extractPackageName "Bar" $
Just { start: []
, end: []
, name: "/path/to/somewhere/src/Bar.purs"
}
)

0 comments on commit 0b26229

Please sign in to comment.