on master help sign in
Fix rendering of module headers
commit21dd3775e7a0d9063ac04490327a649413320e31 authorMark Karpov <markkarpov92@gmail.com> dateFri 24 Apr 2020, 19:43 (6 years ago) parent840acd9
22 files changed+56−77
CHANGELOG.md+3 app/Main.hs+1−4 data/examples/module-header/double-dot-with-names-out.hs+1−6 data/examples/module-header/multiline2.hs+2−2 data/examples/module-header/preceding-comment-with-haddock-out.hs+1−4 data/examples/module-header/singleline-empty-out.hs+1−4 data/examples/module-header/warning-pragma-multiline-out.hs+1−4 data/examples/module-header/warning-pragma-out.hs+3−1 data/examples/other/pragma-comments-out.hs+1−4 data/examples/other/pragma-out.hs+1−4 default.nix+14−14 expected-failures/Agda.txt+1−1 expected-failures/idris.txt+3−3 expected-failures/pandoc.txt+1−1 region-tests/expected-result-5-6.hs → region-tests/expected-result-6-7.hs+2−1 region-tests/expected-result-5-7.hs → region-tests/expected-result-6-8.hs+2−1 region-tests/expected-result-8-12.hs → region-tests/expected-result-9-13.hs+2−1 region-tests/expected-result-all.hs+3−2 region-tests/src.hs+2−1 src/Ormolu/Printer/Meat/Declaration/Warning.hs+1−3 src/Ormolu/Printer/Meat/Module.hs+9−12 tests/Ormolu/PrinterSpec.hs+1−4
CHANGELOG.md+3
@@ -19,6 +19,9 @@
1919 select a region to format. [Issue
2020 516](https://github.com/tweag/ormolu/issues/516).
2121
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+
2225## Ormolu 0.0.4.0
2326
2427* When given several files to format, Ormolu does not stop on the first
app/Main.hs+1−4
@@ -4,10 +4,7 @@
44{-# LANGUAGE TemplateHaskell #-}
55{-# LANGUAGE TypeApplications #-}
66
7−module Main
8− ( main,
9− )
10−where
7+module Main (main) where
118
129import Control.Exception (SomeException, displayException, try)
1310import Control.Monad
data/examples/module-header/double-dot-with-names-out.hs+1−6
@@ -1,11 +1,6 @@
11{-# LANGUAGE PatternSynonyms #-}
22
3−module ExportSyntax
4− ( A (.., NoA),
5− Q (F, ..),
6− G (T, .., U),
7− )
8−where
3+module ExportSyntax (A (.., NoA), Q (F, ..), G (T, .., U)) where
94
105data A = A | B
116
data/examples/module-header/multiline2.hs+2−2
@@ -1,2 +1,2 @@
1−module Foo
2− (foo, bar, baz) where
1+module Foo (
2+ foo, bar, baz) where
data/examples/module-header/preceding-comment-with-haddock-out.hs+1−4
@@ -3,9 +3,6 @@
33-}
44
55-- | This is the module's Haddock.
6−module Main
7− ( main,
8− )
9−where
6+module Main (main) where
107
118main = return ()
data/examples/module-header/singleline-empty-out.hs+1−4
@@ -1,5 +1,2 @@
11-- | This demonstrates a BUG.
2−module Foo
3− (
4− )
5−where
2+module Foo () where
data/examples/module-header/warning-pragma-multiline-out.hs+1−4
@@ -1,9 +1,6 @@
11module Test
22 {-# DEPRECATED "This module is unstable" #-}
3− ( foo,
4− bar,
5− baz,
6− )
3+ (foo, bar, baz)
74where
85
96import Blah
data/examples/module-header/warning-pragma-out.hs+3−1
@@ -1,3 +1,5 @@
11module Test
2− {-# WARNING "This module is very internal" #-}
2+ {-# WARNING
3+ "This module is very internal"
4+ #-}
35where
data/examples/other/pragma-comments-out.hs+1−4
@@ -5,7 +5,4 @@
55{-# OPTIONS_GHC -fno-warn-overlapping-patterns #-}
66
77-- | Header comment.
8−module Foo
9− (
10− )
11−where
8+module Foo () where
data/examples/other/pragma-out.hs+1−4
@@ -9,7 +9,4 @@
99{-# OPTIONS_HADDOCK prune, show-extensions #-}
1010
1111-- | Header comment.
12−module Foo
13− (
14− )
15−where
12+module Foo () where
default.nix+14−14
@@ -134,17 +134,17 @@ in {
134134 cp src.hs result-all-implicit.hs
135135 ormolu --check-idempotency --mode inplace result-all-implicit.hs
136136 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
138138 cp src.hs result-only-start.hs
139139 ormolu --check-idempotency --mode inplace --start-line 1 result-only-start.hs
140140 cp src.hs result-only-end.hs
141− ormolu --check-idempotency --mode inplace --end-line 12 result-only-end.hs
142− cp src.hs result-5-6.hs
143− ormolu --check-idempotency --mode inplace --start-line 5 --end-line 6 result-5-6.hs
144− cp src.hs result-5-7.hs
145− ormolu --check-idempotency --mode inplace --start-line 5 --end-line 7 result-5-7.hs
146− cp src.hs result-8-12.hs
147− ormolu --check-idempotency --mode inplace --start-line 8 --end-line 12 result-8-12.hs
141+ ormolu --check-idempotency --mode inplace --end-line 13 result-only-end.hs
142+ cp src.hs result-6-7.hs
143+ ormolu --check-idempotency --mode inplace --start-line 6 --end-line 7 result-6-7.hs
144+ cp src.hs result-6-8.hs
145+ ormolu --check-idempotency --mode inplace --start-line 6 --end-line 8 result-6-8.hs
146+ cp src.hs result-9-13.hs
147+ ormolu --check-idempotency --mode inplace --start-line 9 --end-line 13 result-9-13.hs
148148 '';
149149 checkPhase = ''
150150 echo result-all-implicit.hs
@@ -155,12 +155,12 @@ in {
155155 diff --color=always expected-result-all.hs result-only-start.hs
156156 echo result-only-end.hs
157157 diff --color=always expected-result-all.hs result-only-end.hs
158− echo result-5-6.hs
159− diff --color=always expected-result-5-6.hs result-5-6.hs
160− echo result-5-7.hs
161− diff --color=always expected-result-5-7.hs result-5-7.hs
162− echo result-8-12.hs
163− diff --color=always expected-result-8-12.hs result-8-12.hs
158+ echo result-6-7.hs
159+ diff --color=always expected-result-6-7.hs result-6-7.hs
160+ echo result-6-8.hs
161+ diff --color=always expected-result-6-8.hs result-6-8.hs
162+ echo result-9-13.hs
163+ diff --color=always expected-result-9-13.hs result-9-13.hs
164164 '';
165165 installPhase = ''
166166 mkdir "$out"
expected-failures/Agda.txt+1−1
@@ -16,7 +16,7 @@ Parsing of formatted code failed:
1616Please, consider reporting the bug.
1717
1818Formatting is not idempotent:
19− src/full/Agda/Termination/CallGraph.hs<rendered>:152:38
19+ src/full/Agda/Termination/CallGraph.hs<rendered>:151:38
2020 before: "2, old2) = -- TODO: "
2121 after: "2, old2) =\n -"
2222Please, consider reporting the bug.
expected-failures/idris.txt+3−3
@@ -23,7 +23,7 @@ Formatting is not idempotent:
2323Please, consider reporting the bug.
2424
2525Formatting is not idempotent:
26− src/Idris/Core/WHNF.hs<rendered>:110:46
26+ src/Idris/Core/WHNF.hs<rendered>:105:46
2727 before: " n b sc) = -- stk mu"
2828 after: " n b sc) =\n -- "
2929Please, consider reporting the bug.
@@ -41,7 +41,7 @@ Formatting is not idempotent:
4141Please, consider reporting the bug.
4242
4343Formatting is not idempotent:
44− src/Idris/ModeCommon.hs<rendered>:40:26
44+ src/Idris/ModeCommon.hs<rendered>:34:26
4545 before: "s toline = -- furthe"
4646 after: "s toline =\n -- furt"
4747Please, consider reporting the bug.
@@ -77,7 +77,7 @@ Formatting is not idempotent:
7777Please, consider reporting the bug.
7878
7979Formatting is not idempotent:
80− src/Idris/Prover.hs<rendered>:244:10
80+ src/Idris/Prover.hs<rendered>:239:10
8181 before: " line <> bindin"
8282 after: " line\n <"
8383Please, consider reporting the bug.
expected-failures/pandoc.txt+1−1
@@ -9,7 +9,7 @@ Formatting is not idempotent:
99Please, consider reporting the bug.
1010
1111Formatting is not idempotent:
12− src/Text/Pandoc/Writers/Docx.hs<rendered>:997:25
12+ src/Text/Pandoc/Writers/Docx.hs<rendered>:994:25
1313 before: " -- w:p\n "
1414 after: " -- w:p\n "
1515Please, consider reporting the bug.
region-tests/expected-result-5-6.hs → region-tests/expected-result-6-7.hsrenamed+2−1
@@ -1,6 +1,7 @@
11{-# LANGUAGE LambdaCase #-}
22
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
45
56foo :: Int
67foo = 5
region-tests/expected-result-5-7.hs → region-tests/expected-result-6-8.hsrenamed+2−1
@@ -1,6 +1,7 @@
11{-# LANGUAGE LambdaCase #-}
22
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
45
56foo :: Int
67foo = 5
region-tests/expected-result-8-12.hs → region-tests/expected-result-9-13.hsrenamed+2−1
@@ -1,6 +1,7 @@
11{-# LANGUAGE LambdaCase #-}
22
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
45
56foo :: Int
67foo = 5
region-tests/expected-result-all.hs+3−2
@@ -1,7 +1,8 @@
11{-# LANGUAGE LambdaCase #-}
22
3−module Main
4− ( main,
3+module Foo
4+ ( foo,
5+ bar,
56 )
67where
78
region-tests/src.hs+2−1
@@ -1,6 +1,7 @@
11{-# LANGUAGE LambdaCase #-}
22
3−module Main (main) where
3+module Foo (
4+ foo, bar) where
45
56foo :: Int
67foo = 5
src/Ormolu/Printer/Meat/Declaration/Warning.hs+1−3
@@ -27,9 +27,7 @@ p_warnDecl (XWarnDecl x) = noExtCon x
2727p_moduleWarning :: WarningTxt -> R ()
2828p_moduleWarning wtxt = do
2929 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
3331
3432p_topLevelWarning :: [Located RdrName] -> WarningTxt -> R ()
3533p_topLevelWarning fnames wtxt = do
src/Ormolu/Printer/Meat/Module.hs+9−12
@@ -36,14 +36,10 @@ p_hsModule ::
3636 -- | AST to print
3737 ParsedSource ->
3838 R ()
39−p_hsModule mstackHeader shebangs pragmas qualifiedPost (L moduleSpan 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
45− spans' = exportSpans ++ deprecSpan ++ [moduleSpan]
46− switchLayout spans' $ do
39+p_hsModule mstackHeader shebangs pragmas qualifiedPost (L _ HsModule {..}) = do
40+ let deprecSpan = maybe [] (\(L s _) -> [s]) hsmodDeprecMessage
41+ exportSpans = maybe [] (\(L s _) -> [s]) hsmodExports
42+ switchLayout (deprecSpan <> exportSpans) $ do
4743 forM_ shebangs $ \(Shebang x) ->
4844 located x $ \shebang -> do
4945 txt (T.pack shebang)
@@ -60,15 +56,16 @@ p_hsModule mstackHeader shebangs pragmas qualifiedPost (L moduleSpan HsModule {.
6056 located hsmodName' $ \name -> do
6157 forM_ hsmodHaddockModHeader (p_hsDocString Pipe True)
6258 p_hsmodName name
59+ breakpoint
6360 forM_ hsmodDeprecMessage $ \w -> do
64− breakpoint
6561 located' p_moduleWarning w
62+ breakpoint
6663 case hsmodExports of
6764 Nothing -> return ()
68− Just hsmodExports' -> do
65+ Just l -> do
66+ located l $ \exports -> do
67+ inci (p_hsmodExports exports)
6968 breakpoint
70− inci (p_hsmodExports (unLoc hsmodExports'))
71− breakpoint
7269 txt "where"
7370 newline
7471 newline
tests/Ormolu/PrinterSpec.hs+1−4
@@ -1,9 +1,6 @@
11{-# LANGUAGE TemplateHaskell #-}
22
3−module Ormolu.PrinterSpec
4− ( spec,
5− )
6−where
3+module Ormolu.PrinterSpec (spec) where
74
85import Control.Exception
96import Control.Monad