@@ -51,8 +51,6 @@ import "ghc-lib-parser" GHC.LanguageExtensions (Extension)
5151import "ghc" HscTypes as RealGHC.HscTypes (hsc_dflags ,
5252 ms_hspp_opts )
5353import Language.Haskell.GhclibParserEx.GHC.Driver.Session as GhclibParserEx (readExtension )
54- import System.Environment (setEnv ,
55- unsetEnv )
5654import System.FilePath (takeFileName )
5755import System.IO (IOMode (WriteMode ),
5856 hClose ,
@@ -86,6 +84,8 @@ import qualified Language.LSP.Types.Lens as LSP
8684import GHC.Generics (Generic )
8785import Text.Regex.TDFA.Text ()
8886
87+ import System.Environment (setEnv ,
88+ unsetEnv )
8989-- ---------------------------------------------------------------------
9090
9191descriptor :: PluginId -> PluginDescriptor IdeState
@@ -385,36 +385,27 @@ applyHint ide nfp mhint =
385385 oldContent <- maybe (liftIO $ T. readFile fp) return mbOldContent
386386 (modsum, _) <- liftIO $ runAction' $ use_ GetModSummary nfp
387387 let dflags = ms_hspp_opts modsum
388+ -- Setting a environment variable with the libdir used by ghc-exactprint.
389+ -- It is a workaround for an error caused by the use of a hadcoded at compile time libdir
390+ -- in ghc-exactprint that makes dependent executables non portables.
391+ -- See https://github.com/alanz/ghc-exactprint/issues/96.
392+ -- WARNING: this code is not thread safe, so if you try to apply several async refactorings
393+ -- it could fail. That case is not very likely so we assume the risk.
394+ let withRuntimeLibdir :: IO a -> IO a
395+ withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key)
396+ where key = " GHC_EXACTPRINT_GHC_LIBDIR"
388397 -- set Nothing as "position" for "applyRefactorings" because
389398 -- applyRefactorings expects the provided position to be _within_ the scope
390399 -- of each refactoring it will apply.
391400 -- But "Idea"s returned by HLint point to starting position of the expressions
392401 -- that contain refactorings, so they are often outside the refactorings' boundaries.
393- -- Example:
394- -- Given an expression "hlintTest = reid $ (myid ())"
395- -- Hlint returns an idea at the position (1,13)
396- -- That contains "Redundant brackets" refactoring at position (1,20):
397- --
398- -- [("src/App/Test.hs:5:13: Warning: Redundant bracket\nFound:\n reid $ (myid ())\nWhy not:\n reid $ myid ()\n",[Replace {rtype = Expr, pos = SrcSpan {startLine = 5, startCol = 20, endLine = 5, endCol = 29}, subts = [("x",SrcSpan {startLine = 5, startCol = 21, endLine = 5, endCol = 28})], orig = "x"}])]
399- --
400- -- If we provide "applyRefactorings" with "Just (1,13)" then
401- -- the "Redundant bracket" hint will never be executed
402- -- because SrcSpan (1,20,??,??) doesn't contain position (1,13).
402+ let position = Nothing
403403#ifdef HLINT_ON_GHC_LIB
404404 let writeFileUTF8NoNewLineTranslation file txt =
405405 withFile file WriteMode $ \ h -> do
406406 hSetEncoding h utf8
407407 hSetNewlineMode h noNewlineTranslation
408408 hPutStr h (T. unpack txt)
409- -- Setting a environment variable with the libdir used by ghc-exactprint.
410- -- It is a workaround for an error caused by the use of a hadcoded at compile time libdir
411- -- in ghc-exactprint that makes dependent executables non portables.
412- -- See https://github.com/alanz/ghc-exactprint/issues/96.
413- -- WARNING: this code is not thread safe, so if you try to apply several async refactorings
414- -- it could fail. That case is not very likely so we assume the risk.
415- let withRuntimeLibdir :: IO a -> IO a
416- withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key)
417- where key = " GHC_EXACTPRINT_GHC_LIBDIR"
418409 res <-
419410 liftIO $ withSystemTempFile (takeFileName fp) $ \ temp h -> do
420411 hClose h
@@ -424,7 +415,7 @@ applyHint ide nfp mhint =
424415 -- We have to reparse extensions to remove the invalid ones
425416 let (enabled, disabled, _invalid) = parseExtensions $ map show exts
426417 let refactExts = map show $ enabled ++ disabled
427- (Right <$> withRuntimeLibdir (applyRefactorings Nothing commands temp refactExts))
418+ (Right <$> withRuntimeLibdir (applyRefactorings position commands temp refactExts))
428419 `catches` errorHandlers
429420#else
430421 mbParsedModule <- liftIO $ runAction' $ getParsedModuleWithComments nfp
@@ -438,7 +429,7 @@ applyHint ide nfp mhint =
438429 let rigidLayout = deltaOptions RigidLayout
439430 (anns', modu') <-
440431 ExceptT $ return $ postParseTransform (Right (anns, [] , dflags, modu)) rigidLayout
441- liftIO $ (Right <$> applyRefactorings' Nothing commands anns' modu')
432+ liftIO $ (Right <$> withRuntimeLibdir ( applyRefactorings' position commands anns' modu') )
442433 `catches` errorHandlers
443434#endif
444435 case res of
0 commit comments