git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Improve implementation avoiding loading extern.purs file
commit358a76307ecc9b923a8d797c563db1fe6c723fce authorAntoine R. Dumont <antoine.romain.dumont@gmail.com> dateFri 31 Oct 2014, 12:31 (12 years ago) parent9dc3e8f
1 file changed+40−15
psci/Main.hs+40−15
psci/Main.hs+40−15
@@ -28,7 +28,7 @@ import qualified Control.Monad.Trans.State.Lazy as L
2828import Control.Monad.Error (ErrorT(..), MonadError)
2929import Control.Monad.Error.Class (MonadError(..))
3030
31−import Data.List (intercalate, isPrefixOf, nub, sortBy)
31+import Data.List (intercalate, isPrefixOf, nub, sortBy, isInfixOf)
3232import Data.Maybe (mapMaybe)
3333import Data.Foldable (traverse_)
3434import Data.Version (showVersion)
@@ -56,6 +56,7 @@ import qualified System.IO.UTF8 as U
5656 (writeFile, putStrLn, print, readFile)
5757import qualified Language.PureScript.Names as N
5858import qualified Language.PureScript.Declarations as D
59+import Language.PureScript.Pretty.Types (prettyPrintType)
5960
6061-- |
6162-- The PSCI state.
@@ -323,22 +324,46 @@ handleImport moduleName = do
323324--
324325handleBrowse :: P.ModuleName -> PSCI ()
325326handleBrowse moduleName = do
326− psciIO $ (readModuleTypes . N.runModuleName) moduleName >>= putStrLn
327+ PSCiState { psciLoadedModules = loadedModules
328+ , psciImportedFilenames = loadedPaths} <- PSCI $ lift get
329+ psciIO $ (readModuleTypes loadedModules . findModulePath loadedPaths . N.runModuleName) moduleName >>= putStrLn
327330 return ()
328331 where
329− -- Compute the file `extern.purs` for the module moduleName.
330− externPursFile :: FilePath -> FilePath
331− externPursFile modName = modulesDir ++ pathSeparator : modName ++ pathSeparator : "externs.purs"
332−
333− -- Read the module's type definition.
334− readModuleTypes :: String -> IO String
335− readModuleTypes [] = return "Module must be specified."
336− readModuleTypes modName = let modFile = externPursFile modName in
337− do
338− exists <- doesFileExist modFile
339− if exists
340− then readFile modFile
341− else return $ "Module '" ++ modName ++ "' not found!"
332+ moduleToPath :: String -> FilePath
333+ moduleToPath = (pathSeparator:) . (++ ".purs") . N.runModuleName . N.moduleNameFromString
334+
335+ -- find the fully qualified module path (key to full module) - "" if not found
336+ findModulePath :: [FilePath] -> String -> Either String FilePath
337+ findModulePath _ [] = Left "Module must be specified."
338+ findModulePath filePaths modName = let partialModulePath = moduleToPath modName in
339+ case filter (isInfixOf partialModulePath) filePaths of
340+ [] -> Left $ "Module ('" ++ modName ++ "','" ++ partialModulePath ++ "') was not found."
341+ (x:_) -> Right x
342+
343+ -- retrieve explicit types
344+ getSignature (P.PositionedDeclaration _ d) = getSignature d
345+ getSignature (P.TypeDeclaration ident mtype) = Just $ show ident ++ " :: " ++ prettyPrintType mtype
346+ getSignature _ = Nothing
347+
348+ -- pretty print module's signature
349+ signatures :: P.Module -> [String]
350+ signatures (P.Module _ ds _) = mapMaybe getSignature ds
351+
352+ -- names :: [P.Module] -> [String]
353+ -- names ms = nub [ show qual
354+ -- | P.Module moduleName ds exts <- ms
355+ -- , ident <- mapMaybe (getDeclName exts) ds
356+ -- , qual <- [ P.Qualified Nothing ident
357+ -- , P.Qualified (Just moduleName) ident]
358+ -- ]
359+
360+ readModuleTypes :: [(FilePath, D.Module)] -> Either String FilePath -> IO String
361+ readModuleTypes [] _ = return "No module(s) loaded."
362+ readModuleTypes _ (Left err) = return err
363+ readModuleTypes loadedModules (Right modName) =
364+ return $ case lookup modName loadedModules of
365+ Just mName -> unlines $ signatures mName
366+ _ -> "Module '" ++ modName ++ "' not found!"
342367
343368-- |
344369-- Takes a value and prints its type