@@ -5,6 +5,10 @@ module Development.IDE.Types.Exports
5
5
(
6
6
IdentInfo (.. ),
7
7
ExportsMap (.. ),
8
+ rendered,
9
+ moduleNameText,
10
+ occNameText,
11
+ isDatacon,
8
12
createExportsMap,
9
13
createExportsMapMg,
10
14
createExportsMapTc,
@@ -24,6 +28,7 @@ import Data.HashSet (HashSet)
24
28
import qualified Data.HashSet as Set
25
29
import Data.List (foldl' , isSuffixOf )
26
30
import Data.Text (Text , pack )
31
+ import Data.Text.Encoding (decodeUtf8 )
27
32
import Development.IDE.GHC.Compat
28
33
import Development.IDE.GHC.Orphans ()
29
34
import Development.IDE.GHC.Util
@@ -61,55 +66,63 @@ instance Monoid ExportsMap where
61
66
type IdentifierText = Text
62
67
type ModuleNameText = Text
63
68
69
+
70
+ rendered :: IdentInfo -> IdentifierText
71
+ rendered = occNameText . name
72
+
73
+ -- | Render an identifier as imported or exported style.
74
+ -- TODO: pattern synonymoccNameText :: OccName -> Text
75
+ occNameText :: OccName -> IdentifierText
76
+ occNameText name
77
+ | isTcOcc name && isSymOcc name = " type " <> renderOcc
78
+ | otherwise = renderOcc
79
+ where
80
+ renderOcc = decodeUtf8 . bytesFS . occNameFS $ name
81
+
82
+ moduleNameText :: IdentInfo -> ModuleNameText
83
+ moduleNameText = moduleNameText' . identModuleName
84
+
85
+ moduleNameText' :: ModuleName -> ModuleNameText
86
+ moduleNameText' = decodeUtf8 . bytesFS . moduleNameFS
87
+
64
88
data IdentInfo = IdentInfo
65
- { name :: ! OccName
66
- , rendered :: Text
67
- , parent :: ! (Maybe Text )
68
- , isDatacon :: ! Bool
69
- , moduleNameText :: ! Text
89
+ { name :: ! OccName
90
+ , parent :: ! (Maybe OccName )
91
+ , identModuleName :: ! ModuleName
70
92
}
71
93
deriving (Generic , Show )
72
94
deriving anyclass Hashable
73
95
96
+ isDatacon :: IdentInfo -> Bool
97
+ isDatacon = isDataOcc . name
98
+
74
99
instance Eq IdentInfo where
75
100
a == b = name a == name b
76
101
&& parent a == parent b
77
- && isDatacon a == isDatacon b
78
- && moduleNameText a == moduleNameText b
102
+ && identModuleName a == identModuleName b
79
103
80
104
instance NFData IdentInfo where
81
105
rnf IdentInfo {.. } =
82
106
-- deliberately skip the rendered field
83
- rnf name `seq` rnf parent `seq` rnf isDatacon `seq` rnf moduleNameText
84
-
85
- -- | Render an identifier as imported or exported style.
86
- -- TODO: pattern synonym
87
- renderIEWrapped :: Name -> Text
88
- renderIEWrapped n
89
- | isTcOcc occ && isSymOcc occ = " type " <> pack (printName n)
90
- | otherwise = pack $ printName n
91
- where
92
- occ = occName n
107
+ rnf name `seq` rnf parent `seq` rnf identModuleName
93
108
94
- mkIdentInfos :: Text -> AvailInfo -> [IdentInfo ]
109
+ mkIdentInfos :: ModuleName -> AvailInfo -> [IdentInfo ]
95
110
mkIdentInfos mod (AvailName n) =
96
- [IdentInfo (nameOccName n) (renderIEWrapped n) Nothing (isDataConName n) mod ]
111
+ [IdentInfo (nameOccName n) Nothing mod ]
97
112
mkIdentInfos mod (AvailFL fl) =
98
- [IdentInfo (nameOccName n) (renderIEWrapped n) Nothing (isDataConName n) mod ]
113
+ [IdentInfo (nameOccName n) Nothing mod ]
99
114
where
100
115
n = flSelector fl
101
116
mkIdentInfos mod (AvailTC parent (n: nn) flds)
102
117
-- Following the GHC convention that parent == n if parent is exported
103
118
| n == parent
104
- = [ IdentInfo (nameOccName n) (renderIEWrapped n) ( Just $! parentP) (isDataConName n ) mod
119
+ = [ IdentInfo (nameOccName n) (Just $! nameOccName parent ) mod
105
120
| n <- nn ++ map flSelector flds
106
121
] ++
107
- [ IdentInfo (nameOccName n) (renderIEWrapped n) Nothing (isDataConName n) mod ]
108
- where
109
- parentP = pack $ printName parent
122
+ [ IdentInfo (nameOccName n) Nothing mod ]
110
123
111
124
mkIdentInfos mod (AvailTC _ nn flds)
112
- = [ IdentInfo (nameOccName n) (renderIEWrapped n) Nothing (isDataConName n) mod
125
+ = [ IdentInfo (nameOccName n) Nothing mod
113
126
| n <- nn ++ map flSelector flds
114
127
]
115
128
@@ -160,25 +173,20 @@ createExportsMapHieDb withHieDb = do
160
173
mods <- withHieDb getAllIndexedMods
161
174
idents <- forM (filter (nonInternalModules . modInfoName . hieModInfo) mods) $ \ m -> do
162
175
let mn = modInfoName $ hieModInfo m
163
- mText = pack $ moduleNameString mn
164
- fmap (wrap . unwrap mText) <$> withHieDb (\ hieDb -> getExportsForModule hieDb mn)
176
+ fmap (wrap . unwrap mn) <$> withHieDb (\ hieDb -> getExportsForModule hieDb mn)
165
177
let exportsMap = Map. fromListWith (<>) (concat idents)
166
- return $ ExportsMap exportsMap $ buildModuleExportMap (concat idents)
178
+ return $! ExportsMap exportsMap $ buildModuleExportMap (concat idents)
167
179
where
168
180
wrap identInfo = (rendered identInfo, Set. fromList [identInfo])
169
181
-- unwrap :: ExportRow -> IdentInfo
170
- unwrap m ExportRow {.. } = IdentInfo exportName n p exportIsDatacon m
171
- where
172
- n = pack (occNameString exportName)
173
- p = pack . occNameString <$> exportParent
182
+ unwrap m ExportRow {.. } = IdentInfo exportName exportParent m
174
183
175
184
unpackAvail :: ModuleName -> IfaceExport -> [(Text , Text , [IdentInfo ])]
176
185
unpackAvail mn
177
- | nonInternalModules mn = map f . mkIdentInfos mod
186
+ | nonInternalModules mn = map f . mkIdentInfos mn
178
187
| otherwise = const []
179
188
where
180
- ! mod = pack $ moduleNameString mn
181
- f id @ IdentInfo {.. } = (printOutputable name, moduleNameText,[id ])
189
+ f id @ IdentInfo {.. } = (printOutputable name, moduleNameText id ,[id ])
182
190
183
191
184
192
identInfoToKeyVal :: IdentInfo -> (ModuleNameText , IdentInfo )
@@ -198,9 +206,9 @@ buildModuleExportMapFrom modIfaces = do
198
206
199
207
extractModuleExports :: ModIface -> (Text , HashSet IdentInfo )
200
208
extractModuleExports modIFace = do
201
- let modName = pack $ moduleNameString $ moduleName $ mi_module modIFace
209
+ let modName = moduleName $ mi_module modIFace
202
210
let functionSet = Set. fromList $ concatMap (mkIdentInfos modName) $ mi_exports modIFace
203
- (modName, functionSet)
211
+ (moduleNameText' modName, functionSet)
204
212
205
213
sortAndGroup :: [(ModuleNameText , IdentInfo )] -> Map. HashMap ModuleNameText (HashSet IdentInfo )
206
214
sortAndGroup assocs = Map. fromListWith (<>) [(k, Set. fromList [v]) | (k, v) <- assocs]
0 commit comments