Skip to content

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 2 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ package *

write-ghc-environment-files: never

index-state: 2023-01-10T00:00:00Z
index-state: 2023-01-27T00:00:00Z

constraints:
-- For GHC 9.4, older versions of entropy fail to build on Windows
Expand Down
5 changes: 1 addition & 4 deletions plugins/hls-hlint-plugin/hls-hlint-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ library
, unordered-containers
, ghc-lib-parser
, ghc-lib-parser-ex
if impl(ghc >= 9.2)
build-depends: apply-refact ^>= 0.11.0.0
else
build-depends: apply-refact ^>= 0.9.0.0
, apply-refact

cpp-options: -DHLINT_ON_GHC_LIB
ghc-options:
Expand Down
66 changes: 55 additions & 11 deletions plugins/hls-hlint-plugin/src/Ide/Plugin/Hlint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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')
Copy link
Collaborator Author

@fendor fendor Jan 27, 2023

Choose a reason for hiding this comment

The 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 withRuntimeLibdir here because the libdir is only needed for parsing and we pass in a module already.

`catches` errorHandlers
#endif
case res of
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ library
, hls-plugin-api ^>=1.3 || ^>=1.4 || ^>= 1.5 || ^>= 1.6
, lens
, lsp
, ormolu ^>=0.1.2 || ^>= 0.2 || ^>= 0.3 || ^>= 0.5
-- we are incompatible with 0.5.3.
-- See upstream ticket: https://github.com/tweag/ormolu/issues/981
, ormolu ^>=0.1.2 || ^>= 0.2 || ^>= 0.3 || (>= 0.5 && < 0.5.3)
, text

default-language: Haskell2010
Expand Down