1
1
mirror of https://github.com/google/ormolu.git synced 2024-09-11 08:05:24 +03:00

Handle magic comments for disabling/enabling with more flexibility

This commit is contained in:
Mark Karpov 2020-06-03 14:03:04 +02:00
parent 2f53306ff7
commit 67e43e0057
7 changed files with 33 additions and 6 deletions

View File

@ -2,6 +2,11 @@
* Duplicate imports in a single import list are now eliminated.
* The magic comments for disabling and enabling Ormolu now can encompass any
fragment of code provided that the remaining code after exclusion of the
disabled part is still syntactically correct. [Issue
601](https://github.com/tweag/ormolu/issues/601).
## Ormolu 0.1.0.0
* Fixed rendering of type signatures concerning several identifiers. [Issue

View File

@ -0,0 +1,7 @@
foo =
{- ORMOLU_DISABLE -}
testCase "Foo" testFoo :
testCase "Bar" testBar :
testCase "Baz" testBaz :
{- ORMOLU_ENABLE -}
[]

View File

@ -0,0 +1,7 @@
foo =
{- ORMOLU_DISABLE -}
testCase "Foo" testFoo :
testCase "Bar" testBar :
testCase "Baz" testBaz :
{- ORMOLU_ENABLE -}
[]

View File

@ -27,6 +27,7 @@ import qualified GHC
import qualified Lexer as GHC
import Ormolu.Parser.Pragma
import Ormolu.Parser.Shebang
import Ormolu.Processing.Common
import Ormolu.Utils (showOutputable)
import SrcLoc
@ -109,11 +110,15 @@ mkComment ls (L l s) = (ls', comment)
Nothing -> s :| []
Just (x :| xs) ->
let getIndent y =
if all isSpace y
if all isSpace y || y == endDisabling
then startIndent
else length (takeWhile isSpace y)
n = minimum (startIndent : fmap getIndent xs)
in x :| (drop n <$> xs)
removeIndent y =
if y == endDisabling
then y
else drop n y
in x :| (removeIndent <$> xs)
else s :| []
(atomsBefore, ls') =
case dropWhile ((< commentLine) . fst) ls of

View File

@ -20,7 +20,7 @@ data OrmoluState
-- | Marker for the beginning of the region where Ormolu should be disabled.
startDisabling :: IsString s => s
startDisabling = "{- ORMOLU_DISABLING_START"
startDisabling = "{- ORMOLU_DISABLE_START"
-- | Marker for the end of the region where Ormolu should be disabled.
endDisabling :: IsString s => s

View File

@ -1,3 +1,5 @@
{-# LANGUAGE ViewPatterns #-}
-- | Postprocessing for the results of printing.
module Ormolu.Processing.Postprocess
( postprocess,
@ -17,5 +19,5 @@ postprocess =
. filter (not . magicComment)
. T.lines
where
magicComment x =
magicComment (T.stripStart -> x) =
x == startDisabling || x == endDisabling

View File

@ -143,7 +143,8 @@ magicComment ::
-- | Whether or not the two strings watch
Bool
magicComment expected s0 = isJust $ do
s1 <- dropWhile isSpace <$> L.stripPrefix "{-" s0
s2 <- dropWhile isSpace <$> L.stripPrefix expected s1
let trim = dropWhile isSpace
s1 <- trim <$> L.stripPrefix "{-" (trim s0)
s2 <- trim <$> L.stripPrefix expected s1
s3 <- L.stripPrefix "-}" s2
guard (all isSpace s3)