@@ -565,15 +565,7 @@ applyHint recorder ide nfp mhint =
565
565
oldContent <- maybe (liftIO $ fmap T. decodeUtf8 (BS. readFile fp)) return mbOldContent
566
566
modsum <- liftIO $ runAction' $ use_ GetModSummary nfp
567
567
let dflags = ms_hspp_opts $ msrModSummary modsum
568
- -- Setting a environment variable with the libdir used by ghc-exactprint.
569
- -- It is a workaround for an error caused by the use of a hardcoded at compile time libdir
570
- -- in ghc-exactprint that makes dependent executables non portables.
571
- -- See https://github.com/alanz/ghc-exactprint/issues/96.
572
- -- WARNING: this code is not thread safe, so if you try to apply several async refactorings
573
- -- it could fail. That case is not very likely so we assume the risk.
574
- let withRuntimeLibdir :: IO a -> IO a
575
- withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key)
576
- where key = " GHC_EXACTPRINT_GHC_LIBDIR"
568
+
577
569
-- set Nothing as "position" for "applyRefactorings" because
578
570
-- applyRefactorings expects the provided position to be _within_ the scope
579
571
-- of each refactoring it will apply.
@@ -594,7 +586,7 @@ applyHint recorder ide nfp mhint =
594
586
-- We have to reparse extensions to remove the invalid ones
595
587
let (enabled, disabled, _invalid) = Refact. parseExtensions $ map show exts
596
588
let refactExts = map show $ enabled ++ disabled
597
- (Right <$> withRuntimeLibdir ( Refact. applyRefactorings position commands temp refactExts) )
589
+ (Right <$> applyRefactorings (topDir dflags) position commands temp refactExts)
598
590
`catches` errorHandlers
599
591
#else
600
592
mbParsedModule <- liftIO $ runAction' $ getParsedModuleWithComments nfp
@@ -609,7 +601,7 @@ applyHint recorder ide nfp mhint =
609
601
(anns', modu') <-
610
602
ExceptT $ mapM (uncurry Refact. applyFixities)
611
603
$ postParseTransform (Right (anns, [] , dflags, modu)) rigidLayout
612
- liftIO $ (Right <$> withRuntimeLibdir ( Refact. applyRefactorings' position commands anns' modu') )
604
+ liftIO $ (Right <$> Refact. applyRefactorings' position commands anns' modu')
613
605
`catches` errorHandlers
614
606
#endif
615
607
case res of
@@ -641,3 +633,55 @@ bimapExceptT f g (ExceptT m) = ExceptT (fmap h m) where
641
633
h (Left e) = Left (f e)
642
634
h (Right a) = Right (g a)
643
635
{-# INLINE bimapExceptT #-}
636
+
637
+ -- ---------------------------------------------------------------------------
638
+ -- Apply-refact compatability, documentation copied from upstream apply-refact
639
+ -- ---------------------------------------------------------------------------
640
+
641
+ -- | Apply a set of refactorings as supplied by HLint
642
+ --
643
+ -- This compatibility function abstracts over https://github.com/mpickering/apply-refact/issues/133
644
+ -- for backwards compatability.
645
+ applyRefactorings ::
646
+ -- | FilePath to [GHC's libdir](https://downloads.haskell.org/ghc/latest/docs/users_guide/using.html#ghc-flag---print-libdir).
647
+ --
648
+ -- It is possible to use @libdir@ from [ghc-paths package](https://hackage.haskell.org/package/ghc-paths), but note
649
+ -- this will make it difficult to provide a binary distribution of your program.
650
+ FilePath ->
651
+ -- | Apply hints relevant to a specific position
652
+ Maybe (Int , Int ) ->
653
+ -- | 'Refactoring's to apply. Each inner list corresponds to an HLint
654
+ -- <https://hackage.haskell.org/package/hlint/docs/Language-Haskell-HLint.html#t:Idea Idea>.
655
+ -- An @Idea@ may have more than one 'Refactoring'.
656
+ --
657
+ -- The @Idea@s are sorted in ascending order of starting location, and are applied
658
+ -- in that order. If two @Idea@s start at the same location, the one with the larger
659
+ -- source span comes first. An @Idea@ is filtered out (ignored) if there is an @Idea@
660
+ -- prior to it which has an overlapping source span and is not filtered out.
661
+ [[Refact. Refactoring Refact. SrcSpan ]] ->
662
+ -- | Target file
663
+ FilePath ->
664
+ -- | GHC extensions, e.g., @LambdaCase@, @NoStarIsType@. The list is processed from left
665
+ -- to right. An extension (e.g., @StarIsType@) may be overridden later (e.g., by @NoStarIsType@).
666
+ --
667
+ -- These are in addition to the @LANGUAGE@ pragmas in the target file. When they conflict
668
+ -- with the @LANGUAGE@ pragmas, pragmas win.
669
+ [String ] ->
670
+ IO String
671
+ applyRefactorings =
672
+ #if MIN_VERSION_apply_refact(0,12,0)
673
+ Refact. applyRefactorings
674
+ #else
675
+ \ libdir pos refacts fp exts -> withRuntimeLibdir libdir (Refact. applyRefactorings pos refacts fp exts)
676
+
677
+ where
678
+ -- Setting a environment variable with the libdir used by ghc-exactprint.
679
+ -- It is a workaround for an error caused by the use of a hardcoded at compile time libdir
680
+ -- in ghc-exactprint that makes dependent executables non portables.
681
+ -- See https://github.com/alanz/ghc-exactprint/issues/96.
682
+ -- WARNING: this code is not thread safe, so if you try to apply several async refactorings
683
+ -- it could fail. That case is not very likely so we assume the risk.
684
+ withRuntimeLibdir :: FilePath -> IO a -> IO a
685
+ withRuntimeLibdir libdir = bracket_ (setEnv key libdir) (unsetEnv key)
686
+ where key = " GHC_EXACTPRINT_GHC_LIBDIR"
687
+ #endif
0 commit comments