on master help sign in
Fix rendering of module headers
21dd377Mark Karpov
CHANGELOG.md+3
@@ -19,6 +19,9 @@
19 select a region to format. [Issue
19 select a region to format. [Issue
20 516](https://github.com/tweag/ormolu/issues/516).
20 516](https://github.com/tweag/ormolu/issues/516).
21
21
22+* Fixed rendering of module headers in the presence of preceding comments or
23+ Haddocks. [Issue 561](https://github.com/tweag/ormolu/issues/561).
24+
22## Ormolu 0.0.4.0
25## Ormolu 0.0.4.0
23
26
24* When given several files to format, Ormolu does not stop on the first
27* When given several files to format, Ormolu does not stop on the first
app/Main.hs+1−4
@@ -4,10 +4,7 @@
4{-# LANGUAGE TemplateHaskell #-}
4{-# LANGUAGE TemplateHaskell #-}
5{-# LANGUAGE TypeApplications #-}
5{-# LANGUAGE TypeApplications #-}
6
6
7−module Main
7+module Main (main) where
8− ( main,
9− )
10−where
11
8
12import Control.Exception (SomeException, displayException, try)
9import Control.Exception (SomeException, displayException, try)
13import Control.Monad
10import Control.Monad
data/examples/module-header/double-dot-with-names-out.hs+1−6
@@ -1,11 +1,6 @@
1{-# LANGUAGE PatternSynonyms #-}
1{-# LANGUAGE PatternSynonyms #-}
2
2
3−module ExportSyntax
3+module ExportSyntax (A (.., NoA), Q (F, ..), G (T, .., U)) where
4− ( A (.., NoA),
5− Q (F, ..),
6− G (T, .., U),
7− )
8−where
9
4
10data A = A | B
5data A = A | B
11
6
data/examples/module-header/multiline2.hs+2−2
@@ -1,2 +1,2 @@
1−module Foo
1+module Foo (
2− (foo, bar, baz) where
2+ foo, bar, baz) where
data/examples/module-header/preceding-comment-with-haddock-out.hs+1−4
@@ -3,9 +3,6 @@
3-}
3-}
4
4
5-- | This is the module's Haddock.
5-- | This is the module's Haddock.
6−module Main
6+module Main (main) where
7− ( main,
8− )
9−where
10
7
11main = return ()
8main = return ()
data/examples/module-header/singleline-empty-out.hs+1−4
@@ -1,5 +1,2 @@
1-- | This demonstrates a BUG.
1-- | This demonstrates a BUG.
2−module Foo
2+module Foo () where
3− (
4− )
5−where
data/examples/module-header/warning-pragma-multiline-out.hs+1−4
@@ -1,9 +1,6 @@
1module Test
1module Test
2 {-# DEPRECATED "This module is unstable" #-}
2 {-# DEPRECATED "This module is unstable" #-}
3− ( foo,
3+ (foo, bar, baz)
4− bar,
5− baz,
6− )
7where
4where
8
5
9import Blah
6import Blah
data/examples/module-header/warning-pragma-out.hs+3−1
@@ -1,3 +1,5 @@
1module Test
1module Test
2+ {-# WARNING
2− {-# WARNING "This module is very internal" #-}
3+ "This module is very internal"
4+ #-}
3where
5where
data/examples/other/pragma-comments-out.hs+1−4
@@ -5,7 +5,4 @@
5{-# OPTIONS_GHC -fno-warn-overlapping-patterns #-}
5{-# OPTIONS_GHC -fno-warn-overlapping-patterns #-}
6
6
7-- | Header comment.
7-- | Header comment.
8−module Foo
8+module Foo () where
9− (
10− )
11−where
data/examples/other/pragma-out.hs+1−4
@@ -9,7 +9,4 @@
9{-# OPTIONS_HADDOCK prune, show-extensions #-}
9{-# OPTIONS_HADDOCK prune, show-extensions #-}
10
10
11-- | Header comment.
11-- | Header comment.
12−module Foo
12+module Foo () where
13− (
14− )
15−where
default.nix+14−14
@@ -134,17 +134,17 @@ in {
134 cp src.hs result-all-implicit.hs
134 cp src.hs result-all-implicit.hs
135 ormolu --check-idempotency --mode inplace result-all-implicit.hs
135 ormolu --check-idempotency --mode inplace result-all-implicit.hs
136 cp src.hs result-all-explicit.hs
136 cp src.hs result-all-explicit.hs
137− ormolu --check-idempotency --mode inplace --start-line 1 --end-line 12 result-all-explicit.hs
137+ ormolu --check-idempotency --mode inplace --start-line 1 --end-line 13 result-all-explicit.hs
138 cp src.hs result-only-start.hs
138 cp src.hs result-only-start.hs
139 ormolu --check-idempotency --mode inplace --start-line 1 result-only-start.hs
139 ormolu --check-idempotency --mode inplace --start-line 1 result-only-start.hs
140 cp src.hs result-only-end.hs
140 cp src.hs result-only-end.hs
141− ormolu --check-idempotency --mode inplace --end-line 12 result-only-end.hs
141+ ormolu --check-idempotency --mode inplace --end-line 13 result-only-end.hs
142− cp src.hs result-5-6.hs
142+ cp src.hs result-6-7.hs
143− ormolu --check-idempotency --mode inplace --start-line 5 --end-line 6 result-5-6.hs
143+ ormolu --check-idempotency --mode inplace --start-line 6 --end-line 7 result-6-7.hs
144− cp src.hs result-5-7.hs
144+ cp src.hs result-6-8.hs
145− ormolu --check-idempotency --mode inplace --start-line 5 --end-line 7 result-5-7.hs
145+ ormolu --check-idempotency --mode inplace --start-line 6 --end-line 8 result-6-8.hs
146− cp src.hs result-8-12.hs
146+ cp src.hs result-9-13.hs
147− ormolu --check-idempotency --mode inplace --start-line 8 --end-line 12 result-8-12.hs
147+ ormolu --check-idempotency --mode inplace --start-line 9 --end-line 13 result-9-13.hs
148 '';
148 '';
149 checkPhase = ''
149 checkPhase = ''
150 echo result-all-implicit.hs
150 echo result-all-implicit.hs
@@ -155,12 +155,12 @@ in {
155 diff --color=always expected-result-all.hs result-only-start.hs
155 diff --color=always expected-result-all.hs result-only-start.hs
156 echo result-only-end.hs
156 echo result-only-end.hs
157 diff --color=always expected-result-all.hs result-only-end.hs
157 diff --color=always expected-result-all.hs result-only-end.hs
158− echo result-5-6.hs
158+ echo result-6-7.hs
159− diff --color=always expected-result-5-6.hs result-5-6.hs
159+ diff --color=always expected-result-6-7.hs result-6-7.hs
160− echo result-5-7.hs
160+ echo result-6-8.hs
161− diff --color=always expected-result-5-7.hs result-5-7.hs
161+ diff --color=always expected-result-6-8.hs result-6-8.hs
162− echo result-8-12.hs
162+ echo result-9-13.hs
163− diff --color=always expected-result-8-12.hs result-8-12.hs
163+ diff --color=always expected-result-9-13.hs result-9-13.hs
164 '';
164 '';
165 installPhase = ''
165 installPhase = ''
166 mkdir "$out"
166 mkdir "$out"
expected-failures/Agda.txt+1−1
@@ -16,7 +16,7 @@ Parsing of formatted code failed:
16Please, consider reporting the bug.
16Please, consider reporting the bug.
17
17
18Formatting is not idempotent:
18Formatting is not idempotent:
19− src/full/Agda/Termination/CallGraph.hs<rendered>:152:38
19+ src/full/Agda/Termination/CallGraph.hs<rendered>:151:38
20 before: "2, old2) = -- TODO: "
20 before: "2, old2) = -- TODO: "
21 after: "2, old2) =\n -"
21 after: "2, old2) =\n -"
22Please, consider reporting the bug.
22Please, consider reporting the bug.
expected-failures/idris.txt+3−3
@@ -23,7 +23,7 @@ Formatting is not idempotent:
23Please, consider reporting the bug.
23Please, consider reporting the bug.
24
24
25Formatting is not idempotent:
25Formatting is not idempotent:
26− src/Idris/Core/WHNF.hs<rendered>:110:46
26+ src/Idris/Core/WHNF.hs<rendered>:105:46
27 before: " n b sc) = -- stk mu"
27 before: " n b sc) = -- stk mu"
28 after: " n b sc) =\n -- "
28 after: " n b sc) =\n -- "
29Please, consider reporting the bug.
29Please, consider reporting the bug.
@@ -41,7 +41,7 @@ Formatting is not idempotent:
41Please, consider reporting the bug.
41Please, consider reporting the bug.
42
42
43Formatting is not idempotent:
43Formatting is not idempotent:
44− src/Idris/ModeCommon.hs<rendered>:40:26
44+ src/Idris/ModeCommon.hs<rendered>:34:26
45 before: "s toline = -- furthe"
45 before: "s toline = -- furthe"
46 after: "s toline =\n -- furt"
46 after: "s toline =\n -- furt"
47Please, consider reporting the bug.
47Please, consider reporting the bug.
@@ -77,7 +77,7 @@ Formatting is not idempotent:
77Please, consider reporting the bug.
77Please, consider reporting the bug.
78
78
79Formatting is not idempotent:
79Formatting is not idempotent:
80− src/Idris/Prover.hs<rendered>:244:10
80+ src/Idris/Prover.hs<rendered>:239:10
81 before: " line <> bindin"
81 before: " line <> bindin"
82 after: " line\n <"
82 after: " line\n <"
83Please, consider reporting the bug.
83Please, consider reporting the bug.
expected-failures/pandoc.txt+1−1
@@ -9,7 +9,7 @@ Formatting is not idempotent:
9Please, consider reporting the bug.
9Please, consider reporting the bug.
10
10
11Formatting is not idempotent:
11Formatting is not idempotent:
12− src/Text/Pandoc/Writers/Docx.hs<rendered>:997:25
12+ src/Text/Pandoc/Writers/Docx.hs<rendered>:994:25
13 before: " -- w:p\n "
13 before: " -- w:p\n "
14 after: " -- w:p\n "
14 after: " -- w:p\n "
15Please, consider reporting the bug.
15Please, consider reporting the bug.
region-tests/expected-result-5-6.hs → region-tests/expected-result-6-7.hsrenamed+2−1
@@ -1,6 +1,7 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2
2
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
4
5
5foo :: Int
6foo :: Int
6foo = 5
7foo = 5
region-tests/expected-result-5-7.hs → region-tests/expected-result-6-8.hsrenamed+2−1
@@ -1,6 +1,7 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2
2
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
4
5
5foo :: Int
6foo :: Int
6foo = 5
7foo = 5
region-tests/expected-result-8-12.hs → region-tests/expected-result-9-13.hsrenamed+2−1
@@ -1,6 +1,7 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2
2
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
4
5
5foo :: Int
6foo :: Int
6foo = 5
7foo = 5
region-tests/expected-result-all.hs+3−2
@@ -1,7 +1,8 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2
2
3−module Main
3+module Foo
4− ( main,
4+ ( foo,
5+ bar,
5 )
6 )
6where
7where
7
8
region-tests/src.hs+2−1
@@ -1,6 +1,7 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2
2
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
4
5
5foo :: Int
6foo :: Int
6foo = 5
7foo = 5
src/Ormolu/Printer/Meat/Declaration/Warning.hs+1−3
@@ -27,9 +27,7 @@ p_warnDecl (XWarnDecl x) = noExtCon x
27p_moduleWarning :: WarningTxt -> R ()
27p_moduleWarning :: WarningTxt -> R ()
28p_moduleWarning wtxt = do
28p_moduleWarning wtxt = do
29 let (pragmaText, lits) = warningText wtxt
29 let (pragmaText, lits) = warningText wtxt
30− switchLayout (getLoc <$> lits)
31− $ inci
32− $ pragma pragmaText (inci $ p_lits lits)
30+ inci $ pragma pragmaText $ inci $ p_lits lits
33
31
34p_topLevelWarning :: [Located RdrName] -> WarningTxt -> R ()
32p_topLevelWarning :: [Located RdrName] -> WarningTxt -> R ()
35p_topLevelWarning fnames wtxt = do
33p_topLevelWarning fnames wtxt = do
src/Ormolu/Printer/Meat/Module.hs+9−12
@@ -36,14 +36,10 @@ p_hsModule ::
36 -- | AST to print
36 -- | AST to print
37 ParsedSource ->
37 ParsedSource ->
38 R ()
38 R ()
39−p_hsModule mstackHeader shebangs pragmas qualifiedPost (L moduleSpan HsModule {..}) = do
39+p_hsModule mstackHeader shebangs pragmas qualifiedPost (L _ HsModule {..}) = do
40− -- If span of exports in multiline, the whole thing is multiline. This is
41− -- especially important because span of module itself always seems to have
42− -- length zero, so it's not reliable for layout selection.
43− let exportSpans = maybe [] (\(L s _) -> [s]) hsmodExports
44− deprecSpan = maybe [] (\(L s _) -> [s]) hsmodDeprecMessage
40+ let deprecSpan = maybe [] (\(L s _) -> [s]) hsmodDeprecMessage
45− spans' = exportSpans ++ deprecSpan ++ [moduleSpan]
41+ exportSpans = maybe [] (\(L s _) -> [s]) hsmodExports
46− switchLayout spans' $ do
42+ switchLayout (deprecSpan <> exportSpans) $ do
47 forM_ shebangs $ \(Shebang x) ->
43 forM_ shebangs $ \(Shebang x) ->
48 located x $ \shebang -> do
44 located x $ \shebang -> do
49 txt (T.pack shebang)
45 txt (T.pack shebang)
@@ -60,15 +56,16 @@ p_hsModule mstackHeader shebangs pragmas qualifiedPost (L moduleSpan HsModule {.
60 located hsmodName' $ \name -> do
56 located hsmodName' $ \name -> do
61 forM_ hsmodHaddockModHeader (p_hsDocString Pipe True)
57 forM_ hsmodHaddockModHeader (p_hsDocString Pipe True)
62 p_hsmodName name
58 p_hsmodName name
59+ breakpoint
63 forM_ hsmodDeprecMessage $ \w -> do
60 forM_ hsmodDeprecMessage $ \w -> do
64− breakpoint
65 located' p_moduleWarning w
61 located' p_moduleWarning w
62+ breakpoint
66 case hsmodExports of
63 case hsmodExports of
67 Nothing -> return ()
64 Nothing -> return ()
68− Just hsmodExports' -> do
65+ Just l -> do
66+ located l $ \exports -> do
67+ inci (p_hsmodExports exports)
69 breakpoint
68 breakpoint
70− inci (p_hsmodExports (unLoc hsmodExports'))
71− breakpoint
72 txt "where"
69 txt "where"
73 newline
70 newline
74 newline
71 newline
tests/Ormolu/PrinterSpec.hs+1−4
@@ -1,9 +1,6 @@
1{-# LANGUAGE TemplateHaskell #-}
1{-# LANGUAGE TemplateHaskell #-}
2
2
3−module Ormolu.PrinterSpec
3+module Ormolu.PrinterSpec (spec) where
4− ( spec,
5− )
6−where
7
4
8import Control.Exception
5import Control.Exception
9import Control.Monad
6import Control.Monad