|
| 1 | +{-# LANGUAGE DerivingVia #-} |
| 2 | +{-# LANGUAGE GADTs #-} |
| 3 | +{-# LANGUAGE KindSignatures #-} |
| 4 | +{-# LANGUAGE RankNTypes #-} |
| 5 | + |
| 6 | +module Development.IDE.Core.UseStale |
| 7 | + ( Age(..) |
| 8 | + , Tracked |
| 9 | + , unTrack |
| 10 | + , PositionMap |
| 11 | + , TrackedStale (..) |
| 12 | + , unsafeMkStale |
| 13 | + , unsafeMkCurrent |
| 14 | + , unsafeCopyAge |
| 15 | + , MapAge (..) |
| 16 | + , dualPositionMap |
| 17 | + , useWithStale |
| 18 | + , useWithStale_ |
| 19 | + ) where |
| 20 | + |
| 21 | +import Control.Arrow |
| 22 | +import Control.Category (Category) |
| 23 | +import qualified Control.Category as C |
| 24 | +import Control.DeepSeq (NFData) |
| 25 | +import Data.Aeson |
| 26 | +import Data.Coerce (coerce) |
| 27 | +import Data.Functor ((<&>)) |
| 28 | +import Data.Functor.Identity (Identity(Identity)) |
| 29 | +import Data.Kind (Type) |
| 30 | +import Data.String (fromString) |
| 31 | +import Development.IDE (NormalizedFilePath, IdeRule, Action, Range, rangeToRealSrcSpan, realSrcSpanToRange) |
| 32 | +import qualified Development.IDE.Core.PositionMapping as P |
| 33 | +import qualified Development.IDE.Core.Shake as IDE |
| 34 | +import qualified FastString as FS |
| 35 | +import SrcLoc |
| 36 | + |
| 37 | + |
| 38 | +------------------------------------------------------------------------------ |
| 39 | +-- | A data kind for 'Tracked'. |
| 40 | +data Age = Current | Stale Type |
| 41 | + |
| 42 | + |
| 43 | +------------------------------------------------------------------------------ |
| 44 | +-- | Some value, tagged with its age. All 'Current' ages are considered to be |
| 45 | +-- the same thing, but 'Stale' values are protected by an untouchable variable |
| 46 | +-- to ensure they can't be unified. |
| 47 | +newtype Tracked (age :: Age) a = UnsafeTracked |
| 48 | + { unTrack :: a |
| 49 | + } |
| 50 | + deriving stock (Functor, Foldable, Traversable) |
| 51 | + deriving newtype (Eq, Ord, Show, Read, ToJSON, FromJSON, NFData) |
| 52 | + deriving (Applicative, Monad) via Identity |
| 53 | + |
| 54 | + |
| 55 | +------------------------------------------------------------------------------ |
| 56 | +-- | Like 'P.PositionMapping', but with annotated ages for how 'Tracked' values |
| 57 | +-- change. Use the 'Category' instance to compose 'PositionMapping's in order |
| 58 | +-- to transform between values of different stale ages. |
| 59 | +newtype PositionMap (from :: Age) (to :: Age) = PositionMap |
| 60 | + { getPositionMapping :: P.PositionMapping |
| 61 | + } |
| 62 | + |
| 63 | +instance Category PositionMap where |
| 64 | + id = coerce P.zeroMapping |
| 65 | + (.) = coerce P.composeDelta |
| 66 | + |
| 67 | + |
| 68 | +------------------------------------------------------------------------------ |
| 69 | +-- | Get a 'PositionMap' that runs in the opposite direction. |
| 70 | +dualPositionMap :: PositionMap from to -> PositionMap to from |
| 71 | +dualPositionMap (PositionMap (P.PositionMapping (P.PositionDelta from to))) = |
| 72 | + PositionMap $ P.PositionMapping $ P.PositionDelta to from |
| 73 | + |
| 74 | + |
| 75 | +------------------------------------------------------------------------------ |
| 76 | +-- | A pair containing a @'Tracked' 'Stale'@ value, as well as |
| 77 | +-- a 'PositionMapping' that will fast-forward values to the current age. |
| 78 | +data TrackedStale a where |
| 79 | + TrackedStale |
| 80 | + :: Tracked (Stale s) a |
| 81 | + -> PositionMap (Stale s) Current |
| 82 | + -> TrackedStale a |
| 83 | + |
| 84 | +instance Functor TrackedStale where |
| 85 | + fmap f (TrackedStale t pm) = TrackedStale (fmap f t) pm |
| 86 | + |
| 87 | + |
| 88 | +------------------------------------------------------------------------------ |
| 89 | +-- | A class for which 'Tracked' values can be run across a 'PositionMapping' |
| 90 | +-- to change their ages. |
| 91 | +class MapAge a where |
| 92 | + {-# MINIMAL mapAgeFrom | mapAgeTo #-} |
| 93 | + mapAgeFrom :: PositionMap from to -> Tracked to a -> Maybe (Tracked from a) |
| 94 | + mapAgeFrom = mapAgeTo . dualPositionMap |
| 95 | + |
| 96 | + mapAgeTo :: PositionMap from to -> Tracked from a -> Maybe (Tracked to a) |
| 97 | + mapAgeTo = mapAgeFrom . dualPositionMap |
| 98 | + |
| 99 | + |
| 100 | +instance MapAge Range where |
| 101 | + mapAgeFrom = coerce P.fromCurrentRange |
| 102 | + mapAgeTo = coerce P.toCurrentRange |
| 103 | + |
| 104 | + |
| 105 | +instance MapAge RealSrcSpan where |
| 106 | + mapAgeFrom = |
| 107 | + invMapAge (\fs -> rangeToRealSrcSpan (fromString $ FS.unpackFS fs)) |
| 108 | + (srcSpanFile &&& realSrcSpanToRange) |
| 109 | + . mapAgeFrom |
| 110 | + |
| 111 | + |
| 112 | +------------------------------------------------------------------------------ |
| 113 | +-- | Helper function for deriving 'MapAge' for values in terms of other |
| 114 | +-- instances. |
| 115 | +invMapAge |
| 116 | + :: (c -> a -> b) |
| 117 | + -> (b -> (c, a)) |
| 118 | + -> (Tracked from a -> Maybe (Tracked to a)) |
| 119 | + -> Tracked from b |
| 120 | + -> Maybe (Tracked to b) |
| 121 | +invMapAge to from f t = |
| 122 | + let (c, t') = unTrack $ fmap from t |
| 123 | + in fmap (fmap $ to c) $ f $ UnsafeTracked t' |
| 124 | + |
| 125 | + |
| 126 | +unsafeMkCurrent :: age -> Tracked 'Current age |
| 127 | +unsafeMkCurrent = coerce |
| 128 | + |
| 129 | + |
| 130 | +unsafeMkStale :: age -> Tracked (Stale s) age |
| 131 | +unsafeMkStale = coerce |
| 132 | + |
| 133 | + |
| 134 | +unsafeCopyAge :: Tracked age a -> b -> Tracked age b |
| 135 | +unsafeCopyAge _ = coerce |
| 136 | + |
| 137 | + |
| 138 | +-- | Request a Rule result, it not available return the last computed result, if any, which may be stale |
| 139 | +useWithStale :: IdeRule k v |
| 140 | + => k -> NormalizedFilePath -> Action (Maybe (TrackedStale v)) |
| 141 | +useWithStale key file = do |
| 142 | + x <- IDE.useWithStale key file |
| 143 | + pure $ x <&> \(v, pm) -> |
| 144 | + TrackedStale (coerce v) (coerce pm) |
| 145 | + |
| 146 | +-- | Request a Rule result, it not available return the last computed result which may be stale. |
| 147 | +-- Errors out if none available. |
| 148 | +useWithStale_ :: IdeRule k v |
| 149 | + => k -> NormalizedFilePath -> Action (TrackedStale v) |
| 150 | +useWithStale_ key file = do |
| 151 | + (v, pm) <- IDE.useWithStale_ key file |
| 152 | + pure $ TrackedStale (coerce v) (coerce pm) |
| 153 | + |
0 commit comments