Skip to content

Commit b1f1feb

Browse files
committed
Fix positionMapping in stale data
1 parent 74466a9 commit b1f1feb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

ghcide/src/Development/IDE/Core/PositionMapping.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Development.IDE.Core.PositionMapping
1111
, toCurrentPosition
1212
, PositionDelta(..)
1313
, addDelta
14+
, addOldDelta
1415
, idDelta
1516
, composeDelta
1617
, mkDelta
@@ -123,6 +124,10 @@ mkDelta cs = foldl' applyChange idDelta cs
123124
addDelta :: PositionDelta -> PositionMapping -> PositionMapping
124125
addDelta delta (PositionMapping pm) = PositionMapping (composeDelta delta pm)
125126

127+
-- | Add a old delta onto a Mapping k n to make a Mapping (k - 1) n
128+
addOldDelta :: PositionDelta -> PositionMapping -> PositionMapping
129+
addOldDelta delta (PositionMapping pm) = PositionMapping (composeDelta pm delta)
130+
126131
-- TODO: We currently ignore the right hand side (if there is only text), as
127132
-- that was what was done with lsp* 1.6 packages
128133
applyChange :: PositionDelta -> TextDocumentContentChangeEvent -> PositionDelta

ghcide/src/Development/IDE/Core/Shake.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ data ShakeExtras = ShakeExtras
266266
-- ^ Map from a text document version to a PositionMapping that describes how to map
267267
-- positions in a version of that document to positions in the latest version
268268
-- First mapping is delta from previous version and second one is an
269-
-- accumulation of all previous mappings.
269+
-- accumulation to the current version.
270270
,progress :: ProgressReporting
271271
,ideTesting :: IdeTesting
272272
-- ^ Whether to enable additional lsp messages used by the test suite for checking invariants
@@ -443,7 +443,7 @@ lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
443443
`catch` (\(_ :: IOException) -> pure Nothing)
444444
atomicallyNamed "lastValueIO 2" $ do
445445
STM.focus (Focus.alter (alterValue $ Stale (Just del) actual_version (toDyn v))) (toKey k file) state
446-
Just . (v,) . addDelta del <$> mappingForVersion positionMapping file actual_version
446+
Just . (v,) . addOldDelta del <$> mappingForVersion positionMapping file actual_version
447447

448448
-- We got a new stale value from the persistent rule, insert it in the map without affecting diagnostics
449449
alterValue new Nothing = Just (ValueWithDiagnostics new mempty) -- If it wasn't in the map, give it empty diagnostics
@@ -459,7 +459,7 @@ lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
459459
Succeeded ver (fromDynamic -> Just v) ->
460460
atomicallyNamed "lastValueIO 5" $ Just . (v,) <$> mappingForVersion positionMapping file ver
461461
Stale del ver (fromDynamic -> Just v) ->
462-
atomicallyNamed "lastValueIO 6" $ Just . (v,) . maybe id addDelta del <$> mappingForVersion positionMapping file ver
462+
atomicallyNamed "lastValueIO 6" $ Just . (v,) . maybe id addOldDelta del <$> mappingForVersion positionMapping file ver
463463
Failed p | not p -> readPersistent
464464
_ -> pure Nothing
465465

@@ -1357,7 +1357,7 @@ updatePositionMapping IdeState{shakeExtras = ShakeExtras{positionMapping}} Versi
13571357
-- Very important to use mapAccum here so that the tails of
13581358
-- each mapping can be shared, otherwise quadratic space is
13591359
-- used which is evident in long running sessions.
1360-
EM.mapAccumRWithKey (\acc _k (delta, _) -> let new = addDelta delta acc in (new, (delta, acc)))
1360+
EM.mapAccumRWithKey (\acc _k (delta, _) -> let new = addOldDelta delta acc in (new, (delta, acc)))
13611361
zeroMapping
13621362
(EM.insert _version (shared_change, zeroMapping) mappingForUri)
13631363
shared_change = mkDelta changes

0 commit comments

Comments
 (0)