Compare commits

..

7 Commits

4 changed files with 53 additions and 13 deletions

View File

@ -0,0 +1,27 @@
#group feature/minimize-parens
#golden minimize parens basic test
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func (abc) (def)
#expected
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func abc def
#test minimize parens test that it keep necessary parens
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func (abc False) ("asd" ++ "def")
#golden minimize nested parens 1
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func ((((((((nested))))))))
#expected
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func nested
#golden minimize nested parens 2
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func ((((((((nested + expression))))))))
#expected
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func (nested + expression)

View File

@ -1060,13 +1060,6 @@ func = do
)
`shouldReturn` thing
#golden minimize parens basic test
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func (abc) (def)
#expected
-- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
func = func abc def
#test wandering comment at end of datadecl
data ReformatParenMode
= ReformatParenModeKeep -- don't modify parens at all

View File

@ -185,7 +185,12 @@ addAllParens topLevelParen = \case
remSuperfluousParens :: Int -> OpTree -> OpTree
remSuperfluousParens outerFixity = \case
x@OpLeaf{} -> x
OpUnknown _ locO locC c [] -> OpUnknown NoParen locO locC c []
OpUnknown _ locO locC c@(OpLeaf doc) [] | isLit doc ->
OpUnknown NoParen locO locC c []
OpUnknown _ locO locC c@(OpUnknown ParenWithSpace _ _ _ _) [] ->
OpUnknown NoParen locO locC (remSuperfluousParens 11 c) []
OpUnknown _ locO locC c@(OpUnknown ParenNoSpace _ _ _ _) [] ->
OpUnknown NoParen locO locC (remSuperfluousParens 11 c) []
x@OpUnknown{} -> x
OpKnown paren locO locC fixity c cs ->
OpKnown
@ -200,7 +205,22 @@ remSuperfluousParens outerFixity = \case
fixity
(remSuperfluousParens (fixLevel fixity) c)
[ (op, remSuperfluousParens (fixLevel fixity) tree) | (op, tree) <- cs ]
where fixLevel (Fixity _ i _) = i
where
fixLevel (Fixity _ i _) = i
isLit = \case
(_, BDFlushCommentsPrior _ x ) -> isLit x
(_, BDFlushCommentsPost _ _ x) -> isLit x
(_, BDQueueComments _ x ) -> isLit x
(_, BDEntryDelta _ x ) -> isLit x
(_, BDForceAlt _ x ) -> isLit x
(_, BDDebug _ x ) -> isLit x
(_, BDAddBaseY _ x ) -> isLit x
(_, BDBaseYPushCur x ) -> isLit x
(_, BDIndentLevelPushCur x ) -> isLit x
(_, BDIndentLevelPop x ) -> isLit x
(_, BDLit{} ) -> True
_ -> False
hardcodedFixity :: Bool -> String -> Maybe Fixity
hardcodedFixity allowUnqualify = \case

View File

@ -165,8 +165,8 @@ processOpTree (unknownTree, hasComments) = do
PRMMinimize -> remSuperfluousParens 11 balancedTree
PRMMaximize -> addAllParens NoParen balancedTree
-- tellDebugMess $ displayOpTree unknownTree
tellDebugMess $ displayOpTree balancedTree
tellDebugMess $ displayOpTree processedTree
-- tellDebugMess $ displayOpTree balancedTree
-- tellDebugMess $ displayOpTree processedTree
layoutOpTree (not hasComments) processedTree
layoutOpTree :: Bool -> OpTree -> ToBriDocM BriDocNumbered