Skip to content

Commit

Permalink
Backport from Metals (#22665)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek authored Feb 25, 2025
2 parents 7995e9b + 666f393 commit a25fe5e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ final class PcRenameProvider(
name: Option[String]
) extends WithSymbolSearchCollector[l.TextEdit](driver, params):
private val forbiddenMethods =
Set("equals", "hashCode", "unapply", "unary_!", "!")
Set("equals", "hashCode", "unapply", "apply", "<init>", "unary_!", "!")

private val soughtSymbolNames = soughtSymbols match
case Some((symbols, _)) =>
symbols.filterNot(_.isError).map(symbol => symbol.decodedName.toString)
case None => Set.empty[String]

def canRenameSymbol(sym: Symbol)(using Context): Boolean =
(!sym.is(Method) || !forbiddenMethods(sym.decodedName))
&& (sym.ownersIterator.drop(1).exists(ow => ow.is(Method))
|| sym.source.path.isWorksheet)
val decodedName = sym.decodedName
def isForbiddenMethod = sym.is(Method) && forbiddenMethods(decodedName)
def local = sym.ownersIterator.drop(1).exists(ow => ow.is(Method))
def isInWorksheet = sym.source.path.isWorksheet
!isForbiddenMethod && (local || isInWorksheet) && soughtSymbolNames(decodedName)

def prepareRename(): Option[l.Range] =
soughtSymbols.flatMap((symbols, pos) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,13 @@ class SelectionRangeSuite extends BaseSelectionRangeSuite:
|}""".stripMargin
)
)

@Test def `def - type params` =
check(
"""|object Main extends App {
| val func = (a@@: Int, b: Int) =>
| a + b
|}""".stripMargin,
List[String](
"""|object Main extends App {
| val func = (>>region>>a: Int<<region<<, b: Int) =>
| a + b
|}""".stripMargin,
"""|object Main extends App {
| val func = (>>region>>a: Int, b: Int<<region<<) =>
| a + b
|}""".stripMargin,
"""|object Main extends App {
| val func = >>region>>(a: Int, b: Int) =>
| a + b<<region<<
|}""".stripMargin,
"""|object Main extends App {
| >>region>>val func = (a: Int, b: Int) =>
| a + b<<region<<
|}""".stripMargin
"object Main extends App { def foo[Type@@ <: T1, B](hi: Int, b: Int, c:Int) = ??? }",
List(
"object Main extends App { def foo[>>region>>Type <: T1<<region<<, B](hi: Int, b: Int, c:Int) = ??? }",
"object Main extends App { def foo[>>region>>Type <: T1, B<<region<<](hi: Int, b: Int, c:Int) = ??? }",
"object Main extends App { >>region>>def foo[Type <: T1, B](hi: Int, b: Int, c:Int) = ???<<region<< }"
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,29 @@ class PcRenameSuite extends BasePcRenameSuite:
| ???
| end bar""".stripMargin
)

@Test def `apply-rename` =
check(
"""|object B {
| def locally = {
| object A{ def app@@ly(a: Int) = ??? }
| A(123)
| A.apply(123)
| }
|}
|""".stripMargin,
wrap = false
)

@Test def `constructor-rename` =
check(
"""|object B {
| def locally = {
| class A(a : String){ def th@@is(a: Int) = this(a.toString) }
| A(123)
| A.apply(123)
| }
|}
|""".stripMargin,
wrap = false
)

0 comments on commit a25fe5e

Please sign in to comment.