on master help sign in
Various minor corrections to the docs and code
0d558f1mrkkrp
src/Ormolu/CommentStream.hs+8−5
@@ -21,13 +21,14 @@ import qualified Data.List.NonEmpty as NE
21import qualified GHC
21import qualified GHC
22import qualified Lexer as GHC
22import qualified Lexer as GHC
23
23
24−-- | A stream of 'RealLocated' comment strings in ascending order with
24+-- | A stream of 'RealLocated' 'Comment's in ascending order with respect to
25−-- respect to beginning of corresponding spans.
25+-- beginning of corresponding spans.
26
26
27newtype CommentStream = CommentStream [RealLocated Comment]
27newtype CommentStream = CommentStream [RealLocated Comment]
28 deriving (Eq, Data, Semigroup, Monoid)
28 deriving (Eq, Data, Semigroup, Monoid)
29
29
30−-- | A wrapper for a single comment.
30+-- | A wrapper for a single comment. The 'NonEmpty' list inside contains
31+-- lines of multiline comment @{- … -}@ or just single item\/line otherwise.
31
32
32newtype Comment = Comment (NonEmpty String)
33newtype Comment = Comment (NonEmpty String)
33 deriving (Eq, Show, Data)
34 deriving (Eq, Show, Data)
@@ -48,13 +49,15 @@ mkCommentStream extraComments pstate
48 . mapMaybe toRealSpan $
49 . mapMaybe toRealSpan $
49 extraComments ++
50 extraComments ++
50 (fmap unAnnotationComment <$> GHC.comment_q pstate) ++
51 (fmap unAnnotationComment <$> GHC.comment_q pstate) ++
51− concatMap (fmap (fmap unAnnotationComment) . snd) (GHC.annotations_comments pstate)
52+ concatMap (fmap (fmap unAnnotationComment) . snd)
53+ (GHC.annotations_comments pstate)
52 where
54 where
53 startOfSpan (L l _) = realSrcSpanStart l
55 startOfSpan (L l _) = realSrcSpanStart l
54 toRealSpan (L (RealSrcSpan l) a) = Just (L l a)
56 toRealSpan (L (RealSrcSpan l) a) = Just (L l a)
55 toRealSpan _ = Nothing
57 toRealSpan _ = Nothing
56
58
57−-- | Test whether a 'Comment' looks like a Haddock following a definition.
59+-- | Test whether a 'Comment' looks like a Haddock following a definition,
60+-- i.e. something starting with @-- ^@.
58
61
59isPrevHaddock :: Comment -> Bool
62isPrevHaddock :: Comment -> Bool
60isPrevHaddock (Comment (x :| _)) = "-- ^" `isPrefixOf` x
63isPrevHaddock (Comment (x :| _)) = "-- ^" `isPrefixOf` x
src/Ormolu/Diff.hs+2−2
@@ -18,8 +18,8 @@ import Ormolu.Imports (sortImports)
18-- positions.
18-- positions.
19
19
20diff
20diff
21− :: (CommentStream, ParsedSource) -- ^ First annotated AST
21+ :: (CommentStream, ParsedSource) -- ^ First comment stream and AST
22− -> (CommentStream, ParsedSource) -- ^ Second annotated AST
22+ -> (CommentStream, ParsedSource) -- ^ Second comment stream and AST
23 -> Bool
23 -> Bool
24diff (cstream0, ps0) (cstream1, ps1) =
24diff (cstream0, ps0) (cstream1, ps1) =
25 not (matchIgnoringSrcSpans cstream0 cstream1)
25 not (matchIgnoringSrcSpans cstream0 cstream1)
src/Ormolu/Exception.hs+1−1
@@ -15,7 +15,7 @@ import System.IO
15import qualified GHC
15import qualified GHC
16import qualified Outputable as GHC
16import qualified Outputable as GHC
17
17
18−-- | Ormolu exception representing all cases when 'ormoluFile' can fail.
18+-- | Ormolu exception representing all cases when Ormolu can fail.
19
19
20data OrmoluException
20data OrmoluException
21 = OrmoluCppEnabled
21 = OrmoluCppEnabled
src/Ormolu/Printer/Combinators.hs+22−16
@@ -15,15 +15,15 @@ module Ormolu.Printer.Combinators
15 , newline
15 , newline
16 , inci
16 , inci
17 , relaxComments
17 , relaxComments
18− , hasMoreComments
19 , located
18 , located
20 , locatedVia
19 , locatedVia
21 , located'
20 , located'
22 , switchLayout
21 , switchLayout
23− , velt
24− , velt'
25 , vlayout
22 , vlayout
26 , breakpoint
23 , breakpoint
24+ -- ** Formatting lists
25+ , velt
26+ , velt'
27 , withSep
27 , withSep
28 , spaceSep
28 , spaceSep
29 , newlineSep
29 , newlineSep
@@ -66,7 +66,7 @@ txt t = ensureIndent >> spit t
66atom :: Outputable a => a -> R ()
66atom :: Outputable a => a -> R ()
67atom = txt . T.pack . showSDocUnsafe . ppr
67atom = txt . T.pack . showSDocUnsafe . ppr
68
68
69−-- | Enter a 'Located' entity. This primitive handles outputting comments
69+-- | Enter a 'Located' entity. This combinator handles outputting comments
70-- that may be associated with the primitive and sets corresponding layout
70-- that may be associated with the primitive and sets corresponding layout
71-- for the inner computation.
71-- for the inner computation.
72
72
@@ -77,7 +77,7 @@ located
77 -> R ()
77 -> R ()
78located loc@(L l _) = locatedVia (Just l) loc
78located loc@(L l _) = locatedVia (Just l) loc
79
79
80−-- | A special version of 'located' that allows to control layout using
80+-- | A special version of 'located' that allows to control layout using an
81-- externally provided span. 'Nothing' means that layout won't be changed.
81-- externally provided span. 'Nothing' means that layout won't be changed.
82
82
83locatedVia
83locatedVia
@@ -129,6 +129,15 @@ switchLayout spn = enterLayout
129 then SingleLine
129 then SingleLine
130 else MultiLine)
130 else MultiLine)
131
131
132+-- | Insert a space if enclosing layout is single-line, or newline if it's
133+-- multiline.
134+
135+breakpoint :: R ()
136+breakpoint = vlayout space newline
137+
138+----------------------------------------------------------------------------
139+-- Formatting lists
140+
132-- | Element of variable layout. This means that the sub-components may be
141-- | Element of variable layout. This means that the sub-components may be
133-- rendered either on single line or each on its own line depending on
142-- rendered either on single line or each on its own line depending on
134-- current layout.
143-- current layout.
@@ -151,12 +160,6 @@ velt' xs = sitcc $ sequence_ (intersperse sep (sitcc <$> xs))
151 where
160 where
152 sep = vlayout (spit " ") newline
161 sep = vlayout (spit " ") newline
153
162
154−-- | Insert a space if enclosing layout is single-line, or newline if it's
155−-- multiline.
156−
157−breakpoint :: R ()
158−breakpoint = vlayout space newline
159−
160-- | Put separator between renderings of items of a list.
163-- | Put separator between renderings of items of a list.
161
164
162withSep
165withSep
@@ -199,9 +202,12 @@ line m = do
199-- | Surround given entity by backticks.
202-- | Surround given entity by backticks.
200
203
201backticks :: R () -> R ()
204backticks :: R () -> R ()
202−backticks m = txt "`" >> m >> txt "`"
205+backticks m = do
206+ txt "`"
207+ m
208+ txt "`"
203
209
204−-- | Surround given entity by curly braces.
210+-- | Surround given entity by curly braces @{@ and @}@.
205
211
206braces :: R () -> R ()
212braces :: R () -> R ()
207braces m = sitcc $ do
213braces m = sitcc $ do
@@ -209,7 +215,7 @@ braces m = sitcc $ do
209 ospaces m
215 ospaces m
210 txt "}"
216 txt "}"
211
217
212−-- | Surround given entity by square brackets.
218+-- | Surround given entity by square brackets @[@ and @]@.
213
219
214brackets :: R () -> R ()
220brackets :: R () -> R ()
215brackets m = sitcc $ do
221brackets m = sitcc $ do
@@ -217,7 +223,7 @@ brackets m = sitcc $ do
217 ospaces m
223 ospaces m
218 txt "]"
224 txt "]"
219
225
220−-- | Surround given entity by parallel array brackets @[:@ ond @:]@.
226+-- | Surround given entity by parallel array brackets @[:@ and @:]@.
221
227
222bracketsPar :: R () -> R ()
228bracketsPar :: R () -> R ()
223bracketsPar m = sitcc $ do
229bracketsPar m = sitcc $ do
@@ -225,7 +231,7 @@ bracketsPar m = sitcc $ do
225 m
231 m
226 txt ":]"
232 txt ":]"
227
233
228−-- | Surround given entity by parentheses.
234+-- | Surround given entity by parentheses @(@ and @)@.
229
235
230parens :: R () -> R ()
236parens :: R () -> R ()
231parens m = sitcc $ do
237parens m = sitcc $ do
src/Ormolu/Printer/Comments.hs+5−3
@@ -1,12 +1,14 @@
1{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3
3
4−-- | Helpers for formatting of comments.
4+-- | Helpers for formatting of comments. This is low-level code, use
5+-- "Ormolu.Printer.Combinators" unless you know what you are doing.
5
6
6module Ormolu.Printer.Comments
7module Ormolu.Printer.Comments
7 ( spitPrecedingComments
8 ( spitPrecedingComments
8 , spitFollowingComments
9 , spitFollowingComments
9 , spitRemainingComments
10 , spitRemainingComments
11+ , hasMoreComments
10 )
12 )
11where
13where
12
14
@@ -80,11 +82,11 @@ spitFollowingComment (L ref a) mlastSpn = do
80 i <- getIndent
82 i <- getIndent
81 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
83 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
82 if theSameLine l ref && not (isModule a)
84 if theSameLine l ref && not (isModule a)
83− then modNextline $ \m -> setIndent i $ do
85+ then modNewline $ \m -> setIndent i $ do
84 spit " "
86 spit " "
85 spitComment comment
87 spitComment comment
86 m
88 m
87− else modNextline $ \m -> setIndent i $ do
89+ else modNewline $ \m -> setIndent i $ do
88 m
90 m
89 when (needsNewlineBefore l mlastSpn) newline
91 when (needsNewlineBefore l mlastSpn) newline
90 spitComment comment
92 spitComment comment
src/Ormolu/Printer/Internal.hs+14−13
@@ -14,7 +14,7 @@ module Ormolu.Printer.Internal
14 -- * Internal functions
14 -- * Internal functions
15 , spit
15 , spit
16 , newline
16 , newline
17− , modNextline
17+ , modNewline
18 , ensureIndent
18 , ensureIndent
19 , inci
19 , inci
20 , sitcc
20 , sitcc
@@ -137,13 +137,7 @@ spit x = do
137 }
137 }
138 traceR "spit_after" Nothing
138 traceR "spit_after" Nothing
139
139
140−-- | Output a newline. The 'modNewline' function can be used to alter what
140+-- | Output a newline.
141−-- will be inserted. This is used to output comments following an element of
142−-- AST because we cannot output comments immediately, e.g. because we need
143−-- to close parentheses first, etc.
144−--
145−-- 'newline' auto-resets its modifications so the changes introduced with
146−-- 'modNewline' only have effect once.
147
141
148newline :: R ()
142newline :: R ()
149newline = do
143newline = do
@@ -165,11 +159,18 @@ newlineRaw = do
165 }
159 }
166 traceR "newline_after" Nothing
160 traceR "newline_after" Nothing
167
161
168−-- | Modify how next newline will be output. The argument of call-back is
162+-- | The 'modNewline' function can be used to alter what will be inserted as
163+-- a newline. This is used to output comments following an element of AST
164+-- because we cannot output comments immediately, e.g. because we need to
165+-- close parentheses first, etc.
166+--
167+-- 'newline' auto-resets its modifications so the changes introduced with
168+-- 'modNewline' only have effect once.
169+--
169−-- the version of 'newline' built so far.
170+-- The argument of the call-back is the version of 'newline' built so far.
170
171
171−modNextline :: (R () -> R ()) -> R ()
172+modNewline :: (R () -> R ()) -> R ()
172−modNextline f = R $ do
173+modNewline f = R $ do
173 old <- gets scNewline
174 old <- gets scNewline
174 modify $ \sc -> sc
175 modify $ \sc -> sc
175 { scNewline = f old
176 { scNewline = f old
@@ -311,7 +312,7 @@ hasMoreComments = R $ do
311getIndent :: R Int
312getIndent :: R Int
312getIndent = R (asks rcIndent)
313getIndent = R (asks rcIndent)
313
314
314−-- | Set indentation level.
315+-- | Set indentation level for the given computation.
315
316
316setIndent :: Int -> R () -> R ()
317setIndent :: Int -> R () -> R ()
317setIndent i m' = do
318setIndent i m' = do
src/Ormolu/SpanStream.hs+3−2
@@ -1,7 +1,7 @@
1{-# LANGUAGE DeriveDataTypeable #-}
1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3
3
4−-- | Build span stream of AST elements.
4+-- | Build span stream from AST.
5
5
6module Ormolu.SpanStream
6module Ormolu.SpanStream
7 ( SpanStream (..)
7 ( SpanStream (..)
@@ -24,7 +24,8 @@ import qualified Data.DList as D
24newtype SpanStream = SpanStream [RealSrcSpan]
24newtype SpanStream = SpanStream [RealSrcSpan]
25 deriving (Eq, Show, Data, Semigroup, Monoid)
25 deriving (Eq, Show, Data, Semigroup, Monoid)
26
26
27−-- | Create 'SpanStream' from a data structure containing 'RealSrcSpan's.
27+-- | Create 'SpanStream' from a data structure containing \"located\"
28+-- elements.
28
29
29mkSpanStream
30mkSpanStream
30 :: Data a
31 :: Data a
src/Ormolu/Utils.hs+4−2
@@ -1,3 +1,5 @@
1+-- | Random utilities used by the code.
2+
1module Ormolu.Utils
3module Ormolu.Utils
2 ( combineSrcSpans'
4 ( combineSrcSpans'
3 , isModule
5 , isModule
@@ -20,9 +22,9 @@ combineSrcSpans' (x:|xs) = foldr combineSrcSpans x xs
20isModule :: Data a => a -> Bool
22isModule :: Data a => a -> Bool
21isModule x = showConstr (toConstr x) == "HsModule"
23isModule x = showConstr (toConstr x) == "HsModule"
22
24
23−-- | Exact inner value from 'Located'.
25+-- | Exact inner value from a 'Located' thing.
24
26
25−unL :: Located e -> e
27+unL :: GenLocated l e -> e
26unL (L _ e) = e
28unL (L _ e) = e
27
29
28-- | Get source span from a 'Located' thing.
30-- | Get source span from a 'Located' thing.