@@ -6,7 +6,8 @@ module Development.IDE.Types.Exports
66 ExportsMap (.. ),
77 createExportsMap,
88 createExportsMapMg,
9- createExportsMapTc
9+ createExportsMapTc,
10+ buildModuleExportMapFrom
1011,createExportsMapHieDb,size) where
1112
1213import Avail (AvailInfo (.. ))
@@ -30,17 +31,24 @@ import HieDb
3031import Name
3132import TcRnTypes (TcGblEnv (.. ))
3233
33- newtype ExportsMap = ExportsMap
34- { getExportsMap :: HashMap IdentifierText (HashSet IdentInfo )}
35- deriving newtype (Monoid , NFData , Show )
34+
35+ data ExportsMap = ExportsMap
36+ { getExportsMap :: HashMap IdentifierText (HashSet IdentInfo )
37+ , getModuleExportsMap :: Map. HashMap ModuleNameText (HashSet IdentInfo )
38+ }
39+ deriving (Show )
3640
3741size :: ExportsMap -> Int
3842size = sum . map length . elems . getExportsMap
3943
4044instance Semigroup ExportsMap where
41- ExportsMap a <> ExportsMap b = ExportsMap $ Map. unionWith (<>) a b
45+ ExportsMap a b <> ExportsMap c d = ExportsMap (Map. unionWith (<>) a c) (Map. unionWith (<>) b d)
46+
47+ instance Monoid ExportsMap where
48+ mempty = ExportsMap Map. empty Map. empty
4249
4350type IdentifierText = Text
51+ type ModuleNameText = Text
4452
4553data IdentInfo = IdentInfo
4654 { name :: ! OccName
@@ -91,25 +99,34 @@ mkIdentInfos mod (AvailTC _ nn flds)
9199 ]
92100
93101createExportsMap :: [ModIface ] -> ExportsMap
94- createExportsMap = ExportsMap . Map. fromListWith (<>) . concatMap doOne
102+ createExportsMap modIface = do
103+ let exportList = concatMap doOne modIface
104+ let exportsMap = Map. fromListWith (<>) $ map (\ (a,_,c) -> (a, c)) exportList
105+ ExportsMap exportsMap $ buildModuleExportMap $ map (\ (_,b,c) -> (b, c)) exportList
95106 where
96- doOne mi = concatMap ( fmap (second Set. fromList) . unpackAvail mn) (mi_exports mi)
97- where
98- mn = moduleName $ mi_module mi
107+ doOne modIFace = do
108+ let getModDetails = unpackAvail $ moduleName $ mi_module modIFace
109+ concatMap ( fmap (second Set. fromList) . getModDetails) (mi_exports modIFace)
99110
100111createExportsMapMg :: [ModGuts ] -> ExportsMap
101- createExportsMapMg = ExportsMap . Map. fromListWith (<>) . concatMap doOne
112+ createExportsMapMg modGuts = do
113+ let exportList = concatMap doOne modGuts
114+ let exportsMap = Map. fromListWith (<>) $ map (\ (a,_,c) -> (a, c)) exportList
115+ ExportsMap exportsMap $ buildModuleExportMap $ map (\ (_,b,c) -> (b, c)) exportList
102116 where
103- doOne mi = concatMap ( fmap (second Set. fromList) . unpackAvail mn) (mg_exports mi)
104- where
105- mn = moduleName $ mg_module mi
117+ doOne mi = do
118+ let getModuleName = moduleName $ mg_module mi
119+ concatMap ( fmap (second Set. fromList) . unpackAvail getModuleName) (mg_exports mi)
106120
107121createExportsMapTc :: [TcGblEnv ] -> ExportsMap
108- createExportsMapTc = ExportsMap . Map. fromListWith (<>) . concatMap doOne
122+ createExportsMapTc modIface = do
123+ let exportList = concatMap doOne modIface
124+ let exportsMap = Map. fromListWith (<>) $ map (\ (a,_,c) -> (a, c)) exportList
125+ ExportsMap exportsMap $ buildModuleExportMap $ map (\ (_,b,c) -> (b, c)) exportList
109126 where
110- doOne mi = concatMap ( fmap (second Set. fromList) . unpackAvail mn) (tcg_exports mi)
111- where
112- mn = moduleName $ tcg_mod mi
127+ doOne mi = do
128+ let getModuleName = moduleName $ tcg_mod mi
129+ concatMap ( fmap (second Set. fromList) . unpackAvail getModuleName) (tcg_exports mi)
113130
114131nonInternalModules :: ModuleName -> Bool
115132nonInternalModules = not . (" .Internal" `isSuffixOf` ) . moduleNameString
@@ -121,7 +138,8 @@ createExportsMapHieDb hiedb = do
121138 let mn = modInfoName $ hieModInfo m
122139 mText = pack $ moduleNameString mn
123140 fmap (wrap . unwrap mText) <$> getExportsForModule hiedb mn
124- return $ ExportsMap $ Map. fromListWith (<>) (concat idents)
141+ let exportsMap = Map. fromListWith (<>) (concat idents)
142+ return $ ExportsMap exportsMap $ buildModuleExportMap (concat idents)
125143 where
126144 wrap identInfo = (rendered identInfo, Set. fromList [identInfo])
127145 -- unwrap :: ExportRow -> IdentInfo
@@ -130,10 +148,35 @@ createExportsMapHieDb hiedb = do
130148 n = pack (occNameString exportName)
131149 p = pack . occNameString <$> exportParent
132150
133- unpackAvail :: ModuleName -> IfaceExport -> [(Text , [IdentInfo ])]
151+ unpackAvail :: ModuleName -> IfaceExport -> [(Text , Text , [IdentInfo ])]
134152unpackAvail mn
135153 | nonInternalModules mn = map f . mkIdentInfos mod
136154 | otherwise = const []
137155 where
138156 ! mod = pack $ moduleNameString mn
139- f id @ IdentInfo {.. } = (pack (prettyPrint name), [id ])
157+ f id @ IdentInfo {.. } = (pack (prettyPrint name), moduleNameText,[id ])
158+
159+
160+ identInfoToKeyVal :: IdentInfo -> (ModuleNameText , IdentInfo )
161+ identInfoToKeyVal identInfo =
162+ (moduleNameText identInfo, identInfo)
163+
164+ buildModuleExportMap :: [(Text , HashSet IdentInfo )] -> Map. HashMap ModuleNameText (HashSet IdentInfo )
165+ buildModuleExportMap exportsMap = do
166+ let lst = concatMap (Set. toList. snd ) exportsMap
167+ let lstThree = map identInfoToKeyVal lst
168+ sortAndGroup lstThree
169+
170+ buildModuleExportMapFrom :: [ModIface ] -> Map. HashMap Text (HashSet IdentInfo )
171+ buildModuleExportMapFrom modIfaces = do
172+ let exports = map extractModuleExports modIfaces
173+ Map. fromListWith (<>) exports
174+
175+ extractModuleExports :: ModIface -> (Text , HashSet IdentInfo )
176+ extractModuleExports modIFace = do
177+ let modName = pack $ moduleNameString $ moduleName $ mi_module modIFace
178+ let functionSet = Set. fromList $ concatMap (mkIdentInfos modName) $ mi_exports modIFace
179+ (modName, functionSet)
180+
181+ sortAndGroup :: [(ModuleNameText , IdentInfo )] -> Map. HashMap ModuleNameText (HashSet IdentInfo )
182+ sortAndGroup assocs = Map. fromListWith (<>) [(k, Set. fromList [v]) | (k, v) <- assocs]
0 commit comments