Fix module-import-hiding-items layout

pull/132/head
Lennart Spitzner 2018-03-23 15:41:41 +01:00
parent 90a2f65ba7
commit d634d34ff1
5 changed files with 89 additions and 50 deletions

View File

@ -734,19 +734,22 @@ import Test hiding ( )
import Test as T import Test as T
hiding ( ) hiding ( )
#test long-module-name #test long-module-name-simple
import TestJustShortEnoughModuleNameLikeThisOne ( ) import TestJustShortEnoughModuleNameLikeThisOne ( )
import TestJustAbitToLongModuleNameLikeThisOneIs import TestJustAbitToLongModuleNameLikeThisOneIs
( ) ( )
#test long-module-name-as
import TestJustShortEnoughModuleNameLikeThisOn as T import TestJustShortEnoughModuleNameLikeThisOn as T
import TestJustAbitToLongModuleNameLikeThisOneI import TestJustAbitToLongModuleNameLikeThisOneI
as T as T
#test long-module-name-hiding
import TestJustShortEnoughModuleNameLike hiding ( ) import TestJustShortEnoughModuleNameLike hiding ( )
import TestJustAbitToLongModuleNameLikeTh import TestJustAbitToLongModuleNameLikeTh
hiding ( ) hiding ( )
#test long-module-name-simple-items
import MoreThanSufficientlyLongModuleNameWithSome import MoreThanSufficientlyLongModuleNameWithSome
( items ( items
, that , that
@ -758,6 +761,20 @@ import MoreThanSufficientlyLongModuleNameWithSome
, layout , layout
) )
#test long-module-name-hiding-items
import TestJustShortEnoughModuleNameLike hiding ( abc
, def
, ghci
, jklm
)
import TestJustAbitToLongModuleNameLikeTh
hiding ( abc
, def
, ghci
, jklm
)
#test long-module-name-other
import {-# SOURCE #-} safe qualified "qualifiers" AlsoAff ( ) import {-# SOURCE #-} safe qualified "qualifiers" AlsoAff ( )
import {-# SOURCE #-} safe qualified "qualifiers" AlsoAffe import {-# SOURCE #-} safe qualified "qualifiers" AlsoAffe
( ) ( )

View File

@ -736,16 +736,27 @@ import Test (T, T2(), T3(..), T4(T4), T5(T5, t5), T6((<|>)), (+))
import Test hiding () import Test hiding ()
import Test as T hiding () import Test as T hiding ()
#test long-module-name #test long-module-name-simple
import TestJustShortEnoughModuleNameLikeThisOne () import TestJustShortEnoughModuleNameLikeThisOne ()
import TestJustAbitToLongModuleNameLikeThisOneIs () import TestJustAbitToLongModuleNameLikeThisOneIs ()
import TestJustShortEnoughModuleNameLikeThisOn as T
import TestJustAbitToLongModuleNameLikeThisOneI as T
import TestJustShortEnoughModuleNameLike hiding ()
import TestJustAbitToLongModuleNameLikeTh hiding ()
import MoreThanSufficientlyLongModuleNameWithSome import MoreThanSufficientlyLongModuleNameWithSome
(items, that, will, not, fit, inA, compact, layout) (items, that, will, not, fit, inA, compact, layout)
#test long-module-name-as
import TestJustShortEnoughModuleNameLikeThisOn as T
import TestJustAbitToLongModuleNameLikeThisOneI as T
#test long-module-name-hiding
import TestJustShortEnoughModuleNameLike hiding ()
import TestJustAbitToLongModuleNameLikeTh hiding ()
#test long-module-name-simple-items
import MoreThanSufficientlyLongModuleNameWithSome
(items, that, will, not, fit, inA, compact, layout)
#test long-module-name-hiding-items
import TestJustShortEnoughModuleNameLike hiding (abc, def, ghci, jklm)
#test import-with-comments #test import-with-comments
-- Test -- Test
import Data.List (nub) -- Test import Data.List (nub) -- Test

View File

@ -56,9 +56,11 @@ data CLayoutConfig f = LayoutConfig
, _lconfig_importColumn :: f (Last Int) , _lconfig_importColumn :: f (Last Int)
-- ^ for import statement layouting, column at which to align the -- ^ for import statement layouting, column at which to align the
-- elements to be imported from a module. -- elements to be imported from a module.
-- It is expected that importAsColumn >= importCol.
, _lconfig_importAsColumn :: f (Last Int) , _lconfig_importAsColumn :: f (Last Int)
-- ^ for import statement layouting, column at which put the module's -- ^ for import statement layouting, column at which put the module's
-- "as" name (which also affects the positioning of the "as" keyword). -- "as" name (which also affects the positioning of the "as" keyword).
-- It is expected that importAsColumn >= importCol.
, _lconfig_altChooser :: f (Last AltChooser) , _lconfig_altChooser :: f (Last AltChooser)
, _lconfig_columnAlignMode :: f (Last ColumnAlignMode) , _lconfig_columnAlignMode :: f (Last ColumnAlignMode)
, _lconfig_alignmentLimit :: f (Last Int) , _lconfig_alignmentLimit :: f (Last Int)

View File

@ -64,7 +64,7 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
qLength = max minQLength qLengthReal qLength = max minQLength qLengthReal
-- Cost in columns of importColumn -- Cost in columns of importColumn
asCost = length "as " asCost = length "as "
bindingCost = if hiding then length "hiding ( " else length "( " hidingParenCost = if hiding then length "hiding ( " else length "( "
nameCost = Text.length modNameT + qLength nameCost = Text.length modNameT + qLength
importQualifiers = docSeq importQualifiers = docSeq
[ appSep $ docLit $ Text.pack "import" [ appSep $ docLit $ Text.pack "import"
@ -77,8 +77,11 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
if compact then id else docEnsureIndent (BrIndentSpecial qLength) if compact then id else docEnsureIndent (BrIndentSpecial qLength)
modNameD = modNameD =
indentName $ appSep $ docLit modNameT indentName $ appSep $ docLit modNameT
hidDoc = hidDocCol = if hiding then importCol - hidingParenCost else importCol - 2
if hiding then appSep $ docLit $ Text.pack "hiding" else docEmpty hidDocColDiff = importCol - 2 - hidDocCol
hidDoc = if hiding
then appSep $ docLit $ Text.pack "hiding"
else docEmpty
importHead = docSeq [importQualifiers, modNameD] importHead = docSeq [importQualifiers, modNameD]
bindingsD = case mllies of bindingsD = case mllies of
Nothing -> docEmpty Nothing -> docEmpty
@ -88,12 +91,14 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
then docSeq [hidDoc, layoutLLIEs True llies] then docSeq [hidDoc, layoutLLIEs True llies]
else do else do
ieDs <- layoutAnnAndSepLLIEs llies ieDs <- layoutAnnAndSepLLIEs llies
docWrapNodeRest llies $ case ieDs of docWrapNodeRest llies
$ docEnsureIndent (BrIndentSpecial hidDocCol)
$ case ieDs of
-- ..[hiding].( ) -- ..[hiding].( )
[] -> if hasComments [] -> if hasComments
then docPar then docPar
(docSeq [hidDoc, docParenLSep, docWrapNode llies docEmpty]) (docSeq [hidDoc, docParenLSep, docWrapNode llies docEmpty])
docParenR (docEnsureIndent (BrIndentSpecial hidDocColDiff) $ docParenR)
else docSeq [hidDoc, docParenLSep, docSeparator, docParenR] else docSeq [hidDoc, docParenLSep, docSeparator, docParenR]
-- ..[hiding].( b ) -- ..[hiding].( b )
[ieD] -> docAltFilter [ieD] -> docAltFilter
@ -109,19 +114,20 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
, ( otherwise , ( otherwise
, docPar , docPar
(docSeq [hidDoc, docParenLSep, docNonBottomSpacing ieD]) (docSeq [hidDoc, docParenLSep, docNonBottomSpacing ieD])
docParenR (docEnsureIndent (BrIndentSpecial hidDocColDiff) docParenR)
) )
] ]
-- ..[hiding].( b -- ..[hiding].( b
-- , b' -- , b'
-- ) -- )
(ieD:ieDs') -> (ieD:ieDs') ->
docPar (docSeq [hidDoc, docSetBaseY $ docSeq [docParenLSep, ieD]]) docPar
(docSeq [hidDoc, docSetBaseY $ docSeq [docParenLSep, ieD]])
( docEnsureIndent (BrIndentSpecial hidDocColDiff)
$ docLines $ docLines
$ ieDs' $ ieDs'
++ [docParenR] ++ [docParenR]
bindingLine = )
docEnsureIndent (BrIndentSpecial (importCol - bindingCost)) bindingsD
makeAsDoc asT = makeAsDoc asT =
docSeq [appSep $ docLit $ Text.pack "as", appSep $ docLit asT] docSeq [appSep $ docLit $ Text.pack "as", appSep $ docLit asT]
if compact if compact
@ -134,14 +140,17 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
] ]
else else
case masT of case masT of
Just n | enoughRoom -> docLines [docSeq [importHead, asDoc], bindingLine] Just n -> if enoughRoom
| otherwise -> docLines [importHead, asDoc, bindingLine] then docLines
[ docSeq [importHead, asDoc], bindingsD]
else docLines [importHead, asDoc, bindingsD]
where where
enoughRoom = nameCost < importAsCol - asCost enoughRoom = nameCost < importAsCol - asCost
asDoc = asDoc =
docEnsureIndent (BrIndentSpecial (importAsCol - asCost)) docEnsureIndent (BrIndentSpecial (importAsCol - asCost))
$ makeAsDoc n $ makeAsDoc n
Nothing | enoughRoom -> docSeq [importHead, bindingLine] Nothing -> if enoughRoom
| otherwise -> docLines [importHead, bindingLine] then docSeq [importHead, bindingsD]
where enoughRoom = nameCost < importCol - bindingCost else docLines [importHead, bindingsD]
where enoughRoom = nameCost < importCol - hidingParenCost
_ -> docEmpty _ -> docEmpty

View File

@ -101,9 +101,9 @@ transformSimplifyFloating = stepBO .> stepFull
Just $ BDDebug s (BDIndentLevelPop x) Just $ BDDebug s (BDIndentLevelPop x)
_ -> Nothing _ -> Nothing
descendAddB = transformDownMay $ \case descendAddB = transformDownMay $ \case
-- AddIndent floats into Lines.
BDAddBaseY BrIndentNone x -> BDAddBaseY BrIndentNone x ->
Just x Just x
-- AddIndent floats into Lines.
BDAddBaseY indent (BDLines lines) -> BDAddBaseY indent (BDLines lines) ->
Just $ BDLines $ BDAddBaseY indent <$> lines Just $ BDLines $ BDAddBaseY indent <$> lines
-- AddIndent floats into last column -- AddIndent floats into last column
@ -145,9 +145,9 @@ transformSimplifyFloating = stepBO .> stepFull
x -> x x -> x
stepFull = -- traceFunctionWith "stepFull" (show . briDocToDocWithAnns) (show . briDocToDocWithAnns) $ stepFull = -- traceFunctionWith "stepFull" (show . briDocToDocWithAnns) (show . briDocToDocWithAnns) $
Uniplate.rewrite $ \case Uniplate.rewrite $ \case
-- AddIndent floats into Lines.
BDAddBaseY BrIndentNone x -> BDAddBaseY BrIndentNone x ->
Just $ x Just $ x
-- AddIndent floats into Lines.
BDAddBaseY indent (BDLines lines) -> BDAddBaseY indent (BDLines lines) ->
Just $ BDLines $ BDAddBaseY indent <$> lines Just $ BDLines $ BDAddBaseY indent <$> lines
-- AddIndent floats into last column -- AddIndent floats into last column