Skip to content

Commit c02472c

Browse files
committed
Rename only if the current module compiles (haskell#3799)
Prefer `useE` over `useWithStaleE`
1 parent c0f7d4c commit c02472c

File tree

1 file changed

+15
-8
lines changed
  • plugins/hls-rename-plugin/src/Ide/Plugin

1 file changed

+15
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Control.Monad.IO.Class
2525
import Control.Monad.Trans.Class
2626
import Control.Monad.Trans.Except
2727
import Data.Bifunctor (first)
28+
import Data.Either
2829
import Data.Generics
2930
import Data.Hashable
3031
import Data.HashSet (HashSet)
@@ -37,6 +38,7 @@ import qualified Data.Set as S
3738
import qualified Data.Text as T
3839
import Development.IDE (Recorder, WithPriority,
3940
usePropertyAction)
41+
import Development.IDE.GHC.Compat
4042
import Development.IDE.Core.PluginUtils
4143
import Development.IDE.Core.PositionMapping
4244
import Development.IDE.Core.RuleTypes
@@ -210,26 +212,26 @@ refsAtName state nfp name = do
210212
)
211213
pure $ nameLocs name ast ++ dbRefs
212214

213-
nameLocs :: Name -> (HieAstResult, PositionMapping) -> [Location]
214-
nameLocs name (HAR _ _ rm _ _, pm) =
215-
mapMaybe (toCurrentLocation pm . realSrcSpanToLocation . fst)
216-
(concat $ M.lookup (Right name) rm)
215+
nameLocs :: Name -> HieAstResult -> [Location]
216+
nameLocs name (HAR _ _ rm _ _) =
217+
map (realSrcSpanToLocation . fst)
218+
(concat $ M.lookup (Right name) rm)
217219

218220
---------------------------------------------------------------------------------------------------
219221
-- Util
220222

221223
getNamesAtPos :: MonadIO m => IdeState -> NormalizedFilePath -> Position -> ExceptT PluginError m [Name]
222224
getNamesAtPos state nfp pos = do
223-
(HAR{hieAst}, pm) <- handleGetHieAst state nfp
224-
pure $ getNamesAtPoint hieAst pos pm
225+
HAR{hieAst} <- handleGetHieAst state nfp
226+
pure $ getNamesAtPoint' hieAst pos
225227

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

234236
-- | We don't want to rename in code generated by GHC as this gives false positives.
235237
-- So we restrict the HIE file to remove all the generated code.
@@ -245,6 +247,11 @@ removeGenerated HAR{..} = HAR{hieAst = go hieAst,..}
245247
hf
246248
#endif
247249

250+
-- | A variant 'getNamesAtPoint' that does not expect a 'PositionMapping'
251+
getNamesAtPoint' :: HieASTs a -> Position -> [Name]
252+
getNamesAtPoint' hf pos =
253+
concat $ pointCommand hf pos (rights . M.keys . getNodeIds)
254+
248255
-- head is safe since groups are non-empty
249256
collectWith :: (Hashable a, Eq a, Eq b) => (a -> b) -> HashSet a -> [(b, HashSet a)]
250257
collectWith f = map (\a -> (f $ head a, HS.fromList a)) . groupOn f . HS.toList

0 commit comments

Comments
 (0)