Fix minimize-parens feature, Add a few basic tests

ghc92
Lennart Spitzner 2023-05-29 22:43:06 +02:00
parent 54043ca9ba
commit b3f8317e99
3 changed files with 51 additions and 11 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

@ -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