{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Ormolu.Processing.Preprocess
( preprocess,
)
where
import Control.Monad
import Data.Array as A
import Data.Bifunctor (bimap)
import Data.Char (isSpace)
import Data.Function ((&))
import Data.IntMap (IntMap)
import Data.IntMap.Strict qualified as IntMap
import Data.IntSet (IntSet)
import Data.IntSet qualified as IntSet
import Data.List qualified as L
import Data.Maybe (isJust)
import Data.Text (Text)
import Data.Text qualified as T
import Ormolu.Config (RegionDeltas (..))
import Ormolu.Processing.Common
import Ormolu.Processing.Cpp
preprocess ::
Bool ->
RegionDeltas ->
Text ->
[Either Text RegionDeltas]
preprocess :: Bool -> RegionDeltas -> Text -> [Either Text RegionDeltas]
preprocess Bool
cppEnabled RegionDeltas
region Text
rawInput = [Either Text RegionDeltas]
rawSnippetsAndRegionsToFormat
where
(IntSet
linesNotToFormat', IntMap Text
replacementLines) = Bool -> RegionDeltas -> Text -> (IntSet, IntMap Text)
linesNotToFormat Bool
cppEnabled RegionDeltas
region Text
rawInput
regionsToFormat :: [RegionDeltas]
regionsToFormat =
Key -> IntSet -> [RegionDeltas]
intSetToRegions Key
rawLineLength forall a b. (a -> b) -> a -> b
$
[Key] -> IntSet
IntSet.fromAscList [Key
1 .. Key
rawLineLength] IntSet -> IntSet -> IntSet
IntSet.\\ IntSet
linesNotToFormat'
regionsNotToFormat :: [RegionDeltas]
regionsNotToFormat = Key -> IntSet -> [RegionDeltas]
intSetToRegions Key
rawLineLength IntSet
linesNotToFormat'
interleave' :: [a] -> [a] -> [a]
interleave' = case [RegionDeltas]
regionsNotToFormat of
RegionDeltas
r : [RegionDeltas]
_ | RegionDeltas -> Key
regionPrefixLength RegionDeltas
r forall a. Eq a => a -> a -> Bool
== Key
0 -> forall {a}. [a] -> [a] -> [a]
interleave
[RegionDeltas]
_ -> forall a b c. (a -> b -> c) -> b -> a -> c
flip forall {a}. [a] -> [a] -> [a]
interleave
rawSnippets :: [Text]
rawSnippets = forall a b c. (a -> b -> c) -> b -> a -> c
flip RegionDeltas -> Text -> Text
linesInRegion Text
updatedInput forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RegionDeltas]
regionsNotToFormat
where
updatedInput :: Text
updatedInput = [Text] -> Text
T.unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Key, Text) -> Text
updateLine forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. [a] -> [b] -> [(a, b)]
zip [Key
1 ..] forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
T.lines forall a b. (a -> b) -> a -> b
$ Text
rawInput
updateLine :: (Key, Text) -> Text
updateLine (Key
i, Text
line) = forall a. a -> Key -> IntMap a -> a
IntMap.findWithDefault Text
line Key
i IntMap Text
replacementLines
rawSnippetsAndRegionsToFormat :: [Either Text RegionDeltas]
rawSnippetsAndRegionsToFormat =
forall {a}. [a] -> [a] -> [a]
interleave' (forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
rawSnippets) (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RegionDeltas]
regionsToFormat)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either Text RegionDeltas -> [Either Text RegionDeltas]
patchSeparatingBlankLines
forall a b. a -> (a -> b) -> b
& forall a. (a -> Bool) -> [a] -> [a]
dropWhile forall {b}. Either Text b -> Bool
isBlankRawSnippet
forall a b. a -> (a -> b) -> b
& forall a. (a -> Bool) -> [a] -> [a]
L.dropWhileEnd forall {b}. Either Text b -> Bool
isBlankRawSnippet
patchSeparatingBlankLines :: Either Text RegionDeltas -> [Either Text RegionDeltas]
patchSeparatingBlankLines = \case
Right r :: RegionDeltas
r@RegionDeltas {Key
regionSuffixLength :: RegionDeltas -> Key
regionSuffixLength :: Key
regionPrefixLength :: Key
regionPrefixLength :: RegionDeltas -> Key
..} ->
if (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isSpace (RegionDeltas -> Text -> Text
linesInRegion RegionDeltas
r Text
rawInput)
then [forall {b}. Either Text b
blankRawSnippet]
else
[forall {b}. Either Text b
blankRawSnippet | Key -> Bool
isBlankLine Key
regionPrefixLength]
forall a. Semigroup a => a -> a -> a
<> [forall a b. b -> Either a b
Right RegionDeltas
r]
forall a. Semigroup a => a -> a -> a
<> [forall {b}. Either Text b
blankRawSnippet | Key -> Bool
isBlankLine (Key
rawLineLength forall a. Num a => a -> a -> a
- Key
regionSuffixLength forall a. Num a => a -> a -> a
- Key
1)]
Left Text
r -> [forall a b. a -> Either a b
Left Text
r]
where
blankRawSnippet :: Either Text b
blankRawSnippet = forall a b. a -> Either a b
Left Text
"\n"
isBlankLine :: Key -> Bool
isBlankLine Key
i = forall a. Maybe a -> Bool
isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isSpace) forall a b. (a -> b) -> a -> b
$ Array Key Text
rawLines forall {a}. Array Key a -> Key -> Maybe a
!!? Key
i
isBlankRawSnippet :: Either Text b -> Bool
isBlankRawSnippet = \case
Left Text
r | (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isSpace Text
r -> Bool
True
Either Text b
_ -> Bool
False
rawLines :: Array Key Text
rawLines = forall i e. Ix i => (i, i) -> [e] -> Array i e
A.listArray (Key
0, forall (t :: * -> *) a. Foldable t => t a -> Key
length [Text]
rawLines' forall a. Num a => a -> a -> a
- Key
1) [Text]
rawLines'
where
rawLines' :: [Text]
rawLines' = Text -> [Text]
T.lines Text
rawInput
rawLineLength :: Key
rawLineLength = forall (t :: * -> *) a. Foldable t => t a -> Key
length Array Key Text
rawLines
interleave :: [a] -> [a] -> [a]
interleave [] [a]
bs = [a]
bs
interleave (a
a : [a]
as) [a]
bs = a
a forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
interleave [a]
bs [a]
as
Array Key a
xs !!? :: Array Key a -> Key -> Maybe a
!!? Key
i = if forall i e. Array i e -> (i, i)
A.bounds Array Key Text
rawLines forall a. Ix a => (a, a) -> a -> Bool
`A.inRange` Key
i then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Array Key a
xs forall i e. Ix i => Array i e -> i -> e
A.! Key
i else forall a. Maybe a
Nothing
linesNotToFormat ::
Bool ->
RegionDeltas ->
Text ->
(IntSet, IntMap Text)
linesNotToFormat :: Bool -> RegionDeltas -> Text -> (IntSet, IntMap Text)
linesNotToFormat Bool
cppEnabled region :: RegionDeltas
region@RegionDeltas {Key
regionSuffixLength :: Key
regionPrefixLength :: Key
regionSuffixLength :: RegionDeltas -> Key
regionPrefixLength :: RegionDeltas -> Key
..} Text
input =
(IntSet
unconsidered forall a. Semigroup a => a -> a -> a
<> IntSet
magicDisabled forall a. Semigroup a => a -> a -> a
<> IntSet
otherDisabled, IntMap Text
lineUpdates)
where
unconsidered :: IntSet
unconsidered =
[Key] -> IntSet
IntSet.fromAscList forall a b. (a -> b) -> a -> b
$
[Key
1 .. Key
regionPrefixLength] forall a. Semigroup a => a -> a -> a
<> [Key
totalLines forall a. Num a => a -> a -> a
- Key
regionSuffixLength forall a. Num a => a -> a -> a
+ Key
1 .. Key
totalLines]
totalLines :: Key
totalLines = forall (t :: * -> *) a. Foldable t => t a -> Key
length (Text -> [Text]
T.lines Text
input)
regionLines :: Text
regionLines = RegionDeltas -> Text -> Text
linesInRegion RegionDeltas
region Text
input
(IntSet
magicDisabled, IntMap Text
lineUpdates) = Text -> (IntSet, IntMap Text)
magicDisabledLines Text
regionLines
otherDisabled :: IntSet
otherDisabled = (forall a. Monoid a => [a] -> a
mconcat [Text -> IntSet]
allLines) Text
regionLines
where
allLines :: [Text -> IntSet]
allLines = [Text -> IntSet
shebangLines, Text -> IntSet
linePragmaLines] forall a. Semigroup a => a -> a -> a
<> [Text -> IntSet
cppLines | Bool
cppEnabled]
data OrmoluState
=
OrmoluEnabled
|
OrmoluDisabled
deriving (OrmoluState -> OrmoluState -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OrmoluState -> OrmoluState -> Bool
$c/= :: OrmoluState -> OrmoluState -> Bool
== :: OrmoluState -> OrmoluState -> Bool
$c== :: OrmoluState -> OrmoluState -> Bool
Eq, Key -> OrmoluState -> ShowS
[OrmoluState] -> ShowS
OrmoluState -> String
forall a.
(Key -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OrmoluState] -> ShowS
$cshowList :: [OrmoluState] -> ShowS
show :: OrmoluState -> String
$cshow :: OrmoluState -> String
showsPrec :: Key -> OrmoluState -> ShowS
$cshowsPrec :: Key -> OrmoluState -> ShowS
Show)
magicDisabledLines :: Text -> (IntSet, IntMap Text)
magicDisabledLines :: Text -> (IntSet, IntMap Text)
magicDisabledLines Text
input =
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap [Key] -> IntSet
IntSet.fromAscList forall a. [(Key, a)] -> IntMap a
IntMap.fromAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$
forall {a}. OrmoluState -> [(Text, a)] -> [([a], [(a, Text)])]
go OrmoluState
OrmoluEnabled (Text -> [Text]
T.lines Text
input forall a b. [a] -> [b] -> [(a, b)]
`zip` [Key
1 ..])
where
go :: OrmoluState -> [(Text, a)] -> [([a], [(a, Text)])]
go OrmoluState
_ [] = []
go OrmoluState
state ((Text
line, a
i) : [(Text, a)]
ls)
| Just Text
rest <- Text -> Text -> Maybe Text
isMagicComment Text
ormoluDisable Text
line,
OrmoluState
state forall a. Eq a => a -> a -> Bool
== OrmoluState
OrmoluEnabled =
([a
i], [(a
i, Text -> Text
magicComment Text
ormoluDisable forall a. Semigroup a => a -> a -> a
<> Text
rest)]) forall a. a -> [a] -> [a]
: OrmoluState -> [(Text, a)] -> [([a], [(a, Text)])]
go OrmoluState
OrmoluDisabled [(Text, a)]
ls
| Just Text
rest <- Text -> Text -> Maybe Text
isMagicComment Text
ormoluEnable Text
line,
OrmoluState
state forall a. Eq a => a -> a -> Bool
== OrmoluState
OrmoluDisabled =
([a
i], [(a
i, Text -> Text
magicComment Text
ormoluEnable forall a. Semigroup a => a -> a -> a
<> Text
rest)]) forall a. a -> [a] -> [a]
: OrmoluState -> [(Text, a)] -> [([a], [(a, Text)])]
go OrmoluState
OrmoluEnabled [(Text, a)]
ls
| Bool
otherwise = forall {a}. ([a], [a])
iIfDisabled forall a. a -> [a] -> [a]
: OrmoluState -> [(Text, a)] -> [([a], [(a, Text)])]
go OrmoluState
state [(Text, a)]
ls
where
iIfDisabled :: ([a], [a])
iIfDisabled = case OrmoluState
state of
OrmoluState
OrmoluDisabled -> ([a
i], [])
OrmoluState
OrmoluEnabled -> ([], [])
linesFiltered :: (Text -> Bool) -> Text -> IntSet
linesFiltered :: (Text -> Bool) -> Text -> IntSet
linesFiltered Text -> Bool
p =
[Key] -> IntSet
IntSet.fromAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. [a] -> [b] -> [(a, b)]
`zip` [Key
1 ..]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
T.lines
shebangLines :: Text -> IntSet
shebangLines :: Text -> IntSet
shebangLines = (Text -> Bool) -> Text -> IntSet
linesFiltered (Text
"#!" `T.isPrefixOf`)
linePragmaLines :: Text -> IntSet
linePragmaLines :: Text -> IntSet
linePragmaLines = (Text -> Bool) -> Text -> IntSet
linesFiltered (Text
"{-# LINE" `T.isPrefixOf`)
ormoluEnable :: Text
ormoluEnable :: Text
ormoluEnable = Text
"ORMOLU_ENABLE"
ormoluDisable :: Text
ormoluDisable :: Text
ormoluDisable = Text
"ORMOLU_DISABLE"
magicComment :: Text -> Text
Text
t = Text
"{- " forall a. Semigroup a => a -> a -> a
<> Text
t forall a. Semigroup a => a -> a -> a
<> Text
" -}"
isMagicComment ::
Text ->
Text ->
Maybe Text
Text
expected Text
s0 = do
Text
s1 <- Text -> Text
T.stripStart forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> Maybe Text
T.stripPrefix Text
"{-" (Text -> Text
T.stripStart Text
s0)
Text
s2 <- Text -> Text
T.stripStart forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> Maybe Text
T.stripPrefix Text
expected Text
s1
Text -> Text -> Maybe Text
T.stripPrefix Text
"-}" Text
s2