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