Skip to content

Commit

Permalink
Use proper record, not anonymous triple
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed Apr 1, 2024
1 parent dd7ada6 commit 517cb35
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 8 additions & 6 deletions rzk/src/Language/Rzk/VSCode/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import Language.Rzk.Free.Syntax (VarIdent)
import Language.Rzk.VSCode.Config (ServerConfig)
import Rzk.TypeCheck (Decl', TypeErrorInScopedContext)

type RzkTypecheckCache = [(FilePath, [Decl'], [TypeErrorInScopedContext VarIdent])]
data RzkCachedModule = RzkCachedModule
{ cachedModuleDecls :: [Decl']
, cachedModuleErrors :: [TypeErrorInScopedContext VarIdent]
}

type RzkTypecheckCache = [(FilePath, RzkCachedModule)]

data RzkEnv = RzkEnv
newtype RzkEnv = RzkEnv
{ rzkEnvTypecheckCache :: TVar RzkTypecheckCache
}

Expand All @@ -19,7 +24,6 @@ defaultRzkEnv = do
return RzkEnv
{ rzkEnvTypecheckCache = typecheckCache }


type LSP = LspT ServerConfig (ReaderT RzkEnv IO)

-- | Override the cache with given typechecked modules.
Expand All @@ -38,9 +42,7 @@ resetCacheForFiles :: [FilePath] -> LSP ()
resetCacheForFiles paths = lift $ do
typecheckCache <- asks rzkEnvTypecheckCache
liftIO $ atomically $ do
modifyTVar typecheckCache (takeWhile ((`notElem` paths) . fst3))
where
fst3 (a,_,_) = a
modifyTVar typecheckCache (takeWhile ((`notElem` paths) . fst))

-- | Get the current state of the cache.
getCachedTypecheckedModules :: LSP RzkTypecheckCache
Expand Down
17 changes: 8 additions & 9 deletions rzk/src/Language/Rzk/VSCode/Handlers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# LANGUAGE RecordWildCards #-}

module Language.Rzk.VSCode.Handlers (
typecheckFromConfigFile,
Expand Down Expand Up @@ -103,7 +104,7 @@ typecheckFromConfigFile = do
let paths = concatMap sort rawPaths

typecheckedCachedModules <- getCachedTypecheckedModules
let cachedModules = map (\(a,b,_) -> (a,b)) typecheckedCachedModules
let cachedModules = map (\(path, RzkCachedModule{..}) -> (path, cachedModuleDecls)) typecheckedCachedModules
let cachedPaths = map fst cachedModules
modifiedFiles = paths \\ cachedPaths

Expand All @@ -127,7 +128,7 @@ typecheckFromConfigFile = do
-- cache well-typed modules
logInfo (show (length checkedModules) ++ " modules successfully typechecked")
logInfo (show (length errors) ++ " errors found")
let checkedModules' = map (\(path, decls) -> (path, decls, filter ((== path) . filepathOfTypeError) errors)) checkedModules
let checkedModules' = map (\(path, decls) -> (path, RzkCachedModule decls (filter ((== path) . filepathOfTypeError) errors))) checkedModules
cacheTypecheckedModules checkedModules'
return (errors, checkedModules)

Expand Down Expand Up @@ -204,10 +205,9 @@ provideCompletions req res = do
logDebug ("Found " ++ show (length cachedModules) ++ " modules in the cache")
let currentFile = fromMaybe "" $ uriToFilePath $ req ^. params . textDocument . uri
-- Take all the modules up to and including the currently open one
let modules = map ignoreErrors $ takeWhileInc ((/= currentFile) . fst3) cachedModules
let modules = map ignoreErrors $ takeWhileInc ((/= currentFile) . fst) cachedModules
where
fst3 (a,_,_) = a
ignoreErrors (a,b,_) = (a,b)
ignoreErrors (path, RzkCachedModule{..}) = (path, cachedModuleDecls)
takeWhileInc _ [] = []
takeWhileInc p (x:xs)
| p x = x : takeWhileInc p xs
Expand Down Expand Up @@ -291,10 +291,9 @@ data IsChanged
-- | Detects if the given path has changes in its declaration compared to what's in the cache
isChanged :: RzkTypecheckCache -> FilePath -> LSP IsChanged
isChanged cache path = toIsChanged $ do
let cacheWithoutDecls = map (\(p, _, e) -> (p, e)) cache
let cacheWithoutErrors = map (\(p, d, _) -> (p, d)) cache
errors <- maybeToEitherLSP $ lookup path cacheWithoutDecls
cachedDecls <- maybeToEitherLSP $ lookup path cacheWithoutErrors
let cacheWithoutErrors = map (fmap cachedModuleDecls) cache
errors <- maybeToEitherLSP $ cachedModuleErrors <$> lookup path cache
cachedDecls <- maybeToEitherLSP $ cachedModuleDecls <$> lookup path cache
module' <- toExceptTLifted $ parseModuleFile path
e <- toExceptTLifted $ try @SomeException $ evaluate $
defaultTypeCheck (typecheckModulesWithLocationIncremental (takeWhile ((/= path) . fst) cacheWithoutErrors) [(path, module')])
Expand Down

0 comments on commit 517cb35

Please sign in to comment.