on master help sign in
Factor out the ‘isModule’ function and the use of module span

Module span is dummy and misleading. Right now it is not used at all, but we still organize code as if it were used.

commit3fbc130a0962ab236d87711beae1895d84a68362 authorMark Karpov <markkarpov92@gmail.com> dateMon 27 Apr 2020, 16:51 (6 years ago) parent5d1612f
8 files changed+33−65
src/GHC.hs−3 src/Ormolu/Diff.hs+5−12 src/Ormolu/Parser.hs+2−2 src/Ormolu/Parser/Result.hs+1−1 src/Ormolu/Printer/Combinators.hs+6−19 src/Ormolu/Printer/Comments.hs+17−20 src/Ormolu/Printer/Meat/Module.hs+2−2 src/Ormolu/Utils.hs−6
src/GHC.hs−3
@@ -1,6 +1,5 @@
11module GHC
22 ( module X,
3− ParsedSource,
43 )
54where
65
@@ -11,5 +10,3 @@ import GHC.Hs.Instances as X ()
1110import Module as X
1211import RdrName as X
1312import SrcLoc as X
14−
15−type ParsedSource = Located (HsModule GhcPs)
src/Ormolu/Diff.hs+5−12
@@ -38,14 +38,16 @@ diffParseResult :: ParseResult -> ParseResult -> Diff
3838diffParseResult
3939 ParseResult
4040 { prCommentStream = cstream0,
41− prParsedSource = ps0
41+ prParsedSource = hs0
4242 }
4343 ParseResult
4444 { prCommentStream = cstream1,
45− prParsedSource = ps1
45+ prParsedSource = hs1
4646 } =
4747 matchIgnoringSrcSpans cstream0 cstream1
48− <> matchIgnoringSrcSpans ps0 ps1
48+ <> matchIgnoringSrcSpans
49+ hs0 {hsmodImports = sortImports (hsmodImports hs0)}
50+ hs1 {hsmodImports = sortImports (hsmodImports hs1)}
4951
5052-- | Compare two values for equality disregarding differences in 'SrcSpan's
5153-- and the ordering of import lists.
@@ -67,7 +69,6 @@ matchIgnoringSrcSpans = genericQuery
6769 gzipWithQ
6870 ( genericQuery
6971 `extQ` srcSpanEq
70− `extQ` hsModuleEq
7172 `extQ` sourceTextEq
7273 `extQ` hsDocStringEq
7374 `extQ` importDeclQualifiedStyleEq
@@ -78,14 +79,6 @@ matchIgnoringSrcSpans = genericQuery
7879 | otherwise = Different []
7980 srcSpanEq :: SrcSpan -> GenericQ Diff
8081 srcSpanEq _ _ = Same
81− hsModuleEq :: HsModule GhcPs -> GenericQ Diff
82− hsModuleEq hs0 hs1' =
83− case cast hs1' :: Maybe (HsModule GhcPs) of
84− Nothing -> Different []
85− Just hs1 ->
86− matchIgnoringSrcSpans
87− hs0 {hsmodImports = sortImports (hsmodImports hs0)}
88− hs1 {hsmodImports = sortImports (hsmodImports hs1)}
8982 sourceTextEq :: SourceText -> GenericQ Diff
9083 sourceTextEq _ _ = Same
9184 importDeclQualifiedStyleEq :: ImportDeclQualifiedStyle -> GenericQ Diff
src/Ormolu/Parser.hs+2−2
@@ -88,7 +88,7 @@ parseModule Config {..} path rawInput = liftIO $ do
8888 case pStateErrors pstate of
8989 Just err -> Left err
9090 Nothing -> error "PFailed does not have an error"
91− GHC.POk pstate pmod ->
91+ GHC.POk pstate (L _ hsModule) ->
9292 case pStateErrors pstate of
9393 -- Some parse errors (pattern/arrow syntax in expr context)
9494 -- do not cause a parse error, but they are replaced with "_"
@@ -100,7 +100,7 @@ parseModule Config {..} path rawInput = liftIO $ do
100100 mkCommentStream extraComments pstate
101101 in Right
102102 ParseResult
103− { prParsedSource = pmod,
103+ { prParsedSource = hsModule,
104104 prAnns = mkAnns pstate,
105105 prStackHeader = stackHeader,
106106 prShebangs = shebangs,
src/Ormolu/Parser/Result.hs+1−1
@@ -17,7 +17,7 @@ import Ormolu.Parser.Shebang (Shebang)
1717-- | A collection of data that represents a parsed module in Ormolu.
1818data ParseResult = ParseResult
1919 { -- | 'ParsedSource' from GHC
20− prParsedSource :: ParsedSource,
20+ prParsedSource :: HsModule GhcPs,
2121 -- | Ormolu-specfic representation of annotations
2222 prAnns :: Anns,
2323 -- | Stack header
src/Ormolu/Printer/Combinators.hs+6−19
@@ -62,12 +62,10 @@ module Ormolu.Printer.Combinators
6262where
6363
6464import Control.Monad
65−import Data.Data (Data)
6665import Data.List (intersperse)
6766import Data.Text (Text)
6867import Ormolu.Printer.Comments
6968import Ormolu.Printer.Internal
70−import Ormolu.Utils (isModule)
7169import SrcLoc
7270
7371----------------------------------------------------------------------------
@@ -79,31 +77,20 @@ import SrcLoc
7977-- 'Located' wrapper, it should be “discharged” with a corresponding
8078-- 'located' invocation.
8179located ::
82− Data a =>
8380 -- | Thing to enter
8481 Located a ->
8582 -- | How to render inner value
8683 (a -> R ()) ->
8784 R ()
88−located loc f = do
89− let withRealLocated (L l a) g =
90− case l of
91− UnhelpfulSpan _ -> return ()
92− RealSrcSpan l' -> g (L l' a)
93− withRealLocated loc spitPrecedingComments
94− let setEnclosingSpan =
95− case getLoc loc of
96− UnhelpfulSpan _ -> id
97− RealSrcSpan orf ->
98− if isModule (unLoc loc)
99− then id
100− else withEnclosingSpan orf
101− setEnclosingSpan $ switchLayout [getLoc loc] (f (unLoc loc))
102− withRealLocated loc spitFollowingComments
85+located (L (UnhelpfulSpan _) a) f = f a
86+located (L (RealSrcSpan l) a) f = do
87+ spitPrecedingComments l
88+ withEnclosingSpan l $
89+ switchLayout [RealSrcSpan l] (f a)
90+ spitFollowingComments l
10391
10492-- | A version of 'located' with arguments flipped.
10593located' ::
106− Data a =>
10794 -- | How to render inner value
10895 (a -> R ()) ->
10996 -- | Thing to enter
src/Ormolu/Printer/Comments.hs+17−20
@@ -14,12 +14,10 @@ where
1414
1515import Control.Monad
1616import Data.Coerce (coerce)
17−import Data.Data (Data)
1817import qualified Data.List.NonEmpty as NE
1918import qualified Data.Text as T
2019import Ormolu.Parser.CommentStream
2120import Ormolu.Printer.Internal
22−import Ormolu.Utils (isModule)
2321import SrcLoc
2422
2523----------------------------------------------------------------------------
@@ -27,9 +25,8 @@ import SrcLoc
2725
2826-- | Output all preceding comments for an element at given location.
2927spitPrecedingComments ::
30− Data a =>
31− -- | AST element to attach comments to
32− RealLocated a ->
28+ -- | Span of the element to attach comments to
29+ RealSrcSpan ->
3330 R ()
3431spitPrecedingComments ref = do
3532 gotSome <- handleCommentSeries (spitPrecedingComment ref)
@@ -37,16 +34,15 @@ spitPrecedingComments ref = do
3734 lastMark <- getSpanMark
3835 -- Insert a blank line between the preceding comments and the thing
3936 -- after them if there was a blank line in the input.
40− when (needsNewlineBefore (getRealSrcSpan ref) lastMark) newline
37+ when (needsNewlineBefore ref lastMark) newline
4138
4239-- | Output all comments following an element at given location.
4340spitFollowingComments ::
44− Data a =>
45− -- | AST element of attach comments to
46− RealLocated a ->
41+ -- | Span of the element to attach comments to
42+ RealSrcSpan ->
4743 R ()
4844spitFollowingComments ref = do
49− trimSpanStream (getRealSrcSpan ref)
45+ trimSpanStream ref
5046 void $ handleCommentSeries (spitFollowingComment ref)
5147
5248-- | Output all remaining comments in the comment stream.
@@ -62,14 +58,13 @@ spitRemainingComments = do
6258
6359-- | Output a single preceding comment for an element at given location.
6460spitPrecedingComment ::
65− Data a =>
66− -- | AST element to attach comments to
67− RealLocated a ->
61+ -- | Span of the element to attach comments to
62+ RealSrcSpan ->
6863 -- | Location of last comment in the series
6964 Maybe SpanMark ->
7065 -- | Are we done?
7166 R Bool
72−spitPrecedingComment (L ref a) mlastMark = do
67+spitPrecedingComment ref mlastMark = do
7368 let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
7469 withPoppedComment p $ \l comment -> do
7570 dirtyLine <-
@@ -80,31 +75,33 @@ spitPrecedingComment (L ref a) mlastMark = do
8075 -- immediately because it'll be attached to the previous element (on
8176 -- the same line) on the next run, so we play safe here and output
8277 -- an extra 'newline' in this case.
83− Nothing -> isLineDirty -- only for very first preceding comment
78+ --
79+ -- We check 'mlastMark' to do this only for the very first preceding
80+ -- comment.
8481 Just _ -> return False
82+ _ -> isLineDirty
8583 when (dirtyLine || needsNewlineBefore l mlastMark) newline
8684 spitCommentNow l comment
87− if theSameLinePre l ref && not (isModule a)
85+ if theSameLinePre l ref
8886 then space
8987 else newline
9088
9189-- | Output a comment that follows element at given location immediately on
9290-- the same line, if there is any.
9391spitFollowingComment ::
94− Data a =>
9592 -- | AST element to attach comments to
96− RealLocated a ->
93+ RealSrcSpan ->
9794 -- | Location of last comment in the series
9895 Maybe SpanMark ->
9996 -- | Are we done?
10097 R Bool
101−spitFollowingComment (L ref a) mlastMark = do
98+spitFollowingComment ref mlastMark = do
10299 mnSpn <- nextEltSpan
103100 -- Get first enclosing span that is not equal to reference span, i.e. it's
104101 -- truly something enclosing the AST element.
105102 meSpn <- getEnclosingSpan (/= ref)
106103 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastMark) $ \l comment ->
107− if theSameLinePost l ref && not (isModule a)
104+ if theSameLinePost l ref
108105 then
109106 if isMultilineComment comment
110107 then space >> spitCommentNow l comment
src/Ormolu/Printer/Meat/Module.hs+2−2
@@ -34,9 +34,9 @@ p_hsModule ::
3434 -- | Whether to use postfix qualified imports
3535 Bool ->
3636 -- | AST to print
37− ParsedSource ->
37+ HsModule GhcPs ->
3838 R ()
39−p_hsModule mstackHeader shebangs pragmas qualifiedPost (L _ HsModule {..}) = do
39+p_hsModule mstackHeader shebangs pragmas qualifiedPost HsModule {..} = do
4040 let deprecSpan = maybe [] (\(L s _) -> [s]) hsmodDeprecMessage
4141 exportSpans = maybe [] (\(L s _) -> [s]) hsmodExports
4242 switchLayout (deprecSpan <> exportSpans) $ do
src/Ormolu/Utils.hs−6
@@ -6,7 +6,6 @@ module Ormolu.Utils
66 ( RelativePos (..),
77 attachRelativePos,
88 combineSrcSpans',
9− isModule,
109 notImplemented,
1110 showOutputable,
1211 splitDocString,
@@ -18,7 +17,6 @@ module Ormolu.Utils
1817 )
1918where
2019
21−import Data.Data (Data, showConstr, toConstr)
2220import Data.List (dropWhileEnd)
2321import qualified Data.List.NonEmpty as NE
2422import Data.List.NonEmpty (NonEmpty (..))
@@ -52,10 +50,6 @@ attachRelativePos = \case
5250combineSrcSpans' :: NonEmpty SrcSpan -> SrcSpan
5351combineSrcSpans' (x :| xs) = foldr combineSrcSpans x xs
5452
55−-- | Return 'True' if given element of AST is module.
56−isModule :: Data a => a -> Bool
57−isModule x = showConstr (toConstr x) == "HsModule"
58−
5953-- | Placeholder for things that are not yet implemented.
6054notImplemented :: String -> a
6155notImplemented msg = error $ "not implemented yet: " ++ msg