module Futhark.LSP.State
( State (..),
emptyState,
getStaleContent,
getStaleMapping,
updateStaleContent,
updateStaleMapping,
)
where
import Data.Map qualified as M
import Futhark.Compiler.Program (LoadedProg)
import Futhark.LSP.PositionMapping (PositionMapping, StaleFile (..))
import Language.LSP.VFS (VirtualFile)
data State = State
{
State -> Maybe LoadedProg
stateProgram :: Maybe LoadedProg,
State -> Map FilePath StaleFile
staleData :: M.Map FilePath StaleFile
}
emptyState :: State
emptyState :: State
emptyState = Maybe LoadedProg -> Map FilePath StaleFile -> State
State forall a. Maybe a
Nothing forall k a. Map k a
M.empty
getStaleContent :: State -> FilePath -> Maybe VirtualFile
getStaleContent :: State -> FilePath -> Maybe VirtualFile
getStaleContent State
state FilePath
file_path = (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. StaleFile -> VirtualFile
staleContent) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup FilePath
file_path (State -> Map FilePath StaleFile
staleData State
state)
getStaleMapping :: State -> FilePath -> Maybe PositionMapping
getStaleMapping :: State -> FilePath -> Maybe PositionMapping
getStaleMapping State
state FilePath
file_path = StaleFile -> Maybe PositionMapping
staleMapping forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup FilePath
file_path (State -> Map FilePath StaleFile
staleData State
state)
updateStaleContent :: FilePath -> VirtualFile -> LoadedProg -> State -> State
updateStaleContent :: FilePath -> VirtualFile -> LoadedProg -> State -> State
updateStaleContent FilePath
file_path VirtualFile
file_content LoadedProg
loadedProg State
state =
Maybe LoadedProg -> Map FilePath StaleFile -> State
State (forall a. a -> Maybe a
Just LoadedProg
loadedProg) (forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert FilePath
file_path (VirtualFile -> Maybe PositionMapping -> StaleFile
StaleFile VirtualFile
file_content forall a. Maybe a
Nothing) (State -> Map FilePath StaleFile
staleData State
state))
updateStaleMapping :: Maybe FilePath -> Maybe PositionMapping -> State -> State
updateStaleMapping :: Maybe FilePath -> Maybe PositionMapping -> State -> State
updateStaleMapping (Just FilePath
file_path) Maybe PositionMapping
mapping State
state = do
case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup FilePath
file_path (State -> Map FilePath StaleFile
staleData State
state) of
Maybe StaleFile
Nothing -> State
state
Just (StaleFile VirtualFile
file_content Maybe PositionMapping
_mapping) ->
Maybe LoadedProg -> Map FilePath StaleFile -> State
State (State -> Maybe LoadedProg
stateProgram State
state) (forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert FilePath
file_path (VirtualFile -> Maybe PositionMapping -> StaleFile
StaleFile VirtualFile
file_content Maybe PositionMapping
mapping) (State -> Map FilePath StaleFile
staleData State
state))
updateStaleMapping Maybe FilePath
_ Maybe PositionMapping
_ State
state = State
state