Skip to content

Commit 93ab0ea

Browse files
kdermejneirapepeiborramergify[bot]
authored
fix suggestAddTypeAnnotation regex (#760)
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>
1 parent 0db2e28 commit 93ab0ea

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

ghcide/src/Development/IDE/Plugin/CodeAction.hs

+7-5
Original file line numberDiff line numberDiff line change
@@ -432,23 +432,25 @@ suggestAddTypeAnnotationToSatisfyContraints sourceOpt Diagnostic{_range=_range,.
432432
-- In the expression: seq "test" seq "test" (traceShow "test")
433433
-- In an equation for ‘f’:
434434
-- f = seq "test" seq "test" (traceShow "test")
435-
| Just [ty, lit] <- matchRegexUnifySpaces _message (pat False False True)
436-
<|> matchRegexUnifySpaces _message (pat False False False)
435+
| Just [ty, lit] <- matchRegexUnifySpaces _message (pat False False True False)
436+
<|> matchRegexUnifySpaces _message (pat False False False True)
437+
<|> matchRegexUnifySpaces _message (pat False False False False)
437438
= codeEdit ty lit (makeAnnotatedLit ty lit)
438439
| Just source <- sourceOpt
439-
, Just [ty, lit] <- matchRegexUnifySpaces _message (pat True True False)
440+
, Just [ty, lit] <- matchRegexUnifySpaces _message (pat True True False False)
440441
= let lit' = makeAnnotatedLit ty lit;
441442
tir = textInRange _range source
442443
in codeEdit ty lit (T.replace lit lit' tir)
443444
| otherwise = []
444445
where
445446
makeAnnotatedLit ty lit = "(" <> lit <> " :: " <> ty <> ")"
446-
pat multiple at inThe = T.concat [ ".*Defaulting the following constraint"
447+
pat multiple at inArg inExpr = T.concat [ ".*Defaulting the following constraint"
447448
, if multiple then "s" else ""
448449
, " to type ‘([^ ]+)’ "
449450
, ".*arising from the literal ‘(.+)’"
450-
, if inThe then ".+In the.+argument" else ""
451+
, if inArg then ".+In the.+argument" else ""
451452
, if at then ".+at" else ""
453+
, if inExpr then ".+In the expression" else ""
452454
, ".+In the expression"
453455
]
454456
codeEdit ty lit replacement =

ghcide/test/exe/Main.hs

+36
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,42 @@ addTypeAnnotationsToLiteralsTest = testGroup "add type annotations to literals t
15591559
, "f = (1 :: Integer)"
15601560
])
15611561

1562+
, testSession "add default type to satisfy one contraint in nested expressions" $
1563+
testFor
1564+
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
1565+
, "module A where"
1566+
, ""
1567+
, "f ="
1568+
, " let x = 3"
1569+
, " in x"
1570+
])
1571+
[ (DsWarning, (4, 12), "Defaulting the following constraint") ]
1572+
"Add type annotation ‘Integer’ to ‘3’"
1573+
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
1574+
, "module A where"
1575+
, ""
1576+
, "f ="
1577+
, " let x = (3 :: Integer)"
1578+
, " in x"
1579+
])
1580+
, testSession "add default type to satisfy one contraint in more nested expressions" $
1581+
testFor
1582+
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
1583+
, "module A where"
1584+
, ""
1585+
, "f ="
1586+
, " let x = let y = 5 in y"
1587+
, " in x"
1588+
])
1589+
[ (DsWarning, (4, 20), "Defaulting the following constraint") ]
1590+
"Add type annotation ‘Integer’ to ‘5’"
1591+
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"
1592+
, "module A where"
1593+
, ""
1594+
, "f ="
1595+
, " let x = let y = (5 :: Integer) in y"
1596+
, " in x"
1597+
])
15621598
, testSession "add default type to satisfy one contraint with duplicate literals" $
15631599
testFor
15641600
(T.unlines [ "{-# OPTIONS_GHC -Wtype-defaults #-}"

0 commit comments

Comments
 (0)