Skip to content

Commit 967e8dd

Browse files
Backport "fix: completions when parenthesis already provided" to 3.5.2 (#21470)
Backports #21299 to the 3.5.2 branch. PR submitted by the release tooling. [skip ci]
2 parents dbb21fb + 0175068 commit 967e8dd

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala

+13-8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class Completions(
6060

6161
private lazy val shouldAddSnippet =
6262
path match
63+
case (_: (Import | Export)) :: _ => false
64+
case _ :: (_: (Import | Export)) :: _ => false
65+
// UnApply has patterns included in MatchCaseCompletions
66+
case _ :: (_: UnApply) :: _ => false
67+
case _ => true
68+
69+
private lazy val shouldAddSuffix = shouldAddSnippet &&
70+
(path match
6371
/* In case of `method@@()` we should not add snippets and the path
6472
* will contain apply as the parent of the current tree.
6573
*/
@@ -72,11 +80,8 @@ class Completions(
7280
case _ :: (withcursor @ Select(fun, name)) :: (appl: GenericApply) :: _
7381
if appl.fun == withcursor && name.decoded == Cursor.value =>
7482
false
75-
case (_: (Import | Export)) :: _ => false
76-
case _ :: (_: (Import | Export)) :: _ => false
77-
// UnApply has patterns included in MatchCaseCompletions
78-
case _ :: (_: UnApply) :: _ => false
79-
case _ => true
83+
case _ => true)
84+
8085

8186
private lazy val isNew: Boolean = Completion.isInNewContext(adjustedPath)
8287

@@ -198,12 +203,12 @@ class Completions(
198203
private def findSuffix(symbol: Symbol): CompletionAffix =
199204
CompletionAffix.empty
200205
.chain { suffix => // for [] suffix
201-
if shouldAddSnippet && symbol.info.typeParams.nonEmpty then
206+
if shouldAddSuffix && symbol.info.typeParams.nonEmpty then
202207
suffix.withNewSuffixSnippet(Affix(SuffixKind.Bracket))
203208
else suffix
204209
}
205210
.chain { suffix => // for () suffix
206-
if shouldAddSnippet && symbol.is(Flags.Method) then
211+
if shouldAddSuffix && symbol.is(Flags.Method) then
207212
val paramss = getParams(symbol)
208213
paramss match
209214
case Nil => suffix
@@ -224,7 +229,7 @@ class Completions(
224229
else suffix
225230
}
226231
.chain { suffix => // for {} suffix
227-
if shouldAddSnippet && isNew && isAbstractType(symbol) then
232+
if shouldAddSuffix && isNew && isAbstractType(symbol) then
228233
if suffix.hasSnippet then suffix.withNewSuffix(Affix(SuffixKind.Template))
229234
else suffix.withNewSuffixSnippet(Affix(SuffixKind.Template))
230235
else suffix

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSnippetSuite.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ class CompletionSnippetSuite extends BaseCompletionSuite:
289289
|}
290290
|""".stripMargin,
291291
"scala.util.Try@@(1)",
292-
"scala.util.Try(1)"
292+
"scala.util.Try(1)",
293+
assertSingleItem = false
293294
)
294295

295296
@Test def `case-class` =
@@ -300,7 +301,8 @@ class CompletionSnippetSuite extends BaseCompletionSuite:
300301
|""".stripMargin,
301302
"scala.util.Tr@@(1)",
302303
"scala.util.Try(1)",
303-
filter = str => str.contains("Try")
304+
filter = str => str.contains("Try"),
305+
assertSingleItem = false
304306
)
305307

306308
@Test def `case-class2` =

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

+11
Original file line numberDiff line numberDiff line change
@@ -2030,3 +2030,14 @@ class CompletionSuite extends BaseCompletionSuite:
20302030
""".stripMargin,
20312031
filter = _.contains("name")
20322032
)
2033+
2034+
@Test def `with-parenthesis` =
2035+
check(
2036+
"""|package a
2037+
|class MyClass
2038+
|val i = MyClass@@()
2039+
|""".stripMargin,
2040+
"""|MyClass(): MyClass (Constructor)
2041+
|""".stripMargin,
2042+
includeCompletionKind = true
2043+
)

0 commit comments

Comments
 (0)