Skip to content

Commit 2d985ec

Browse files
committed
Rename only if the current module compiles (haskell#3799)
Prefer `useE` over `useWithStaleE`
1 parent 9593d04 commit 2d985ec

File tree

1 file changed

+14
-7
lines changed
  • plugins/hls-rename-plugin/src/Ide/Plugin

1 file changed

+14
-7
lines changed

plugins/hls-rename-plugin/src/Ide/Plugin/Rename.hs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO)
1616
import Control.Monad.Trans.Class (lift)
1717
import Data.Bifunctor (first)
1818
import Data.Foldable (fold)
19+
import Data.Either (rights)
1920
import Data.Generics
2021
import Data.Hashable
2122
import Data.HashSet (HashSet)
@@ -35,6 +36,7 @@ import Development.IDE.Core.PositionMapping
3536
import Development.IDE.Core.RuleTypes
3637
import Development.IDE.Core.Service
3738
import Development.IDE.Core.Shake
39+
import Development.IDE.GHC.Compat
3840
import Development.IDE.GHC.Compat.Core
3941
import Development.IDE.GHC.Compat.ExactPrint
4042
import Development.IDE.GHC.Compat.Parser
@@ -212,26 +214,26 @@ refsAtName state nfp name = do
212214
)
213215
pure $ nameLocs name ast ++ dbRefs
214216

215-
nameLocs :: Name -> (HieAstResult, PositionMapping) -> [Location]
216-
nameLocs name (HAR _ _ rm _ _, pm) =
217-
concatMap (mapMaybe (toCurrentLocation pm . realSrcSpanToLocation . fst))
217+
nameLocs :: Name -> HieAstResult -> [Location]
218+
nameLocs name (HAR _ _ rm _ _) =
219+
concatMap (map (realSrcSpanToLocation . fst))
218220
(M.lookup (Right name) rm)
219221

220222
---------------------------------------------------------------------------------------------------
221223
-- Util
222224

223225
getNamesAtPos :: MonadIO m => IdeState -> NormalizedFilePath -> Position -> ExceptT PluginError m [Name]
224226
getNamesAtPos state nfp pos = do
225-
(HAR{hieAst}, pm) <- handleGetHieAst state nfp
226-
pure $ getNamesAtPoint hieAst pos pm
227+
HAR{hieAst} <- handleGetHieAst state nfp
228+
pure $ getNamesAtPoint' hieAst pos
227229

228230
handleGetHieAst ::
229231
MonadIO m =>
230232
IdeState ->
231233
NormalizedFilePath ->
232-
ExceptT PluginError m (HieAstResult, PositionMapping)
234+
ExceptT PluginError m HieAstResult
233235
handleGetHieAst state nfp =
234-
fmap (first removeGenerated) $ runActionE "Rename.GetHieAst" state $ useWithStaleE GetHieAst nfp
236+
fmap removeGenerated $ runActionE "Rename.GetHieAst" state $ useE GetHieAst nfp
235237

236238
-- | We don't want to rename in code generated by GHC as this gives false positives.
237239
-- So we restrict the HIE file to remove all the generated code.
@@ -246,6 +248,11 @@ removeGenerated HAR{..} = HAR{hieAst = go hieAst,..}
246248
collectWith :: (Hashable a, Eq b) => (a -> b) -> HashSet a -> [(b, HashSet a)]
247249
collectWith f = map (\(a :| as) -> (f a, HS.fromList (a:as))) . groupWith f . HS.toList
248250

251+
-- | A variant 'getNamesAtPoint' that does not expect a 'PositionMapping'
252+
getNamesAtPoint' :: HieASTs a -> Position -> [Name]
253+
getNamesAtPoint' hf pos =
254+
concat $ pointCommand hf pos (rights . M.keys . getNodeIds)
255+
249256
locToUri :: Location -> Uri
250257
locToUri (Location uri _) = uri
251258

0 commit comments

Comments
 (0)