11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE OverloadedStrings #-}
3- {-# OPTIONS_GHC -Wall -Werror #-}
3+ {-# OPTIONS_GHC -Wall #-}
44
55{-| Logic for renaming qualified import aliases.
66
@@ -47,6 +47,7 @@ import Data.Generics
4747import qualified Data.Map as M
4848import Data.Maybe
4949import qualified Data.Text as T
50+ import Debug.Trace (trace , traceShowWith )
5051import Development.IDE (realSrcSpanToCodePointRange )
5152import Development.IDE.Core.FileStore (getVersionedTextDoc )
5253import Development.IDE.Core.PluginUtils
@@ -55,6 +56,7 @@ import Development.IDE.Core.Service hiding (Log)
5556import Development.IDE.Core.Shake hiding (Log )
5657import Development.IDE.GHC.Compat hiding (importDecl )
5758import GHC.Data.FastString (lengthFS )
59+ import GHC.Utils.Outputable (showPprUnsafe )
5860import Ide.Plugin.Error
5961import qualified Language.LSP.Protocol.Lens as L
6062import Language.LSP.Protocol.Message
@@ -193,8 +195,7 @@ findAliasUseAtPos ::
193195 Maybe (VFS. CodePointRange , [ImportAlias ])
194196findAliasUseAtPos pos exports imports hsDecls =
195197 let qualifiersAtPos = do
196- locatedRdrName :: XRec GhcPs RdrName <-
197- listify (const True ) exports ++ listify (const True ) hsDecls
198+ locatedRdrName <- locateRdrNames exports hsDecls True
198199 Qual qualifier _ <- [unLoc locatedRdrName]
199200 RealSrcSpan qualifiedNameSpan _ <- [getLoc locatedRdrName]
200201 let qualifiedNameRange = realSrcSpanToCodePointRange qualifiedNameSpan
@@ -204,7 +205,7 @@ findAliasUseAtPos pos exports imports hsDecls =
204205 qualifierRange = qualifiedNameRange
205206 & VFS. end .~ (qualifierStart & VFS. character +~ qualifierLength)
206207 guard (rangeContainsPositionInclusive qualifierRange pos)
207- [(qualifierRange, qualifier)]
208+ traceShowWith ( \ x -> " findAliasUseAtPos/qualifierAtPos: " <> show x) [(qualifierRange, qualifier)]
208209 in case qualifiersAtPos of
209210 [] -> Nothing
210211 (rangeAtPos, qualifierAtPos) : _ -> Just $ (,) rangeAtPos $ do
@@ -229,8 +230,7 @@ aliasUseSiteRanges ::
229230aliasUseSiteRanges importAlias exports hsDecls = nubOrd $ do
230231 let ImportAlias {aliasName} = importAlias
231232 aliasLength = fromIntegral (moduleNameLength aliasName)
232- locatedRdrName :: XRec GhcPs RdrName <-
233- listify (const True ) exports ++ listify (const True ) hsDecls
233+ locatedRdrName <- locateRdrNames exports hsDecls False
234234 Qual qualifier _ <- [unLoc locatedRdrName]
235235 guard (qualifier == aliasName)
236236 RealSrcSpan qualifiedNameSpan _ <- [getLoc locatedRdrName]
@@ -275,8 +275,7 @@ aliasUseSiteRangesDisambiguated tcModule importAlias exports hsDecls = nubOrd $
275275 let rdrEnv = tcg_rdr_env (tmrTypechecked tcModule)
276276 ImportAlias {aliasModuleName, aliasName} = importAlias
277277 aliasLength = fromIntegral (moduleNameLength aliasName)
278- locatedRdrName :: XRec GhcPs RdrName <-
279- listify (const True ) exports ++ listify (const True ) hsDecls
278+ locatedRdrName <- locateRdrNames exports hsDecls False
280279 rdrName@ (Qual qualifier name) <- [unLoc locatedRdrName]
281280 guard (qualifier == aliasName)
282281 nameGREElement <- pickGREs rdrName $ lookupGlobalRdrEnv rdrEnv name
@@ -306,6 +305,24 @@ ambiguousAliasErrorMessage _ = ""
306305---------------------------------------------------------------------------------------------------
307306-- Utility functions
308307
308+ instance Show RdrName where
309+ show (Unqual name) = show name
310+ show (Qual moduleName name) = show moduleName ++ " ." ++ show name
311+ show (Orig mod name) = " Orig: " <> show mod <> " ." <> show name
312+ show (Exact name) = " Exact: " <> showPprUnsafe name
313+
314+ -- | Locate 'RdrName' identifiers in the given export list and declarations.
315+ locateRdrNames ::
316+ Maybe (XRec GhcPs [LIE GhcPs ]) ->
317+ [LHsDecl GhcPs ] ->
318+ Bool ->
319+ [XRec GhcPs RdrName ]
320+ locateRdrNames exports hsDecls traceEveryProducedName =
321+ listify const_True exports ++ listify const_True hsDecls
322+ where const_True = if traceEveryProducedName
323+ then \ x -> trace (" listify: " <> show (unLoc x) <> " at " <> show (getLoc x)) True
324+ else const True
325+
309326-- | Check whether a 'CodePointRange' contains a 'CodePointPosition' (inclusive
310327-- start, inclusive end).
311328-- NOTE: The use of inclusive end allows the user to place the cursor at the end
0 commit comments