on master help sign in
Respect the ‘ImportQualifiedPost’ language extension

It is tempting to enable ‘ImportQualifiedPost’ automatically for all files, but in that case we won't be able to tell when to format in the classical (prefix) style vs the new postfix style. Checking the list of pragmas on per-file basis is not good enough because the extension can be enabled in other ways, such as via the --ghc-opt flag.

commit100fa44dbbb011f547a86b6f2872493a0a4eaad2 authorUtku Demir <me@utdemir.com> dateSun 29 Mar 2020, 15:45 (7 years ago) parentb0a261f
8 files changed+45−10
data/examples/import/qualified-post-out.hs+5 data/examples/import/qualified-post.hs+5 src/Ormolu/Diff.hs+8 src/Ormolu/Parser.hs+6−2 src/Ormolu/Parser/Result.hs+3−1 src/Ormolu/Printer.hs+6−1 src/Ormolu/Printer/Meat/ImportExport.hs+7−4 src/Ormolu/Printer/Meat/Module.hs+5−2
data/examples/import/qualified-post-out.hsnew file+5
@@ -0,0 +1,5 @@
1+{-# LANGUAGE ImportQualifiedPost #-}
2+
3+import Data.Text qualified as T
4+import Data.Text qualified (a, b, c)
5+import Data.Text qualified hiding (a, b, c)
data/examples/import/qualified-post.hsnew file+5
@@ -0,0 +1,5 @@
1+{-# LANGUAGE ImportQualifiedPost #-}
2+
3+import qualified Data.Text as T
4+import qualified Data.Text (a, c, b)
5+import Data.Text qualified hiding (c, b, a)
src/Ormolu/Diff.hs+8
@@ -71,6 +71,7 @@ matchIgnoringSrcSpans = genericQuery
7171 `extQ` hsModuleEq
7272 `extQ` sourceTextEq
7373 `extQ` hsDocStringEq
74+ `extQ` importDeclQualifiedStyleEq
7475 `ext2Q` forLocated
7576 )
7677 x
@@ -88,6 +89,13 @@ matchIgnoringSrcSpans = genericQuery
8889 hs1 {hsmodImports = sortImports (hsmodImports hs1)}
8990 sourceTextEq :: SourceText -> GenericQ Diff
9091 sourceTextEq _ _ = Same
92+ importDeclQualifiedStyleEq :: ImportDeclQualifiedStyle -> GenericQ Diff
93+ importDeclQualifiedStyleEq d0 d1' =
94+ case (d0, cast d1' :: Maybe ImportDeclQualifiedStyle) of
95+ (x, Just x') | x == x' -> Same
96+ (QualifiedPre, Just QualifiedPost) -> Same
97+ (QualifiedPost, Just QualifiedPre) -> Same
98+ _ -> Different []
9199 hsDocStringEq :: HsDocString -> GenericQ Diff
92100 hsDocStringEq str0 str1' =
93101 case cast str1' :: Maybe HsDocString of
src/Ormolu/Parser.hs+6−2
@@ -101,7 +101,9 @@ parseModule Config {..} path input' = liftIO $ do
101101 prCommentStream = comments,
102102 prExtensions = exts,
103103 prShebangs = shebangs,
104− prUseRecordDot = useRecordDot
104+ prUseRecordDot = useRecordDot,
105+ prImportQualifiedPost =
106+ GHC.xopt ImportQualifiedPost dynFlags
105107 }
106108 return (warnings, r)
107109
@@ -124,9 +126,11 @@ manualExts =
124126 MonadComprehensions,
125127 UnboxedSums,
126128 UnicodeSyntax, -- gives special meanings to operators like (→)
127− TemplateHaskellQuotes -- enables TH subset of quasi-quotes, this
129+ TemplateHaskellQuotes, -- enables TH subset of quasi-quotes, this
128130 -- apparently interferes with QuasiQuotes in
129131 -- weird ways
132+ ImportQualifiedPost -- affects how Ormolu renders imports, so the
133+ -- decision of enabling this style is left to the user
130134 ]
131135
132136----------------------------------------------------------------------------
src/Ormolu/Parser/Result.hs+3−1
@@ -26,7 +26,9 @@ data ParseResult
2626 -- | Shebangs found in the input
2727 prShebangs :: [Located String],
2828 -- | Whether or not record dot syntax is enabled
29− prUseRecordDot :: Bool
29+ prUseRecordDot :: Bool,
30+ -- | Whether or not ImportQualifiedPost is enabled
31+ prImportQualifiedPost :: Bool
3032 }
3133
3234-- | Pretty-print a 'ParseResult'.
src/Ormolu/Printer.hs+6−1
@@ -20,7 +20,12 @@ printModule ::
2020 Text
2121printModule ParseResult {..} =
2222 runR
23− (p_hsModule prShebangs prExtensions prParsedSource)
23+ ( p_hsModule
24+ prShebangs
25+ prExtensions
26+ prImportQualifiedPost
27+ prParsedSource
28+ )
2429 (mkSpanStream prParsedSource)
2530 prCommentStream
2631 prAnns
src/Ormolu/Printer/Meat/ImportExport.hs+7−4
@@ -25,8 +25,8 @@ p_hsmodExports xs =
2525 layout <- getLayout
2626 sep breakpoint (sitcc . located' (uncurry (p_lie layout))) (attachPositions xs)
2727
28−p_hsmodImport :: ImportDecl GhcPs -> R ()
29−p_hsmodImport ImportDecl {..} = do
28+p_hsmodImport :: Bool -> ImportDecl GhcPs -> R ()
29+p_hsmodImport useQualifiedPost ImportDecl {..} = do
3030 txt "import"
3131 space
3232 when ideclSource (txt "{-# SOURCE #-}")
@@ -34,7 +34,7 @@ p_hsmodImport ImportDecl {..} = do
3434 when ideclSafe (txt "safe")
3535 space
3636 when
37− (isImportDeclQualified ideclQualified)
37+ (isImportDeclQualified ideclQualified && not useQualifiedPost)
3838 (txt "qualified")
3939 space
4040 case ideclPkgQual of
@@ -43,6 +43,9 @@ p_hsmodImport ImportDecl {..} = do
4343 space
4444 inci $ do
4545 located ideclName atom
46+ when
47+ (isImportDeclQualified ideclQualified && useQualifiedPost)
48+ (space >> txt "qualified")
4649 case ideclAs of
4750 Nothing -> return ()
4851 Just l -> do
@@ -63,7 +66,7 @@ p_hsmodImport ImportDecl {..} = do
6366 layout <- getLayout
6467 sep breakpoint (sitcc . located' (uncurry (p_lie layout))) (attachPositions xs)
6568 newline
66−p_hsmodImport (XImportDecl x) = noExtCon x
69+p_hsmodImport _ (XImportDecl x) = noExtCon x
6770
6871p_lie :: Layout -> (Int, Int) -> IE GhcPs -> R ()
6972p_lie encLayout (i, totalItems) = \case
src/Ormolu/Printer/Meat/Module.hs+5−2
@@ -1,3 +1,4 @@
1+{-# LANGUAGE LambdaCase #-}
12{-# LANGUAGE OverloadedStrings #-}
23{-# LANGUAGE RecordWildCards #-}
34
@@ -26,10 +27,12 @@ p_hsModule ::
2627 [Located String] ->
2728 -- | Pragmas
2829 [Pragma] ->
30+ -- | Whether to use postfix qualified imports
31+ Bool ->
2932 -- | AST to print
3033 ParsedSource ->
3134 R ()
32−p_hsModule shebangs pragmas (L moduleSpan HsModule {..}) = do
35+p_hsModule shebangs pragmas qualifiedPost (L moduleSpan HsModule {..}) = do
3336 -- If span of exports in multiline, the whole thing is multiline. This is
3437 -- especially important because span of module itself always seems to have
3538 -- length zero, so it's not reliable for layout selection.
@@ -63,7 +66,7 @@ p_hsModule shebangs pragmas (L moduleSpan HsModule {..}) = do
6366 txt "where"
6467 newline
6568 newline
66− forM_ (sortImports hsmodImports) (located' p_hsmodImport)
69+ forM_ (sortImports hsmodImports) (located' (p_hsmodImport qualifiedPost))
6770 newline
6871 switchLayout (getLoc <$> hsmodDecls) $ do
6972 p_hsDecls Free hsmodDecls