on master help sign in
Handle magic comments for disabling/enabling with more flexibility
commit67e43e0057cc624698e508d0236d82225d13b61b authorMark Karpov <markkarpov92@gmail.com> dateWed 3 Jun 2020, 14:03 (6 years ago) parent2f53306
7 files changed+33−6
CHANGELOG.md+5 data/examples/other/disabling/indented-fragment-out.hs+7 data/examples/other/disabling/indented-fragment.hs+7 src/Ormolu/Parser/CommentStream.hs+7−2 src/Ormolu/Processing/Common.hs+1−1 src/Ormolu/Processing/Postprocess.hs+3−1 src/Ormolu/Processing/Preprocess.hs+3−2
CHANGELOG.md+5
@@ -2,6 +2,11 @@
22
33* Duplicate imports in a single import list are now eliminated.
44
5+* The magic comments for disabling and enabling Ormolu now can encompass any
6+ fragment of code provided that the remaining code after exclusion of the
7+ disabled part is still syntactically correct. [Issue
8+ 601](https://github.com/tweag/ormolu/issues/601).
9+
510## Ormolu 0.1.0.0
611
712* Fixed rendering of type signatures concerning several identifiers. [Issue
data/examples/other/disabling/indented-fragment-out.hsnew file+7
@@ -0,0 +1,7 @@
1+foo =
2+ {- ORMOLU_DISABLE -}
3+ testCase "Foo" testFoo :
4+ testCase "Bar" testBar :
5+ testCase "Baz" testBaz :
6+ {- ORMOLU_ENABLE -}
7+ []
data/examples/other/disabling/indented-fragment.hsnew file+7
@@ -0,0 +1,7 @@
1+foo =
2+ {- ORMOLU_DISABLE -}
3+ testCase "Foo" testFoo :
4+ testCase "Bar" testBar :
5+ testCase "Baz" testBaz :
6+ {- ORMOLU_ENABLE -}
7+ []
src/Ormolu/Parser/CommentStream.hs+7−2
@@ -27,6 +27,7 @@ import qualified GHC
2727import qualified Lexer as GHC
2828import Ormolu.Parser.Pragma
2929import Ormolu.Parser.Shebang
30+import Ormolu.Processing.Common
3031import Ormolu.Utils (showOutputable)
3132import SrcLoc
3233
@@ -109,11 +110,15 @@ mkComment ls (L l s) = (ls', comment)
109110 Nothing -> s :| []
110111 Just (x :| xs) ->
111112 let getIndent y =
112− if all isSpace y
113+ if all isSpace y || y == endDisabling
113114 then startIndent
114115 else length (takeWhile isSpace y)
115116 n = minimum (startIndent : fmap getIndent xs)
116− in x :| (drop n <$> xs)
117+ removeIndent y =
118+ if y == endDisabling
119+ then y
120+ else drop n y
121+ in x :| (removeIndent <$> xs)
117122 else s :| []
118123 (atomsBefore, ls') =
119124 case dropWhile ((< commentLine) . fst) ls of
src/Ormolu/Processing/Common.hs+1−1
@@ -20,7 +20,7 @@ data OrmoluState
2020
2121-- | Marker for the beginning of the region where Ormolu should be disabled.
2222startDisabling :: IsString s => s
23−startDisabling = "{- ORMOLU_DISABLING_START"
23+startDisabling = "{- ORMOLU_DISABLE_START"
2424
2525-- | Marker for the end of the region where Ormolu should be disabled.
2626endDisabling :: IsString s => s
src/Ormolu/Processing/Postprocess.hs+3−1
@@ -1,3 +1,5 @@
1+{-# LANGUAGE ViewPatterns #-}
2+
13-- | Postprocessing for the results of printing.
24module Ormolu.Processing.Postprocess
35 ( postprocess,
@@ -17,5 +19,5 @@ postprocess =
1719 . filter (not . magicComment)
1820 . T.lines
1921 where
20− magicComment x =
22+ magicComment (T.stripStart -> x) =
2123 x == startDisabling || x == endDisabling
src/Ormolu/Processing/Preprocess.hs+3−2
@@ -143,7 +143,8 @@ magicComment ::
143143 -- | Whether or not the two strings watch
144144 Bool
145145magicComment expected s0 = isJust $ do
146− s1 <- dropWhile isSpace <$> L.stripPrefix "{-" s0
147− s2 <- dropWhile isSpace <$> L.stripPrefix expected s1
146+ let trim = dropWhile isSpace
147+ s1 <- trim <$> L.stripPrefix "{-" (trim s0)
148+ s2 <- trim <$> L.stripPrefix expected s1
148149 s3 <- L.stripPrefix "-}" s2
149150 guard (all isSpace s3)