From 8de56ba11d4e2442a648f28573254edd5f54e403 Mon Sep 17 00:00:00 2001
From: Lennart Spitzner <hexagoxel@hexagoxel.de>
Date: Wed, 21 Mar 2018 01:02:44 +0100
Subject: [PATCH] Support import column vs import-as column

---
 src-literatetests/Main.hs                                  | 1 +
 src-unittests/TestUtils.hs                                 | 1 +
 src/Language/Haskell/Brittany/Internal/Config.hs           | 3 +++
 src/Language/Haskell/Brittany/Internal/Config/Types.hs     | 7 ++++++-
 src/Language/Haskell/Brittany/Internal/Layouters/Import.hs | 5 +++--
 5 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src-literatetests/Main.hs b/src-literatetests/Main.hs
index 47fd801..ebe2a08 100644
--- a/src-literatetests/Main.hs
+++ b/src-literatetests/Main.hs
@@ -169,6 +169,7 @@ defaultTestConfig = Config
     , _lconfig_indentWhereSpecial        = coerce True
     , _lconfig_indentListSpecial         = coerce True
     , _lconfig_importColumn              = coerce (60 :: Int)
+    , _lconfig_importAsColumn            = coerce (60 :: Int)
     , _lconfig_altChooser                = coerce $ AltChooserBoundedSearch 3
     , _lconfig_columnAlignMode           = coerce (ColumnAlignModeMajority 0.7)
     , _lconfig_alignmentLimit            = coerce (30 :: Int)
diff --git a/src-unittests/TestUtils.hs b/src-unittests/TestUtils.hs
index 2e9487c..d10f85a 100644
--- a/src-unittests/TestUtils.hs
+++ b/src-unittests/TestUtils.hs
@@ -51,6 +51,7 @@ defaultTestConfig = Config
     , _lconfig_indentWhereSpecial        = coerce True
     , _lconfig_indentListSpecial         = coerce True
     , _lconfig_importColumn              = coerce (60 :: Int)
+    , _lconfig_importAsColumn            = coerce (60 :: Int)
     , _lconfig_altChooser                = coerce $ AltChooserBoundedSearch 3
     , _lconfig_columnAlignMode           = coerce (ColumnAlignModeMajority 0.7)
     , _lconfig_alignmentLimit            = coerce (30 :: Int)
diff --git a/src/Language/Haskell/Brittany/Internal/Config.hs b/src/Language/Haskell/Brittany/Internal/Config.hs
index ea01253..d660e6e 100644
--- a/src/Language/Haskell/Brittany/Internal/Config.hs
+++ b/src/Language/Haskell/Brittany/Internal/Config.hs
@@ -59,6 +59,7 @@ staticDefaultConfig = Config
     , _lconfig_indentWhereSpecial        = coerce True
     , _lconfig_indentListSpecial         = coerce True
     , _lconfig_importColumn              = coerce (50 :: Int)
+    , _lconfig_importAsColumn            = coerce (50 :: Int)
     , _lconfig_altChooser                = coerce (AltChooserBoundedSearch 3)
     , _lconfig_columnAlignMode           = coerce (ColumnAlignModeMajority 0.7)
     , _lconfig_alignmentLimit            = coerce (30 :: Int)
@@ -106,6 +107,7 @@ configParser = do
   ind                <- addFlagReadParams "" ["indent"] "AMOUNT" (flagHelpStr "spaces per indentation level")
   cols               <- addFlagReadParams "" ["columns"] "AMOUNT" (flagHelpStr "target max columns (80 is an old default for this)")
   importCol          <- addFlagReadParams "" ["import-col"] "N" (flagHelpStr "column to align import lists at")
+  importAsCol        <- addFlagReadParams "" ["import-as-col"] "N" (flagHelpStr "column to qualified-as module names at")
 
   dumpConfig         <- addSimpleBoolFlag "" ["dump-config"] (flagHelp $ parDoc "dump the programs full config (merged commandline + file + defaults)")
   dumpAnnotations    <- addSimpleBoolFlag "" ["dump-annotations"] (flagHelp $ parDoc "dump the full annotations returned by ghc-exactprint")
@@ -155,6 +157,7 @@ configParser = do
       , _lconfig_indentWhereSpecial        = mempty -- falseToNothing _
       , _lconfig_indentListSpecial         = mempty -- falseToNothing _
       , _lconfig_importColumn              = optionConcat importCol
+      , _lconfig_importAsColumn            = optionConcat importAsCol
       , _lconfig_altChooser                = mempty
       , _lconfig_columnAlignMode           = mempty
       , _lconfig_alignmentLimit            = mempty
diff --git a/src/Language/Haskell/Brittany/Internal/Config/Types.hs b/src/Language/Haskell/Brittany/Internal/Config/Types.hs
index 0f6d48b..03f7d9a 100644
--- a/src/Language/Haskell/Brittany/Internal/Config/Types.hs
+++ b/src/Language/Haskell/Brittany/Internal/Config/Types.hs
@@ -53,7 +53,12 @@ data CLayoutConfig f = LayoutConfig
   , _lconfig_indentListSpecial  :: f (Last Bool) -- use some special indentation for ","
                                                  -- when creating zero-indentation
                                                  -- multi-line list literals.
-  , _lconfig_importColumn :: f (Last Int)
+  , _lconfig_importColumn    :: f (Last Int)
+    -- ^ for import statement layouting, column at which to align the
+    -- elements to be imported from a module.
+  , _lconfig_importAsColumn  :: f (Last Int)
+    -- ^ for import statement layouting, column at which put the module's
+    -- "as" name (which also affects the positioning of the "as" keyword).
   , _lconfig_altChooser      :: f (Last AltChooser)
   , _lconfig_columnAlignMode :: f (Last ColumnAlignMode)
   , _lconfig_alignmentLimit  :: f (Last Int)
diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs
index e7fb03c..a98f642 100644
--- a/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs
+++ b/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs
@@ -46,6 +46,7 @@ layoutImport :: ToBriDoc ImportDecl
 layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
   ImportDecl _ (L _ modName) pkg src safe q False mas mllies -> do
     importCol <- mAsk <&> _conf_layout .> _lconfig_importColumn .> confUnpack
+    importAsCol <- mAsk <&> _conf_layout .> _lconfig_importAsColumn .> confUnpack
     indentPolicy <- mAsk <&>  _conf_layout .> _lconfig_indentPolicy .> confUnpack
     let
       compact  = indentPolicy == IndentPolicyLeft
@@ -136,9 +137,9 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
         Just n | enoughRoom -> docLines [docSeq [importHead, asDoc], bindingLine]
                | otherwise  -> docLines [importHead, asDoc, bindingLine]
          where
-          enoughRoom = nameCost < importCol - asCost
+          enoughRoom = nameCost < importAsCol - asCost
           asDoc =
-            docEnsureIndent (BrIndentSpecial (importCol - asCost))
+            docEnsureIndent (BrIndentSpecial (importAsCol - asCost))
               $ makeAsDoc n
         Nothing | enoughRoom -> docSeq [importHead, bindingLine]
                 | otherwise  -> docLines [importHead, bindingLine]