Hannes Siebenhandl pushed to branch wip/fendor/ghci-unitid-syntax at Glasgow Haskell Compiler / GHC

Commits:

1 changed file:

Changes:

  • ghc/GHCi/UI.hs
    ... ... @@ -90,7 +90,7 @@ import qualified GHC.Parser.Header as Header
    90 90
     import GHC.Types.UnresolvedImport ( ImportDeclOrigin(..) )
    
    91 91
     import GHC.Types.PkgQual
    
    92 92
     
    
    93
    -import GHC.Unit
    
    93
    +import GHC.Unit hiding (mkModuleName)
    
    94 94
     import GHC.Unit.Finder as Finder
    
    95 95
     import GHC.Unit.Module.Graph (filterToposortToModules)
    
    96 96
     import GHC.Unit.Module.ModSummary
    
    ... ... @@ -132,6 +132,7 @@ import Data.Array
    132 132
     import qualified Data.ByteString.Char8 as BS
    
    133 133
     import Data.Char
    
    134 134
     import Data.Containers.ListUtils (nubOrd)
    
    135
    +import Data.Either (partitionEithers)
    
    135 136
     import Data.Function
    
    136 137
     import qualified Data.Foldable as Foldable
    
    137 138
     import Data.IORef ( IORef, modifyIORef, newIORef, readIORef, writeIORef )
    
    ... ... @@ -2437,7 +2438,7 @@ reloadModule m = do
    2437 2438
               pure $ LoadUpTo []
    
    2438 2439
           | otherwise = do
    
    2439 2440
               mod_graph <- GHC.getModuleGraph
    
    2440
    -          mods <- traverse (lookupHomeUnitModuleName mod_graph) [mkModuleName modl | modl <- modls]
    
    2441
    +          mods <- traverse (lookupHomeUnitModuleName mod_graph) [GHC.mkModuleName modl | modl <- modls]
    
    2441 2442
               pure $ LoadUpTo mods
    
    2442 2443
     
    
    2443 2444
     reloadModuleDefer :: GhciMonad m => String -> m ()
    
    ... ... @@ -3071,26 +3072,26 @@ browseModule bang modl exports_only = do
    3071 3072
     -- "remembered_ctx" and "transient_ctx" in GhciMonad.
    
    3072 3073
     
    
    3073 3074
     moduleCmd :: GhciMonad m => String -> m ()
    
    3074
    -moduleCmd str
    
    3075
    -  | all sensible strs = cmd
    
    3076
    -  | otherwise = reportError (GhciCommandSyntaxError "module [+/-] [*]M1 ... [*]Mn")
    
    3075
    +moduleCmd str =
    
    3076
    +  case mode of
    
    3077
    +    Just m ->
    
    3078
    +      case m of
    
    3079
    +        AddModules starMods unstarredMods ->    addModulesToContext   starMods unstarredMods
    
    3080
    +        RemoveModules starMods unstarredMods -> remModulesFromContext starMods unstarredMods
    
    3081
    +        SetModules starMods unstarredMods ->    setContext            starMods unstarredMods
    
    3082
    +    Nothing -> reportError (GhciCommandSyntaxError "module [+/-] [*]M1 ... [*]Mn")
    
    3077 3083
       where
    
    3078
    -    (cmd, strs) =
    
    3079
    -        case str of
    
    3080
    -          '+':stuff -> rest addModulesToContext   stuff
    
    3081
    -          '-':stuff -> rest remModulesFromContext stuff
    
    3082
    -          stuff     -> rest setContext            stuff
    
    3084
    +    mode =
    
    3085
    +      case str of
    
    3086
    +        '+':stuff -> uncurry AddModules <$> validateModules stuff
    
    3087
    +        '-':stuff -> uncurry RemoveModules <$> validateModules stuff
    
    3088
    +        stuff -> uncurry SetModules <$> validateModules stuff
    
    3083 3089
     
    
    3084
    -    rest op stuff = (op as bs, stuffs)
    
    3085
    -       where (as,bs) = partitionWith starred stuffs
    
    3086
    -             stuffs  = words stuff
    
    3087
    -
    
    3088
    -    sensible ('*':m) = looksLikeModuleName m
    
    3089
    -    sensible m       = looksLikeModuleName m
    
    3090
    -
    
    3091
    -    starred ('*':m) = Left  (GHC.mkModuleName m)
    
    3092
    -    starred m       = Right (GHC.mkModuleName m)
    
    3090
    +    validateModules strs =
    
    3091
    +      fmap partitionEithers $ sequenceA starred str
    
    3093 3092
     
    
    3093
    +    starred ('*':m) = fmap Left  (parseModuleNameWithOptionalUnitId m)
    
    3094
    +    starred m       = fmap Right (parseModuleNameWithOptionalUnitId m)
    
    3094 3095
     
    
    3095 3096
     -- -----------------------------------------------------------------------------
    
    3096 3097
     -- Four ways to manipulate the context:
    
    ... ... @@ -3099,25 +3100,25 @@ moduleCmd str
    3099 3100
     --   (c) :module <stuff>:      setContext
    
    3100 3101
     --   (d) import <module>...:   addImportToContext
    
    3101 3102
     
    
    3102
    -addModulesToContext :: GhciMonad m => [ModuleName] -> [ModuleName] -> m ()
    
    3103
    +addModulesToContext :: GhciMonad m => [InteractiveModuleName] -> [InteractiveModuleName] -> m ()
    
    3103 3104
     addModulesToContext starred unstarred = restoreContextOnFailure $ do
    
    3104 3105
        addModulesToContext_ starred unstarred
    
    3105 3106
     
    
    3106
    -addModulesToContext_ :: GhciMonad m => [ModuleName] -> [ModuleName] -> m ()
    
    3107
    +addModulesToContext_ :: GhciMonad m => [InteractiveModuleName] -> [InteractiveModuleName] -> m ()
    
    3107 3108
     addModulesToContext_ starred unstarred = do
    
    3108 3109
       starredModules <- traverse lookupModuleName starred
    
    3109 3110
       mapM_ addII (map mkIIModule starredModules ++ map mkIIDecl unstarred)
    
    3110 3111
       setGHCContextFromGHCiState
    
    3111 3112
     
    
    3112
    -remModulesFromContext :: GhciMonad m => [ModuleName] -> [ModuleName] -> m ()
    
    3113
    -remModulesFromContext  starred unstarred = do
    
    3113
    +remModulesFromContext :: GhciMonad m => [InteractiveModuleName] -> [InteractiveModuleName] -> m ()
    
    3114
    +remModulesFromContext starred unstarred = do
    
    3114 3115
        -- we do *not* call restoreContextOnFailure here.  If the user
    
    3115 3116
        -- is trying to fix up a context that contains errors by removing
    
    3116 3117
        -- modules, we don't want GHC to silently put them back in again.
    
    3117 3118
        mapM_ rm (starred ++ unstarred)
    
    3118 3119
        setGHCContextFromGHCiState
    
    3119 3120
      where
    
    3120
    -   rm :: GhciMonad m => ModuleName -> m ()
    
    3121
    +   rm :: GhciMonad m => InteractiveModuleName -> m ()
    
    3121 3122
        rm str = do
    
    3122 3123
          m <- moduleName <$> lookupModuleName str
    
    3123 3124
          let filt = filter ((/=) m . iiModuleName)
    
    ... ... @@ -3125,7 +3126,7 @@ remModulesFromContext starred unstarred = do
    3125 3126
             st { remembered_ctx = filt (remembered_ctx st)
    
    3126 3127
                , transient_ctx  = filt (transient_ctx st) }
    
    3127 3128
     
    
    3128
    -setContext :: GhciMonad m => [ModuleName] -> [ModuleName] -> m ()
    
    3129
    +setContext :: GhciMonad m => [InteractiveModuleName] -> [InteractiveModuleName] -> m ()
    
    3129 3130
     setContext starred unstarred = restoreContextOnFailure $ do
    
    3130 3131
       modifyGHCiState $ \st -> st { remembered_ctx = [], transient_ctx = [] }
    
    3131 3132
                                     -- delete the transient context
    
    ... ... @@ -3165,6 +3166,24 @@ restoreContextOnFailure do_this = do
    3165 3166
       do_this `MC.onException` (modifyGHCiState $ \st' ->
    
    3166 3167
          st' { remembered_ctx = rc, transient_ctx = tc })
    
    3167 3168
     
    
    3169
    +
    
    3170
    +type InteractiveModuleName = GenModule (Maybe UnitId)
    
    3171
    +
    
    3172
    +mkModuleName :: String -> InteractiveModuleName
    
    3173
    +mkModuleName mn = Module Nothing (GHC.mkModuleName mn)
    
    3174
    +
    
    3175
    +mkModuleNameWithUnitId :: String -> UnitId -> InteractiveModuleName
    
    3176
    +mkModuleNameWithUnitId mn uid = Module (Just uid) (GHC.mkModuleName mn)
    
    3177
    +
    
    3178
    +parseModuleNameWithOptionalUnitId :: String -> Maybe InteractiveModuleName
    
    3179
    +parseModuleNameWithOptionalUnitId m =
    
    3180
    +  case split ':' m of
    
    3181
    +    [mn]           -> mkModuleName mn <$ guard (looksLikeModuleName mn)
    
    3182
    +    [part1, part2] ->
    
    3183
    +          mkModuleNameWithUnitId part1 (stringToUnitId part2) <$ guard (looksLikeModuleName part1)
    
    3184
    +      <|> mkModuleNameWithUnitId part2 (stringToUnitId part1) <$ guard (looksLikeModuleName part2)
    
    3185
    +    _ -> Nothing
    
    3186
    +
    
    3168 3187
     -- -----------------------------------------------------------------------------
    
    3169 3188
     -- Validate a module that we want to add to the context
    
    3170 3189
     
    
    ... ... @@ -4824,6 +4843,14 @@ listAround pan do_highlight = do
    4824 4843
                indent = BS.pack ("  " ++ replicate (length (show no)) ' ')
    
    4825 4844
                nl = BS.singleton '\n'
    
    4826 4845
     
    
    4846
    +-- --------------------------------------------------------------------------
    
    4847
    +-- Module Context manipulation types
    
    4848
    +
    
    4849
    +data ContextAction
    
    4850
    +  = AddModules [InteractiveModuleName] [InteractiveModuleName]
    
    4851
    +  | RemoveModules [InteractiveModuleName] [InteractiveModuleName]
    
    4852
    +  | SetModules [InteractiveModuleName] [InteractiveModuleName]
    
    4853
    +
    
    4827 4854
     
    
    4828 4855
     -- --------------------------------------------------------------------------
    
    4829 4856
     -- Tick arrays
    
    ... ... @@ -4993,10 +5020,10 @@ tryBool m = do
    4993 5020
     lookupModule :: GHC.GhcMonad m => String -> m Module
    
    4994 5021
     lookupModule mName = lookupModuleName (GHC.mkModuleName mName)
    
    4995 5022
     
    
    4996
    -lookupModuleName :: GHC.GhcMonad m => ModuleName -> m Module
    
    5023
    +lookupModuleName :: GHC.GhcMonad m => InteractiveModuleName -> m Module
    
    4997 5024
     lookupModuleName mName = lookupQualifiedModuleName NoPkgQual mName
    
    4998 5025
     
    
    4999
    -lookupQualifiedModuleName :: GHC.GhcMonad m => PkgQual -> ModuleName -> m Module
    
    5026
    +lookupQualifiedModuleName :: GHC.GhcMonad m => PkgQual -> InteractiveModuleName -> m Module
    
    5000 5027
     lookupQualifiedModuleName qual modl = do
    
    5001 5028
       GHC.lookupAllQualifiedModuleNames qual modl >>= \case
    
    5002 5029
         [] -> throwGhciCommandError (GhciModuleError $ GhciModuleNameNotFound modl)