Skip to content

Commit 1c2e3b7

Browse files
committed
fix use correct currentPosition
1 parent 9ba894b commit 1c2e3b7

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

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

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,32 @@ getAtPoint file pos = runMaybeT $ do
6666
!pos' <- MaybeT (return $ fromCurrentPosition mapping pos)
6767
MaybeT $ liftIO $ fmap (first (toCurrentRange mapping =<<)) <$> AtPoint.atPoint opts hf dkMap env pos'
6868

69-
-- | For each Location, determine if we have the PositionMapping
70-
-- for the correct file. If not, get the correct position mapping
71-
-- and then apply the position mapping to the location.
72-
toCurrentLocations
69+
-- | Converts locations in the source code to their current positions,
70+
-- taking into account changes that may have occurred due to edits.
71+
toCurrentLocation
7372
:: PositionMapping
7473
-> NormalizedFilePath
75-
-> [Location]
76-
-> IdeAction [Location]
77-
toCurrentLocations mapping file = mapMaybeM go
74+
-> Location
75+
-> IdeAction (Maybe Location)
76+
toCurrentLocation mapping file (Location uri range) =
77+
-- The Location we are going to might be in a different
78+
-- file than the one we are calling gotoDefinition from.
79+
-- So we check that the location file matches the file
80+
-- we are in.
81+
if nUri == normalizedFilePathToUri file
82+
-- The Location matches the file, so use the PositionMapping
83+
-- we have.
84+
then pure $ Location uri <$> toCurrentRange mapping range
85+
-- The Location does not match the file, so get the correct
86+
-- PositionMapping and use that instead.
87+
else do
88+
otherLocationMapping <- fmap (fmap snd) $ runMaybeT $ do
89+
otherLocationFile <- MaybeT $ pure $ uriToNormalizedFilePath nUri
90+
useWithStaleFastMT GetHieAst otherLocationFile
91+
pure $ Location uri <$> (flip toCurrentRange range =<< otherLocationMapping)
7892
where
79-
go :: Location -> IdeAction (Maybe Location)
80-
go (Location uri range) =
81-
-- The Location we are going to might be in a different
82-
-- file than the one we are calling gotoDefinition from.
83-
-- So we check that the location file matches the file
84-
-- we are in.
85-
if nUri == normalizedFilePathToUri file
86-
-- The Location matches the file, so use the PositionMapping
87-
-- we have.
88-
then pure $ Location uri <$> toCurrentRange mapping range
89-
-- The Location does not match the file, so get the correct
90-
-- PositionMapping and use that instead.
91-
else do
92-
otherLocationMapping <- fmap (fmap snd) $ runMaybeT $ do
93-
otherLocationFile <- MaybeT $ pure $ uriToNormalizedFilePath nUri
94-
useWithStaleFastMT GetHieAst otherLocationFile
95-
pure $ Location uri <$> (flip toCurrentRange range =<< otherLocationMapping)
96-
where
97-
nUri :: NormalizedUri
98-
nUri = toNormalizedUri uri
93+
nUri :: NormalizedUri
94+
nUri = toNormalizedUri uri
9995

10096
-- | Goto Definition.
10197
getDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [(Location, Identifier)])
@@ -106,10 +102,11 @@ getDefinition file pos = runMaybeT $ do
106102
(ImportMap imports, _) <- useWithStaleFastMT GetImportMap file
107103
!pos' <- MaybeT (pure $ fromCurrentPosition mapping pos)
108104
locationsWithIdentifier <- AtPoint.gotoDefinition withHieDb (lookupMod hiedbWriter) opts imports hf pos'
109-
MaybeT $ do
110-
let (locations, names) = unzip locationsWithIdentifier
111-
curLocations <- toCurrentLocations mapping file locations
112-
pure (Just $ zip curLocations names)
105+
mapMaybeM (\(location, identifier) -> do
106+
fixedLocation <- MaybeT $ toCurrentLocation mapping file location
107+
pure $ Just (fixedLocation, identifier)
108+
) locationsWithIdentifier
109+
113110

114111
getTypeDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [(Location, Identifier)])
115112
getTypeDefinition file pos = runMaybeT $ do
@@ -118,10 +115,10 @@ getTypeDefinition file pos = runMaybeT $ do
118115
(hf, mapping) <- useWithStaleFastMT GetHieAst file
119116
!pos' <- MaybeT (return $ fromCurrentPosition mapping pos)
120117
locationsWithIdentifier <- AtPoint.gotoTypeDefinition withHieDb (lookupMod hiedbWriter) opts hf pos'
121-
MaybeT $ do
122-
let (locations, names) = unzip locationsWithIdentifier
123-
curLocations <- toCurrentLocations mapping file locations
124-
pure (Just $ zip curLocations names)
118+
mapMaybeM (\(location, identifier) -> do
119+
fixedLocation <- MaybeT $ toCurrentLocation mapping file location
120+
pure $ Just (fixedLocation, identifier)
121+
) locationsWithIdentifier
125122

126123
highlightAtPoint :: NormalizedFilePath -> Position -> IdeAction (Maybe [DocumentHighlight])
127124
highlightAtPoint file pos = runMaybeT $ do

0 commit comments

Comments
 (0)