Browse.hs 932 B

123456789101112131415161718192021222324252627282930313233
  1. module Browse (browseModule) where
  2. import Control.Applicative
  3. import Data.Char
  4. import Data.List
  5. import GHC
  6. import GHCApi
  7. import Name
  8. import Types
  9. ----------------------------------------------------------------
  10. browseModule :: Options -> String -> IO String
  11. browseModule opt mdlName = convert opt . format <$> browse opt mdlName
  12. where
  13. format
  14. | operators opt = formatOps
  15. | otherwise = removeOps
  16. removeOps = sort . filter (isAlpha.head)
  17. formatOps = sort . map formatOps'
  18. formatOps' x@(s:_)
  19. | isAlpha s = x
  20. | otherwise = '(' : x ++ ")"
  21. formatOps' [] = error "formatOps'"
  22. browse :: Options -> String -> IO [String]
  23. browse opt mdlName = withGHC $ do
  24. _ <- initSession0 opt
  25. maybeNamesToStrings <$> lookupModuleInfo
  26. where
  27. lookupModuleInfo = findModule (mkModuleName mdlName) Nothing >>= getModuleInfo
  28. maybeNamesToStrings = maybe [] (map getOccString . modInfoExports)