Skip to content

Commit

Permalink
Merged branch idea243.x-timaliberdov into idea243.x
Browse files Browse the repository at this point in the history
  • Loading branch information
builduser committed Dec 18, 2024
2 parents 74832f9 + 811c50a commit 0a94402
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class ScalaDirectClassInheritorsSearcher extends QueryExecutor[PsiClass, DirectC
}

for (candidate <- candidates if inReadAction { candidate.showAsInheritor }) {
ProgressManager.checkCanceled()
if (inReadAction(candidate.isInheritor(clazz, false)))
add(candidate)
add(candidate)
}

if (map.nonEmpty) {
Expand Down Expand Up @@ -104,4 +102,4 @@ class ScalaDirectClassInheritorsSearcher extends QueryExecutor[PsiClass, DirectC

true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.plugins.scala.lang.psi.stubs.util

import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiClass
import com.intellij.psi.search.searches.ClassInheritorsSearch
Expand Down Expand Up @@ -87,46 +88,32 @@ object ScalaInheritors {

val inheritorsBuilder = ArraySeq.newBuilder[ScTemplateDefinition]

def possibleAliases: List[(String, String)] = {
val possibleNames: List[String] = {
val typeAliases =
ALIASED_CLASS_NAME_KEY.elements(name, scope).map(ta => (ta.name, ta.qualifiedNameOpt.getOrElse(ta.name)))
ALIASED_CLASS_NAME_KEY.elements(name, scope).map(_.name)
.toList
val importAliases =
ALIASED_IMPORT_KEY.elements(name, scope).flatMap(_.aliasName.map(x => (x, x)))
.toList
ALIASED_IMPORT_KEY.elements(name, scope).flatMap(_.aliasName).toList

typeAliases ::: importAliases
name :: typeAliases ::: importAliases
}

def addCandidates(superName: String, superQName: String): Unit = {

possibleNames.foreach { superName =>
val extendsBlockIterable = SUPER_CLASS_NAME_KEY.elements(superName, scope)
val extendsBlocks = extendsBlockIterable.iterator

while (extendsBlocks.hasNext) {
ProgressManager.checkCanceled()

val extendsBlock = extendsBlocks.next()
extendsBlock.getParent match {
case tp: ScTemplateDefinition =>
// simple names are stored in index, but in decompiled files they are qualified
val superReferenceTexts =
directSuperReferenceTexts(extendsBlock)
.iterator
.map(_.stripPrefix("_root_.").stripPrefix("super."))

if (superReferenceTexts.exists(superQName.endsWith)) {
inheritorsBuilder += tp
}
case tp: ScTemplateDefinition if tp.isInheritor(clazz, false) =>
inheritorsBuilder += tp
case _ =>
}
}
}

val qName = clazz.qualifiedNameOpt.getOrElse(name)
val nameWithPossibleAliases = (name, qName) :: possibleAliases
nameWithPossibleAliases.foreach {
case (name, qName) => addCandidates(name, qName)
}

inheritorsBuilder.result()
}

Expand Down Expand Up @@ -216,7 +203,7 @@ object ScalaInheritors {

clazz match {
case td: ScTypeDefinition if !td.isEffectivelyFinal =>
val directInheritors = directInheritorCandidates(clazz, clazz.resolveScope).filter(_.isInheritor(td, false))
val directInheritors = directInheritorCandidates(clazz, clazz.resolveScope)
directInheritors.foreach(inner)

//todo collect inheritors of java classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package org.jetbrains.plugins.scala.lang.search

import com.intellij.psi.PsiClass
import com.intellij.psi.search.searches.ClassInheritorsSearch
import org.jetbrains.plugins.scala.ScalaVersion
import org.jetbrains.plugins.scala.base.ScalaLightCodeInsightFixtureTestCase
import org.jetbrains.plugins.scala.extensions.{PsiElementExt, PsiNamedElementExt}
import org.junit.Assert.{assertEquals, assertTrue}

import scala.jdk.CollectionConverters.CollectionHasAsScala

class InheritorsSearchTest extends ScalaLightCodeInsightFixtureTestCase {
private def doTest(fileText: String, expectedSubclassNames: String*): Unit = {
abstract class InheritorsSearchTestBase extends ScalaLightCodeInsightFixtureTestCase {
protected def doTest(fileText: String, expectedSubclassNames: String*): Unit = {
val file = configureFromFileText(fileText)
val caretOffset = getEditor.getCaretModel.getOffset
assertTrue("Caret position is missing", caretOffset > 0)
Expand Down Expand Up @@ -63,6 +64,29 @@ class InheritorsSearchTest extends ScalaLightCodeInsightFixtureTestCase {
"AA2", "AA3"
)

// SCL-23294
def testPackageAliasImport(): Unit = {
myFixture.addFileToProject(
"bar/X.scala",
"""package bar
|
|import _root_.{foo => F}
|
|class X extends F.C
|""".stripMargin
)

doTest(
s"""package foo
|
|class ${CARET}C {
| def hello(): Unit = println("Hello")
|}
|""".stripMargin,
"X"
)
}

def testTypeAlias_WithAliasedFullyQualifiedName_1(): Unit = doTest(
s"""package org.example
|
Expand Down Expand Up @@ -128,7 +152,6 @@ class InheritorsSearchTest extends ScalaLightCodeInsightFixtureTestCase {
|""".stripMargin,
"AA2", "AA3")


def testImportAlias(): Unit = doTest(
s"""
|object X {
Expand Down Expand Up @@ -166,7 +189,6 @@ class InheritorsSearchTest extends ScalaLightCodeInsightFixtureTestCase {
|""".stripMargin,
"B1", "B2")


//SCL-18672
def testPrivateSealedTrait(): Unit = doTest(
s"""
Expand Down Expand Up @@ -197,4 +219,35 @@ class InheritorsSearchTest extends ScalaLightCodeInsightFixtureTestCase {
|}
|""".stripMargin,
"SuccessResult", "NotFoundResult", "WrongResult")
}
}

final class InheritorsSearchTest extends InheritorsSearchTestBase {
override def supportedIn(version: ScalaVersion): Boolean = version == ScalaVersion.Latest.Scala_2_13
}

final class InheritorsSearchTest_Scala3 extends InheritorsSearchTestBase {
override def supportedIn(version: ScalaVersion): Boolean = version == ScalaVersion.Latest.Scala_3

// SCL-23294
def testPackageAliasUnqualifiedImport(): Unit = {
myFixture.addFileToProject(
"bar/X.scala",
"""package bar
|
|import foo as F
|
|class X extends F.C
|""".stripMargin
)

doTest(
s"""package foo
|
|class ${CARET}C {
| def hello(): Unit = println("Hello")
|}
|""".stripMargin,
"X"
)
}
}

0 comments on commit 0a94402

Please sign in to comment.