|
10 | 10 |
|
11 | 11 | module Glean.Glass.SymbolId.Hs ({- instances -}) where |
12 | 12 |
|
13 | | -import Data.Text (Text) |
| 13 | +import Data.Char |
14 | 14 | import qualified Data.Text as Text |
| 15 | +import TextShow |
15 | 16 |
|
16 | 17 | import Glean.Glass.SymbolId.Class |
17 | 18 | import Glean.Glass.Types (Name(..)) |
18 | | -import Glean.Schema.CodeHs.Types as Hs (Entity (..)) |
| 19 | +import Glean.Schema.CodeHs.Types as Hs (Entity) |
19 | 20 | import qualified Glean |
20 | 21 | import qualified Glean.Schema.Hs.Types as Hs |
21 | 22 | import qualified Glean.Schema.Src.Types as Src |
22 | 23 |
|
| 24 | +-- REPO/hs/containers/Data/Map/{var|datacon|tyvar|tycon}/toList[/START/END] |
| 25 | + |
23 | 26 | instance Symbol Hs.Entity where |
24 | | - toSymbol (Hs.Entity_definition d) = toSymbolPredicate d |
25 | | - toSymbol (Hs.Entity_function_ d) = toSymbolPredicate d |
26 | | - toSymbol (Hs.Entity_class_ d) = toSymbolPredicate d |
27 | | - toSymbol Hs.Entity_EMPTY = return [] |
| 27 | + toSymbol = toSymbolPredicate |
28 | 28 |
|
29 | | -instance Symbol Hs.Definition_key where |
30 | | - toSymbol (Hs.Definition_key name _) = do |
31 | | - n <- Glean.keyOf name |
32 | | - return (Text.splitOn "." n) |
| 29 | +instance Symbol Hs.Name_key where |
| 30 | + toSymbol (Hs.Name_key occ mod sort) = do |
| 31 | + m <- toSymbol mod |
| 32 | + o <- toSymbol occ |
| 33 | + s <- toSymbol sort |
| 34 | + return $ m <> o <> s |
33 | 35 |
|
34 | | -instance Symbol Hs.FunctionDefinition_key where |
35 | | - toSymbol (Hs.FunctionDefinition_key fnName Src.Range{..}) = do |
36 | | - name <- Glean.keyOf fnName |
37 | | - fname <- Glean.keyOf range_file |
38 | | - return (fname : Text.splitOn "." name) |
| 36 | +instance Symbol Hs.Module where |
| 37 | + toSymbol = toSymbolPredicate |
39 | 38 |
|
40 | | -instance Symbol Hs.Class_key where |
41 | | - toSymbol (Hs.Class_key clsName Src.Range{..}) = do |
42 | | - name <- Glean.keyOf clsName |
43 | | - fname <- Glean.keyOf range_file |
44 | | - return (fname : Text.splitOn "." name) |
| 39 | +instance Symbol Hs.OccName where |
| 40 | + toSymbol = toSymbolPredicate |
45 | 41 |
|
46 | | -instance ToQName Hs.Entity where |
47 | | - toQName (Hs.Entity_definition d) = Glean.keyOf d >>= toQName |
48 | | - toQName (Hs.Entity_function_ d) = Glean.keyOf d >>= toQName |
49 | | - toQName (Hs.Entity_class_ d) = Glean.keyOf d >>= toQName |
50 | | - toQName Hs.Entity_EMPTY = |
51 | | - return $ Left "toQName: Haskell: empty qname" |
52 | | - |
53 | | -instance ToQName Hs.Definition_key where |
54 | | - toQName (Hs.Definition_key name _) = do |
| 42 | +instance Symbol Hs.Module_key where |
| 43 | + toSymbol (Hs.Module_key name unit) = do |
| 44 | + u <- Glean.keyOf unit |
55 | 45 | n <- Glean.keyOf name |
56 | | - return $ case reverse (Text.splitOn "." n) of |
57 | | - [] -> Left "toQName: Haskell: empty function qname" |
58 | | - [x] -> Right (Name x, Name "") |
59 | | - (x:xs) -> Right (Name x, joinDotted xs) |
| 46 | + -- unit names are things like glean-0.1.0.0-inplace-core |
| 47 | + -- let's strip the version and everything after it |
| 48 | + let pkg = Text.intercalate "-" (fst (break isVer (Text.splitOn "-" u))) |
| 49 | + return (pkg : Text.splitOn "." n) |
| 50 | + where |
| 51 | + isVer t |
| 52 | + | Just (d, _) <- Text.uncons t = isDigit d |
| 53 | + | otherwise = False |
60 | 54 |
|
61 | | -instance ToQName Hs.FunctionDefinition_key where |
62 | | - toQName (Hs.FunctionDefinition_key fnName _) = do |
63 | | - name <- Glean.keyOf fnName |
64 | | - return $ case reverse (Text.splitOn "." name) of |
65 | | - [] -> Left "toQName: Haskell: empty function qname" |
66 | | - [x] -> Right (Name x, Name "") |
67 | | - (x:xs) -> Right (Name x, joinDotted xs) |
| 55 | +instance Symbol Hs.OccName_key where |
| 56 | + toSymbol (Hs.OccName_key name namespace) = do |
| 57 | + let sp = case namespace of |
| 58 | + Hs.Namespace_var_ -> "var" |
| 59 | + Hs.Namespace_datacon -> "con" |
| 60 | + Hs.Namespace_tyvar -> "tyvar" |
| 61 | + Hs.Namespace_tycon -> "ty" |
| 62 | + _ -> error "namespace" |
| 63 | + return [sp,name] |
68 | 64 |
|
| 65 | +instance Symbol Hs.NameSort where |
| 66 | + toSymbol Hs.NameSort_external{} = return [] |
| 67 | + toSymbol (Hs.NameSort_internal (Src.ByteSpan start end)) = |
| 68 | + return [showt (Glean.fromNat start), showt (Glean.fromNat end)] |
69 | 69 |
|
70 | | -instance ToQName Hs.Class_key where |
71 | | - toQName (Hs.Class_key clsName _) = do |
72 | | - name <- Glean.keyOf clsName |
73 | | - return $ case reverse (Text.splitOn "." name) of |
74 | | - [] -> Left "toQName: Haskell: empty class qname" |
75 | | - [x] -> Right (Name x, Name "") |
76 | | - (x:xs) -> Right (Name x, joinDotted xs) |
| 70 | +instance ToQName Hs.Entity where |
| 71 | + toQName n = Glean.keyOf n >>= toQName |
77 | 72 |
|
78 | | -joinDotted :: [Text] -> Name |
79 | | -joinDotted = Name . Text.intercalate "." . reverse |
| 73 | +instance ToQName Hs.Name_key where |
| 74 | + toQName (Hs.Name_key occ mod sort) = do |
| 75 | + Hs.Module_key m _ <- Glean.keyOf mod |
| 76 | + modname <- Glean.keyOf m |
| 77 | + Hs.OccName_key n _ <- Glean.keyOf occ |
| 78 | + return $ Right (Name modname, Name n) |
0 commit comments