From f1dd5c4be6065bedc8cd764767b7c05420a9d40d Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Tue, 5 Jul 2016 15:21:44 +0200 Subject: Fix error in overlapping blocks --- lib/Language/Haskell/Stylish/Block.hs | 11 ++++++++--- lib/Language/Haskell/Stylish/Editor.hs | 9 ++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/Language/Haskell/Stylish/Block.hs b/lib/Language/Haskell/Stylish/Block.hs index fd680a8..d4cca7d 100644 --- a/lib/Language/Haskell/Stylish/Block.hs +++ b/lib/Language/Haskell/Stylish/Block.hs @@ -16,6 +16,7 @@ module Language.Haskell.Stylish.Block -------------------------------------------------------------------------------- import Control.Arrow (arr, (&&&), (>>>)) +import qualified Data.IntSet as IS import qualified Language.Haskell.Exts.Annotated as H @@ -73,10 +74,14 @@ merge (Block s1 e1) (Block s2 e2) = Block (min s1 s2) (max e1 e2) -------------------------------------------------------------------------------- overlapping :: [Block a] -> Bool -overlapping blocks = - any (uncurry overlapping') $ zip blocks (drop 1 blocks) +overlapping = go IS.empty where - overlapping' (Block _ e1) (Block s2 _) = e1 >= s2 + go _ [] = False + go acc (b : bs) = + let ints = [blockStart b .. blockEnd b] in + if any (`IS.member` acc) ints + then True + else go (IS.union acc $ IS.fromList ints) bs -------------------------------------------------------------------------------- diff --git a/lib/Language/Haskell/Stylish/Editor.hs b/lib/Language/Haskell/Stylish/Editor.hs index 5d5a864..24d0a94 100644 --- a/lib/Language/Haskell/Stylish/Editor.hs +++ b/lib/Language/Haskell/Stylish/Editor.hs @@ -18,6 +18,10 @@ module Language.Haskell.Stylish.Editor ) where +-------------------------------------------------------------------------------- +import Data.List (intercalate) + + -------------------------------------------------------------------------------- import Language.Haskell.Stylish.Block @@ -40,11 +44,14 @@ applyChanges :: [Change a] -> [a] -> [a] applyChanges changes | overlapping blocks = error $ "Language.Haskell.Stylish.Editor.applyChanges: " ++ - "refusing to make overlapping changes" + "refusing to make overlapping changes on lines " ++ + intercalate ", " (map printBlock blocks) | otherwise = go 1 changes where blocks = map changeBlock changes + printBlock b = show (blockStart b) ++ "-" ++ show (blockEnd b) + go _ [] ls = ls go n (ch : chs) ls = -- Divide the remaining lines into: -- cgit v1.2.3