Skip to content

Commit b1e4b01

Browse files
committed
Implement the focus command for limiting externs
1 parent e06b9cc commit b1e4b01

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

src/Language/PureScript/Ide.hs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Protolude hiding (moduleName)
2222

2323
import "monad-logger" Control.Monad.Logger (MonadLogger, logWarnN)
2424
import Data.Map qualified as Map
25+
import Data.Set qualified as Set
2526
import Data.Text qualified as T
2627
import Language.PureScript qualified as P
2728
import Language.PureScript.Glob (toInputGlobs, PSCGlobs(..))
@@ -37,7 +38,7 @@ import Language.PureScript.Ide.Matcher (Matcher)
3738
import Language.PureScript.Ide.Prim (idePrimDeclarations)
3839
import Language.PureScript.Ide.Rebuild (rebuildFileAsync, rebuildFileSync)
3940
import Language.PureScript.Ide.SourceFile (parseModulesFromFiles)
40-
import Language.PureScript.Ide.State (getAllModules, getLoadedModulenames, insertExterns, insertModule, populateVolatileState, populateVolatileStateSync, resetIdeState)
41+
import Language.PureScript.Ide.State (getAllModules, getLoadedModulenames, insertExterns, insertModule, populateVolatileState, populateVolatileStateSync, resetIdeState, setFocusedModules, getFocusedModules)
4142
import Language.PureScript.Ide.Types (Annotation(..), Ide, IdeConfiguration(..), IdeDeclarationAnn(..), IdeEnvironment(..), Success(..))
4243
import Language.PureScript.Ide.Util (discardAnn, identifierFromIdeDeclaration, namespaceForDeclaration, withEmptyAnn)
4344
import Language.PureScript.Ide.Usage (findUsages)
@@ -102,6 +103,8 @@ handleCommand c = case c of
102103
rebuildFileAsync file actualFile targets
103104
RebuildSync file actualFile targets ->
104105
rebuildFileSync file actualFile targets
106+
Focus modulesToFocus ->
107+
setFocusedModules modulesToFocus $> TextResult "Focused modules have been set."
105108
Cwd ->
106109
TextResult . T.pack <$> liftIO getCurrentDirectory
107110
Reset ->
@@ -215,10 +218,18 @@ loadModules
215218
=> [P.ModuleName]
216219
-> m Success
217220
loadModules moduleNames = do
221+
focusedModules <- getFocusedModules
218222
-- We resolve all the modulenames to externs files and load these into memory.
219223
oDir <- outputDirectory
220-
let efPaths =
221-
map (\mn -> oDir </> toS (P.runModuleName mn) </> P.externsFileName) moduleNames
224+
let
225+
-- But we only load the externs files that are in the focusedModules.
226+
efModules =
227+
if Set.null focusedModules then
228+
moduleNames
229+
else
230+
Set.toList $ Set.fromList moduleNames `Set.intersection` focusedModules
231+
efPaths =
232+
map (\mn -> oDir </> toS (P.runModuleName mn) </> P.externsFileName) efModules
222233
efiles <- traverse readExternFile efPaths
223234
traverse_ insertExterns efiles
224235

src/Language/PureScript/Ide/Command.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ data Command
6262
| List { listType :: ListType }
6363
| Rebuild FilePath (Maybe FilePath) (Set P.CodegenTarget)
6464
| RebuildSync FilePath (Maybe FilePath) (Set P.CodegenTarget)
65+
| Focus [P.ModuleName]
6566
| Cwd
6667
| Reset
6768
| Quit
@@ -79,6 +80,7 @@ commandName c = case c of
7980
List{} -> "List"
8081
Rebuild{} -> "Rebuild"
8182
RebuildSync{} -> "RebuildSync"
83+
Focus{} -> "Focus"
8284
Cwd{} -> "Cwd"
8385
Reset{} -> "Reset"
8486
Quit{} -> "Quit"
@@ -176,6 +178,13 @@ instance FromJSON Command where
176178
<$> params .: "file"
177179
<*> params .:? "actualFile"
178180
<*> (parseCodegenTargets =<< params .:? "codegen" .!= [ "js" ])
181+
"focus" -> do
182+
params' <- o .:? "params"
183+
case params' of
184+
Nothing ->
185+
pure (Focus [])
186+
Just params ->
187+
Focus <$> (map P.moduleNameFromString <$> params .:? "modules" .!= [])
179188
c -> fail ("Unknown command: " <> show c)
180189
where
181190
parseCodegenTargets ts =

src/Language/PureScript/Ide/State.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ module Language.PureScript.Ide.State
3131
, populateVolatileStateSTM
3232
, getOutputDirectory
3333
, updateCacheTimestamp
34+
, getFocusedModules
35+
, setFocusedModules
36+
, setFocusedModulesSTM
3437
-- for tests
3538
, resolveOperatorsForModule
3639
, resolveInstances
@@ -44,6 +47,7 @@ import Control.Lens (Ixed(..), preview, view, (%~), (.~), (^.))
4447
import "monad-logger" Control.Monad.Logger (MonadLogger, logWarnN)
4548
import Data.IORef (readIORef, writeIORef)
4649
import Data.Map.Lazy qualified as Map
50+
import Data.Set qualified as Set
4751
import Data.Time.Clock (UTCTime)
4852
import Data.Zip (unzip)
4953
import Language.PureScript qualified as P
@@ -141,6 +145,23 @@ setVolatileStateSTM ref vs = do
141145
x {ideVolatileState = vs}
142146
pure ()
143147

148+
-- | Retrieves the ModifierState from the State.
149+
getModifierState :: Ide m => m IdeModifierState
150+
getModifierState = do
151+
st <- ideStateVar <$> ask
152+
liftIO (atomically (getModifierStateSTM st))
153+
154+
-- | STM version of getModifierState
155+
getModifierStateSTM :: TVar IdeState -> STM IdeModifierState
156+
getModifierStateSTM ref = ideModifierState <$> readTVar ref
157+
158+
-- | Sets the ModifierState inside Ide's state
159+
setModifierStateSTM :: TVar IdeState -> IdeModifierState -> STM ()
160+
setModifierStateSTM ref md = do
161+
modifyTVar ref $ \x ->
162+
x {ideModifierState = md}
163+
pure ()
164+
144165
-- | Checks if the given ModuleName matches the last rebuild cache and if it
145166
-- does returns all loaded definitions + the definitions inside the rebuild
146167
-- cache
@@ -450,3 +471,18 @@ resolveDataConstructorsForModule decls =
450471
& mapMaybe (preview (idaDeclaration . _IdeDeclDataConstructor))
451472
& foldr (\(IdeDataConstructor name typeName type') ->
452473
Map.insertWith (<>) typeName [(name, type')]) Map.empty
474+
475+
getFocusedModules :: Ide m => m (Set P.ModuleName)
476+
getFocusedModules = do
477+
IdeModifierState{mdFocusedModules = focusedModules} <- getModifierState
478+
pure focusedModules
479+
480+
setFocusedModules :: Ide m => [P.ModuleName] -> m ()
481+
setFocusedModules modulesToFocus = do
482+
st <- ideStateVar <$> ask
483+
liftIO (atomically (setFocusedModulesSTM st modulesToFocus))
484+
485+
setFocusedModulesSTM :: TVar IdeState -> [P.ModuleName] -> STM ()
486+
setFocusedModulesSTM ref modulesToFocus = do
487+
IdeModifierState{} <- getModifierStateSTM ref
488+
setModifierStateSTM ref (IdeModifierState (Set.fromList modulesToFocus))

src/Language/PureScript/Ide/Types.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,20 @@ type Ide m = (MonadIO m, MonadReader IdeEnvironment m)
178178
data IdeState = IdeState
179179
{ ideFileState :: IdeFileState
180180
, ideVolatileState :: IdeVolatileState
181+
, ideModifierState :: IdeModifierState
181182
} deriving (Show)
182183

183184
emptyIdeState :: IdeState
184-
emptyIdeState = IdeState emptyFileState emptyVolatileState
185+
emptyIdeState = IdeState emptyFileState emptyVolatileState emptyModifierState
185186

186187
emptyFileState :: IdeFileState
187188
emptyFileState = IdeFileState M.empty M.empty
188189

189190
emptyVolatileState :: IdeVolatileState
190191
emptyVolatileState = IdeVolatileState (AstData M.empty) M.empty Nothing
191192

193+
emptyModifierState :: IdeModifierState
194+
emptyModifierState = IdeModifierState mempty
192195

193196
-- | @IdeFileState@ holds data that corresponds 1-to-1 to an entity on the
194197
-- filesystem. Externs correspond to the ExternsFiles the compiler emits into
@@ -213,6 +216,10 @@ data IdeVolatileState = IdeVolatileState
213216
, vsCachedRebuild :: Maybe (P.ModuleName, P.ExternsFile)
214217
} deriving (Show)
215218

219+
data IdeModifierState = IdeModifierState
220+
{ mdFocusedModules :: Set P.ModuleName
221+
} deriving (Show)
222+
216223
newtype Match a = Match (P.ModuleName, a)
217224
deriving (Show, Eq, Functor)
218225

0 commit comments

Comments
 (0)