From 1968a60265087851918c816f789856e85947b1ce Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Thu, 4 May 2023 10:47:54 +0900 Subject: [PATCH 1/2] Let language extensions be effective over CPP blocks --- TESTS.md | 11 +++++++++++ src/HIndent.hs | 19 ++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/TESTS.md b/TESTS.md index 7fb83994b..bec1fb820 100644 --- a/TESTS.md +++ b/TESTS.md @@ -3397,6 +3397,17 @@ Escaped newlines x ``` +Language extensions are effective across CPP boundaries. + +```haskell +{-# LANGUAGE PatternSynonyms #-} +#if 1 +pattern Foo :: Int -> Bar +#else +pattern Foo :: Int -> Bar +#endif +``` + ## Literate Haskell Code with `>`s diff --git a/src/HIndent.hs b/src/HIndent.hs index e40fe65fa..40b6b1b10 100644 --- a/src/HIndent.hs +++ b/src/HIndent.hs @@ -110,9 +110,10 @@ reformat :: -> Maybe FilePath -> ByteString -> Either ParseError ByteString -reformat config mexts mfilepath = +reformat config mexts mfilepath rawCode = preserveTrailingNewline (fmap (mconcat . intersperse "\n") . mapM processBlock . cppSplitBlocks) + rawCode where processBlock :: CodeBlock -> Either ParseError ByteString processBlock (Shebang text) = Right text @@ -121,12 +122,6 @@ reformat config mexts mfilepath = let ls = S8.lines text prefix = findPrefix ls code = unlines' (map (stripPrefix prefix) ls) - allExts = - CE.uniqueExtensions $ - concatMap (\x -> x : extensionImplies x) $ - mexts ++ - configExtensions config ++ - collectLanguageExtensionsFromSource (UTF8.toString code) in case parseModule mfilepath allExts (UTF8.toString code) of POk _ m -> Right $ @@ -176,6 +171,16 @@ reformat config mexts mfilepath = else x' <> "\n") (f x) | otherwise = f x + allExts = + CE.uniqueExtensions $ + concatMap (\x -> x : extensionImplies x) $ + mexts ++ configExtensions config ++ allExtsFromCode + allExtsFromCode = concatMap f codeBlocks + where + f (HaskellSource _ text) = + collectLanguageExtensionsFromSource $ UTF8.toString text + f _ = [] + codeBlocks = cppSplitBlocks rawCode -- | Generate an AST from the given module for debugging. testAst :: ByteString -> Either ParseError HsModule' From 12731b7364d1daf954161151f08940af3cd0306a Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Thu, 4 May 2023 10:50:33 +0900 Subject: [PATCH 2/2] Add a changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa3778f29..3f5df3060 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Misplaced comments in do expressions ([#707]). - Misplaced comments in case expressions ([#708]). - The bug of extensions specified via pragmas not enabling implied ones ([#727]). +- Language extensions enabled via pragmas were not effective across CPP boundaries ([#731]). ### Removed @@ -348,6 +349,7 @@ This version is accidentally pushlished, and is the same as 5.3.3. [@uhbif19]: https://github.com/uhbif19 [@toku-sa-n]: https://github.com/toku-sa-n +[#731]: https://github.com/mihaimaruseac/hindent/pull/731 [#729]: https://github.com/mihaimaruseac/hindent/pull/729 [#728]: https://github.com/mihaimaruseac/hindent/pull/728 [#727]: https://github.com/mihaimaruseac/hindent/pull/727