Skip to content

Commit 0b26229

Browse files
f-fklntsky
andauthored
Consider something a builtin when there's no sourceSpan (#32)
* 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]>
1 parent 1e531e2 commit 0b26229

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

src/Docs/Search/Declarations.purs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module 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)
44
import Docs.Search.PackageIndex (Scores)
55
import Docs.Search.SearchResult (ResultInfo(..), SearchResult(..))
66
import Docs.Search.TypeDecoder (Constraint(..), QualifiedName(..), Type(..), Kind, joinForAlls)
@@ -87,15 +87,15 @@ resultsForDeclaration
8787
resultsForDeclaration 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' ->
9494
let result = SearchResult { name: title
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`.

src/Docs/Search/DocsJson.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ instance decodeJsonDocsJson :: DecodeJson DocsJson where
3333
instance encodeJsonDocsJson :: EncodeJson DocsJson where
3434
encodeJson = encodeJson <<< unwrap
3535

36+
type SourceSpan = { start :: Array Int
37+
, end :: Array Int
38+
, name :: String
39+
}
3640

3741
newtype Declaration
3842
= Declaration { title :: String
@@ -46,10 +50,7 @@ newtype Declaration
4650
, arguments :: Maybe (Array TypeArgument)
4751
, fundeps :: Maybe FunDeps
4852
}
49-
, sourceSpan :: { start :: Array Int
50-
, end :: Array Int
51-
, name :: String
52-
}
53+
, sourceSpan :: Maybe SourceSpan
5354
, children :: Array ChildDeclaration
5455
}
5556

test/Main.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Prelude
55
import Docs.Search.TypeDecoder (Constraint(..), FunDep(..), FunDeps(..), Kind(..), QualifiedName(..), Type(..))
66
import Test.TypeQuery as TypeQuery
77
import Test.IndexBuilder as IndexBuilder
8+
import Test.Declarations as Declarations
89

910
import Test.Extra (assertRight)
1011

@@ -26,6 +27,7 @@ mainTest :: TestSuite
2627
mainTest = do
2728
TypeQuery.tests
2829
IndexBuilder.tests
30+
Declarations.tests
2931

3032
let mkJson x = unsafePartial $ fromRight $ jsonParser x
3133
suite "FunDeps decoder" do

test/Test/Declarations.purs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Test.Declarations where
2+
3+
import Prelude
4+
5+
import Data.Maybe (Maybe(..))
6+
import Docs.Search.Declarations (extractPackageName)
7+
8+
import Test.Unit (TestSuite, suite, test)
9+
import Test.Unit.Assert as Assert
10+
11+
tests :: TestSuite
12+
tests = do
13+
suite "Declarations" do
14+
test "extractPackageName" do
15+
Assert.equal "<builtin>" (extractPackageName "Prim" Nothing)
16+
Assert.equal "<builtin>" (extractPackageName "Prim.Foo" Nothing)
17+
Assert.equal "<builtin>" (extractPackageName "Prim.Foo.Bar" Nothing)
18+
Assert.equal "<unknown>" (extractPackageName "Primitive" Nothing)
19+
Assert.equal "foo"
20+
(extractPackageName "Foo" $
21+
Just { start: []
22+
, end: []
23+
, name: ".spago/foo/src/Foo.purs"
24+
}
25+
)
26+
Assert.equal "bar"
27+
(extractPackageName "Bar" $
28+
Just { start: []
29+
, end: []
30+
, name: "/path/to/somewhere/bower_components/bar/src/Bar.purs"
31+
}
32+
)
33+
Assert.equal "<local package>"
34+
(extractPackageName "Bar" $
35+
Just { start: []
36+
, end: []
37+
, name: "/path/to/somewhere/src/Bar.purs"
38+
}
39+
)

0 commit comments

Comments
 (0)