@@ -110,6 +110,8 @@ import Development.IDE.GHC.Compat.Util (emptyUDFM, plusUDFM)
110
110
import qualified Language.LSP.Server as LSP
111
111
import qualified Language.LSP.Types as LSP
112
112
import Unsafe.Coerce
113
+ import Data.Tuple.Extra (first )
114
+ import Data.Tuple (swap )
113
115
114
116
-- | Given a string buffer, return the string (after preprocessing) and the 'ParsedModule'.
115
117
parseModule
@@ -992,26 +994,80 @@ getDocsBatch
992
994
:: HscEnv
993
995
-> Module -- ^ a moudle where the names are in scope
994
996
-> [Name ]
997
+ -- 2021-11-19: NOTE: Don't forget these 'Map' currently lazy.
995
998
-- 2021-11-18: NOTE: Map Int would become IntMap if next GHCs.
996
999
-> IO (Either ErrorMessages (Map. Map Name (Either GetDocsFailure (Maybe HsDocString , Maybe (Map. Map Int HsDocString )))))
997
1000
-- ^ Return a 'Map' of 'Name's to 'Either' (no docs messages) (general doc body & arg docs)
998
1001
getDocsBatch hsc_env _mod _names = do
999
- ((_warns,errs), res) <- initTc hsc_env HsSrcFile False _mod fakeSpan $ Map. fromList <$> traverse findNameInfo _names
1002
+ ((_warns,errs), res) <- initTc hsc_env HsSrcFile False _mod fakeSpan $ Map. fromList <$> traverse undefined undefined
1000
1003
pure $ maybeToEither errs res
1001
1004
where
1002
- findNameInfo :: Name -> IOEnv (Env TcGblEnv TcLclEnv ) (Name , Either GetDocsFailure (Maybe HsDocString , Maybe (Map. Map Int HsDocString )))
1003
- findNameInfo name =
1004
- case nameModule_maybe name of
1005
- Nothing -> return (name, Left $ NameHasNoModule name)
1006
- Just mod -> do
1005
+ -- fun1 :: (Map Name (IOEnv (Env TcGblEnv TcLclEnv) (Maybe HsDocString, Maybe (Map Int HsDocString))))
1006
+ -- fun1 = Map.fromList fun
1007
+
1008
+ fun :: IOEnv (Env TcGblEnv TcLclEnv ) (Map Name (Maybe HsDocString , Maybe (Map Int HsDocString )))
1009
+ fun = _ getAskedIfaceDocs loadIfaces
1010
+
1011
+ getAskedIfaceDocs :: ((Map Name HsDocString , Map Name (Map Int HsDocString )), [Name ]) -> Map Name (Maybe HsDocString , Maybe (Map Int HsDocString ))
1012
+ getAskedIfaceDocs a = Map. fromList $
1013
+ fun2 <$> snd a
1014
+ where
1015
+ fun2 :: Name -> (Name , (Maybe HsDocString , Maybe (Map Int HsDocString )))
1016
+ fun2 n = (n, bimap (Map. lookup n) (Map. lookup n) $ fst a)
1017
+
1018
+ loadIfaces :: IOEnv (Env TcGblEnv TcLclEnv ) [((Map Name HsDocString , Map Name (Map Int HsDocString )), [Name ])]
1019
+ loadIfaces = fun3 (fmap (first getIfaceGenNArgDocMaps) loadModules)
1020
+ where
1021
+ fun3 :: [(env ms , ns )] -> env [(ms , ns )]
1022
+ fun3 a = (fmap . fmap ) swap $ sequenceA $ fmap swap a
1023
+
1024
+ getIfaceGenNArgDocMaps :: TcRn ModIface -> IOEnv (Env TcGblEnv TcLclEnv ) (Map Name HsDocString , Map Name (Map Int HsDocString ))
1025
+ getIfaceGenNArgDocMaps mi = do
1026
+ ModIface
1027
+ { mi_doc_hdr = mb_doc_hdr
1028
+ , mi_decl_docs = DeclDocMap dmap
1029
+ , mi_arg_docs = ArgDocMap amap
1030
+ }
1031
+ <- mi
1032
+ pure $
1033
+ if isNothing mb_doc_hdr && Map. null dmap && Map. null amap
1034
+ then error " Instead of 'error' here handle 'NoDocsInIface mod $ isCompiled name' case"
1035
+ else (dmap, amap)
1036
+
1037
+ loadModules :: [(TcRn ModIface , [Name ])]
1038
+ loadModules = fmap loadAvailableModules namesGroupedByModule
1039
+ where
1040
+ loadAvailableModules :: (Module , [Name ]) -> (TcRn ModIface , [Name ])
1041
+ loadAvailableModules = first loadModuleInterfaceOnce
1042
+
1043
+
1044
+ loadModuleInterfaceOnce :: Module -> TcRn ModIface
1045
+ loadModuleInterfaceOnce =
1046
+ loadModuleInterface " getModuleInterface"
1047
+
1048
+ namesGroupedByModule :: [(Module , [Name ])]
1049
+ namesGroupedByModule =
1050
+ groupSort $ fmap (first (fromMaybe (error " Instead of 'error' handle here 'NameHasNoModule' case" ) . nameModule_maybe) . dupe) _names
1051
+
1052
+ -- modulesPartitionedOnAvalability :: [(Either (Name -> GetDocsFailure) Module, [Name])]
1053
+ -- modulesPartitionedOnAvalability = fmap partitionOnModuleAvalibility namesGroupedByModule
1054
+
1055
+ -- partitionOnModuleAvalibility :: (Maybe Module, [Name]) -> (Either (Name -> GetDocsFailure) Module, [Name])
1056
+ -- partitionOnModuleAvalibility =
1057
+ -- first (maybeToEither NameHasNoModule)
1058
+
1059
+
1060
+ -- 2021-11-18: NOTE: This code initially was taken from: https://hackage.haskell.org/package/ghc-9.2.1/docs/src/GHC.Runtime.Eval.html#getDocs
1061
+ findNameInfo :: Maybe Module -> Name -> IOEnv (Env TcGblEnv TcLclEnv ) (Name , Either GetDocsFailure (Maybe HsDocString , Maybe (Map. Map Int HsDocString )))
1062
+ findNameInfo Nothing name = return (name, Left $ NameHasNoModule name)
1063
+ findNameInfo (Just mod ) name = do
1007
1064
ModIface
1008
1065
{ mi_doc_hdr = mb_doc_hdr
1009
1066
, mi_decl_docs = DeclDocMap dmap
1010
1067
, mi_arg_docs = ArgDocMap amap
1011
1068
}
1012
1069
<- loadModuleInterface " getModuleInterface" mod
1013
1070
pure . (name,) $
1014
- -- 2021-11-17: NOTE: one does not simply check into Mordor (not 1 mode)
1015
1071
if isNothing mb_doc_hdr && Map. null dmap && Map. null amap
1016
1072
then Left $ NoDocsInIface mod $ isCompiled name
1017
1073
else Right (Map. lookup name dmap, Map. lookup name amap)
0 commit comments