git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
More completion fixes and improvements for PSCI.

* complete type names for `:k`; * complete data constructors where identifiers are expected; * only complete unqualified names when the module that contains them is imported (otherwise they're unavailable!); * fix Language.PureScript.AST.Declarations.exportedDctors.

commitc542b4ff52695a753d95d7a50c46223995bdebd8 authorVladimir Korablin <vkorablin@gmail.com> dateWed 7 Jan 2015, 18:32 (12 years ago) parent430b4e2
3 files changed+65−31
psci/Main.hs+59−28 src/Language/PureScript/AST/Declarations.hs+6 src/Language/PureScript/ModuleDependencies.hs−3
psci/Main.hs+59−28
@@ -185,7 +185,8 @@ quitMessage = "See ya!"
185185
186186-- Haskeline completions
187187
188−data CompletionContext = Command String | FilePath String | Module | Identifier | Fixed [String] | Multiple [CompletionContext]
188+data CompletionContext = Command String | FilePath String | Module | Identifier
189+ | Type | Fixed [String] | Multiple [CompletionContext]
189190 deriving (Show)
190191
191192-- |
@@ -203,7 +204,7 @@ completionContext (':' : c : _) word = case c of
203204 '?' -> Nothing
204205 's' -> Just $ Fixed ["import", "loaded"]
205206 't' -> Just Identifier
206− 'k' -> Just Identifier
207+ 'k' -> Just Type
207208 _ -> Nothing
208209completionContext _ _ = Just Identifier
209210
@@ -228,7 +229,8 @@ completion = completeWordWithPrev Nothing " \t\n\r" findCompletions
228229 getCompletion (Command s) = return $ (map Left) $ nub $ filter (isPrefixOf s) C.commands
229230 getCompletion (FilePath f) = (map Right) <$> listFiles f
230231 getCompletion Module = (map Left) <$> getModuleNames
231− getCompletion Identifier = (map Left) <$> getIdentNames
232+ getCompletion Identifier = (map Left) <$> ((++) <$> getIdentNames <*> getDctorNames)
233+ getCompletion Type = (map Left) <$> getTypeNames
232234 getCompletion (Fixed list) = return $ (map Left) list
233235 getCompletion (Multiple contexts) = concat <$> mapM getCompletion contexts
234236
@@ -238,30 +240,60 @@ completion = completeWordWithPrev Nothing " \t\n\r" findCompletions
238240 getModuleNames :: StateT PSCiState IO [String]
239241 getModuleNames = moduleNames <$> getLoadedModules
240242
243+ mapLoadedModulesAndQualify :: (Show a) => (P.Module -> [a]) -> StateT PSCiState IO [String]
244+ mapLoadedModulesAndQualify f = do
245+ ms <- getLoadedModules
246+ q <- sequence [qualifyIfNeeded m (f m) | m <- ms]
247+ return $ concat q
248+
241249 getIdentNames :: StateT PSCiState IO [String]
242− getIdentNames = identNames <$> getLoadedModules
243−
244− getDeclName :: Maybe [P.DeclarationRef] -> P.Declaration -> Maybe P.Ident
245− getDeclName exts (P.ValueDeclaration ident _ _ _) | isExported ident exts = Just ident
246− getDeclName exts (P.ExternDeclaration _ ident _ _) | isExported ident exts = Just ident
247− getDeclName exts (P.PositionedDeclaration _ d) = getDeclName exts d
248− getDeclName _ _ = Nothing
249−
250− isExported :: N.Ident -> Maybe [P.DeclarationRef] -> Bool
251− isExported ident = maybe True (any exports)
252− where
253− exports :: P.DeclarationRef -> Bool
254− exports (P.ValueRef ident') = ident == ident'
255− exports (P.PositionedDeclarationRef _ r) = exports r
256− exports _ = False
257−
258− identNames :: [P.Module] -> [String]
259− identNames ms = nub [ show qual
260− | P.Module moduleName ds exts <- ms
261− , ident <- mapMaybe (getDeclName exts) ds
262− , qual <- [ P.Qualified Nothing ident
263− , P.Qualified (Just moduleName) ident]
264− ]
250+ getIdentNames = mapLoadedModulesAndQualify identNames
251+
252+ getDctorNames :: StateT PSCiState IO [String]
253+ getDctorNames = mapLoadedModulesAndQualify dctorNames
254+
255+ getTypeNames :: StateT PSCiState IO [String]
256+ getTypeNames = mapLoadedModulesAndQualify typeDecls
257+
258+ qualifyIfNeeded :: (Show a) => P.Module -> [a] -> StateT PSCiState IO [String]
259+ qualifyIfNeeded m decls = do
260+ let name = P.getModuleName m
261+ imported <- psciImportedModuleNames <$> get
262+ let qualified = map (P.Qualified $ Just name) decls
263+ if name `elem` imported then
264+ return $ map show $ qualified ++ (map (P.Qualified Nothing) decls)
265+ else
266+ return $ map show qualified
267+
268+ typeDecls :: P.Module -> [N.ProperName]
269+ typeDecls m = mapMaybe getTypeName $ filter P.isDataDecl (P.exportedDeclarations m)
270+ where getTypeName :: P.Declaration -> Maybe N.ProperName
271+ getTypeName (P.TypeSynonymDeclaration name _ _) = Just name
272+ getTypeName (P.DataDeclaration _ name _ _) = Just name
273+ getTypeName (P.PositionedDeclaration _ d) = getTypeName d
274+ getTypeName _ = Nothing
275+
276+ identNames :: P.Module -> [N.Ident]
277+ identNames (P.Module _ ds exports) = nub [ ident | ident <- mapMaybe (getDeclName exports) ds ]
278+ where getDeclName :: Maybe [P.DeclarationRef] -> P.Declaration -> Maybe P.Ident
279+ getDeclName exts decl@(P.ValueDeclaration ident _ _ _) | P.isExported exts decl = Just ident
280+ getDeclName exts decl@(P.ExternDeclaration _ ident _ _) | P.isExported exts decl = Just ident
281+ getDeclName exts (P.PositionedDeclaration _ d) = getDeclName exts d
282+ getDeclName _ _ = Nothing
283+
284+ dctorNames :: P.Module -> [N.ProperName]
285+ dctorNames m = nub $ concat $ map (P.exportedDctors m) dnames
286+ where getDataDeclName :: P.Declaration -> Maybe N.ProperName
287+ getDataDeclName (P.DataDeclaration _ name _ _) = Just name
288+ getDataDeclName (P.PositionedDeclaration _ d) = getDataDeclName d
289+ getDataDeclName _ = Nothing
290+
291+ dnames :: [N.ProperName]
292+ dnames = (mapMaybe getDataDeclName onlyDataDecls)
293+
294+ onlyDataDecls :: [P.Declaration]
295+ onlyDataDecls = (filter P.isDataDecl (P.exportedDeclarations m))
296+
265297 moduleNames :: [P.Module] -> [String]
266298 moduleNames ms = nub [show moduleName | P.Module moduleName _ _ <- ms]
267299
@@ -601,7 +633,6 @@ main = execParser opts >>= loop
601633 infoModList = fullDesc <> headerInfo <> footerInfo
602634 headerInfo = header "psci - Interactive mode for PureScript"
603635 footerInfo = footer $ "psci " ++ showVersion Paths.version
604−
636+
605637 version :: Parser (a -> a)
606638 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> Opts.help "Show the version number" <> hidden
607−
src/Language/PureScript/AST/Declarations.hs+6
@@ -34,6 +34,10 @@ import Language.PureScript.Environment
3434--
3535data Module = Module ModuleName [Declaration] (Maybe [DeclarationRef]) deriving (Show, D.Data, D.Typeable)
3636
37+-- | Return a module's name.
38+getModuleName :: Module -> ModuleName
39+getModuleName (Module name _ _) = name
40+
3741-- |
3842-- Test if a declaration is exported, given a module's export list.
3943--
@@ -44,6 +48,7 @@ isExported exps (PositionedDeclaration _ d) = isExported exps d
4448isExported (Just exps) decl = any (matches decl) exps
4549 where
4650 matches (TypeDeclaration ident _) (ValueRef ident') = ident == ident'
51+ matches (ValueDeclaration ident _ _ _) (ValueRef ident') = ident == ident'
4752 matches (ExternDeclaration _ ident _ _) (ValueRef ident') = ident == ident'
4853 matches (DataDeclaration _ ident _ _) (TypeRef ident' _) = ident == ident'
4954 matches (ExternDataDeclaration ident _) (TypeRef ident' _) = ident == ident'
@@ -77,6 +82,7 @@ exportedDctors (Module _ decls exps) ident =
7782 where
7883 dctors = concatMap getDctors decls
7984 getDctors (DataDeclaration _ _ _ ctors) = map fst ctors
85+ getDctors (PositionedDeclaration _ d) = getDctors d
8086 getDctors _ = []
8187
8288-- |
src/Language/PureScript/ModuleDependencies.hs−3
@@ -64,9 +64,6 @@ usedModules = let (f, _, _, _, _) = everythingOnValues (++) forDecls forValues (
6464 forTypes (ConstrainedType cs _) = mapMaybe (\(Qualified mn _, _) -> mn) cs
6565 forTypes _ = []
6666
67−getModuleName :: Module -> ModuleName
68−getModuleName (Module mn _ _) = mn
69−
7067-- |
7168-- Convert a strongly connected component of the module graph to a module
7269--