Skip to content

Commit a699502

Browse files
committed
fix: completions when parenthesis already provided
1 parent 4429d73 commit a699502

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

Diff for: 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

Diff for: 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` =

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

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

0 commit comments

Comments
 (0)