From 48acd71b347dbf4dd7a8ea7326e63c40448a2d07 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Fri, 26 Jul 2024 18:09:02 +0200 Subject: [PATCH] fix: don't suggest completions for param names in definition (#6620) --- .../internal/pc/completions/Completions.scala | 3 + .../ParamDefinitionCompletions.scala | 23 ++++++ .../tests/pc/CompletionKeywordSuite.scala | 81 +------------------ .../test/scala/tests/pc/CompletionSuite.scala | 13 +++ 4 files changed, 42 insertions(+), 78 deletions(-) create mode 100644 mtags/src/main/scala-3/scala/meta/internal/pc/completions/ParamDefinitionCompletions.scala diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala index a5b9fa37e13..4b6a3c24604 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala @@ -527,6 +527,9 @@ class Completions( case Literal(Constant(null)) :: tl => advancedCompletions(tl, pos, completionPos) + // def foo(a@@) + case ParamDefinitionCompletions(allowKeywords) => + (ParamDefinitionCompletions.contribute(allowKeywords, completionPos), true) case _ => val args = NamedArgCompletions.contribute( pos, diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/ParamDefinitionCompletions.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/ParamDefinitionCompletions.scala new file mode 100644 index 00000000000..7a82dfbcdbd --- /dev/null +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/ParamDefinitionCompletions.scala @@ -0,0 +1,23 @@ +package scala.meta.internal.pc.completions + +import dotty.tools.dotc.ast.tpd.* +import dotty.tools.dotc.core.Contexts.Context + +object ParamDefinitionCompletions: + private val possibleKeywords = List("using", "implicit") + private def completion(keyword: String) = CompletionValue.keyword(keyword, keyword ++ " ") + + def unapply(path: List[Tree])(using Context): Option[Boolean] = + path match + case (vd: ValDef) :: (d : DefDef) :: _ => + d.paramss.lastOption.map(_.indexWhere(_.span.contains(vd.span))).filter(_ >= 0) + match + case Some(ind) if ind == 0 => Some(true) + case Some(_) => Some(false) + case _ => None + case _ => None + + def contribute(allowKeywords: Boolean, pos: CompletionPos): List[CompletionValue] = + if allowKeywords + then possibleKeywords.filter(_.startsWith(pos.query)).map(completion) + else Nil diff --git a/tests/cross/src/test/scala/tests/pc/CompletionKeywordSuite.scala b/tests/cross/src/test/scala/tests/pc/CompletionKeywordSuite.scala index 09268693488..e9e8606e133 100644 --- a/tests/cross/src/test/scala/tests/pc/CompletionKeywordSuite.scala +++ b/tests/cross/src/test/scala/tests/pc/CompletionKeywordSuite.scala @@ -574,47 +574,9 @@ class CompletionKeywordSuite extends BaseCompletionSuite { | def hello(u@@) |}""".stripMargin, """|using (commit: '') - |unsafeExceptions scala (commit: '') - |unchecked scala (commit: '') - |unsafeNulls - scala.runtime.stdLibPatches.language (commit: '') - |unshared(): unshared (commit: '')""".stripMargin, + |""".stripMargin, includeCommitCharacter = true, - compat = Map( - "2" -> "", - "3.3.0" -> """|using (commit: '') - |unsafeExceptions scala (commit: '') - |unchecked scala (commit: '') - |unsafe - scala.caps (commit: '') - |unsafeNulls - scala.runtime.stdLibPatches.language (commit: '') - |""".stripMargin, - "3.3.1" -> """|using (commit: '') - |unsafeExceptions scala (commit: '') - |unchecked scala (commit: '') - |unsafe - scala.caps (commit: '') - |unsafeNulls - scala.runtime.stdLibPatches.language (commit: '') - |""".stripMargin, - "3.3.3" -> """|using (commit: '') - |unsafeExceptions scala (commit: '') - |unchecked scala (commit: '') - |unsafe - caps (commit: '') - |unsafeNulls - scala.runtime.stdLibPatches.language (commit: '') - |""".stripMargin, - "3.4" -> """|using (commit: '') - |unsafeExceptions scala (commit: '') - |unchecked scala (commit: '') - |unsafe - scala.caps (commit: '') - |unsafeNulls - scala.runtime.stdLibPatches.language (commit: '') - |""".stripMargin, - ">=3.4.1-RC1-bin-20240201-hash-NIGHTLY" -> - """|using (commit: '') - |unsafe - scala.caps (commit: '') - |unsafeNulls - scala.runtime.stdLibPatches.language (commit: '') - |unused - scala.annotation (commit: '') - |unshared - scala.annotation.internal (commit: '') - |""".stripMargin, - ">=3.4.2-RC1-bin-20240301-hash-NIGHTLY" -> "using (commit: '')" - ), - topLines = Some(5) + compat = Map("2" -> "") ) check( @@ -622,44 +584,7 @@ class CompletionKeywordSuite extends BaseCompletionSuite { """|object A{ | def hello(a: String, u@@) |}""".stripMargin, - "", - compat = Map( - "3" -> """|unsafeExceptions scala - |unchecked scala - |unsafeNulls - scala.runtime.stdLibPatches.language - |unshared(): unshared - |uncheckedStable(): uncheckedStable""".stripMargin, - "3.3.0" -> """|unsafeExceptions scala - |unchecked scala - |unsafe - scala.caps - |unsafeNulls - scala.runtime.stdLibPatches.language - |unshared(): unshared""".stripMargin, - "3.3.1" -> """|unsafeExceptions scala - |unchecked scala - |unsafe - scala.caps - |unsafeNulls - scala.runtime.stdLibPatches.language - |unshared(): unshared""".stripMargin, - "3.3.3" -> """|unsafeExceptions scala - |unchecked scala - |unsafe - caps - |unsafeNulls - scala.runtime.stdLibPatches.language - |unshared(): unshared""".stripMargin, - "3.4" -> """|unsafeExceptions scala - |unchecked scala - |unsafe - scala.caps - |unsafeNulls - scala.runtime.stdLibPatches.language - |unused - scala.annotation - |""".stripMargin, - ">=3.4.1-RC1-bin-20240201-hash-NIGHTLY" -> - """|unsafe - scala.caps - |unsafeNulls - scala.runtime.stdLibPatches.language - |unused - scala.annotation - |unshared - scala.annotation.internal - |unspecialized - scala.annotation - |""".stripMargin, - ">=3.4.2-RC1-bin-20240301-hash-NIGHTLY" -> "" - ), - topLines = Some(5) + "" ) check( diff --git a/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala b/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala index 6cc7aa9e2bb..cde0213a656 100644 --- a/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala +++ b/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala @@ -2368,4 +2368,17 @@ class CompletionSuite extends BaseCompletionSuite { itemIndex = 1 ) + check( + "def-arg", + """|package a + |object W { + | val aaaaaa = 1 + |} + |object O { + | def foo(aa@@) + |} + |""".stripMargin, + "" + ) + }