Skip to content
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

Fix stripping of the leading shebang line #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions src/Refact/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,7 @@ parseModuleWithArgs libdir (es, ds) fp = ghcWrapper libdir $ do
liftIO $ writeIORef' dynFlagsRef (Just flags)
res <- parseModuleEpAnnsWithCppInternal defaultCppOptions flags fp

-- pure $ postParseTransform res rigidLayout
case postParseTransform res of
case postParseTransform' res of
Left e -> pure (Left e)
Right ast -> pure $ Right (makeDeltaAst ast)

Expand Down Expand Up @@ -769,3 +768,25 @@ readExtension s = flagSpecFlag <$> find ((== s) . flagSpecName) xFlags
dynFlagsRef :: IORef (Maybe GHC.DynFlags)
dynFlagsRef = unsafePerformIO $ newIORef Nothing
{-# NOINLINE dynFlagsRef #-}

-- A wrapper around @ghc-exactprint@'s @postParseTransform@, that prepends injected
-- comments to the beginning of the parsed module before passing it to the underlying
-- @postParseTransform@, which ignores them.
postParseTransform' :: Either a ([GHC.LEpaComment], GHC.DynFlags, GHC.ParsedSource)
-> Either a GHC.ParsedSource
postParseTransform' = postParseTransform . fmap attachCommentsToSource
where
attachCommentsToSource (cm, df, ps)
| GHC.L s m <- ps
, GHC.HsModule { hsmodExt = ext } <- m
, GHC.XModulePs { hsmodAnn = ann } <- ext
, GHC.EpAnn { comments = comments } <- ann =
let ann' = ann { GHC.comments = prepredEpaComments cm comments }
ext' = ext { GHC.hsmodAnn = ann' }
m' = m { GHC.hsmodExt = ext' }
in ([], df, GHC.L s m')
| otherwise = (cm, df, ps)

prepredEpaComments cm = \case
GHC.EpaCommentsBalanced p f -> GHC.EpaCommentsBalanced (cm ++ p) f
GHC.EpaComments f -> GHC.EpaCommentsBalanced cm f
8 changes: 8 additions & 0 deletions tests/examples/T140A.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env cabal

{- cabal:
build-depends: base
-}

main :: IO ()
main = print $ "test"
8 changes: 8 additions & 0 deletions tests/examples/T140A.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env cabal

{- cabal:
build-depends: base
-}

main :: IO ()
main = print "test"
1 change: 1 addition & 0 deletions tests/examples/T140A.hs.refact
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[("T140A.hs:8:14: Suggestion: Redundant $\nFound:\n print $ \"test\"\nPerhaps:\n print \"test\"\n",[Replace {rtype = Expr, pos = SrcSpan {startLine = 8, startCol = 8, endLine = 8, endCol = 22}, subts = [("a",SrcSpan {startLine = 8, startCol = 8, endLine = 8, endCol = 13}),("b",SrcSpan {startLine = 8, startCol = 16, endLine = 8, endCol = 22})], orig = "a b"}])]
10 changes: 10 additions & 0 deletions tests/examples/T140B.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env cabal

module Main where

{- cabal:
build-depends: base
-}

main :: IO ()
main = print $ "test"
10 changes: 10 additions & 0 deletions tests/examples/T140B.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env cabal

module Main where

{- cabal:
build-depends: base
-}

main :: IO ()
main = print "test"
1 change: 1 addition & 0 deletions tests/examples/T140B.hs.refact
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[("T140B.hs:10:14: Suggestion: Redundant $\nFound:\n print $ \"test\"\nPerhaps:\n print \"test\"\n",[Replace {rtype = Expr, pos = SrcSpan {startLine = 10, startCol = 8, endLine = 10, endCol = 22}, subts = [("a",SrcSpan {startLine = 10, startCol = 8, endLine = 10, endCol = 13}),("b",SrcSpan {startLine = 10, startCol = 16, endLine = 10, endCol = 22})], orig = "a b"}])]