Skip to content

Commit 3435ddb

Browse files
bugfix: Fix adjust type when already exists (#23455)
Fixes: scalameta/metals#6018
1 parent a7da42c commit 3435ddb

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

presentation-compiler/src/main/dotty/tools/pc/InferredTypeProvider.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ final class InferredTypeProvider(
182182
typeNameEdit ::: imports
183183

184184
rhs match
185-
case t: Tree[?]
186-
if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent =>
185+
case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent =>
187186
inferredTypeEdits(
188187
Some(
189188
AdjustTypeOpts(
@@ -223,8 +222,7 @@ final class InferredTypeProvider(
223222
while i >= 0 && sourceText(i) != ':' do i -= 1
224223
i
225224
rhs match
226-
case t: Tree[?]
227-
if t.typeOpt.isErroneous && retryType && !tpt.sourcePos.span.isZeroExtent =>
225+
case t: Tree[?] if !tpt.sourcePos.span.isZeroExtent =>
228226
inferredTypeEdits(
229227
Some(
230228
AdjustTypeOpts(

presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredTypeSuite.scala

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,70 @@ class InsertInferredTypeSuite extends BaseCodeActionSuite:
10551055
|""".stripMargin
10561056
)
10571057

1058+
@Test def `Adjust type for val` =
1059+
checkEdit(
1060+
"""|object A{
1061+
| val <<alpha>>:String = 123
1062+
|}""".stripMargin,
1063+
1064+
"""|object A{
1065+
| val alpha: Int = 123
1066+
|}""".stripMargin,
1067+
)
1068+
1069+
@Test def `Adjust type for val2` =
1070+
checkEdit(
1071+
"""|object A{
1072+
| val <<alpha>>:Int = 123
1073+
|}""".stripMargin,
1074+
"""|object A{
1075+
| val alpha: Int = 123
1076+
|}""".stripMargin,
1077+
)
1078+
1079+
@Test def `Adjust type for val3` =
1080+
checkEdit(
1081+
"""|object A{
1082+
| val <<alpha>>: Int = 123
1083+
|}""".stripMargin,
1084+
"""|object A{
1085+
| val alpha: Int = 123
1086+
|}""".stripMargin,
1087+
)
1088+
1089+
@Test def `Adjust type for def` =
1090+
checkEdit(
1091+
"""|object A{
1092+
| def <<alpha>>:String = 123
1093+
|}""".stripMargin,
1094+
1095+
"""|object A{
1096+
| def alpha: Int = 123
1097+
|}""".stripMargin,
1098+
)
1099+
1100+
@Test def `Adjust type for def2` =
1101+
checkEdit(
1102+
"""|object A{
1103+
| def <<alpha>>:Int = 123
1104+
|}""".stripMargin,
1105+
"""|object A{
1106+
| def alpha: Int = 123
1107+
|}""".stripMargin,
1108+
)
1109+
1110+
1111+
@Test def `Adjust type for def3` =
1112+
checkEdit(
1113+
"""|object A{
1114+
| def <<alpha>>: Int = 123
1115+
|}""".stripMargin,
1116+
"""|object A{
1117+
| def alpha: Int = 123
1118+
|}""".stripMargin,
1119+
)
1120+
1121+
10581122
def checkEdit(
10591123
original: String,
10601124
expected: String

0 commit comments

Comments
 (0)