Skip to content

Commit 87fb2e0

Browse files
authored
[ghc-9.2] Fix rename plugin (#2593)
* Introduce a Compat layer for retrie Annotated type * Use GetAnnotatedParsedSource in rename plugin We already have a cached delta AST at hand * fix build issues * remove debugging traces * Fix hlint
1 parent acff2bd commit 87fb2e0

File tree

9 files changed

+61
-52
lines changed

9 files changed

+61
-52
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ jobs:
202202
name: Test hls-call-hierarchy-plugin test suite
203203
run: cabal test hls-call-hierarchy-plugin --test-options="$TEST_OPTS" || cabal test hls-call-hierarchy-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-call-hierarchy-plugin --test-options="$TEST_OPTS"
204204

205-
- if: matrix.test && matrix.ghc != '9.2.1'
205+
- if: matrix.test
206206
name: Test hls-rename-plugin test suite
207207
run: cabal test hls-rename-plugin --test-options="$TEST_OPTS" || cabal test hls-rename-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-rename-plugin --test-options="$TEST_OPTS"
208208

cabal-ghc921.project

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ constraints:
6565
-retrie
6666
-splice
6767
-stylishhaskell
68-
-tactic
69-
-- the rename plugin builds, but doesn't work
70-
-rename,
68+
-tactic,
7169
ghc-lib-parser ^>= 9.2,
7270
attoparsec ^>= 0.14.3,
7371
ghc-exactprint >= 1.3,

ghcide/.hlint.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
- Development.IDE.GHC.Compat
9696
- Development.IDE.GHC.Compat.Core
9797
- Development.IDE.GHC.Compat.Env
98+
- Development.IDE.GHC.Compat.ExactPrint
9899
- Development.IDE.GHC.Compat.Iface
99100
- Development.IDE.GHC.Compat.Logger
100101
- Development.IDE.GHC.Compat.Outputable
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE FlexibleInstances #-}
3+
{-# LANGUAGE PatternSynonyms #-}
34

45
-- | This module contains compatibility constructs to write type signatures across
56
-- multiple ghc-exactprint versions, accepting that anything more ambitious is
@@ -8,15 +9,16 @@ module Development.IDE.GHC.Compat.ExactPrint
89
( ExactPrint
910
, exactPrint
1011
, makeDeltaAst
11-
#if !MIN_VERSION_ghc(9,2,0)
12-
, Annotated(..)
13-
#endif
12+
, Retrie.Annotated, pattern Annotated, astA, annsA
1413
) where
1514

16-
import Language.Haskell.GHC.ExactPrint
1715
#if !MIN_VERSION_ghc(9,2,0)
18-
import Retrie.ExactPrint (Annotated (..))
16+
import Control.Arrow ((&&&))
17+
#else
18+
import Development.IDE.GHC.Compat.Parser
1919
#endif
20+
import Language.Haskell.GHC.ExactPrint as Retrie
21+
import qualified Retrie.ExactPrint as Retrie
2022

2123
#if !MIN_VERSION_ghc(9,2,0)
2224
class ExactPrint ast where
@@ -26,3 +28,10 @@ class ExactPrint ast where
2628
instance ExactPrint ast
2729
#endif
2830

31+
#if !MIN_VERSION_ghc(9,2,0)
32+
pattern Annotated :: ast -> Anns -> Retrie.Annotated ast
33+
pattern Annotated {astA, annsA} <- (Retrie.astA &&& Retrie.annsA -> (astA, annsA))
34+
#else
35+
pattern Annotated :: ast -> ApiAnns -> Retrie.Annotated ast
36+
pattern Annotated {astA, annsA} <- ((,()) . Retrie.astA -> (astA, annsA))
37+
#endif

ghcide/src/Development/IDE/GHC/ExactPrint.hs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ module Development.IDE.GHC.ExactPrint
4343
GetAnnotatedParsedSource(..),
4444
ASTElement (..),
4545
ExceptStringT (..),
46-
Annotated(..),
4746
TransformT,
4847
)
4948
where
@@ -82,8 +81,8 @@ import Ide.PluginUtils
8281
import Language.Haskell.GHC.ExactPrint.Parsers
8382
import Language.LSP.Types
8483
import Language.LSP.Types.Capabilities (ClientCapabilities)
85-
import Retrie.ExactPrint hiding (parseDecl,
86-
parseExpr,
84+
import Retrie.ExactPrint hiding (Annotated (..),
85+
parseDecl, parseExpr,
8786
parsePattern,
8887
parseType)
8988
#if MIN_VERSION_ghc(9,2,0)
@@ -107,11 +106,7 @@ data GetAnnotatedParsedSource = GetAnnotatedParsedSource
107106

108107
instance Hashable GetAnnotatedParsedSource
109108
instance NFData GetAnnotatedParsedSource
110-
#if MIN_VERSION_ghc(9,2,0)
111-
type instance RuleResult GetAnnotatedParsedSource = ParsedSource
112-
#else
113109
type instance RuleResult GetAnnotatedParsedSource = Annotated ParsedSource
114-
#endif
115110

116111
-- | Get the latest version of the annotated parse source with comments.
117112
getAnnotatedParsedSourceRule :: Rules ()
@@ -120,9 +115,8 @@ getAnnotatedParsedSourceRule = define $ \GetAnnotatedParsedSource nfp -> do
120115
return ([], fmap annotateParsedSource pm)
121116

122117
#if MIN_VERSION_ghc(9,2,0)
123-
annotateParsedSource :: ParsedModule -> ParsedSource
124-
annotateParsedSource (ParsedModule _ ps _ _) = makeDeltaAst ps
125-
118+
annotateParsedSource :: ParsedModule -> Annotated ParsedSource
119+
annotateParsedSource (ParsedModule _ ps _ _) = unsafeMkA (makeDeltaAst ps) 0
126120
#else
127121
annotateParsedSource :: ParsedModule -> Annotated ParsedSource
128122
annotateParsedSource = fixAnns

ghcide/src/Development/IDE/Plugin/CodeAction/Args.hs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ data CodeActionArgs = CodeActionArgs
145145
caaParsedModule :: IO (Maybe ParsedModule),
146146
caaContents :: IO (Maybe T.Text),
147147
caaDf :: IO (Maybe DynFlags),
148-
#if !MIN_VERSION_ghc(9,2,0)
149148
caaAnnSource :: IO (Maybe (Annotated ParsedSource)),
150-
#else
151-
caaAnnSource :: IO (Maybe ParsedSource),
152-
#endif
153149
caaTmr :: IO (Maybe TcModuleResult),
154150
caaHar :: IO (Maybe HieAstResult),
155151
caaBindings :: IO (Maybe Bindings),
@@ -220,11 +216,7 @@ toCodeAction3 get f = ReaderT $ \caa -> get caa >>= flip runReaderT caa . toCode
220216
instance ToCodeAction r => ToCodeAction (ParsedSource -> r) where
221217
toCodeAction f = ReaderT $ \caa@CodeActionArgs {caaAnnSource = x} ->
222218
x >>= \case
223-
#if !MIN_VERSION_ghc(9,2,0)
224219
Just s -> flip runReaderT caa . toCodeAction . f . astA $ s
225-
#else
226-
Just s -> flip runReaderT caa . toCodeAction . f $ s
227-
#endif
228220
_ -> pure []
229221

230222
instance ToCodeAction r => ToCodeAction (ExportsMap -> r) where
@@ -254,17 +246,11 @@ instance ToCodeAction r => ToCodeAction (Maybe DynFlags -> r) where
254246
instance ToCodeAction r => ToCodeAction (DynFlags -> r) where
255247
toCodeAction = toCodeAction2 caaDf
256248

257-
#if !MIN_VERSION_ghc(9,2,0)
258249
instance ToCodeAction r => ToCodeAction (Maybe (Annotated ParsedSource) -> r) where
259250
toCodeAction = toCodeAction1 caaAnnSource
260251

261252
instance ToCodeAction r => ToCodeAction (Annotated ParsedSource -> r) where
262253
toCodeAction = toCodeAction2 caaAnnSource
263-
#else
264-
-- | this instance returns a delta AST, useful for exactprint transforms
265-
instance ToCodeAction r => ToCodeAction (Maybe ParsedSource -> r) where
266-
toCodeAction = toCodeAction1 caaAnnSource
267-
#endif
268254

269255
instance ToCodeAction r => ToCodeAction (Maybe TcModuleResult -> r) where
270256
toCodeAction = toCodeAction1 caaTmr

ghcide/src/Development/IDE/Plugin/Completions.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,7 @@ extendImportHandler' ideState ExtendImport {..}
239239
Just p -> p <> "(" <> newThing <> ")"
240240
t <- liftMaybe $ snd <$> newImportToEdit
241241
n
242-
#if !MIN_VERSION_ghc(9,2,0)
243242
(astA ps)
244-
#else
245-
ps
246-
#endif
247243
(fromMaybe "" contents)
248244
return (nfp, WorkspaceEdit {_changes=Just (fromList [(doc,List [t])]), _documentChanges=Nothing, _changeAnnotations=Nothing})
249245
| otherwise =

plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import qualified Data.Map as Map
1515
import qualified Data.Text as T
1616
import Development.IDE hiding (pluginHandlers)
1717
import Development.IDE.GHC.Compat
18-
import Development.IDE.GHC.ExactPrint (GetAnnotatedParsedSource (..),
19-
annsA, astA)
18+
import Development.IDE.GHC.ExactPrint (GetAnnotatedParsedSource (..))
2019
import Ide.Types
2120
import Language.Haskell.GHC.ExactPrint
2221
import Language.Haskell.GHC.ExactPrint.Types hiding (GhcPs)

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
{-# LANGUAGE CPP #-}
2-
{-# LANGUAGE DataKinds #-}
3-
{-# LANGUAGE GADTs #-}
4-
{-# LANGUAGE NamedFieldPuns #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE NamedFieldPuns #-}
5+
{-# LANGUAGE RankNTypes #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE TypeApplications #-}
58

69
module Ide.Plugin.Rename (descriptor) where
710

@@ -11,7 +14,7 @@ import Control.Monad.Trans.Class
1114
import Control.Monad.Trans.Except
1215
import Data.Containers.ListUtils
1316
import Data.Generics
14-
import Data.List.Extra hiding (nubOrd)
17+
import Data.List.Extra hiding (nubOrd, replace)
1518
import qualified Data.Map as M
1619
import Data.Maybe
1720
import qualified Data.Text as T
@@ -20,11 +23,16 @@ import Development.IDE.Core.PositionMapping
2023
import Development.IDE.Core.Shake
2124
import Development.IDE.GHC.Compat
2225
import Development.IDE.Spans.AtPoint
26+
#if MIN_VERSION_ghc(9,2,1)
27+
import GHC.Parser.Annotation (AnnContext, AnnList,
28+
AnnParen, AnnPragma)
29+
#endif
2330
#if MIN_VERSION_ghc(9,0,1)
2431
import GHC.Types.Name
2532
#else
2633
import Name
2734
#endif
35+
import Development.IDE.GHC.ExactPrint (GetAnnotatedParsedSource (GetAnnotatedParsedSource))
2836
import HieDb.Query
2937
import Ide.Plugin.Config
3038
import Ide.PluginUtils
@@ -46,7 +54,6 @@ renameProvider state pluginId (RenameParams (TextDocumentIdentifier uri) pos _pr
4654
workspaceRefs <- refsAtName state nfp oldName
4755
let filesRefs = groupOn locToUri workspaceRefs
4856
getFileEdits = ap (getSrcEdits state . renameModRefs newNameText) (locToUri . head)
49-
5057
fileEdits <- mapM getFileEdits filesRefs
5158
pure $ foldl' (<>) mempty fileEdits
5259

@@ -67,14 +74,14 @@ getSrcEdits ::
6774
getSrcEdits state updateMod uri = do
6875
ccs <- lift getClientCapabilities
6976
nfp <- safeUriToNfp uri
70-
~ParsedModule{pm_parsed_source = ps, pm_annotations = apiAnns} <-
77+
annotatedAst <-
7178
handleMaybeM "Error: could not get parsed source" $ liftIO $ runAction
7279
"Rename.GetParsedModuleWithComments"
7380
state
74-
(use GetParsedModuleWithComments nfp)
81+
(use GetAnnotatedParsedSource nfp)
82+
let (ps, anns) = (astA annotatedAst, annsA annotatedAst)
7583
#if !MIN_VERSION_ghc(9,2,1)
76-
let anns = relativiseApiAnns ps apiAnns
77-
src = T.pack $ exactPrint ps anns
84+
let src = T.pack $ exactPrint ps anns
7885
res = T.pack $ exactPrint (updateMod <$> ps) anns
7986
#else
8087
let src = T.pack $ exactPrint ps
@@ -94,12 +101,32 @@ renameModRefs ::
94101
HsModule GhcPs
95102
-> HsModule GhcPs
96103
#endif
104+
#if MIN_VERSION_ghc(9,2,1)
105+
renameModRefs newNameText refs = everywhere $
106+
-- there has to be a better way...
107+
mkT (replace @AnnListItem) `extT`
108+
-- replace @AnnList `extT` -- not needed
109+
-- replace @AnnParen `extT` -- not needed
110+
-- replace @AnnPragma `extT` -- not needed
111+
-- replace @AnnContext `extT` -- not needed
112+
-- replace @NoEpAnns `extT` -- not needed
113+
replace @NameAnn
114+
where
115+
replace :: forall an. Typeable an => LocatedAn an RdrName -> LocatedAn an RdrName
116+
replace (L srcSpan oldRdrName)
117+
| isRef (locA srcSpan) = L srcSpan $ newRdrName oldRdrName
118+
replace lOldRdrName = lOldRdrName
119+
#else
97120
renameModRefs newNameText refs = everywhere $ mkT replace
98121
where
99122
replace :: Located RdrName -> Located RdrName
100123
replace (L srcSpan oldRdrName)
101124
| isRef srcSpan = L srcSpan $ newRdrName oldRdrName
102125
replace lOldRdrName = lOldRdrName
126+
#endif
127+
128+
isRef :: SrcSpan -> Bool
129+
isRef = (`elem` refs) . fromJust . srcSpanToLocation
103130

104131
newRdrName :: RdrName -> RdrName
105132
newRdrName oldRdrName = case oldRdrName of
@@ -108,9 +135,8 @@ renameModRefs newNameText refs = everywhere $ mkT replace
108135

109136
newOccName = mkTcOcc $ T.unpack newNameText
110137

111-
isRef :: SrcSpan -> Bool
112-
isRef = (`elem` refs) . fromJust . srcSpanToLocation
113-
138+
newRdrName :: RdrName -> RdrName
139+
newRdrName = error "not implemented"
114140
-------------------------------------------------------------------------------
115141
-- Reference finding
116142

0 commit comments

Comments
 (0)