1
+ {-# LANGUAGE DefaultSignatures #-}
2
+ {-# LANGUAGE CPP #-}
1
3
{-# LANGUAGE DerivingStrategies #-}
4
+ {-# LANGUAGE FlexibleContexts #-}
2
5
{-# LANGUAGE GADTs #-}
3
6
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4
7
{-# LANGUAGE LambdaCase #-}
5
8
{-# LANGUAGE OverloadedStrings #-}
9
+ {-# LANGUAGE PackageImports #-}
6
10
{-# LANGUAGE RankNTypes #-}
7
11
{-# LANGUAGE RecordWildCards #-}
8
12
{-# LANGUAGE ScopedTypeVariables #-}
13
+ {-# LANGUAGE TypeFamilyDependencies #-}
9
14
10
15
module Ide.TreeTransform
11
16
( Graft (.. ),
@@ -47,9 +52,10 @@ import Language.Haskell.GHC.ExactPrint
47
52
import Language.Haskell.GHC.ExactPrint.Parsers
48
53
import Language.Haskell.LSP.Types
49
54
import Language.Haskell.LSP.Types.Capabilities (ClientCapabilities )
50
- import Outputable (Outputable , ppr , showSDoc , trace )
55
+ import Outputable (Outputable , ppr , showSDoc )
51
56
import Retrie.ExactPrint hiding (parseDecl , parseExpr , parsePattern , parseType )
52
- import Control.Arrow (Arrow (second ))
57
+ import qualified "ghc" SrcLoc
58
+
53
59
------------------------------------------------------------------------------
54
60
55
61
-- | Get the latest version of the annotated parse source.
@@ -152,7 +158,7 @@ graft ::
152
158
forall ast a .
153
159
(Data a , ASTElement ast ) =>
154
160
SrcSpan ->
155
- Located ast ->
161
+ ToL ast GhcPs ->
156
162
Graft (Either String ) a
157
163
graft dst val = Graft $ \ dflags a -> do
158
164
(anns, val') <- annotate dflags $ maybeParensAST val
@@ -161,7 +167,7 @@ graft dst val = Graft $ \dflags a -> do
161
167
everywhere'
162
168
( mkT $
163
169
\ case
164
- (L src _ :: Located ast ) | src == dst -> val'
170
+ (src :: ToL ast GhcPs ) | location src == dst -> val'
165
171
l -> l
166
172
)
167
173
a
@@ -172,14 +178,14 @@ graftWithM ::
172
178
forall ast m a .
173
179
(Fail. MonadFail m , Data a , ASTElement ast ) =>
174
180
SrcSpan ->
175
- (Located ast -> TransformT m (Maybe (Located ast ))) ->
181
+ (ToL ast GhcPs -> TransformT m (Maybe (ToL ast GhcPs ))) ->
176
182
Graft m a
177
183
graftWithM dst trans = Graft $ \ dflags a -> do
178
184
everywhereM'
179
185
( mkM $
180
186
\ case
181
- val @ ( L src _ :: Located ast )
182
- | src == dst -> do
187
+ (val :: ToL ast GhcPs )
188
+ | getLoc val == dst -> do
183
189
mval <- trans val
184
190
case mval of
185
191
Just val' -> do
@@ -197,14 +203,14 @@ graftWithSmallestM ::
197
203
forall ast m a .
198
204
(Fail. MonadFail m , Data a , ASTElement ast ) =>
199
205
SrcSpan ->
200
- (Located ast -> TransformT m (Maybe (Located ast ))) ->
206
+ (ToL ast GhcPs -> TransformT m (Maybe (ToL ast GhcPs ))) ->
201
207
Graft m a
202
208
graftWithSmallestM dst trans = Graft $ \ dflags a -> do
203
209
everywhereM'
204
210
( mkM $
205
211
\ case
206
- val @ ( L src _ :: Located ast )
207
- | dst `isSubspanOf` src -> do
212
+ (val :: ToL ast GhcPs )
213
+ | dst `isSubspanOf` getLoc val -> do
208
214
mval <- trans val
209
215
case mval of
210
216
Just val' -> do
@@ -264,23 +270,64 @@ everywhereM' f = go
264
270
go :: GenericM m
265
271
go = gmapM go <=< f
266
272
267
- class (Data ast , Outputable ast ) => ASTElement ast where
268
- parseAST :: Parser (Located ast )
269
- maybeParensAST :: Located ast -> Located ast
270
-
271
- instance p ~ GhcPs => ASTElement (HsExpr p ) where
273
+ class
274
+ ( Data (ast GhcPs ), Outputable (ast GhcPs ),
275
+ HasSrcSpan (ToL ast GhcPs ), Data (ToL ast GhcPs ),
276
+ Outputable (ToL ast GhcPs )
277
+ )
278
+ => ASTElement ast where
279
+ -- | This is to absorb the implementation difference of 'LPat',
280
+ -- which is equal to Located Pat in 8.6 and 8.10, but
281
+ -- is isomorphic to Pat in 8.8.
282
+ type ToL ast p = (r :: * ) | r -> ast
283
+ type ToL ast p = Located (ast p )
284
+ withL :: SrcSpan -> ast GhcPs -> ToL ast GhcPs
285
+ default withL
286
+ :: ToL ast GhcPs ~ Located (ast GhcPs )
287
+ => SrcSpan -> ast GhcPs -> ToL ast GhcPs
288
+ withL = L
289
+ toLocated :: ToL ast GhcPs -> Located (ast GhcPs )
290
+ default toLocated
291
+ :: ToL ast GhcPs ~ Located (ast GhcPs ) => ToL ast GhcPs -> Located (ast GhcPs )
292
+ toLocated = id
293
+ unLocated :: ToL ast GhcPs -> ast GhcPs
294
+ default unLocated
295
+ :: ToL ast GhcPs ~ Located (ast GhcPs ) => ToL ast GhcPs -> ast GhcPs
296
+ unLocated = unLoc
297
+ location :: ToL ast GhcPs -> SrcSpan
298
+ location = SrcLoc. getLoc . toLocated
299
+
300
+ parseAST :: Parser (ToL ast GhcPs )
301
+ maybeParensAST :: ToL ast GhcPs -> ToL ast GhcPs
302
+
303
+ instance ASTElement HsExpr where
304
+ type ToL HsExpr p = LHsExpr p
272
305
parseAST = parseExpr
273
306
maybeParensAST = parenthesize
274
307
275
- instance p ~ GhcPs => ASTElement (Pat p ) where
308
+ instance ASTElement Pat where
309
+ type ToL Pat p = LPat p
310
+ #if __GLASGOW_HASKELL__ == 808
311
+ toLocated p@ (XPat (L loc _))= L loc p
312
+ toLocated p = L noSrcSpan p
313
+ unLocated = id
314
+ withL = flip const
315
+ #else
316
+ toLocated = id
317
+ unLocated = unLoc
318
+ #endif
319
+
276
320
parseAST = parsePattern
277
321
maybeParensAST = parenthesizePat appPrec
278
322
279
- instance p ~ GhcPs => ASTElement (HsType p ) where
323
+
324
+ instance ASTElement HsType where
325
+ type ToL HsType p = LHsType p
280
326
parseAST = parseType
281
327
maybeParensAST = parenthesizeHsType appPrec
282
328
283
- instance p ~ GhcPs => ASTElement (HsDecl p ) where
329
+ instance ASTElement HsDecl where
330
+ type ToL HsDecl p = LHsDecl p
284
331
parseAST = parseDecl
285
332
maybeParensAST = id
286
333
@@ -295,12 +342,17 @@ fixAnns ParsedModule {..} =
295
342
------------------------------------------------------------------------------
296
343
297
344
-- | Given an 'LHSExpr', compute its exactprint annotations.
298
- annotate :: ASTElement ast => DynFlags -> Located ast -> TransformT (Either String ) (Anns , Located ast )
345
+ annotate
346
+ :: forall ast . ASTElement ast
347
+ => DynFlags -> ToL ast GhcPs
348
+ -> TransformT (Either String ) (Anns , ToL ast GhcPs )
299
349
annotate dflags ast = do
300
350
uniq <- show <$> uniqueSrcSpanT
301
351
let rendered = render dflags ast
302
352
(anns, expr') <- lift $ either (Left . show ) Right $ parseAST dflags uniq rendered
303
- let anns' = setPrecedingLines expr' 0 1 anns
353
+ let anns' = setPrecedingLines
354
+ (toLocated expr' :: Located (ast GhcPs ))
355
+ 0 1 anns
304
356
pure (anns', expr')
305
357
306
358
-- | Given an 'LHsDecl', compute its exactprint annotations.
0 commit comments