{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GADTs       #-}

module Development.IDE.Core.UseStale
  ( Age(..)
  , Tracked
  , unTrack
  , PositionMap
  , TrackedStale (..)
  , untrackedStaleValue
  , unsafeMkStale
  , unsafeMkCurrent
  , unsafeCopyAge
  , MapAge (..)
  , dualPositionMap
  , useWithStale
  , useWithStale_
  ) where

import           Control.Arrow
import           Control.Category                     (Category)
import qualified Control.Category                     as C
import           Control.DeepSeq                      (NFData)
import           Data.Aeson
import           Data.Coerce                          (coerce)
import           Data.Functor                         ((<&>))
import           Data.Functor.Identity                (Identity (Identity))
import           Data.Kind                            (Type)
import           Data.String                          (fromString)
import           Development.IDE                      (Action, IdeRule,
                                                       NormalizedFilePath,
                                                       Range,
                                                       rangeToRealSrcSpan,
                                                       realSrcSpanToRange)
import qualified Development.IDE.Core.PositionMapping as P
import qualified Development.IDE.Core.Shake           as IDE
import           Development.IDE.GHC.Compat           (RealSrcSpan, srcSpanFile)
import           Development.IDE.GHC.Compat.Util      (unpackFS)


------------------------------------------------------------------------------
-- | A data kind for 'Tracked'.
data Age = Current | Stale Type


------------------------------------------------------------------------------
-- | Some value, tagged with its age. All 'Current' ages are considered to be
-- the same thing, but 'Stale' values are protected by an untouchable variable
-- to ensure they can't be unified.
newtype Tracked (age :: Age) a  = UnsafeTracked
  { forall (age :: Age) a. Tracked age a -> a
unTrack :: a
  }
  deriving stock ((forall a b. (a -> b) -> Tracked age a -> Tracked age b)
-> (forall a b. a -> Tracked age b -> Tracked age a)
-> Functor (Tracked age)
forall a b. a -> Tracked age b -> Tracked age a
forall a b. (a -> b) -> Tracked age a -> Tracked age b
forall (age :: Age) a b. a -> Tracked age b -> Tracked age a
forall (age :: Age) a b. (a -> b) -> Tracked age a -> Tracked age b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (age :: Age) a b. (a -> b) -> Tracked age a -> Tracked age b
fmap :: forall a b. (a -> b) -> Tracked age a -> Tracked age b
$c<$ :: forall (age :: Age) a b. a -> Tracked age b -> Tracked age a
<$ :: forall a b. a -> Tracked age b -> Tracked age a
Functor, (forall m. Monoid m => Tracked age m -> m)
-> (forall m a. Monoid m => (a -> m) -> Tracked age a -> m)
-> (forall m a. Monoid m => (a -> m) -> Tracked age a -> m)
-> (forall a b. (a -> b -> b) -> b -> Tracked age a -> b)
-> (forall a b. (a -> b -> b) -> b -> Tracked age a -> b)
-> (forall b a. (b -> a -> b) -> b -> Tracked age a -> b)
-> (forall b a. (b -> a -> b) -> b -> Tracked age a -> b)
-> (forall a. (a -> a -> a) -> Tracked age a -> a)
-> (forall a. (a -> a -> a) -> Tracked age a -> a)
-> (forall a. Tracked age a -> [a])
-> (forall a. Tracked age a -> Bool)
-> (forall a. Tracked age a -> Int)
-> (forall a. Eq a => a -> Tracked age a -> Bool)
-> (forall a. Ord a => Tracked age a -> a)
-> (forall a. Ord a => Tracked age a -> a)
-> (forall a. Num a => Tracked age a -> a)
-> (forall a. Num a => Tracked age a -> a)
-> Foldable (Tracked age)
forall a. Eq a => a -> Tracked age a -> Bool
forall a. Num a => Tracked age a -> a
forall a. Ord a => Tracked age a -> a
forall m. Monoid m => Tracked age m -> m
forall a. Tracked age a -> Bool
forall a. Tracked age a -> Int
forall a. Tracked age a -> [a]
forall a. (a -> a -> a) -> Tracked age a -> a
forall m a. Monoid m => (a -> m) -> Tracked age a -> m
forall b a. (b -> a -> b) -> b -> Tracked age a -> b
forall a b. (a -> b -> b) -> b -> Tracked age a -> b
forall (age :: Age) a. Eq a => a -> Tracked age a -> Bool
forall (age :: Age) a. Num a => Tracked age a -> a
forall (age :: Age) a. Ord a => Tracked age a -> a
forall (age :: Age) m. Monoid m => Tracked age m -> m
forall (age :: Age) a. Tracked age a -> Bool
forall (age :: Age) a. Tracked age a -> Int
forall (age :: Age) a. Tracked age a -> [a]
forall (age :: Age) a. (a -> a -> a) -> Tracked age a -> a
forall (age :: Age) m a. Monoid m => (a -> m) -> Tracked age a -> m
forall (age :: Age) b a. (b -> a -> b) -> b -> Tracked age a -> b
forall (age :: Age) a b. (a -> b -> b) -> b -> Tracked age a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall (age :: Age) m. Monoid m => Tracked age m -> m
fold :: forall m. Monoid m => Tracked age m -> m
$cfoldMap :: forall (age :: Age) m a. Monoid m => (a -> m) -> Tracked age a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Tracked age a -> m
$cfoldMap' :: forall (age :: Age) m a. Monoid m => (a -> m) -> Tracked age a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Tracked age a -> m
$cfoldr :: forall (age :: Age) a b. (a -> b -> b) -> b -> Tracked age a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Tracked age a -> b
$cfoldr' :: forall (age :: Age) a b. (a -> b -> b) -> b -> Tracked age a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Tracked age a -> b
$cfoldl :: forall (age :: Age) b a. (b -> a -> b) -> b -> Tracked age a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Tracked age a -> b
$cfoldl' :: forall (age :: Age) b a. (b -> a -> b) -> b -> Tracked age a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Tracked age a -> b
$cfoldr1 :: forall (age :: Age) a. (a -> a -> a) -> Tracked age a -> a
foldr1 :: forall a. (a -> a -> a) -> Tracked age a -> a
$cfoldl1 :: forall (age :: Age) a. (a -> a -> a) -> Tracked age a -> a
foldl1 :: forall a. (a -> a -> a) -> Tracked age a -> a
$ctoList :: forall (age :: Age) a. Tracked age a -> [a]
toList :: forall a. Tracked age a -> [a]
$cnull :: forall (age :: Age) a. Tracked age a -> Bool
null :: forall a. Tracked age a -> Bool
$clength :: forall (age :: Age) a. Tracked age a -> Int
length :: forall a. Tracked age a -> Int
$celem :: forall (age :: Age) a. Eq a => a -> Tracked age a -> Bool
elem :: forall a. Eq a => a -> Tracked age a -> Bool
$cmaximum :: forall (age :: Age) a. Ord a => Tracked age a -> a
maximum :: forall a. Ord a => Tracked age a -> a
$cminimum :: forall (age :: Age) a. Ord a => Tracked age a -> a
minimum :: forall a. Ord a => Tracked age a -> a
$csum :: forall (age :: Age) a. Num a => Tracked age a -> a
sum :: forall a. Num a => Tracked age a -> a
$cproduct :: forall (age :: Age) a. Num a => Tracked age a -> a
product :: forall a. Num a => Tracked age a -> a
Foldable, Functor (Tracked age)
Foldable (Tracked age)
(Functor (Tracked age), Foldable (Tracked age)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Tracked age a -> f (Tracked age b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Tracked age (f a) -> f (Tracked age a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Tracked age a -> m (Tracked age b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Tracked age (m a) -> m (Tracked age a))
-> Traversable (Tracked age)
forall (age :: Age). Functor (Tracked age)
forall (age :: Age). Foldable (Tracked age)
forall (age :: Age) (m :: * -> *) a.
Monad m =>
Tracked age (m a) -> m (Tracked age a)
forall (age :: Age) (f :: * -> *) a.
Applicative f =>
Tracked age (f a) -> f (Tracked age a)
forall (age :: Age) (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Tracked age a -> m (Tracked age b)
forall (age :: Age) (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Tracked age a -> f (Tracked age b)
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
Tracked age (m a) -> m (Tracked age a)
forall (f :: * -> *) a.
Applicative f =>
Tracked age (f a) -> f (Tracked age a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Tracked age a -> m (Tracked age b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Tracked age a -> f (Tracked age b)
$ctraverse :: forall (age :: Age) (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Tracked age a -> f (Tracked age b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Tracked age a -> f (Tracked age b)
$csequenceA :: forall (age :: Age) (f :: * -> *) a.
Applicative f =>
Tracked age (f a) -> f (Tracked age a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Tracked age (f a) -> f (Tracked age a)
$cmapM :: forall (age :: Age) (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Tracked age a -> m (Tracked age b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Tracked age a -> m (Tracked age b)
$csequence :: forall (age :: Age) (m :: * -> *) a.
Monad m =>
Tracked age (m a) -> m (Tracked age a)
sequence :: forall (m :: * -> *) a.
Monad m =>
Tracked age (m a) -> m (Tracked age a)
Traversable)
  deriving newtype (Tracked age a -> Tracked age a -> Bool
(Tracked age a -> Tracked age a -> Bool)
-> (Tracked age a -> Tracked age a -> Bool) -> Eq (Tracked age a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (age :: Age) a.
Eq a =>
Tracked age a -> Tracked age a -> Bool
$c== :: forall (age :: Age) a.
Eq a =>
Tracked age a -> Tracked age a -> Bool
== :: Tracked age a -> Tracked age a -> Bool
$c/= :: forall (age :: Age) a.
Eq a =>
Tracked age a -> Tracked age a -> Bool
/= :: Tracked age a -> Tracked age a -> Bool
Eq, Eq (Tracked age a)
Eq (Tracked age a) =>
(Tracked age a -> Tracked age a -> Ordering)
-> (Tracked age a -> Tracked age a -> Bool)
-> (Tracked age a -> Tracked age a -> Bool)
-> (Tracked age a -> Tracked age a -> Bool)
-> (Tracked age a -> Tracked age a -> Bool)
-> (Tracked age a -> Tracked age a -> Tracked age a)
-> (Tracked age a -> Tracked age a -> Tracked age a)
-> Ord (Tracked age a)
Tracked age a -> Tracked age a -> Bool
Tracked age a -> Tracked age a -> Ordering
Tracked age a -> Tracked age a -> Tracked age a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (age :: Age) a. Ord a => Eq (Tracked age a)
forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Bool
forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Ordering
forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Tracked age a
$ccompare :: forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Ordering
compare :: Tracked age a -> Tracked age a -> Ordering
$c< :: forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Bool
< :: Tracked age a -> Tracked age a -> Bool
$c<= :: forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Bool
<= :: Tracked age a -> Tracked age a -> Bool
$c> :: forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Bool
> :: Tracked age a -> Tracked age a -> Bool
$c>= :: forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Bool
>= :: Tracked age a -> Tracked age a -> Bool
$cmax :: forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Tracked age a
max :: Tracked age a -> Tracked age a -> Tracked age a
$cmin :: forall (age :: Age) a.
Ord a =>
Tracked age a -> Tracked age a -> Tracked age a
min :: Tracked age a -> Tracked age a -> Tracked age a
Ord, Int -> Tracked age a -> ShowS
[Tracked age a] -> ShowS
Tracked age a -> String
(Int -> Tracked age a -> ShowS)
-> (Tracked age a -> String)
-> ([Tracked age a] -> ShowS)
-> Show (Tracked age a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (age :: Age) a. Show a => Int -> Tracked age a -> ShowS
forall (age :: Age) a. Show a => [Tracked age a] -> ShowS
forall (age :: Age) a. Show a => Tracked age a -> String
$cshowsPrec :: forall (age :: Age) a. Show a => Int -> Tracked age a -> ShowS
showsPrec :: Int -> Tracked age a -> ShowS
$cshow :: forall (age :: Age) a. Show a => Tracked age a -> String
show :: Tracked age a -> String
$cshowList :: forall (age :: Age) a. Show a => [Tracked age a] -> ShowS
showList :: [Tracked age a] -> ShowS
Show, ReadPrec [Tracked age a]
ReadPrec (Tracked age a)
Int -> ReadS (Tracked age a)
ReadS [Tracked age a]
(Int -> ReadS (Tracked age a))
-> ReadS [Tracked age a]
-> ReadPrec (Tracked age a)
-> ReadPrec [Tracked age a]
-> Read (Tracked age a)
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall (age :: Age) a. Read a => ReadPrec [Tracked age a]
forall (age :: Age) a. Read a => ReadPrec (Tracked age a)
forall (age :: Age) a. Read a => Int -> ReadS (Tracked age a)
forall (age :: Age) a. Read a => ReadS [Tracked age a]
$creadsPrec :: forall (age :: Age) a. Read a => Int -> ReadS (Tracked age a)
readsPrec :: Int -> ReadS (Tracked age a)
$creadList :: forall (age :: Age) a. Read a => ReadS [Tracked age a]
readList :: ReadS [Tracked age a]
$creadPrec :: forall (age :: Age) a. Read a => ReadPrec (Tracked age a)
readPrec :: ReadPrec (Tracked age a)
$creadListPrec :: forall (age :: Age) a. Read a => ReadPrec [Tracked age a]
readListPrec :: ReadPrec [Tracked age a]
Read, [Tracked age a] -> Value
[Tracked age a] -> Encoding
Tracked age a -> Bool
Tracked age a -> Value
Tracked age a -> Encoding
(Tracked age a -> Value)
-> (Tracked age a -> Encoding)
-> ([Tracked age a] -> Value)
-> ([Tracked age a] -> Encoding)
-> (Tracked age a -> Bool)
-> ToJSON (Tracked age a)
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
forall (age :: Age) a. ToJSON a => [Tracked age a] -> Value
forall (age :: Age) a. ToJSON a => [Tracked age a] -> Encoding
forall (age :: Age) a. ToJSON a => Tracked age a -> Bool
forall (age :: Age) a. ToJSON a => Tracked age a -> Value
forall (age :: Age) a. ToJSON a => Tracked age a -> Encoding
$ctoJSON :: forall (age :: Age) a. ToJSON a => Tracked age a -> Value
toJSON :: Tracked age a -> Value
$ctoEncoding :: forall (age :: Age) a. ToJSON a => Tracked age a -> Encoding
toEncoding :: Tracked age a -> Encoding
$ctoJSONList :: forall (age :: Age) a. ToJSON a => [Tracked age a] -> Value
toJSONList :: [Tracked age a] -> Value
$ctoEncodingList :: forall (age :: Age) a. ToJSON a => [Tracked age a] -> Encoding
toEncodingList :: [Tracked age a] -> Encoding
$comitField :: forall (age :: Age) a. ToJSON a => Tracked age a -> Bool
omitField :: Tracked age a -> Bool
ToJSON, Maybe (Tracked age a)
Value -> Parser [Tracked age a]
Value -> Parser (Tracked age a)
(Value -> Parser (Tracked age a))
-> (Value -> Parser [Tracked age a])
-> Maybe (Tracked age a)
-> FromJSON (Tracked age a)
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
forall (age :: Age) a. FromJSON a => Maybe (Tracked age a)
forall (age :: Age) a.
FromJSON a =>
Value -> Parser [Tracked age a]
forall (age :: Age) a.
FromJSON a =>
Value -> Parser (Tracked age a)
$cparseJSON :: forall (age :: Age) a.
FromJSON a =>
Value -> Parser (Tracked age a)
parseJSON :: Value -> Parser (Tracked age a)
$cparseJSONList :: forall (age :: Age) a.
FromJSON a =>
Value -> Parser [Tracked age a]
parseJSONList :: Value -> Parser [Tracked age a]
$comittedField :: forall (age :: Age) a. FromJSON a => Maybe (Tracked age a)
omittedField :: Maybe (Tracked age a)
FromJSON, Tracked age a -> ()
(Tracked age a -> ()) -> NFData (Tracked age a)
forall a. (a -> ()) -> NFData a
forall (age :: Age) a. NFData a => Tracked age a -> ()
$crnf :: forall (age :: Age) a. NFData a => Tracked age a -> ()
rnf :: Tracked age a -> ()
NFData)
  deriving (Functor (Tracked age)
Functor (Tracked age) =>
(forall a. a -> Tracked age a)
-> (forall a b.
    Tracked age (a -> b) -> Tracked age a -> Tracked age b)
-> (forall a b c.
    (a -> b -> c) -> Tracked age a -> Tracked age b -> Tracked age c)
-> (forall a b. Tracked age a -> Tracked age b -> Tracked age b)
-> (forall a b. Tracked age a -> Tracked age b -> Tracked age a)
-> Applicative (Tracked age)
forall a. a -> Tracked age a
forall a b. Tracked age a -> Tracked age b -> Tracked age a
forall a b. Tracked age a -> Tracked age b -> Tracked age b
forall a b. Tracked age (a -> b) -> Tracked age a -> Tracked age b
forall a b c.
(a -> b -> c) -> Tracked age a -> Tracked age b -> Tracked age c
forall (age :: Age). Functor (Tracked age)
forall (age :: Age) a. a -> Tracked age a
forall (age :: Age) a b.
Tracked age a -> Tracked age b -> Tracked age a
forall (age :: Age) a b.
Tracked age a -> Tracked age b -> Tracked age b
forall (age :: Age) a b.
Tracked age (a -> b) -> Tracked age a -> Tracked age b
forall (age :: Age) a b c.
(a -> b -> c) -> Tracked age a -> Tracked age b -> Tracked age c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall (age :: Age) a. a -> Tracked age a
pure :: forall a. a -> Tracked age a
$c<*> :: forall (age :: Age) a b.
Tracked age (a -> b) -> Tracked age a -> Tracked age b
<*> :: forall a b. Tracked age (a -> b) -> Tracked age a -> Tracked age b
$cliftA2 :: forall (age :: Age) a b c.
(a -> b -> c) -> Tracked age a -> Tracked age b -> Tracked age c
liftA2 :: forall a b c.
(a -> b -> c) -> Tracked age a -> Tracked age b -> Tracked age c
$c*> :: forall (age :: Age) a b.
Tracked age a -> Tracked age b -> Tracked age b
*> :: forall a b. Tracked age a -> Tracked age b -> Tracked age b
$c<* :: forall (age :: Age) a b.
Tracked age a -> Tracked age b -> Tracked age a
<* :: forall a b. Tracked age a -> Tracked age b -> Tracked age a
Applicative, Applicative (Tracked age)
Applicative (Tracked age) =>
(forall a b.
 Tracked age a -> (a -> Tracked age b) -> Tracked age b)
-> (forall a b. Tracked age a -> Tracked age b -> Tracked age b)
-> (forall a. a -> Tracked age a)
-> Monad (Tracked age)
forall a. a -> Tracked age a
forall a b. Tracked age a -> Tracked age b -> Tracked age b
forall a b. Tracked age a -> (a -> Tracked age b) -> Tracked age b
forall (age :: Age). Applicative (Tracked age)
forall (age :: Age) a. a -> Tracked age a
forall (age :: Age) a b.
Tracked age a -> Tracked age b -> Tracked age b
forall (age :: Age) a b.
Tracked age a -> (a -> Tracked age b) -> Tracked age b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall (age :: Age) a b.
Tracked age a -> (a -> Tracked age b) -> Tracked age b
>>= :: forall a b. Tracked age a -> (a -> Tracked age b) -> Tracked age b
$c>> :: forall (age :: Age) a b.
Tracked age a -> Tracked age b -> Tracked age b
>> :: forall a b. Tracked age a -> Tracked age b -> Tracked age b
$creturn :: forall (age :: Age) a. a -> Tracked age a
return :: forall a. a -> Tracked age a
Monad) via Identity


------------------------------------------------------------------------------
-- | Like 'P.PositionMapping', but with annotated ages for how 'Tracked' values
-- change. Use the 'Category' instance to compose 'PositionMapping's in order
-- to transform between values of different stale ages.
newtype PositionMap (from :: Age) (to :: Age) = PositionMap
  { forall (from :: Age) (to :: Age).
PositionMap from to -> PositionMapping
_getPositionMapping :: P.PositionMapping
  }

instance Category PositionMap where
  id :: forall (a :: Age). PositionMap a a
id  = PositionMapping -> PositionMap a a
forall a b. Coercible a b => a -> b
coerce PositionMapping
P.zeroMapping
  . :: forall (b :: Age) (c :: Age) (a :: Age).
PositionMap b c -> PositionMap a b -> PositionMap a c
(.) = (PositionDelta -> PositionDelta -> PositionDelta)
-> PositionMap b c -> PositionMap a b -> PositionMap a c
forall a b. Coercible a b => a -> b
coerce PositionDelta -> PositionDelta -> PositionDelta
P.composeDelta


------------------------------------------------------------------------------
-- | Get a 'PositionMap' that runs in the opposite direction.
dualPositionMap :: PositionMap from to -> PositionMap to from
dualPositionMap :: forall (from :: Age) (to :: Age).
PositionMap from to -> PositionMap to from
dualPositionMap (PositionMap (P.PositionMapping (P.PositionDelta Position -> PositionResult Position
from Position -> PositionResult Position
to))) =
  PositionMapping -> PositionMap to from
forall (from :: Age) (to :: Age).
PositionMapping -> PositionMap from to
PositionMap (PositionMapping -> PositionMap to from)
-> PositionMapping -> PositionMap to from
forall a b. (a -> b) -> a -> b
$ PositionDelta -> PositionMapping
P.PositionMapping (PositionDelta -> PositionMapping)
-> PositionDelta -> PositionMapping
forall a b. (a -> b) -> a -> b
$ (Position -> PositionResult Position)
-> (Position -> PositionResult Position) -> PositionDelta
P.PositionDelta Position -> PositionResult Position
to Position -> PositionResult Position
from


------------------------------------------------------------------------------
-- | A pair containing a @'Tracked' 'Stale'@ value, as well as
-- a 'PositionMapping' that will fast-forward values to the current age.
data TrackedStale a where
  TrackedStale
      :: Tracked (Stale s) a
      -> PositionMap (Stale s) Current
      -> TrackedStale a

instance Functor TrackedStale where
  fmap :: forall a b. (a -> b) -> TrackedStale a -> TrackedStale b
fmap a -> b
f (TrackedStale Tracked ('Stale s) a
t PositionMap ('Stale s) 'Current
pm) = Tracked ('Stale s) b
-> PositionMap ('Stale s) 'Current -> TrackedStale b
forall s a.
Tracked ('Stale s) a
-> PositionMap ('Stale s) 'Current -> TrackedStale a
TrackedStale ((a -> b) -> Tracked ('Stale s) a -> Tracked ('Stale s) b
forall a b.
(a -> b) -> Tracked ('Stale s) a -> Tracked ('Stale s) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Tracked ('Stale s) a
t) PositionMap ('Stale s) 'Current
pm


untrackedStaleValue :: TrackedStale a -> a
untrackedStaleValue :: forall a. TrackedStale a -> a
untrackedStaleValue (TrackedStale Tracked ('Stale s) a
ta PositionMap ('Stale s) 'Current
_) = Tracked ('Stale s) a -> a
forall a b. Coercible a b => a -> b
coerce Tracked ('Stale s) a
ta


------------------------------------------------------------------------------
-- | A class for which 'Tracked' values can be run across a 'PositionMapping'
-- to change their ages.
class MapAge a where
  {-# MINIMAL mapAgeFrom | mapAgeTo #-}
  mapAgeFrom :: PositionMap from to -> Tracked to   a -> Maybe (Tracked from a)
  mapAgeFrom = PositionMap to from -> Tracked to a -> Maybe (Tracked from a)
forall a (from :: Age) (to :: Age).
MapAge a =>
PositionMap from to -> Tracked from a -> Maybe (Tracked to a)
forall (from :: Age) (to :: Age).
PositionMap from to -> Tracked from a -> Maybe (Tracked to a)
mapAgeTo (PositionMap to from -> Tracked to a -> Maybe (Tracked from a))
-> (PositionMap from to -> PositionMap to from)
-> PositionMap from to
-> Tracked to a
-> Maybe (Tracked from a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PositionMap from to -> PositionMap to from
forall (from :: Age) (to :: Age).
PositionMap from to -> PositionMap to from
dualPositionMap

  mapAgeTo   :: PositionMap from to -> Tracked from a -> Maybe (Tracked to   a)
  mapAgeTo = PositionMap to from -> Tracked from a -> Maybe (Tracked to a)
forall a (from :: Age) (to :: Age).
MapAge a =>
PositionMap from to -> Tracked to a -> Maybe (Tracked from a)
forall (from :: Age) (to :: Age).
PositionMap from to -> Tracked to a -> Maybe (Tracked from a)
mapAgeFrom (PositionMap to from -> Tracked from a -> Maybe (Tracked to a))
-> (PositionMap from to -> PositionMap to from)
-> PositionMap from to
-> Tracked from a
-> Maybe (Tracked to a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PositionMap from to -> PositionMap to from
forall (from :: Age) (to :: Age).
PositionMap from to -> PositionMap to from
dualPositionMap


instance MapAge Range where
  mapAgeFrom :: forall (from :: Age) (to :: Age).
PositionMap from to
-> Tracked to Range -> Maybe (Tracked from Range)
mapAgeFrom = (PositionMapping -> Range -> Maybe Range)
-> PositionMap from to
-> Tracked to Range
-> Maybe (Tracked from Range)
forall a b. Coercible a b => a -> b
coerce PositionMapping -> Range -> Maybe Range
P.fromCurrentRange
  mapAgeTo :: forall (from :: Age) (to :: Age).
PositionMap from to
-> Tracked from Range -> Maybe (Tracked to Range)
mapAgeTo   = (PositionMapping -> Range -> Maybe Range)
-> PositionMap from to
-> Tracked from Range
-> Maybe (Tracked to Range)
forall a b. Coercible a b => a -> b
coerce PositionMapping -> Range -> Maybe Range
P.toCurrentRange


instance MapAge RealSrcSpan where
  mapAgeFrom :: forall (from :: Age) (to :: Age).
PositionMap from to
-> Tracked to RealSrcSpan -> Maybe (Tracked from RealSrcSpan)
mapAgeFrom =
    (FastString -> Range -> RealSrcSpan)
-> (RealSrcSpan -> (FastString, Range))
-> (Tracked to Range -> Maybe (Tracked from Range))
-> Tracked to RealSrcSpan
-> Maybe (Tracked from RealSrcSpan)
forall c a b (from :: Age) (to :: Age).
(c -> a -> b)
-> (b -> (c, a))
-> (Tracked from a -> Maybe (Tracked to a))
-> Tracked from b
-> Maybe (Tracked to b)
invMapAge (\FastString
fs -> NormalizedFilePath -> Range -> RealSrcSpan
rangeToRealSrcSpan (String -> NormalizedFilePath
forall a. IsString a => String -> a
fromString (String -> NormalizedFilePath) -> String -> NormalizedFilePath
forall a b. (a -> b) -> a -> b
$ FastString -> String
unpackFS FastString
fs))
              (RealSrcSpan -> FastString
srcSpanFile (RealSrcSpan -> FastString)
-> (RealSrcSpan -> Range) -> RealSrcSpan -> (FastString, Range)
forall b c c'. (b -> c) -> (b -> c') -> b -> (c, c')
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& RealSrcSpan -> Range
realSrcSpanToRange)
      ((Tracked to Range -> Maybe (Tracked from Range))
 -> Tracked to RealSrcSpan -> Maybe (Tracked from RealSrcSpan))
-> (PositionMap from to
    -> Tracked to Range -> Maybe (Tracked from Range))
-> PositionMap from to
-> Tracked to RealSrcSpan
-> Maybe (Tracked from RealSrcSpan)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  PositionMap from to
-> Tracked to Range -> Maybe (Tracked from Range)
forall a (from :: Age) (to :: Age).
MapAge a =>
PositionMap from to -> Tracked to a -> Maybe (Tracked from a)
forall (from :: Age) (to :: Age).
PositionMap from to
-> Tracked to Range -> Maybe (Tracked from Range)
mapAgeFrom


------------------------------------------------------------------------------
-- | Helper function for deriving 'MapAge' for values in terms of other
-- instances.
invMapAge
    :: (c -> a -> b)
    -> (b -> (c, a))
    -> (Tracked from a -> Maybe (Tracked to a))
    -> Tracked from b
    -> Maybe (Tracked to b)
invMapAge :: forall c a b (from :: Age) (to :: Age).
(c -> a -> b)
-> (b -> (c, a))
-> (Tracked from a -> Maybe (Tracked to a))
-> Tracked from b
-> Maybe (Tracked to b)
invMapAge c -> a -> b
to b -> (c, a)
from Tracked from a -> Maybe (Tracked to a)
f Tracked from b
t =
  let (c
c, a
t') = Tracked from (c, a) -> (c, a)
forall (age :: Age) a. Tracked age a -> a
unTrack (Tracked from (c, a) -> (c, a)) -> Tracked from (c, a) -> (c, a)
forall a b. (a -> b) -> a -> b
$ (b -> (c, a)) -> Tracked from b -> Tracked from (c, a)
forall a b. (a -> b) -> Tracked from a -> Tracked from b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> (c, a)
from Tracked from b
t
   in (Tracked to a -> Tracked to b)
-> Maybe (Tracked to a) -> Maybe (Tracked to b)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Tracked to a -> Tracked to b
forall a b. (a -> b) -> Tracked to a -> Tracked to b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Tracked to a -> Tracked to b)
-> (a -> b) -> Tracked to a -> Tracked to b
forall a b. (a -> b) -> a -> b
$ c -> a -> b
to c
c) (Maybe (Tracked to a) -> Maybe (Tracked to b))
-> Maybe (Tracked to a) -> Maybe (Tracked to b)
forall a b. (a -> b) -> a -> b
$ Tracked from a -> Maybe (Tracked to a)
f (Tracked from a -> Maybe (Tracked to a))
-> Tracked from a -> Maybe (Tracked to a)
forall a b. (a -> b) -> a -> b
$ a -> Tracked from a
forall (age :: Age) a. a -> Tracked age a
UnsafeTracked a
t'


unsafeMkCurrent :: age ->  Tracked 'Current age
unsafeMkCurrent :: forall age. age -> Tracked 'Current age
unsafeMkCurrent = age -> Tracked 'Current age
forall a b. Coercible a b => a -> b
coerce


unsafeMkStale :: age -> Tracked (Stale s) age
unsafeMkStale :: forall age s. age -> Tracked ('Stale s) age
unsafeMkStale = age -> Tracked ('Stale s) age
forall a b. Coercible a b => a -> b
coerce


unsafeCopyAge :: Tracked age a -> b -> Tracked age b
unsafeCopyAge :: forall (age :: Age) a b. Tracked age a -> b -> Tracked age b
unsafeCopyAge Tracked age a
_ = b -> Tracked age b
forall a b. Coercible a b => a -> b
coerce


-- | Request a Rule result, it not available return the last computed result, if any, which may be stale
useWithStale :: IdeRule k v
    => k -> NormalizedFilePath -> Action (Maybe (TrackedStale v))
useWithStale :: forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> Action (Maybe (TrackedStale v))
useWithStale k
key NormalizedFilePath
file = do
  Maybe (v, PositionMapping)
x <- k -> NormalizedFilePath -> Action (Maybe (v, PositionMapping))
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> Action (Maybe (v, PositionMapping))
IDE.useWithStale k
key NormalizedFilePath
file
  Maybe (TrackedStale v) -> Action (Maybe (TrackedStale v))
forall a. a -> Action a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (TrackedStale v) -> Action (Maybe (TrackedStale v)))
-> Maybe (TrackedStale v) -> Action (Maybe (TrackedStale v))
forall a b. (a -> b) -> a -> b
$ Maybe (v, PositionMapping)
x Maybe (v, PositionMapping)
-> ((v, PositionMapping) -> TrackedStale v)
-> Maybe (TrackedStale v)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(v
v, PositionMapping
pm) ->
    Tracked ('Stale Any) v
-> PositionMap ('Stale Any) 'Current -> TrackedStale v
forall s a.
Tracked ('Stale s) a
-> PositionMap ('Stale s) 'Current -> TrackedStale a
TrackedStale (v -> Tracked ('Stale Any) v
forall a b. Coercible a b => a -> b
coerce v
v) (PositionMapping -> PositionMap ('Stale Any) 'Current
forall a b. Coercible a b => a -> b
coerce PositionMapping
pm)

-- | Request a Rule result, it not available return the last computed result which may be stale.
--   Errors out if none available.
useWithStale_ :: IdeRule k v
    => k -> NormalizedFilePath -> Action (TrackedStale v)
useWithStale_ :: forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> Action (TrackedStale v)
useWithStale_ k
key NormalizedFilePath
file = do
  (v
v, PositionMapping
pm) <- k -> NormalizedFilePath -> Action (v, PositionMapping)
forall k v.
IdeRule k v =>
k -> NormalizedFilePath -> Action (v, PositionMapping)
IDE.useWithStale_ k
key NormalizedFilePath
file
  TrackedStale v -> Action (TrackedStale v)
forall a. a -> Action a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TrackedStale v -> Action (TrackedStale v))
-> TrackedStale v -> Action (TrackedStale v)
forall a b. (a -> b) -> a -> b
$ Tracked ('Stale Any) v
-> PositionMap ('Stale Any) 'Current -> TrackedStale v
forall s a.
Tracked ('Stale s) a
-> PositionMap ('Stale s) 'Current -> TrackedStale a
TrackedStale (v -> Tracked ('Stale Any) v
forall a b. Coercible a b => a -> b
coerce v
v) (PositionMapping -> PositionMap ('Stale Any) 'Current
forall a b. Coercible a b => a -> b
coerce PositionMapping
pm)