Skip to content

Commit 1744db7

Browse files
Backport "fix: completions when parenthesis already provided" to LTS (#22085)
Backports #21299 to the 3.3.5. PR submitted by the release tooling. [skip ci]
2 parents 1b52444 + b7c3bde commit 1744db7

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-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

+25
Original file line numberDiff line numberDiff line change
@@ -1983,3 +1983,28 @@ class CompletionSuite extends BaseCompletionSuite:
19831983
|val foo: SomeClass
19841984
|""".stripMargin,
19851985
)
1986+
1987+
@Test def `Selectable without namedTuple Fields mamber` =
1988+
check(
1989+
"""|class NonNamedTupleSelectable extends Selectable {
1990+
| def selectDynamic(name: String): Any = ???
1991+
|}
1992+
|
1993+
|val person2 = new NonNamedTupleSelectable {}
1994+
|
1995+
|val n = person2.na@@""".stripMargin,
1996+
"""|selectDynamic(name: String): Any
1997+
""".stripMargin,
1998+
filter = _.contains("name")
1999+
)
2000+
2001+
@Test def `with-parenthesis` =
2002+
check(
2003+
"""|package a
2004+
|class MyClass
2005+
|val i = MyClass@@()
2006+
|""".stripMargin,
2007+
"""|MyClass(): MyClass (Constructor)
2008+
|""".stripMargin,
2009+
includeCompletionKind = true
2010+
)

0 commit comments

Comments
 (0)