Previously, if the input did not end with a newline
character and config had 'ppconf_hackAroundIncludes = True'
then due to using unlines/lines the output contained an
additional newline.
remotes/eborden/dev
Lennart Spitzner 2017-09-10 16:37:35 +02:00
parent 0e7adeef5f
commit 9703ebead5
3 changed files with 23 additions and 6 deletions

View File

@ -198,7 +198,7 @@ coreIO putErrorLnIO config suppressOutput inputPathM outputPathM = EitherT.runEi
-- TODO: refactor this hack to not be mixed into parsing logic -- TODO: refactor this hack to not be mixed into parsing logic
let hackF s = if "#include" `isPrefixOf` s then "-- BRITTANY_INCLUDE_HACK " ++ s else s let hackF s = if "#include" `isPrefixOf` s then "-- BRITTANY_INCLUDE_HACK " ++ s else s
let hackTransform = let hackTransform =
if hackAroundIncludes && not exactprintOnly then List.unlines . fmap hackF . List.lines else id if hackAroundIncludes && not exactprintOnly then List.intercalate "\n" . fmap hackF . lines' else id
inputString <- liftIO $ System.IO.hGetContents System.IO.stdin inputString <- liftIO $ System.IO.hGetContents System.IO.stdin
liftIO $ parseModuleFromString ghcOptions "stdin" cppCheckFunc (hackTransform inputString) liftIO $ parseModuleFromString ghcOptions "stdin" cppCheckFunc (hackTransform inputString)
Just p -> liftIO $ parseModule ghcOptions p cppCheckFunc Just p -> liftIO $ parseModule ghcOptions p cppCheckFunc
@ -221,7 +221,9 @@ coreIO putErrorLnIO config suppressOutput inputPathM outputPathM = EitherT.runEi
then return $ pPrintModule config anns parsedSource then return $ pPrintModule config anns parsedSource
else liftIO $ pPrintModuleAndCheck config anns parsedSource else liftIO $ pPrintModuleAndCheck config anns parsedSource
let hackF s = fromMaybe s $ TextL.stripPrefix (TextL.pack "-- BRITTANY_INCLUDE_HACK ") s let hackF s = fromMaybe s $ TextL.stripPrefix (TextL.pack "-- BRITTANY_INCLUDE_HACK ") s
pure $ if hackAroundIncludes then (ews, TextL.unlines $ fmap hackF $ TextL.lines outRaw) else (ews, outRaw) pure $ if hackAroundIncludes
then (ews, TextL.intercalate (TextL.pack "\n") $ fmap hackF $ TextL.splitOn (TextL.pack "\n") outRaw)
else (ews, outRaw)
let customErrOrder ErrorInput{} = 4 let customErrOrder ErrorInput{} = 4
customErrOrder LayoutWarning{} = 0 :: Int customErrOrder LayoutWarning{} = 0 :: Int
customErrOrder ErrorOutputCheck{} = 1 customErrOrder ErrorOutputCheck{} = 1

View File

@ -73,7 +73,7 @@ parsePrintModule configRaw inputText = runEitherT $ do
then "-- BRITTANY_INCLUDE_HACK " ++ s then "-- BRITTANY_INCLUDE_HACK " ++ s
else s else s
let hackTransform = if hackAroundIncludes let hackTransform = if hackAroundIncludes
then List.unlines . fmap hackF . List.lines then List.intercalate "\n" . fmap hackF . lines'
else id else id
let cppCheckFunc dynFlags = if GHC.xopt GHC.Cpp dynFlags let cppCheckFunc dynFlags = if GHC.xopt GHC.Cpp dynFlags
then case cppMode of then case cppMode of
@ -101,7 +101,12 @@ parsePrintModule configRaw inputText = runEitherT $ do
let hackF s = fromMaybe s let hackF s = fromMaybe s
$ TextL.stripPrefix (TextL.pack "-- BRITTANY_INCLUDE_HACK ") s $ TextL.stripPrefix (TextL.pack "-- BRITTANY_INCLUDE_HACK ") s
pure $ if hackAroundIncludes pure $ if hackAroundIncludes
then (ews, TextL.unlines $ fmap hackF $ TextL.lines outRaw) then
( ews
, TextL.intercalate (TextL.pack "\n") $ fmap hackF $ TextL.splitOn
(TextL.pack "\n")
outRaw
)
else (ews, outRaw) else (ews, outRaw)
let customErrOrder ErrorInput{} = 4 let customErrOrder ErrorInput{} = 4
customErrOrder LayoutWarning{} = 0 :: Int customErrOrder LayoutWarning{} = 0 :: Int
@ -115,6 +120,7 @@ parsePrintModule configRaw inputText = runEitherT $ do
if hasErrors then left $ errsWarns else pure $ TextL.toStrict outputTextL if hasErrors then left $ errsWarns else pure $ TextL.toStrict outputTextL
-- BrittanyErrors can be non-fatal warnings, thus both are returned instead -- BrittanyErrors can be non-fatal warnings, thus both are returned instead
-- of an Either. -- of an Either.
-- This should be cleaned up once it is clear what kinds of errors really -- This should be cleaned up once it is clear what kinds of errors really

View File

@ -23,6 +23,7 @@ module Language.Haskell.Brittany.Internal.Utils
, transformDownMay , transformDownMay
, FirstLastView(..) , FirstLastView(..)
, splitFirstLast , splitFirstLast
, lines'
) )
where where
@ -280,3 +281,11 @@ transformDownMay :: Uniplate.Uniplate on => (on -> Maybe on) -> (on -> on)
transformDownMay f = g where g x = maybe x (Uniplate.descend g) $ f x transformDownMay f = g where g x = maybe x (Uniplate.descend g) $ f x
_transformDownRec :: Uniplate.Uniplate on => (on -> Maybe on) -> (on -> on) _transformDownRec :: Uniplate.Uniplate on => (on -> Maybe on) -> (on -> on)
_transformDownRec f = g where g x = maybe (Uniplate.descend g x) g $ f x _transformDownRec f = g where g x = maybe (Uniplate.descend g x) g $ f x
-- | similar to List.lines, but treating the case of final newline character
-- in such a manner that this function is the inverse of @intercalate "\n"@.
lines' :: String -> [String]
lines' s = case break (== '\n') s of
(s1, []) -> [s1]
(s1, [_]) -> [s1, ""]
(s1, (_:r)) -> s1 : lines' r