on master help sign in
Various minor corrections to the docs and code
commit0d558f1cb623fc62d7b637e43545b1903f676075 authormrkkrp <mark.karpov@tweag.io> dateWed 8 May 2019, 23:03 (7 years ago) parentb9c8b64
8 files changed+59−44
src/Ormolu/CommentStream.hs+8−5 src/Ormolu/Diff.hs+2−2 src/Ormolu/Exception.hs+1−1 src/Ormolu/Printer/Combinators.hs+22−16 src/Ormolu/Printer/Comments.hs+5−3 src/Ormolu/Printer/Internal.hs+14−13 src/Ormolu/SpanStream.hs+3−2 src/Ormolu/Utils.hs+4−2
src/Ormolu/CommentStream.hs+8−5
@@ -21,13 +21,14 @@ import qualified Data.List.NonEmpty as NE
2121import qualified GHC
2222import qualified Lexer as GHC
2323
24−-- | A stream of 'RealLocated' comment strings in ascending order with
25−-- respect to beginning of corresponding spans.
24+-- | A stream of 'RealLocated' 'Comment's in ascending order with respect to
25+-- beginning of corresponding spans.
2626
2727newtype CommentStream = CommentStream [RealLocated Comment]
2828 deriving (Eq, Data, Semigroup, Monoid)
2929
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.
3132
3233newtype Comment = Comment (NonEmpty String)
3334 deriving (Eq, Show, Data)
@@ -48,13 +49,15 @@ mkCommentStream extraComments pstate
4849 . mapMaybe toRealSpan $
4950 extraComments ++
5051 (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)
5254 where
5355 startOfSpan (L l _) = realSrcSpanStart l
5456 toRealSpan (L (RealSrcSpan l) a) = Just (L l a)
5557 toRealSpan _ = Nothing
5658
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 @-- ^@.
5861
5962isPrevHaddock :: Comment -> Bool
6063isPrevHaddock (Comment (x :| _)) = "-- ^" `isPrefixOf` x
src/Ormolu/Diff.hs+2−2
@@ -18,8 +18,8 @@ import Ormolu.Imports (sortImports)
1818-- positions.
1919
2020diff
21− :: (CommentStream, ParsedSource) -- ^ First annotated AST
22− -> (CommentStream, ParsedSource) -- ^ Second annotated AST
21+ :: (CommentStream, ParsedSource) -- ^ First comment stream and AST
22+ -> (CommentStream, ParsedSource) -- ^ Second comment stream and AST
2323 -> Bool
2424diff (cstream0, ps0) (cstream1, ps1) =
2525 not (matchIgnoringSrcSpans cstream0 cstream1)
src/Ormolu/Exception.hs+1−1
@@ -15,7 +15,7 @@ import System.IO
1515import qualified GHC
1616import qualified Outputable as GHC
1717
18−-- | Ormolu exception representing all cases when 'ormoluFile' can fail.
18+-- | Ormolu exception representing all cases when Ormolu can fail.
1919
2020data OrmoluException
2121 = OrmoluCppEnabled
src/Ormolu/Printer/Combinators.hs+22−16
@@ -15,15 +15,15 @@ module Ormolu.Printer.Combinators
1515 , newline
1616 , inci
1717 , relaxComments
18− , hasMoreComments
1918 , located
2019 , locatedVia
2120 , located'
2221 , switchLayout
23− , velt
24− , velt'
2522 , vlayout
2623 , breakpoint
24+ -- ** Formatting lists
25+ , velt
26+ , velt'
2727 , withSep
2828 , spaceSep
2929 , newlineSep
@@ -66,7 +66,7 @@ txt t = ensureIndent >> spit t
6666atom :: Outputable a => a -> R ()
6767atom = txt . T.pack . showSDocUnsafe . ppr
6868
69−-- | Enter a 'Located' entity. This primitive handles outputting comments
69+-- | Enter a 'Located' entity. This combinator handles outputting comments
7070-- that may be associated with the primitive and sets corresponding layout
7171-- for the inner computation.
7272
@@ -77,7 +77,7 @@ located
7777 -> R ()
7878located loc@(L l _) = locatedVia (Just l) loc
7979
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
8181-- externally provided span. 'Nothing' means that layout won't be changed.
8282
8383locatedVia
@@ -129,6 +129,15 @@ switchLayout spn = enterLayout
129129 then SingleLine
130130 else MultiLine)
131131
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+
132141-- | Element of variable layout. This means that the sub-components may be
133142-- rendered either on single line or each on its own line depending on
134143-- current layout.
@@ -151,12 +160,6 @@ velt' xs = sitcc $ sequence_ (intersperse sep (sitcc <$> xs))
151160 where
152161 sep = vlayout (spit " ") newline
153162
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−
160163-- | Put separator between renderings of items of a list.
161164
162165withSep
@@ -199,9 +202,12 @@ line m = do
199202-- | Surround given entity by backticks.
200203
201204backticks :: R () -> R ()
202−backticks m = txt "`" >> m >> txt "`"
205+backticks m = do
206+ txt "`"
207+ m
208+ txt "`"
203209
204−-- | Surround given entity by curly braces.
210+-- | Surround given entity by curly braces @{@ and @}@.
205211
206212braces :: R () -> R ()
207213braces m = sitcc $ do
@@ -209,7 +215,7 @@ braces m = sitcc $ do
209215 ospaces m
210216 txt "}"
211217
212−-- | Surround given entity by square brackets.
218+-- | Surround given entity by square brackets @[@ and @]@.
213219
214220brackets :: R () -> R ()
215221brackets m = sitcc $ do
@@ -217,7 +223,7 @@ brackets m = sitcc $ do
217223 ospaces m
218224 txt "]"
219225
220−-- | Surround given entity by parallel array brackets @[:@ ond @:]@.
226+-- | Surround given entity by parallel array brackets @[:@ and @:]@.
221227
222228bracketsPar :: R () -> R ()
223229bracketsPar m = sitcc $ do
@@ -225,7 +231,7 @@ bracketsPar m = sitcc $ do
225231 m
226232 txt ":]"
227233
228−-- | Surround given entity by parentheses.
234+-- | Surround given entity by parentheses @(@ and @)@.
229235
230236parens :: R () -> R ()
231237parens m = sitcc $ do
src/Ormolu/Printer/Comments.hs+5−3
@@ -1,12 +1,14 @@
11{-# LANGUAGE LambdaCase #-}
22{-# LANGUAGE OverloadedStrings #-}
33
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.
56
67module Ormolu.Printer.Comments
78 ( spitPrecedingComments
89 , spitFollowingComments
910 , spitRemainingComments
11+ , hasMoreComments
1012 )
1113where
1214
@@ -80,11 +82,11 @@ spitFollowingComment (L ref a) mlastSpn = do
8082 i <- getIndent
8183 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
8284 if theSameLine l ref && not (isModule a)
83− then modNextline $ \m -> setIndent i $ do
85+ then modNewline $ \m -> setIndent i $ do
8486 spit " "
8587 spitComment comment
8688 m
87− else modNextline $ \m -> setIndent i $ do
89+ else modNewline $ \m -> setIndent i $ do
8890 m
8991 when (needsNewlineBefore l mlastSpn) newline
9092 spitComment comment
src/Ormolu/Printer/Internal.hs+14−13
@@ -14,7 +14,7 @@ module Ormolu.Printer.Internal
1414 -- * Internal functions
1515 , spit
1616 , newline
17− , modNextline
17+ , modNewline
1818 , ensureIndent
1919 , inci
2020 , sitcc
@@ -137,13 +137,7 @@ spit x = do
137137 }
138138 traceR "spit_after" Nothing
139139
140−-- | Output a newline. The 'modNewline' function can be used to alter what
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.
140+-- | Output a newline.
147141
148142newline :: R ()
149143newline = do
@@ -165,11 +159,18 @@ newlineRaw = do
165159 }
166160 traceR "newline_after" Nothing
167161
168−-- | Modify how next newline will be output. The argument of call-back is
169−-- the version of 'newline' built so far.
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+--
170+-- The argument of the call-back is the version of 'newline' built so far.
170171
171−modNextline :: (R () -> R ()) -> R ()
172−modNextline f = R $ do
172+modNewline :: (R () -> R ()) -> R ()
173+modNewline f = R $ do
173174 old <- gets scNewline
174175 modify $ \sc -> sc
175176 { scNewline = f old
@@ -311,7 +312,7 @@ hasMoreComments = R $ do
311312getIndent :: R Int
312313getIndent = R (asks rcIndent)
313314
314−-- | Set indentation level.
315+-- | Set indentation level for the given computation.
315316
316317setIndent :: Int -> R () -> R ()
317318setIndent i m' = do
src/Ormolu/SpanStream.hs+3−2
@@ -1,7 +1,7 @@
11{-# LANGUAGE DeriveDataTypeable #-}
22{-# LANGUAGE GeneralizedNewtypeDeriving #-}
33
4−-- | Build span stream of AST elements.
4+-- | Build span stream from AST.
55
66module Ormolu.SpanStream
77 ( SpanStream (..)
@@ -24,7 +24,8 @@ import qualified Data.DList as D
2424newtype SpanStream = SpanStream [RealSrcSpan]
2525 deriving (Eq, Show, Data, Semigroup, Monoid)
2626
27−-- | Create 'SpanStream' from a data structure containing 'RealSrcSpan's.
27+-- | Create 'SpanStream' from a data structure containing \"located\"
28+-- elements.
2829
2930mkSpanStream
3031 :: Data a
src/Ormolu/Utils.hs+4−2
@@ -1,3 +1,5 @@
1+-- | Random utilities used by the code.
2+
13module Ormolu.Utils
24 ( combineSrcSpans'
35 , isModule
@@ -20,9 +22,9 @@ combineSrcSpans' (x:|xs) = foldr combineSrcSpans x xs
2022isModule :: Data a => a -> Bool
2123isModule x = showConstr (toConstr x) == "HsModule"
2224
23−-- | Exact inner value from 'Located'.
25+-- | Exact inner value from a 'Located' thing.
2426
25−unL :: Located e -> e
27+unL :: GenLocated l e -> e
2628unL (L _ e) = e
2729
2830-- | Get source span from a 'Located' thing.