From a699502ecbf80ecd05df92ac308dc9beb6a64ecd Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Tue, 30 Jul 2024 17:07:39 +0200 Subject: [PATCH] fix: completions when parenthesis already provided --- .../tools/pc/completions/Completions.scala | 21 ++++++++++++------- .../completion/CompletionSnippetSuite.scala | 6 ++++-- .../pc/tests/completion/CompletionSuite.scala | 11 ++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala index 2cd8db318690..d043a2cfddbf 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala @@ -60,6 +60,14 @@ class Completions( private lazy val shouldAddSnippet = path match + case (_: (Import | Export)) :: _ => false + case _ :: (_: (Import | Export)) :: _ => false + // UnApply has patterns included in MatchCaseCompletions + case _ :: (_: UnApply) :: _ => false + case _ => true + + private lazy val shouldAddSuffix = shouldAddSnippet && + (path match /* In case of `method@@()` we should not add snippets and the path * will contain apply as the parent of the current tree. */ @@ -72,11 +80,8 @@ class Completions( case _ :: (withcursor @ Select(fun, name)) :: (appl: GenericApply) :: _ if appl.fun == withcursor && name.decoded == Cursor.value => false - case (_: (Import | Export)) :: _ => false - case _ :: (_: (Import | Export)) :: _ => false - // UnApply has patterns included in MatchCaseCompletions - case _ :: (_: UnApply) :: _ => false - case _ => true + case _ => true) + private lazy val isNew: Boolean = Completion.isInNewContext(adjustedPath) @@ -198,12 +203,12 @@ class Completions( private def findSuffix(symbol: Symbol): CompletionAffix = CompletionAffix.empty .chain { suffix => // for [] suffix - if shouldAddSnippet && symbol.info.typeParams.nonEmpty then + if shouldAddSuffix && symbol.info.typeParams.nonEmpty then suffix.withNewSuffixSnippet(Affix(SuffixKind.Bracket)) else suffix } .chain { suffix => // for () suffix - if shouldAddSnippet && symbol.is(Flags.Method) then + if shouldAddSuffix && symbol.is(Flags.Method) then val paramss = getParams(symbol) paramss match case Nil => suffix @@ -224,7 +229,7 @@ class Completions( else suffix } .chain { suffix => // for {} suffix - if shouldAddSnippet && isNew && isAbstractType(symbol) then + if shouldAddSuffix && isNew && isAbstractType(symbol) then if suffix.hasSnippet then suffix.withNewSuffix(Affix(SuffixKind.Template)) else suffix.withNewSuffixSnippet(Affix(SuffixKind.Template)) else suffix diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSnippetSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSnippetSuite.scala index 2c91f71d8d19..381375c65131 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSnippetSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSnippetSuite.scala @@ -289,7 +289,8 @@ class CompletionSnippetSuite extends BaseCompletionSuite: |} |""".stripMargin, "scala.util.Try@@(1)", - "scala.util.Try(1)" + "scala.util.Try(1)", + assertSingleItem = false ) @Test def `case-class` = @@ -300,7 +301,8 @@ class CompletionSnippetSuite extends BaseCompletionSuite: |""".stripMargin, "scala.util.Tr@@(1)", "scala.util.Try(1)", - filter = str => str.contains("Try") + filter = str => str.contains("Try"), + assertSingleItem = false ) @Test def `case-class2` = diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala index 6cccc923a5f5..437fe606932b 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala @@ -2031,3 +2031,14 @@ class CompletionSuite extends BaseCompletionSuite: """.stripMargin, filter = _.contains("name") ) + + @Test def `with-parenthesis` = + check( + """|package a + |class MyClass + |val i = MyClass@@() + |""".stripMargin, + """|MyClass(): MyClass (Constructor) + |""".stripMargin, + includeCompletionKind = true + )