Skip to content

Commit

Permalink
fix suggestAddTypeAnnotation regex (#760)
Browse files Browse the repository at this point in the history
Co-authored-by: Javier Neira <atreyu.bbb@gmail.com>
Co-authored-by: Pepe Iborra <pepeiborra@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 5, 2021
1 parent 0db2e28 commit 93ab0ea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
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

0 comments on commit 93ab0ea

Please # to comment.