module Language.Haskell.Brittany.Internal.Layouters.Import (layoutImport) where

#include "prelude.inc"

import           Language.Haskell.Brittany.Internal.Types
import           Language.Haskell.Brittany.Internal.LayouterBasics
import           Language.Haskell.Brittany.Internal.Layouters.IE
import           Language.Haskell.Brittany.Internal.Config.Types

import           GHC                                      ( unLoc
                                                          , GenLocated(L)
                                                          , moduleNameString
                                                          , Located
                                                          )
#if MIN_VERSION_ghc(8,10,1)   /* ghc-8.10.1 */
import           GHC.Hs
#else
import           HsSyn
#endif
import           Name
import           FieldLabel
import qualified FastString
import           BasicTypes
import qualified Language.Haskell.GHC.ExactPrint.Types as ExactPrint.Types

import           Language.Haskell.Brittany.Internal.Utils



prepPkg :: SourceText -> String
prepPkg :: SourceText -> String
prepPkg SourceText
rawN = case SourceText
rawN of
  SourceText String
n -> String
n
  -- This would be odd to encounter and the
  -- result will most certainly be wrong
  SourceText
NoSourceText -> String
""
prepModName :: Located e -> e
prepModName :: Located e -> e
prepModName = Located e -> e
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc

layoutImport :: ImportDecl GhcPs -> ToBriDocM BriDocNumbered
layoutImport :: ImportDecl GhcPs -> ToBriDocM BriDocNumbered
layoutImport ImportDecl GhcPs
importD = case ImportDecl GhcPs
importD of
  ImportDecl XCImportDecl GhcPs
_ SourceText
_ (L SrcSpan
_ ModuleName
modName) Maybe StringLiteral
pkg Bool
src Bool
safe ImportDeclQualifiedStyle
q Bool
False Maybe (GenLocated SrcSpan ModuleName)
mas Maybe (Bool, Located [LIE GhcPs])
mllies -> do
    Int
importCol <- MultiRWST
  '[Config, Anns]
  '[[BrittanyError], Seq String]
  '[NodeAllocIndex]
  Identity
  Config
forall a (m :: * -> *). MonadMultiReader a m => m a
mAsk MultiRWST
  '[Config, Anns]
  '[[BrittanyError], Seq String]
  '[NodeAllocIndex]
  Identity
  Config
-> (Config -> Int)
-> MultiRWST
     '[Config, Anns]
     '[[BrittanyError], Seq String]
     '[NodeAllocIndex]
     Identity
     Int
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Config -> CLayoutConfig Identity
forall (f :: * -> *). CConfig f -> CLayoutConfig f
_conf_layout (Config -> CLayoutConfig Identity)
-> (CLayoutConfig Identity -> Identity (Last Int))
-> Config
-> Identity (Last Int)
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> CLayoutConfig Identity -> Identity (Last Int)
forall (f :: * -> *). CLayoutConfig f -> f (Last Int)
_lconfig_importColumn (Config -> Identity (Last Int))
-> (Identity (Last Int) -> Int) -> Config -> Int
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> Identity (Last Int) -> Int
forall a b. Coercible a b => Identity a -> b
confUnpack
    Int
importAsCol <- MultiRWST
  '[Config, Anns]
  '[[BrittanyError], Seq String]
  '[NodeAllocIndex]
  Identity
  Config
forall a (m :: * -> *). MonadMultiReader a m => m a
mAsk MultiRWST
  '[Config, Anns]
  '[[BrittanyError], Seq String]
  '[NodeAllocIndex]
  Identity
  Config
-> (Config -> Int)
-> MultiRWST
     '[Config, Anns]
     '[[BrittanyError], Seq String]
     '[NodeAllocIndex]
     Identity
     Int
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Config -> CLayoutConfig Identity
forall (f :: * -> *). CConfig f -> CLayoutConfig f
_conf_layout (Config -> CLayoutConfig Identity)
-> (CLayoutConfig Identity -> Identity (Last Int))
-> Config
-> Identity (Last Int)
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> CLayoutConfig Identity -> Identity (Last Int)
forall (f :: * -> *). CLayoutConfig f -> f (Last Int)
_lconfig_importAsColumn (Config -> Identity (Last Int))
-> (Identity (Last Int) -> Int) -> Config -> Int
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> Identity (Last Int) -> Int
forall a b. Coercible a b => Identity a -> b
confUnpack
    IndentPolicy
indentPolicy <- MultiRWST
  '[Config, Anns]
  '[[BrittanyError], Seq String]
  '[NodeAllocIndex]
  Identity
  Config
forall a (m :: * -> *). MonadMultiReader a m => m a
mAsk MultiRWST
  '[Config, Anns]
  '[[BrittanyError], Seq String]
  '[NodeAllocIndex]
  Identity
  Config
-> (Config -> IndentPolicy)
-> MultiRWST
     '[Config, Anns]
     '[[BrittanyError], Seq String]
     '[NodeAllocIndex]
     Identity
     IndentPolicy
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&>  Config -> CLayoutConfig Identity
forall (f :: * -> *). CConfig f -> CLayoutConfig f
_conf_layout (Config -> CLayoutConfig Identity)
-> (CLayoutConfig Identity -> Identity (Last IndentPolicy))
-> Config
-> Identity (Last IndentPolicy)
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> CLayoutConfig Identity -> Identity (Last IndentPolicy)
forall (f :: * -> *). CLayoutConfig f -> f (Last IndentPolicy)
_lconfig_indentPolicy (Config -> Identity (Last IndentPolicy))
-> (Identity (Last IndentPolicy) -> IndentPolicy)
-> Config
-> IndentPolicy
forall a b c. (a -> b) -> (b -> c) -> a -> c
.> Identity (Last IndentPolicy) -> IndentPolicy
forall a b. Coercible a b => Identity a -> b
confUnpack
    let
      compact :: Bool
compact  = IndentPolicy
indentPolicy IndentPolicy -> IndentPolicy -> Bool
forall a. Eq a => a -> a -> Bool
/= IndentPolicy
IndentPolicyFree
      modNameT :: Text
modNameT = String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ModuleName -> String
moduleNameString ModuleName
modName
      pkgNameT :: Maybe Text
pkgNameT = String -> Text
Text.pack (String -> Text)
-> (StringLiteral -> String) -> StringLiteral -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceText -> String
prepPkg (SourceText -> String)
-> (StringLiteral -> SourceText) -> StringLiteral -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StringLiteral -> SourceText
sl_st (StringLiteral -> Text) -> Maybe StringLiteral -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe StringLiteral
pkg
      masT :: Maybe Text
masT     = String -> Text
Text.pack (String -> Text)
-> (GenLocated SrcSpan ModuleName -> String)
-> GenLocated SrcSpan ModuleName
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleName -> String
moduleNameString (ModuleName -> String)
-> (GenLocated SrcSpan ModuleName -> ModuleName)
-> GenLocated SrcSpan ModuleName
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpan ModuleName -> ModuleName
forall e. Located e -> e
prepModName (GenLocated SrcSpan ModuleName -> Text)
-> Maybe (GenLocated SrcSpan ModuleName) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (GenLocated SrcSpan ModuleName)
mas
      hiding :: Bool
hiding   = Bool
-> ((Bool, Located [LIE GhcPs]) -> Bool)
-> Maybe (Bool, Located [LIE GhcPs])
-> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Bool, Located [LIE GhcPs]) -> Bool
forall a b. (a, b) -> a
fst Maybe (Bool, Located [LIE GhcPs])
mllies
      minQLength :: Int
minQLength = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"import qualified "
      qLengthReal :: Int
qLengthReal =
#if MIN_VERSION_ghc(8,10,1)   /* ghc-8.10.1 */
        let qualifiedPart :: Int
qualifiedPart = if ImportDeclQualifiedStyle
q ImportDeclQualifiedStyle -> ImportDeclQualifiedStyle -> Bool
forall a. Eq a => a -> a -> Bool
/= ImportDeclQualifiedStyle
NotQualified then String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"qualified " else Int
0
#else
        let qualifiedPart = if q then length "qualified " else 0
#endif
            safePart :: Int
safePart      = if Bool
safe then String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"safe " else Int
0
            pkgPart :: Int
pkgPart       = Int -> (Text -> Int) -> Maybe Text -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 ((Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> Int) -> (Text -> Int) -> Text -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int
Text.length) Maybe Text
pkgNameT
            srcPart :: Int
srcPart       = if Bool
src then String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"{-# SOURCE #-} " else Int
0
        in  String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"import " Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
srcPart Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
safePart Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
qualifiedPart Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
pkgPart
      qLength :: Int
qLength          = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
minQLength Int
qLengthReal
      -- Cost in columns of importColumn
      asCost :: Int
asCost           = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"as "
      hidingParenCost :: Int
hidingParenCost  = if Bool
hiding then String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"hiding ( " else String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
"( "
      nameCost :: Int
nameCost         = Text -> Int
Text.length Text
modNameT Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
qLength
      importQualifiers :: ToBriDocM BriDocNumbered
importQualifiers = [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq
        [ ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit (Text -> ToBriDocM BriDocNumbered)
-> Text -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
"import"
        , if Bool
src then ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit (Text -> ToBriDocM BriDocNumbered)
-> Text -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
"{-# SOURCE #-}" else ToBriDocM BriDocNumbered
docEmpty
        , if Bool
safe then ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit (Text -> ToBriDocM BriDocNumbered)
-> Text -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
"safe" else ToBriDocM BriDocNumbered
docEmpty
#if MIN_VERSION_ghc(8,10,1)   /* ghc-8.10.1 */
        , if ImportDeclQualifiedStyle
q ImportDeclQualifiedStyle -> ImportDeclQualifiedStyle -> Bool
forall a. Eq a => a -> a -> Bool
/= ImportDeclQualifiedStyle
NotQualified then ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit (Text -> ToBriDocM BriDocNumbered)
-> Text -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
"qualified" else ToBriDocM BriDocNumbered
docEmpty
#else
        , if q then appSep $ docLit $ Text.pack "qualified" else docEmpty
#endif
        , ToBriDocM BriDocNumbered
-> (Text -> ToBriDocM BriDocNumbered)
-> Maybe Text
-> ToBriDocM BriDocNumbered
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ToBriDocM BriDocNumbered
docEmpty (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> (Text -> ToBriDocM BriDocNumbered)
-> Text
-> ToBriDocM BriDocNumbered
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ToBriDocM BriDocNumbered
docLit) Maybe Text
pkgNameT
        ]
      indentName :: ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
indentName =
        if Bool
compact then ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a. a -> a
id else BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docEnsureIndent (Int -> BrIndent
BrIndentSpecial Int
qLength)
      modNameD :: ToBriDocM BriDocNumbered
modNameD =
        ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
indentName (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit Text
modNameT
      hidDocCol :: Int
hidDocCol = if Bool
hiding then Int
importCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hidingParenCost else Int
importCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
      hidDocColDiff :: Int
hidDocColDiff = Int
importCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hidDocCol
      hidDoc :: ToBriDocM BriDocNumbered
hidDoc = if Bool
hiding
        then ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit (Text -> ToBriDocM BriDocNumbered)
-> Text -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
"hiding"
        else ToBriDocM BriDocNumbered
docEmpty
      importHead :: ToBriDocM BriDocNumbered
importHead = [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
importQualifiers, ToBriDocM BriDocNumbered
modNameD]
      bindingsD :: ToBriDocM BriDocNumbered
bindingsD  = case Maybe (Bool, Located [LIE GhcPs])
mllies of
        Maybe (Bool, Located [LIE GhcPs])
Nothing -> ToBriDocM BriDocNumbered
docEmpty
        Just (Bool
_, Located [LIE GhcPs]
llies) -> do
          Bool
hasComments <- Located [LIE GhcPs] -> ToBriDocM Bool
forall ast. Data ast => Located ast -> ToBriDocM Bool
hasAnyCommentsBelow Located [LIE GhcPs]
llies
          if Bool
compact
          then [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docAlt
            [ [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
hidDoc, ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docForceSingleline (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Bool
-> SortItemsFlag -> Located [LIE GhcPs] -> ToBriDocM BriDocNumbered
layoutLLIEs Bool
True SortItemsFlag
ShouldSortItems Located [LIE GhcPs]
llies]
            , let makeParIfHiding :: ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
makeParIfHiding = if Bool
hiding
                    then BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docAddBaseY BrIndent
BrIndentRegular (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docPar ToBriDocM BriDocNumbered
hidDoc
                    else ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a. a -> a
id
              in ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
makeParIfHiding (Bool
-> SortItemsFlag -> Located [LIE GhcPs] -> ToBriDocM BriDocNumbered
layoutLLIEs Bool
True SortItemsFlag
ShouldSortItems Located [LIE GhcPs]
llies)
            ]
          else do
            [ToBriDocM BriDocNumbered]
ieDs <- SortItemsFlag
-> Located [LIE GhcPs] -> ToBriDocM [ToBriDocM BriDocNumbered]
layoutAnnAndSepLLIEs SortItemsFlag
ShouldSortItems Located [LIE GhcPs]
llies
            Located [LIE GhcPs]
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a ast. (DocWrapable a, Data ast) => Located ast -> a -> a
docWrapNodeRest Located [LIE GhcPs]
llies
              (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docEnsureIndent (Int -> BrIndent
BrIndentSpecial Int
hidDocCol)
              (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ case [ToBriDocM BriDocNumbered]
ieDs of
                -- ..[hiding].( )
                [] -> if Bool
hasComments
                  then ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docPar
                    ([ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
hidDoc, ToBriDocM BriDocNumbered
docParenLSep, Located [LIE GhcPs]
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a ast. (DocWrapable a, Data ast) => Located ast -> a -> a
docWrapNode Located [LIE GhcPs]
llies ToBriDocM BriDocNumbered
docEmpty])
                    (BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docEnsureIndent (Int -> BrIndent
BrIndentSpecial Int
hidDocColDiff) ToBriDocM BriDocNumbered
docParenR)
                  else [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
hidDoc, ToBriDocM BriDocNumbered
docParenLSep, ToBriDocM BriDocNumbered
docSeparator, ToBriDocM BriDocNumbered
docParenR]
                -- ..[hiding].( b )
                [ToBriDocM BriDocNumbered
ieD] -> CollectAltM () -> ToBriDocM BriDocNumbered
runFilteredAlternative (CollectAltM () -> ToBriDocM BriDocNumbered)
-> CollectAltM () -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ do
                  Bool -> ToBriDocM BriDocNumbered -> CollectAltM ()
addAlternativeCond (Bool -> Bool
not Bool
hasComments)
                    (ToBriDocM BriDocNumbered -> CollectAltM ())
-> ToBriDocM BriDocNumbered -> CollectAltM ()
forall a b. (a -> b) -> a -> b
$ [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq
                    [ ToBriDocM BriDocNumbered
hidDoc
                    , ToBriDocM BriDocNumbered
docParenLSep
                    , ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docForceSingleline ToBriDocM BriDocNumbered
ieD
                    , ToBriDocM BriDocNumbered
docSeparator
                    , ToBriDocM BriDocNumbered
docParenR
                    ]
                  ToBriDocM BriDocNumbered -> CollectAltM ()
addAlternative (ToBriDocM BriDocNumbered -> CollectAltM ())
-> ToBriDocM BriDocNumbered -> CollectAltM ()
forall a b. (a -> b) -> a -> b
$ ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docPar
                    ([ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
hidDoc, ToBriDocM BriDocNumbered
docParenLSep, ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docNonBottomSpacing ToBriDocM BriDocNumbered
ieD])
                    (BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docEnsureIndent (Int -> BrIndent
BrIndentSpecial Int
hidDocColDiff) ToBriDocM BriDocNumbered
docParenR)
                -- ..[hiding].( b
                --            , b'
                --            )
                (ToBriDocM BriDocNumbered
ieD:[ToBriDocM BriDocNumbered]
ieDs') ->
                  ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docPar
                    ([ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
hidDoc, ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docSetBaseY (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
docParenLSep, ToBriDocM BriDocNumbered
ieD]])
                    (  BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docEnsureIndent (Int -> BrIndent
BrIndentSpecial Int
hidDocColDiff)
                    (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$  [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docLines
                    ([ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered)
-> [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$  [ToBriDocM BriDocNumbered]
ieDs'
                    [ToBriDocM BriDocNumbered]
-> [ToBriDocM BriDocNumbered] -> [ToBriDocM BriDocNumbered]
forall a. [a] -> [a] -> [a]
++ [ToBriDocM BriDocNumbered
docParenR]
                    )
      makeAsDoc :: Text -> ToBriDocM BriDocNumbered
makeAsDoc Text
asT =
        [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit (Text -> ToBriDocM BriDocNumbered)
-> Text -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
"as", ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
appSep (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
docLit Text
asT]
    if Bool
compact
    then
      let asDoc :: ToBriDocM BriDocNumbered
asDoc = ToBriDocM BriDocNumbered
-> (Text -> ToBriDocM BriDocNumbered)
-> Maybe Text
-> ToBriDocM BriDocNumbered
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ToBriDocM BriDocNumbered
docEmpty Text -> ToBriDocM BriDocNumbered
makeAsDoc Maybe Text
masT
      in [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docAlt
        [ ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docForceSingleline (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
importHead, ToBriDocM BriDocNumbered
asDoc, ToBriDocM BriDocNumbered
bindingsD]
        , BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docAddBaseY BrIndent
BrIndentRegular (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$
            ToBriDocM BriDocNumbered
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docPar ([ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
importHead, ToBriDocM BriDocNumbered
asDoc]) ToBriDocM BriDocNumbered
bindingsD
        ]
    else
      case Maybe Text
masT of
        Just Text
n -> if Bool
enoughRoom
          then [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docLines
                 [ [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
importHead, ToBriDocM BriDocNumbered
asDoc], ToBriDocM BriDocNumbered
bindingsD]
          else [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docLines [ToBriDocM BriDocNumbered
importHead, ToBriDocM BriDocNumbered
asDoc, ToBriDocM BriDocNumbered
bindingsD]
         where
          enoughRoom :: Bool
enoughRoom = Int
nameCost Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
importAsCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
asCost
          asDoc :: ToBriDocM BriDocNumbered
asDoc =
            BrIndent -> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
docEnsureIndent (Int -> BrIndent
BrIndentSpecial (Int
importAsCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
asCost))
              (ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered)
-> ToBriDocM BriDocNumbered -> ToBriDocM BriDocNumbered
forall a b. (a -> b) -> a -> b
$ Text -> ToBriDocM BriDocNumbered
makeAsDoc Text
n
        Maybe Text
Nothing -> if Bool
enoughRoom
          then [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq [ToBriDocM BriDocNumbered
importHead, ToBriDocM BriDocNumbered
bindingsD]
          else [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docLines [ToBriDocM BriDocNumbered
importHead, ToBriDocM BriDocNumbered
bindingsD]
          where enoughRoom :: Bool
enoughRoom = Int
nameCost Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
importCol Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hidingParenCost
  ImportDecl GhcPs
_ -> ToBriDocM BriDocNumbered
docEmpty