Skip to content

Fix suggestAddTypeAnnotation regex #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 5, 2021
12 changes: 7 additions & 5 deletions ghcide/src/Development/IDE/Plugin/CodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -432,23 +432,25 @@ suggestAddTypeAnnotationToSatisfyContraints sourceOpt Diagnostic{_range=_range,.
-- In the expression: seq "test" seq "test" (traceShow "test")
-- In an equation for ‘f’:
-- f = seq "test" seq "test" (traceShow "test")
| Just [ty, lit] <- matchRegexUnifySpaces _message (pat False False True)
<|> matchRegexUnifySpaces _message (pat False False False)
| Just [ty, lit] <- matchRegexUnifySpaces _message (pat False False True False)
<|> matchRegexUnifySpaces _message (pat False False False True)
<|> matchRegexUnifySpaces _message (pat False False False False)
= codeEdit ty lit (makeAnnotatedLit ty lit)
| Just source <- sourceOpt
, Just [ty, lit] <- matchRegexUnifySpaces _message (pat True True False)
, Just [ty, lit] <- matchRegexUnifySpaces _message (pat True True False False)
= let lit' = makeAnnotatedLit ty lit;
tir = textInRange _range source
in codeEdit ty lit (T.replace lit lit' tir)
| otherwise = []
where
makeAnnotatedLit ty lit = "(" <> lit <> " :: " <> ty <> ")"
pat multiple at inThe = T.concat [ ".*Defaulting the following constraint"
pat multiple at inArg inExpr = T.concat [ ".*Defaulting the following constraint"
, if multiple then "s" else ""
, " to type ‘([^ ]+)’ "
, ".*arising from the literal ‘(.+)’"
, if inThe then ".+In the.+argument" else ""
, if inArg then ".+In the.+argument" else ""
, if at then ".+at" else ""
, if inExpr then ".+In the expression" else ""
, ".+In the expression"
]
codeEdit ty lit replacement =
Expand Down
36 changes: 36 additions & 0 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,42 @@ addTypeAnnotationsToLiteralsTest = testGroup "add type annotations to literals t
, "f = (1 :: Integer)"
])

, testSession "add default type to satisfy one contraint in nested expressions" $
testFor
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
, "module A where"
, ""
, "f ="
, " let x = 3"
, " in x"
])
[ (DsWarning, (4, 12), "Defaulting the following constraint") ]
"Add type annotation ‘Integer’ to ‘3’"
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
, "module A where"
, ""
, "f ="
, " let x = (3 :: Integer)"
, " in x"
])
, testSession "add default type to satisfy one contraint in more nested expressions" $
testFor
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
, "module A where"
, ""
, "f ="
, " let x = let y = 5 in y"
, " in x"
])
[ (DsWarning, (4, 20), "Defaulting the following constraint") ]
"Add type annotation ‘Integer’ to ‘5’"
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
, "module A where"
, ""
, "f ="
, " let x = let y = (5 :: Integer) in y"
, " in x"
])
, testSession "add default type to satisfy one contraint with duplicate literals" $
testFor
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
Expand Down