@@ -13,10 +13,11 @@ import Language.Haskell.GHC.ExactPrint
13
13
import Language.Haskell.GHC.ExactPrint.Parsers
14
14
15
15
import Data.Either.Extra (eitherToMaybe )
16
+ import Data.Functor.Identity (Identity )
16
17
import GHC.Parser.Annotation
18
+ import Language.LSP.Protocol.Types (Range )
17
19
18
20
makeEditText :: Monad m => ParsedModule -> DynFlags -> AddMinimalMethodsParams -> MaybeT m (T. Text , T. Text )
19
- -- addMethodDecls :: ParsedSource -> [(LHsDecl GhcPs, LHsDecl GhcPs)] -> Range -> Bool -> TransformT Identity (Located HsModule)
20
21
makeEditText pm df AddMinimalMethodsParams {.. } = do
21
22
mDecls <- MaybeT . pure $ traverse (makeMethodDecl df) methodGroup
22
23
let ps = makeDeltaAst $ pm_parsed_source pm
@@ -31,14 +32,21 @@ makeMethodDecl df (mName, sig) = do
31
32
sig' <- eitherToMaybe $ parseDecl df (T. unpack sig) $ T. unpack sig
32
33
pure (name, sig')
33
34
35
+ #if MIN_VERSION_ghc(9,5,0)
36
+ addMethodDecls :: ParsedSource -> [(LHsDecl GhcPs , LHsDecl GhcPs )] -> Range -> Bool -> TransformT Identity (Located (HsModule GhcPs ))
37
+ #else
38
+ addMethodDecls :: ParsedSource -> [(LHsDecl GhcPs , LHsDecl GhcPs )] -> Range -> Bool -> TransformT Identity (Located HsModule )
39
+ #endif
34
40
addMethodDecls ps mDecls range withSig
35
41
| withSig = go (concatMap (\ (decl, sig) -> [sig, decl]) mDecls)
36
42
| otherwise = go (map fst mDecls)
37
43
where
38
44
go inserting = do
39
45
allDecls <- hsDecls ps
40
- let (before, ((L l inst): after)) = break (inRange range . getLoc) allDecls
41
- replaceDecls ps (before ++ (L l (addWhere inst)): (map newLine inserting ++ after))
46
+ case break (inRange range . getLoc) allDecls of
47
+ (before, L l inst : after) -> replaceDecls ps (before ++ L l (addWhere inst): (map newLine inserting ++ after))
48
+ (before, [] ) -> replaceDecls ps before
49
+
42
50
-- Add `where` keyword for `instance X where` if `where` is missing.
43
51
--
44
52
-- The `where` in ghc-9.2 is now stored in the instance declaration
@@ -48,15 +56,17 @@ addMethodDecls ps mDecls range withSig
48
56
--
49
57
-- See the link for the original definition:
50
58
-- https://hackage.haskell.org/package/ghc-9.2.1/docs/Language-Haskell-Syntax-Extension.html#t:XCClsInstDecl
51
- addWhere (InstD xInstD (ClsInstD ext decl@ ClsInstDecl {.. })) =
52
- let (EpAnn entry anns comments, key) = cid_ext
53
- in InstD xInstD (ClsInstD ext decl {
54
- cid_ext = (EpAnn
55
- entry
56
- (AddEpAnn AnnWhere (EpaDelta (SameLine 1 ) [] ) : anns)
57
- comments
58
- , key)
59
- })
59
+ addWhere instd@ (InstD xInstD (ClsInstD ext decl@ ClsInstDecl {.. })) =
60
+ case cid_ext of
61
+ (EpAnn entry anns comments, key) ->
62
+ InstD xInstD (ClsInstD ext decl {
63
+ cid_ext = (EpAnn
64
+ entry
65
+ (AddEpAnn AnnWhere (EpaDelta (SameLine 1 ) [] ) : anns)
66
+ comments
67
+ , key)
68
+ })
69
+ _ -> instd
60
70
addWhere decl = decl
61
71
62
72
newLine (L l e) =
0 commit comments