-
-
Notifications
You must be signed in to change notification settings - Fork 391
Use apply-refact 0.12.0 #3469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Use apply-refact 0.12.0 #3469
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -565,15 +565,7 @@ applyHint recorder ide nfp mhint = | |
oldContent <- maybe (liftIO $ fmap T.decodeUtf8 (BS.readFile fp)) return mbOldContent | ||
modsum <- liftIO $ runAction' $ use_ GetModSummary nfp | ||
let dflags = ms_hspp_opts $ msrModSummary modsum | ||
-- Setting a environment variable with the libdir used by ghc-exactprint. | ||
-- It is a workaround for an error caused by the use of a hardcoded at compile time libdir | ||
-- in ghc-exactprint that makes dependent executables non portables. | ||
-- See https://github.com/alanz/ghc-exactprint/issues/96. | ||
-- WARNING: this code is not thread safe, so if you try to apply several async refactorings | ||
-- it could fail. That case is not very likely so we assume the risk. | ||
let withRuntimeLibdir :: IO a -> IO a | ||
withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key) | ||
where key = "GHC_EXACTPRINT_GHC_LIBDIR" | ||
|
||
-- set Nothing as "position" for "applyRefactorings" because | ||
-- applyRefactorings expects the provided position to be _within_ the scope | ||
-- of each refactoring it will apply. | ||
|
@@ -594,7 +586,7 @@ applyHint recorder ide nfp mhint = | |
-- We have to reparse extensions to remove the invalid ones | ||
let (enabled, disabled, _invalid) = Refact.parseExtensions $ map show exts | ||
let refactExts = map show $ enabled ++ disabled | ||
(Right <$> withRuntimeLibdir (Refact.applyRefactorings position commands temp refactExts)) | ||
(Right <$> applyRefactorings (topDir dflags) position commands temp refactExts) | ||
`catches` errorHandlers | ||
#else | ||
mbParsedModule <- liftIO $ runAction' $ getParsedModuleWithComments nfp | ||
|
@@ -609,7 +601,7 @@ applyHint recorder ide nfp mhint = | |
(anns', modu') <- | ||
ExceptT $ mapM (uncurry Refact.applyFixities) | ||
$ postParseTransform (Right (anns, [], dflags, modu)) rigidLayout | ||
liftIO $ (Right <$> withRuntimeLibdir (Refact.applyRefactorings' position commands anns' modu')) | ||
liftIO $ (Right <$> Refact.applyRefactorings' position commands anns' modu') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is curious, this code-path is definitely not exercised for GHC >= 9.2, since the API in apply-refact has changed in 0.11.0. Additionally, I don't think we need the hack |
||
`catches` errorHandlers | ||
#endif | ||
case res of | ||
|
@@ -641,3 +633,55 @@ bimapExceptT f g (ExceptT m) = ExceptT (fmap h m) where | |
h (Left e) = Left (f e) | ||
h (Right a) = Right (g a) | ||
{-# INLINE bimapExceptT #-} | ||
|
||
-- --------------------------------------------------------------------------- | ||
-- Apply-refact compatability, documentation copied from upstream apply-refact | ||
-- --------------------------------------------------------------------------- | ||
|
||
-- | Apply a set of refactorings as supplied by HLint | ||
-- | ||
-- This compatibility function abstracts over https://github.com/mpickering/apply-refact/issues/133 | ||
-- for backwards compatability. | ||
applyRefactorings :: | ||
-- | FilePath to [GHC's libdir](https://downloads.haskell.org/ghc/latest/docs/users_guide/using.html#ghc-flag---print-libdir). | ||
-- | ||
-- It is possible to use @libdir@ from [ghc-paths package](https://hackage.haskell.org/package/ghc-paths), but note | ||
-- this will make it difficult to provide a binary distribution of your program. | ||
FilePath -> | ||
-- | Apply hints relevant to a specific position | ||
Maybe (Int, Int) -> | ||
-- | 'Refactoring's to apply. Each inner list corresponds to an HLint | ||
-- <https://hackage.haskell.org/package/hlint/docs/Language-Haskell-HLint.html#t:Idea Idea>. | ||
-- An @Idea@ may have more than one 'Refactoring'. | ||
-- | ||
-- The @Idea@s are sorted in ascending order of starting location, and are applied | ||
-- in that order. If two @Idea@s start at the same location, the one with the larger | ||
-- source span comes first. An @Idea@ is filtered out (ignored) if there is an @Idea@ | ||
-- prior to it which has an overlapping source span and is not filtered out. | ||
[[Refact.Refactoring Refact.SrcSpan]] -> | ||
-- | Target file | ||
FilePath -> | ||
-- | GHC extensions, e.g., @LambdaCase@, @NoStarIsType@. The list is processed from left | ||
-- to right. An extension (e.g., @StarIsType@) may be overridden later (e.g., by @NoStarIsType@). | ||
-- | ||
-- These are in addition to the @LANGUAGE@ pragmas in the target file. When they conflict | ||
-- with the @LANGUAGE@ pragmas, pragmas win. | ||
[String] -> | ||
IO String | ||
applyRefactorings = | ||
#if MIN_VERSION_apply_refact(0,12,0) | ||
Refact.applyRefactorings | ||
#else | ||
\libdir pos refacts fp exts -> withRuntimeLibdir libdir (Refact.applyRefactorings pos refacts fp exts) | ||
|
||
where | ||
-- Setting a environment variable with the libdir used by ghc-exactprint. | ||
-- It is a workaround for an error caused by the use of a hardcoded at compile time libdir | ||
-- in ghc-exactprint that makes dependent executables non portables. | ||
-- See https://github.com/alanz/ghc-exactprint/issues/96. | ||
-- WARNING: this code is not thread safe, so if you try to apply several async refactorings | ||
-- it could fail. That case is not very likely so we assume the risk. | ||
withRuntimeLibdir :: FilePath -> IO a -> IO a | ||
withRuntimeLibdir libdir = bracket_ (setEnv key libdir) (unsetEnv key) | ||
where key = "GHC_EXACTPRINT_GHC_LIBDIR" | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.