diff --git a/data/12-features/minimize-parentheses.blt b/data/12-features/minimize-parentheses.blt new file mode 100644 index 0000000..7b831c3 --- /dev/null +++ b/data/12-features/minimize-parentheses.blt @@ -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) diff --git a/data/15-regressions.blt b/data/15-regressions.blt index a35ced2..1e5d039 100644 --- a/data/15-regressions.blt +++ b/data/15-regressions.blt @@ -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 diff --git a/source/library/Language/Haskell/Brittany/Internal/Components/OpTree.hs b/source/library/Language/Haskell/Brittany/Internal/Components/OpTree.hs index 1e4ce18..7d43ba1 100644 --- a/source/library/Language/Haskell/Brittany/Internal/Components/OpTree.hs +++ b/source/library/Language/Haskell/Brittany/Internal/Components/OpTree.hs @@ -184,9 +184,14 @@ addAllParens topLevelParen = \case remSuperfluousParens :: Int -> OpTree -> OpTree remSuperfluousParens outerFixity = \case - x@OpLeaf{} -> x - OpUnknown _ locO locC c [] -> OpUnknown NoParen locO locC c [] - x@OpUnknown{} -> x + x@OpLeaf{} -> x + 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 -- We do not support removing superfluous parens around @@ -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