@@ -51,8 +51,6 @@ import "ghc-lib-parser" GHC.LanguageExtensions (Extension)
51
51
import "ghc" HscTypes as RealGHC.HscTypes (hsc_dflags ,
52
52
ms_hspp_opts )
53
53
import Language.Haskell.GhclibParserEx.GHC.Driver.Session as GhclibParserEx (readExtension )
54
- import System.Environment (setEnv ,
55
- unsetEnv )
56
54
import System.FilePath (takeFileName )
57
55
import System.IO (IOMode (WriteMode ),
58
56
hClose ,
@@ -86,6 +84,8 @@ import qualified Language.LSP.Types.Lens as LSP
86
84
import GHC.Generics (Generic )
87
85
import Text.Regex.TDFA.Text ()
88
86
87
+ import System.Environment (setEnv ,
88
+ unsetEnv )
89
89
-- ---------------------------------------------------------------------
90
90
91
91
descriptor :: PluginId -> PluginDescriptor IdeState
@@ -380,36 +380,27 @@ applyHint ide nfp mhint =
380
380
oldContent <- maybe (liftIO $ T. readFile fp) return mbOldContent
381
381
(modsum, _) <- liftIO $ runAction' $ use_ GetModSummary nfp
382
382
let dflags = ms_hspp_opts modsum
383
+ -- Setting a environment variable with the libdir used by ghc-exactprint.
384
+ -- It is a workaround for an error caused by the use of a hadcoded at compile time libdir
385
+ -- in ghc-exactprint that makes dependent executables non portables.
386
+ -- See https://github.com/alanz/ghc-exactprint/issues/96.
387
+ -- WARNING: this code is not thread safe, so if you try to apply several async refactorings
388
+ -- it could fail. That case is not very likely so we assume the risk.
389
+ let withRuntimeLibdir :: IO a -> IO a
390
+ withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key)
391
+ where key = " GHC_EXACTPRINT_GHC_LIBDIR"
383
392
-- set Nothing as "position" for "applyRefactorings" because
384
393
-- applyRefactorings expects the provided position to be _within_ the scope
385
394
-- of each refactoring it will apply.
386
395
-- But "Idea"s returned by HLint point to starting position of the expressions
387
396
-- that contain refactorings, so they are often outside the refactorings' boundaries.
388
- -- Example:
389
- -- Given an expression "hlintTest = reid $ (myid ())"
390
- -- Hlint returns an idea at the position (1,13)
391
- -- That contains "Redundant brackets" refactoring at position (1,20):
392
- --
393
- -- [("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"}])]
394
- --
395
- -- If we provide "applyRefactorings" with "Just (1,13)" then
396
- -- the "Redundant bracket" hint will never be executed
397
- -- because SrcSpan (1,20,??,??) doesn't contain position (1,13).
397
+ let position = Nothing
398
398
#ifdef HLINT_ON_GHC_LIB
399
399
let writeFileUTF8NoNewLineTranslation file txt =
400
400
withFile file WriteMode $ \ h -> do
401
401
hSetEncoding h utf8
402
402
hSetNewlineMode h noNewlineTranslation
403
403
hPutStr h (T. unpack txt)
404
- -- Setting a environment variable with the libdir used by ghc-exactprint.
405
- -- It is a workaround for an error caused by the use of a hadcoded at compile time libdir
406
- -- in ghc-exactprint that makes dependent executables non portables.
407
- -- See https://github.com/alanz/ghc-exactprint/issues/96.
408
- -- WARNING: this code is not thread safe, so if you try to apply several async refactorings
409
- -- it could fail. That case is not very likely so we assume the risk.
410
- let withRuntimeLibdir :: IO a -> IO a
411
- withRuntimeLibdir = bracket_ (setEnv key $ topDir dflags) (unsetEnv key)
412
- where key = " GHC_EXACTPRINT_GHC_LIBDIR"
413
404
res <-
414
405
liftIO $ withSystemTempFile (takeFileName fp) $ \ temp h -> do
415
406
hClose h
@@ -419,7 +410,7 @@ applyHint ide nfp mhint =
419
410
-- We have to reparse extensions to remove the invalid ones
420
411
let (enabled, disabled, _invalid) = parseExtensions $ map show exts
421
412
let refactExts = map show $ enabled ++ disabled
422
- (Right <$> withRuntimeLibdir (applyRefactorings Nothing commands temp refactExts))
413
+ (Right <$> withRuntimeLibdir (applyRefactorings position commands temp refactExts))
423
414
`catches` errorHandlers
424
415
#else
425
416
mbParsedModule <- liftIO $ runAction' $ getParsedModuleWithComments nfp
@@ -433,7 +424,7 @@ applyHint ide nfp mhint =
433
424
let rigidLayout = deltaOptions RigidLayout
434
425
(anns', modu') <-
435
426
ExceptT $ return $ postParseTransform (Right (anns, [] , dflags, modu)) rigidLayout
436
- liftIO $ (Right <$> applyRefactorings' Nothing commands anns' modu')
427
+ liftIO $ (Right <$> withRuntimeLibdir ( applyRefactorings' position commands anns' modu') )
437
428
`catches` errorHandlers
438
429
#endif
439
430
case res of
0 commit comments