{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Preprocessing for input source code.
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 the specified region of the input into raw snippets
-- and subregions to be formatted.
preprocess ::
  -- | Whether CPP is enabled
  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'
    -- We want to interleave the regionsToFormat and regionsNotToFormat.
    -- If the first non-formattable region starts at the first line, it is
    -- the first interleaved region, otherwise, we start with the first
    -- region to format.
    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
    -- For every formattable region, we want to ensure that it is separated by
    -- a blank line from preceding/succeeding raw snippets if it starts/ends
    -- with a blank line.
    -- Empty formattable regions are replaced by a blank line instead.
    -- Extraneous raw snippets at the start/end are dropped afterwards.
    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

-- | All lines we are not supposed to format, and a set of replacements
-- for specific lines.
linesNotToFormat ::
  -- | Whether CPP is enabled
  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]

-- | Ormolu state.
data OrmoluState
  = -- | Enabled
    OrmoluEnabled
  | -- | Disabled
    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)

-- | All lines which are disabled by Ormolu's magic comments,
-- as well as normalizing replacements.
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 -> ([], [])

-- | All lines which satisfy a predicate.
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

-- | Lines which contain a shebang.
shebangLines :: Text -> IntSet
shebangLines :: Text -> IntSet
shebangLines = (Text -> Bool) -> Text -> IntSet
linesFiltered (Text
"#!" `T.isPrefixOf`)

-- | Lines which contain a LINE pragma.
linePragmaLines :: Text -> IntSet
linePragmaLines :: Text -> IntSet
linePragmaLines = (Text -> Bool) -> Text -> IntSet
linesFiltered (Text
"{-# LINE" `T.isPrefixOf`)

-- | Inner text of a magic enabling marker.
ormoluEnable :: Text
ormoluEnable :: Text
ormoluEnable = Text
"ORMOLU_ENABLE"

-- | Inner text of a magic disabling marker.
ormoluDisable :: Text
ormoluDisable :: Text
ormoluDisable = Text
"ORMOLU_DISABLE"

-- | Creates a magic comment with the given inner text.
magicComment :: Text -> Text
magicComment :: Text -> Text
magicComment Text
t = Text
"{- " forall a. Semigroup a => a -> a -> a
<> Text
t forall a. Semigroup a => a -> a -> a
<> Text
" -}"

-- | Construct a function for whitespace-insensitive matching of string.
isMagicComment ::
  -- | What to expect
  Text ->
  -- | String to test
  Text ->
  -- | If the two strings match, we return the rest of the line.
  Maybe Text
isMagicComment :: Text -> Text -> Maybe Text
isMagicComment 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