git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
First stab at better imports in psci, #753
69c6da4Harry Garrood
psci/Commands.hs+3−1
@@ -32,7 +32,7 @@ data Command
32 -- |
32 -- |
33 -- Import a module from a loaded file
33 -- Import a module from a loaded file
34 --
34 --
35− | Import ModuleName
35+ | Import ImportedModule
36 -- |
36 -- |
37 -- Browse a module
37 -- Browse a module
38 --
38 --
@@ -65,3 +65,5 @@ data Command
65 -- Show command
65 -- Show command
66 --
66 --
67 | Show String
67 | Show String
68+
69+type ImportedModule = (ModuleName, ImportDeclarationType, Maybe ModuleName)
psci/Main.hs+43−20
@@ -72,12 +72,16 @@ data PSCiOptions = PSCiOptions
72--
72--
73data PSCiState = PSCiState
73data PSCiState = PSCiState
74 { psciImportedFilenames :: [FilePath]
74 { psciImportedFilenames :: [FilePath]
75− , psciImportedModuleNames :: [P.ModuleName]
75+ , psciImportedModules :: [C.ImportedModule]
76 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
76 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
77 , psciLetBindings :: [P.Declaration]
77 , psciLetBindings :: [P.Declaration]
78 , psciNodeFlags :: [String]
78 , psciNodeFlags :: [String]
79 }
79 }
80
80
81+psciImportedModuleNames :: PSCiState -> [P.ModuleName]
82+psciImportedModuleNames (PSCiState{psciImportedModules = is}) =
83+ map (\(mn, _, _) -> mn) is
84+
81-- State helpers
85-- State helpers
82
86
83-- |
87-- |
@@ -89,8 +93,8 @@ updateImportedFiles filename st = st { psciImportedFilenames = filename : psciIm
89-- |
93-- |
90-- Updates the state to have more imported modules.
94-- Updates the state to have more imported modules.
91--
95--
92−updateImports :: P.ModuleName -> PSCiState -> PSCiState
96+updateImportedModules :: C.ImportedModule -> PSCiState -> PSCiState
93−updateImports name st = st { psciImportedModuleNames = name : psciImportedModuleNames st }
97+updateImportedModules im st = st { psciImportedModules = im : psciImportedModules st }
94
98
95-- |
99-- |
96-- Updates the state to have more loaded files.
100-- Updates the state to have more loaded files.
@@ -108,8 +112,8 @@ updateLets ds st = st { psciLetBindings = psciLetBindings st ++ ds }
108-- |
112-- |
109-- Load the necessary modules.
113-- Load the necessary modules.
110--
114--
111−defaultImports :: [P.ModuleName]
115+defaultImports :: [C.ImportedModule]
112−defaultImports = [P.ModuleName [P.ProperName "Prelude"]]
116+defaultImports = [(P.ModuleName [P.ProperName "Prelude"], D.Unqualified, Nothing)]
113
117
114-- |
118-- |
115-- Locates the node executable.
119-- Locates the node executable.
@@ -181,7 +185,7 @@ helpMessage = "The following commands are available:\n\n " ++
181 , replicate (11 - length arg) ' '
185 , replicate (11 - length arg) ' '
182 , desc
186 , desc
183 ]
187 ]
184− where cmd = ":" ++ head (D.commands dir)
188+ where cmd = ":" ++ head (D.commands dir)
185
189
186-- |
190-- |
187-- The welcome prologue.
191-- The welcome prologue.
@@ -370,10 +374,9 @@ mkdirp = createDirectoryIfMissing True . takeDirectory
370-- Makes a volatile module to execute the current expression.
374-- Makes a volatile module to execute the current expression.
371--
375--
372createTemporaryModule :: Bool -> PSCiState -> P.Expr -> P.Module
376createTemporaryModule :: Bool -> PSCiState -> P.Expr -> P.Module
373−createTemporaryModule exec PSCiState{psciImportedModuleNames = imports, psciLetBindings = lets} val =
377+createTemporaryModule exec PSCiState{psciImportedModules = imports, psciLetBindings = lets} val =
374 let
378 let
375 moduleName = P.ModuleName [P.ProperName "$PSCI"]
379 moduleName = P.ModuleName [P.ProperName "$PSCI"]
376− importDecl m = P.ImportDeclaration m P.Unqualified Nothing
377 traceModule = P.ModuleName [P.ProperName "Debug", P.ProperName "Trace"]
380 traceModule = P.ModuleName [P.ProperName "Debug", P.ProperName "Trace"]
378 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
381 trace = P.Var (P.Qualified (Just traceModule) (P.Ident "print"))
379 mainValue = P.App trace (P.Var (P.Qualified Nothing (P.Ident "it")))
382 mainValue = P.App trace (P.Var (P.Qualified Nothing (P.Ident "it")))
@@ -388,10 +391,9 @@ createTemporaryModule exec PSCiState{psciImportedModuleNames = imports, psciLetB
388-- Makes a volatile module to hold a non-qualified type synonym for a fully-qualified data type declaration.
391-- Makes a volatile module to hold a non-qualified type synonym for a fully-qualified data type declaration.
389--
392--
390createTemporaryModuleForKind :: PSCiState -> P.Type -> P.Module
393createTemporaryModuleForKind :: PSCiState -> P.Type -> P.Module
391−createTemporaryModuleForKind PSCiState{psciImportedModuleNames = imports} typ =
394+createTemporaryModuleForKind PSCiState{psciImportedModules = imports} typ =
392 let
395 let
393 moduleName = P.ModuleName [P.ProperName "$PSCI"]
396 moduleName = P.ModuleName [P.ProperName "$PSCI"]
394− importDecl m = P.ImportDeclaration m P.Unqualified Nothing
395 itDecl = P.TypeSynonymDeclaration (P.ProperName "IT") [] typ
397 itDecl = P.TypeSynonymDeclaration (P.ProperName "IT") [] typ
396 in
398 in
397 P.Module [] moduleName ((importDecl `map` imports) ++ [itDecl]) Nothing
399 P.Module [] moduleName ((importDecl `map` imports) ++ [itDecl]) Nothing
@@ -400,13 +402,15 @@ createTemporaryModuleForKind PSCiState{psciImportedModuleNames = imports} typ =
400-- Makes a volatile module to execute the current imports.
402-- Makes a volatile module to execute the current imports.
401--
403--
402createTemporaryModuleForImports :: PSCiState -> P.Module
404createTemporaryModuleForImports :: PSCiState -> P.Module
403−createTemporaryModuleForImports PSCiState{psciImportedModuleNames = imports} =
405+createTemporaryModuleForImports PSCiState{psciImportedModules = imports} =
404 let
406 let
405 moduleName = P.ModuleName [P.ProperName "$PSCI"]
407 moduleName = P.ModuleName [P.ProperName "$PSCI"]
406− importDecl m = P.ImportDeclaration m P.Unqualified Nothing
407 in
408 in
408 P.Module [] moduleName (importDecl `map` imports) Nothing
409 P.Module [] moduleName (importDecl `map` imports) Nothing
409
410
411+importDecl :: C.ImportedModule -> P.Declaration
412+importDecl (mn, declType, asQ) = P.ImportDeclaration mn declType asQ
413+
410modulesDir :: FilePath
414modulesDir :: FilePath
411modulesDir = ".psci_modules" ++ pathSeparator : "node_modules"
415modulesDir = ".psci_modules" ++ pathSeparator : "node_modules"
412
416
@@ -463,17 +467,36 @@ handleShowLoadedModules = do
463--
467--
464handleShowImportedModules :: PSCI ()
468handleShowImportedModules :: PSCI ()
465handleShowImportedModules = do
469handleShowImportedModules = do
466− PSCiState { psciImportedModuleNames = importedModuleNames } <- PSCI $ lift get
470+ PSCiState { psciImportedModules = importedModules } <- PSCI $ lift get
467− psciIO $ readModules importedModuleNames >>= putStrLn
471+ psciIO $ showModules importedModules >>= putStrLn
468 return ()
472 return ()
473+ where
469− where readModules = return . unlines . sort . map N.runModuleName
474+ showModules = return . unlines . sort . map showModule
475+ showModule (mn, declType, asQ) =
476+ case asQ of
477+ Just mn' -> "qualified " ++ N.runModuleName mn ++ " as " ++ N.runModuleName mn'
478+ Nothing -> N.runModuleName mn ++ " " ++ showDeclType declType
479+ showDeclType D.Unqualified = ""
480+ showDeclType (D.Qualifying refs) = refsList refs
481+ showDeclType (D.Hiding refs) = "hiding " ++ refsList refs
482+ refsList refs = "(" ++ commaList (map showRef refs) ++ ")"
483+
484+ showRef :: P.DeclarationRef -> String
485+ showRef (D.TypeRef pn dctors) = show pn ++ "(" ++ maybe ".." (commaList . map N.runProperName) dctors ++ ")"
486+ showRef (D.ValueRef ident) = show ident
487+ showRef (D.TypeClassRef pn) = show pn
488+ showRef (D.TypeInstanceRef ident) = show ident
489+ showRef (D.PositionedDeclarationRef _ _ ref) = showRef ref
490+
491+ commaList :: [String] -> String
492+ commaList = intercalate ", "
470
493
471-- |
494-- |
472-- Imports a module, preserving the initial state on failure.
495-- Imports a module, preserving the initial state on failure.
473--
496--
474−handleImport :: P.ModuleName -> PSCI ()
497+handleImport :: C.ImportedModule -> PSCI ()
475−handleImport moduleName = do
498+handleImport im = do
476− st <- updateImports moduleName <$> PSCI (lift get)
499+ st <- updateImportedModules im <$> PSCI (lift get)
477 let m = createTemporaryModuleForImports st
500 let m = createTemporaryModuleForImports st
478 e <- psciIO . runMake $ P.make modulesDir (psciLoadedModules st ++ [(Left P.RebuildAlways, m)]) []
501 e <- psciIO . runMake $ P.make modulesDir (psciLoadedModules st ++ [(Left P.RebuildAlways, m)]) []
479 case e of
502 case e of
@@ -575,7 +598,7 @@ getCommand singleLineMode = do
575handleCommand :: C.Command -> PSCI ()
598handleCommand :: C.Command -> PSCI ()
576handleCommand (C.Expression val) = handleDeclaration val
599handleCommand (C.Expression val) = handleDeclaration val
577handleCommand C.Help = PSCI $ outputStrLn helpMessage
600handleCommand C.Help = PSCI $ outputStrLn helpMessage
578−handleCommand (C.Import moduleName) = handleImport moduleName
601+handleCommand (C.Import im) = handleImport im
579handleCommand (C.Let l) = handleLet l
602handleCommand (C.Let l) = handleLet l
580handleCommand (C.LoadFile filePath) = do
603handleCommand (C.LoadFile filePath) = do
581 absPath <- psciIO $ expandTilde filePath
604 absPath <- psciIO $ expandTilde filePath
@@ -592,7 +615,7 @@ handleCommand C.Reset = do
592 files <- psciImportedFilenames <$> PSCI (lift get)
615 files <- psciImportedFilenames <$> PSCI (lift get)
593 PSCI . lift . modify $ \st -> st
616 PSCI . lift . modify $ \st -> st
594 { psciImportedFilenames = files
617 { psciImportedFilenames = files
595− , psciImportedModuleNames = defaultImports
618+ , psciImportedModules = defaultImports
596 , psciLetBindings = []
619 , psciLetBindings = []
597 }
620 }
598 loadAllImportedModules
621 loadAllImportedModules
psci/Parser.hs+1−1
@@ -60,7 +60,7 @@ parseCommand cmdString =
60 Just D.Help -> return C.Help
60 Just D.Help -> return C.Help
61 Just D.Quit -> return C.Quit
61 Just D.Quit -> return C.Quit
62 Just D.Reset -> return C.Reset
62 Just D.Reset -> return C.Reset
63− Just D.Import -> C.Import <$> parseRest P.moduleName arg
63+ Just D.Import -> C.Import <$> parseRest P.parseImportDeclarationTail arg
64 Just D.Browse -> C.Browse <$> parseRest P.moduleName arg
64 Just D.Browse -> C.Browse <$> parseRest P.moduleName arg
65 Just D.Load -> return $ C.LoadFile (trim arg)
65 Just D.Load -> return $ C.LoadFile (trim arg)
66 Just D.Show -> return $ C.Show (trim arg)
66 Just D.Show -> return $ C.Show (trim arg)
src/Language/PureScript/Parser/Declarations.hs+11−3
@@ -24,6 +24,7 @@ module Language.PureScript.Parser.Declarations (
24 parseGuard,
24 parseGuard,
25 parseBinder,
25 parseBinder,
26 parseBinderNoParens,
26 parseBinderNoParens,
27+ parseImportDeclarationTail,
27) where
28) where
28
29
29import Prelude hiding (lex)
30import Prelude hiding (lex)
@@ -148,6 +149,13 @@ parseImportDeclaration :: TokenParser Declaration
148parseImportDeclaration = do
149parseImportDeclaration = do
149 reserved "import"
150 reserved "import"
150 indented
151 indented
152+ (mn, declType, asQ) <- parseImportDeclarationTail
153+ return $ ImportDeclaration mn declType asQ
154+
155+-- |
156+-- The part of an import statement following the 'import'.
157+parseImportDeclarationTail :: TokenParser (ModuleName, ImportDeclarationType, (Maybe ModuleName))
158+parseImportDeclarationTail =
151 qualImport <|> stdImport
159 qualImport <|> stdImport
152 where
160 where
153 stdImport = do
161 stdImport = do
@@ -157,10 +165,10 @@ parseImportDeclaration = do
157 stdImportHiding mn = do
165 stdImportHiding mn = do
158 reserved "hiding"
166 reserved "hiding"
159 declType <- importDeclarationType Hiding
167 declType <- importDeclarationType Hiding
160− return $ ImportDeclaration mn declType Nothing
168+ return (mn, declType, Nothing)
161 stdImportQualifying mn = do
169 stdImportQualifying mn = do
162 declType <- importDeclarationType Qualifying
170 declType <- importDeclarationType Qualifying
163− return $ ImportDeclaration mn declType Nothing
171+ return (mn, declType, Nothing)
164 qualImport = do
172 qualImport = do
165 reserved "qualified"
173 reserved "qualified"
166 indented
174 indented
@@ -168,7 +176,7 @@ parseImportDeclaration = do
168 declType <- importDeclarationType Qualifying
176 declType <- importDeclarationType Qualifying
169 reserved "as"
177 reserved "as"
170 asQ <- moduleName
178 asQ <- moduleName
171− return $ ImportDeclaration moduleName' declType (Just asQ)
179+ return (moduleName', declType, Just asQ)
172 importDeclarationType expectedType = do
180 importDeclarationType expectedType = do
173 idents <- P.optionMaybe $ indented *> (parens $ commaSep parseDeclarationRef)
181 idents <- P.optionMaybe $ indented *> (parens $ commaSep parseDeclarationRef)
174 return $ fromMaybe Unqualified (expectedType <$> idents)
182 return $ fromMaybe Unqualified (expectedType <$> idents)
src/Language/PureScript/Pretty/Values.hs+1−1
@@ -1,7 +1,7 @@
1-----------------------------------------------------------------------------
1-----------------------------------------------------------------------------
2--
2--
3-- Module : Language.PureScript.Pretty.Values
3-- Module : Language.PureScript.Pretty.Values
4−-- Copyright : Kinds.hs(c) Phil Freeman 2013
4+-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
5-- License : MIT
6--
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
7-- Maintainer : Phil Freeman <paf31@cantab.net>