From af70623be043f15675217a55624098f7c54a4465 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Fri, 15 Dec 2023 20:01:51 +0100 Subject: [PATCH] refactor: Tweaks after reviews --- .../internal/pc/CompilerSearchVisitor.scala | 14 ++++---- .../meta/internal/pc/SemanticdbSymbols.scala | 8 ++--- .../internal/pc/completions/Completions.scala | 9 +++-- .../internal/mtags/ScalaToplevelMtags.scala | 3 +- .../pc/CompletionExtensionMethodSuite.scala | 36 +++++++++++-------- .../resources/expect/toplevels-scala3.expect | 1 - 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/CompilerSearchVisitor.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/CompilerSearchVisitor.scala index 6cdc980d473..8cfa3cb2fd0 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/CompilerSearchVisitor.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/CompilerSearchVisitor.scala @@ -21,13 +21,15 @@ class CompilerSearchVisitor( val logger: Logger = Logger.getLogger(classOf[CompilerSearchVisitor].getName) + private def isAccessibleImplicitClass(sym: Symbol) = + val owner = sym.maybeOwner + owner != NoSymbol && owner.isClass && + owner.is(Flags.Implicit) && + owner.isStatic && owner.isPublic + private def isAccessible(sym: Symbol): Boolean = try - sym != NoSymbol && sym.isPublic && sym.isStatic || { - val owner = sym.maybeOwner - owner != NoSymbol && owner.isClass && - owner.is(Flags.Implicit) && - owner.isStatic && owner.isPublic - } + sym != NoSymbol && sym.isPublic && sym.isStatic || + isAccessibleImplicitClass(sym) catch case err: AssertionError => logger.log(Level.WARNING, err.getMessage()) diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/SemanticdbSymbols.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/SemanticdbSymbols.scala index 92429cbdd73..3c8bcfa732b 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/SemanticdbSymbols.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/SemanticdbSymbols.scala @@ -51,15 +51,11 @@ object SemanticdbSymbols: /** * Looks like decl doesn't work for: * package a: - * implicit class A (i: Int): + * implicit class <> (i: Int): * def inc = i + 1 */ else if typeSym == NoSymbol then - val searched = typeName(value) - owner.info.allMembers - .find(_.name == searched) - .map(_.symbol) - .toList + owner.info.member(typeName(value)).symbol :: Nil else typeSym :: Nil end if case Descriptor.Term(value) => 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 7a3ac0d3491..d51d8307bd2 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 @@ -599,9 +599,14 @@ class Completions( qualType.widenDealias <:< sym.extensionParam.info.widenDealias def isImplicitClass(owner: Symbol) = + val constructorParam = - owner.info.allMembers - .find(_.symbol.isAllOf(Flags.PrivateParamAccessor)) + owner.info + .membersBasedOnFlags( + Flags.ParamAccessor, + Flags.EmptyFlags, + ) + .headOption .map(_.info) owner.isClass && owner.is(Flags.Implicit) && constructorParam.exists(p => diff --git a/mtags/src/main/scala/scala/meta/internal/mtags/ScalaToplevelMtags.scala b/mtags/src/main/scala/scala/meta/internal/mtags/ScalaToplevelMtags.scala index 418ae3f197d..46c4ba40a72 100644 --- a/mtags/src/main/scala/scala/meta/internal/mtags/ScalaToplevelMtags.scala +++ b/mtags/src/main/scala/scala/meta/internal/mtags/ScalaToplevelMtags.scala @@ -222,7 +222,8 @@ class ScalaToplevelMtags( loop( indent, isAfterNewline = false, - if (needsToGenerateFileClass) currRegion.withTermOwner(owner) else currRegion, + if (needsToGenerateFileClass) currRegion.withTermOwner(owner) + else currRegion, template ) // also covers extension methods because of `def` inside diff --git a/tests/cross/src/test/scala/tests/pc/CompletionExtensionMethodSuite.scala b/tests/cross/src/test/scala/tests/pc/CompletionExtensionMethodSuite.scala index 05e0c2ead35..dea2f793671 100644 --- a/tests/cross/src/test/scala/tests/pc/CompletionExtensionMethodSuite.scala +++ b/tests/cross/src/test/scala/tests/pc/CompletionExtensionMethodSuite.scala @@ -22,7 +22,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "simple-old-syntax", + "simple-old-syntax".tag(IgnoreForScala3CompilerPC), """|package example | |object Test: @@ -51,7 +51,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "simple2-old-syntax", + "simple2-old-syntax".tag(IgnoreForScala3CompilerPC), """|package example | |object enrichments: @@ -81,7 +81,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "simple-empty-old", + "simple-empty-old".tag(IgnoreForScala3CompilerPC), """|package example | |object enrichments: @@ -113,7 +113,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "filter-by-type-old", + "filter-by-type-old".tag(IgnoreForScala3CompilerPC), """|package example | |object enrichments: @@ -125,7 +125,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { |def main = "foo".iden@@ |""".stripMargin, """|identity: String (implicit) - |""".stripMargin // incr won't be available + |""".stripMargin // identity2 won't be available ) @@ -148,7 +148,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "filter-by-type-subtype-old", + "filter-by-type-subtype-old".tag(IgnoreForScala3CompilerPC), """|package example | |class A @@ -188,7 +188,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) checkEdit( - "simple-edit-old", + "simple-edit-old".tag(IgnoreForScala3CompilerPC), """|package example | |object enrichments: @@ -262,11 +262,11 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) checkEdit( - "simple-edit-suffix-old", + "simple-edit-suffix-old".tag(IgnoreForScala3CompilerPC), """|package example | |object enrichments: - | implicit class A (num: Int): + | implicit class A (val num: Int): | def plus(other: Int): Int = num + other | |def main = 100.pl@@ @@ -276,7 +276,7 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { |import example.enrichments.A | |object enrichments: - | implicit class A (num: Int): + | implicit class A (val num: Int): | def plus(other: Int): Int = num + other | |def main = 100.plus($0) @@ -300,7 +300,9 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "directly-in-pkg1-old".tag(IgnoreScalaVersion.forLessThan("3.2.2")), + "directly-in-pkg1-old" + .tag(IgnoreScalaVersion.forLessThan("3.2.2")) + .tag(IgnoreForScala3CompilerPC), """| |package examples: | implicit class A(num: Int): @@ -328,7 +330,9 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "directly-in-pkg2-old".tag(IgnoreScalaVersion.forLessThan("3.2.2")), + "directly-in-pkg2-old" + .tag(IgnoreScalaVersion.forLessThan("3.2.2")) + .tag(IgnoreForScala3CompilerPC), """|package examples: | object X: | def fooBar(num: Int) = num + 1 @@ -359,7 +363,9 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) checkEdit( - "directly-in-pkg3-old".tag(IgnoreScalaVersion.forLessThan("3.2.2")), + "directly-in-pkg3-old" + .tag(IgnoreScalaVersion.forLessThan("3.2.2")) + .tag(IgnoreForScala3CompilerPC), """|package examples: | implicit class A (num: Int) { def incr: Int = num + 1 } | @@ -394,7 +400,9 @@ class CompletionExtensionMethodSuite extends BaseCompletionSuite { ) check( - "nested-pkg-old".tag(IgnoreScalaVersion.forLessThan("3.2.2")), + "nested-pkg-old" + .tag(IgnoreScalaVersion.forLessThan("3.2.2")) + .tag(IgnoreForScala3CompilerPC), """|package aa: // some comment | package cc: | implicit class A (num: Int): diff --git a/tests/unit/src/test/resources/expect/toplevels-scala3.expect b/tests/unit/src/test/resources/expect/toplevels-scala3.expect index 96c318a2e61..2d139454969 100644 --- a/tests/unit/src/test/resources/expect/toplevels-scala3.expect +++ b/tests/unit/src/test/resources/expect/toplevels-scala3.expect @@ -42,7 +42,6 @@ example/Scalalib.scala -> example/Scalalib# example/StructuralTypes.scala -> example/StructuralTypes. example/ToplevelDefVal.scala -> example/ToplevelDefVal$package. example/ToplevelImplicit.scala -> example/ToplevelImplicit$package. -example/ToplevelImplicit.scala -> example/ToplevelImplicit$package. example/ToplevelImplicit.scala -> example/ToplevelImplicit$package.Xtension# example/ToplevelImplicit.scala -> example/ToplevelImplicit$package.XtensionAnyVal# example/TryCatch.scala -> example/TryCatch#