Skip to content
Browse files

classTypeNames

Add class type name completions, generalize a bit to use the focusedModule if
present, falling back to the module graph when necessary.
  • Loading branch information...
1 parent ea86a46 commit f27a79e75ae850e45734b72551ff2ad3122b9ed0 Scott Michel committed Feb 11, 2011
Showing with 82 additions and 25 deletions.
  1. +4 −4 lib/Scion/Inspect.hs
  2. +13 −0 lib/Scion/Inspect/Completions.hs
  3. +19 −18 lib/Scion/Session.hs
  4. +8 −0 lib/Scion/Types.hs
  5. +38 −3 server/Scion/Server/Commands.hs
View
8 lib/Scion/Inspect.hs
@@ -416,10 +416,10 @@ tokenArbitraryAtPoint projectRoot contents line column literate =
-- | Extract occurrences based on lexing
occurrences :: FilePath -- ^ Project root or base directory for absolute path conversion
- -> String -- ^ Contents to be parsed
- -> String -- ^ Token value to find
- -> Bool -- ^ Literate source flag (True = literate, False = ordinary)
- -> ScionM (Either Note [TokenDef])
+ -> String -- ^ Contents to be parsed
+ -> String -- ^ Token value to find
+ -> Bool -- ^ Literate source flag (True = literate, False = ordinary)
+ -> ScionM (Either Note [TokenDef])
occurrences projectRoot contents query literate =
let -- Get the list of tokens matching the queru for relevant token types
tokensMatching :: [TokenDef] -> [TokenDef]
View
13 lib/Scion/Inspect/Completions.hs
@@ -3,6 +3,7 @@
module Scion.Inspect.Completions
( getTypeCompletions
, getVarIdCompletions
+ , getClassTypeNameCompletions
)
where
@@ -35,6 +36,12 @@ getVarIdCompletions :: Maybe ModSummary
getVarIdCompletions (Just modSum) = generateCompletions modSum onlyIEVarIdThings hasMIdDecl
getVarIdCompletions Nothing = return []
+-- | Generate the completions for class type names
+getClassTypeNameCompletions :: Maybe ModSummary
+ -> ScionM CompletionTuples
+getClassTypeNameCompletions (Just modSum) = generateCompletions modSum onlyIEClassThings hasMClassDecl
+getClassTypeNameCompletions Nothing = return []
+
-- | The main workhorse for generating completion tuples
generateCompletions :: ModSummary
-> (IE RdrName -> Bool)
@@ -187,3 +194,9 @@ onlyIETypeThings _ = False
onlyIEVarIdThings :: (IE RdrName) -> Bool
onlyIEVarIdThings (IEVar _) = True
onlyIEVarIdThings _ = False
+
+-- | Filter predicate for extracting class type names from import entities
+
+-- N.B.:
+onlyIEClassThings :: (IE RdrName) -> Bool
+onlyIEClassThings = onlyIETypeThings
View
37 lib/Scion/Session.hs
@@ -84,7 +84,7 @@ resetSessionState :: ScionM ()
resetSessionState =
unload
>> getSessionSelector initialDynFlags
- >>= (\dflags0 -> setSessionDynFlags (initialScionDynFlags dflags0))
+ >>= (setSessionDynFlags . initialScionDynFlags)
>> return ()
-- | Root directory of the current Cabal project.
@@ -116,8 +116,7 @@ setComponentDynFlags ::
-- build-depends of the loaded component.
--
-- TODO: do something with this depending on Scion mode?
-setComponentDynFlags (Component c) = do
- addCmdLineFlags =<< componentOptions c
+setComponentDynFlags (Component c) = addCmdLineFlags =<< componentOptions c
-- | Set the targets for a 'GHC.load' command from the meta data of
-- the current component.
@@ -130,8 +129,7 @@ setComponentDynFlags (Component c) = do
-- contain the specified component.
--
setComponentTargets :: Component -> ScionM ()
-setComponentTargets (Component c) = do
- setTargets =<< componentTargets c
+setComponentTargets (Component c) = setTargets =<< componentTargets c
-- | Load the specified component.
--
@@ -143,13 +141,12 @@ setComponentTargets (Component c) = do
-- contain the specified component.
--
loadComponent :: Component
- -> ScionM CompilationResult
+ -> ScionM CompilationResult
loadComponent comp = loadComponent' comp defaultLoadOptions
loadComponent' :: Component
- -> LoadOptions -- ^ Should we build on disk?, etc
- -> ScionM CompilationResult
- -- ^ The compilation result.
+ -> LoadOptions -- ^ Should we build on disk?, etc
+ -> ScionM CompilationResult -- ^ The compilation result.
loadComponent' comp options = do
-- TODO: group warnings by file
resetSessionState
@@ -159,14 +156,17 @@ loadComponent' comp options = do
_ <- setComponentDynFlags comp
dflags0 <- getSessionDynFlags
let dflags1
- | lo_output options = dflags0{ hscTarget = defaultObjectTarget
- , ghcMode = CompManager
- , ghcLink = LinkBinary
- }
- | otherwise = dflags0
+ | lo_output options
+ = dflags0 {
+ hscTarget = defaultObjectTarget
+ , ghcMode = CompManager
+ , ghcLink = LinkBinary
+ }
+ | otherwise
+ = dflags0
-- DefinitionSite is not up to date if we have pregenerated .hi or .o files
let dflags
- | lo_forcerecomp options=dopt_set dflags1 Opt_ForceRecomp
+ | lo_forcerecomp options =dopt_set dflags1 Opt_ForceRecomp
| otherwise = dflags1
_ <- setSessionDynFlags dflags
_ <- setComponentTargets comp
@@ -178,7 +178,7 @@ loadComponent' comp options = do
-- | Utility method to regenerate defSiteDB after loading.
getDefSiteDB :: CompilationResult -- ^ The result of loading.
-> ScionM ()
-getDefSiteDB rslt = do
+getDefSiteDB rslt =
getModuleGraph
>>= (\mg ->
projectRootDir
@@ -211,7 +211,7 @@ setActiveComponent the_comp@(Component comp) = do
modifySessionState $ \sess ->
sess { activeComponent = Just the_comp }
Just msg -> do
- liftIO $ throwIO $ userError msg -- XXX
+ liftIO $ throwIO $ userError msg
where
needs_unloading (Just c) | c /= the_comp = True
needs_unloading _ = False
@@ -391,7 +391,8 @@ backgroundTypecheckFile fname0 = do
modifySessionState (\s -> s { bgTcCache = tc_res
, lastCompResult = comp_rslt'
- , moduleCache = updModCache })
+ , moduleCache = updModCache
+ } )
return $ Right comp_rslt'
View
8 lib/Scion/Types.hs
@@ -553,3 +553,11 @@ hasMIdDecl decls =
let hasMIdDecl' MIdDecl = True
hasMIdDecl' _ = False
in Fold.and $ Set.map hasMIdDecl' decls
+
+-- | Does the mod declaration set have a 'MClassDecl'?
+hasMClassDecl :: ModSymDecls
+ -> Bool
+hasMClassDecl decls =
+ let hasMClassDecl' (MClassDecl _) = True
+ hasMClassDecl' _ = False
+ in Fold.and $ Set.map hasMClassDecl' decls
View
41 server/Scion/Server/Commands.hs
@@ -189,6 +189,7 @@ allCommands =
, cmdModuleGraph
, cmdCompletionTypes
, cmdCompletionVarIds
+ , cmdCompletionClassTypeNames
, cmdOccurrences
]
@@ -743,6 +744,10 @@ cmdDumpNameDB :: Cmd
cmdDumpNameDB =
Cmd "dump-name-db" $ noArgs $ buildNameDB >>= dumpNameDB >> return ()
+-- | Type name completions: generate the list of type names currently visible within the
+-- current module. The IDE is responsible for prefix or name filtering.
+
+-- FIXME: Use focused_mod here, when available, just like what bgTypeCheck does.
cmdCompletionTypes :: Cmd
cmdCompletionTypes = Cmd "completion-types" $ fileNameArg $ cmd
where
@@ -757,8 +762,38 @@ cmdCompletionTypes = Cmd "completion-types" $ fileNameArg $ cmd
cmd fname = filePathToProjectModule fname
>>= allTyCons
+-- | Variable id completions: generate the list of variable identifiers currently visible within
+-- the current module. The IDE is responsible for prefix or name filtering.
+
+-- FIXME: Use focused_mod here, when available, mimicing bgTypeCheck.
cmdCompletionVarIds :: Cmd
-cmdCompletionVarIds = Cmd "completion-varIds" $ fileNameArg $ cmd
+cmdCompletionVarIds = Cmd "completion-varIds" $ fileNameArg $ generateCompletions getVarIdCompletions
+
+-- | Class type name completions: generate the list of class names currently visible within the
+-- current module. The IDE is repsonsible for prefix or name filering.
+
+-- FIXME: Use focused_mod here, when available, mimicing bgTypeCheck.
+cmdCompletionClassTypeNames :: Cmd
+cmdCompletionClassTypeNames = Cmd "completion-classTypeNames" $ fileNameArg $ generateCompletions getClassTypeNameCompletions
+
+-- | Generate the completion tuple list using a completion function and file name
+generateCompletions :: forall a.
+ (Maybe ModSummary -> ScionM a) -- ^ The completion function (see Completions.hs)
+ -> FilePath -- ^ File name, if there is no currently focused module.
+ -> ScionM a
+generateCompletions completionFunc fpath = withSessionState $ getCompletions fpath
where
- cmd fname = filePathToProjectModule fname
- >>= getVarIdCompletions
+ getCompletions fname scion =
+ case focusedModule scion of
+ modsum@(Just ms) -> validFocusedModuleSource fname (ml_hs_file (ms_location ms)) modsum
+ Nothing -> getCompletionsFromProjectModule fname
+
+ validFocusedModuleSource fname (Just f) modsum
+ | f == fname
+ = completionFunc modsum
+ | otherwise
+ = getCompletionsFromProjectModule fname
+ validFocusedModuleSource fname Nothing _ = getCompletionsFromProjectModule fname
+
+ -- Default: If we can't use focusedModule, find the module summary in the module graph
+ getCompletionsFromProjectModule fname = filePathToProjectModule fname >>= completionFunc

0 comments on commit f27a79e

Please sign in to comment.
Something went wrong with that request. Please try again.