Skip to content

Commit ec610bd

Browse files
committed
chore: check name when renaming
1 parent 7995e9b commit ec610bd

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

Diff for: presentation-compiler/src/main/dotty/tools/pc/PcRenameProvider.scala

+12-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ final class PcRenameProvider(
1818
name: Option[String]
1919
) extends WithSymbolSearchCollector[l.TextEdit](driver, params):
2020
private val forbiddenMethods =
21-
Set("equals", "hashCode", "unapply", "unary_!", "!")
21+
Set("equals", "hashCode", "unapply", "apply", "<init>", "unary_!", "!")
22+
23+
private val soughtSymbolNames = soughtSymbols match
24+
case Some((symbols, _)) =>
25+
symbols.filterNot(_.isError).map(symbol => symbol.decodedName.toString)
26+
case None => Set.empty[String]
27+
2228
def canRenameSymbol(sym: Symbol)(using Context): Boolean =
23-
(!sym.is(Method) || !forbiddenMethods(sym.decodedName))
24-
&& (sym.ownersIterator.drop(1).exists(ow => ow.is(Method))
25-
|| sym.source.path.isWorksheet)
29+
val decodedName = sym.decodedName
30+
def isForbiddenMethod = sym.is(Method) && forbiddenMethods(decodedName)
31+
def local = sym.ownersIterator.drop(1).exists(ow => ow.is(Method))
32+
def isInWorksheet = sym.source.path.isWorksheet
33+
!isForbiddenMethod && (local || isInWorksheet) && soughtSymbolNames(decodedName)
2634

2735
def prepareRename(): Option[l.Range] =
2836
soughtSymbols.flatMap((symbols, pos) =>

Diff for: presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala

+26
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,29 @@ class PcRenameSuite extends BasePcRenameSuite:
508508
| ???
509509
| end bar""".stripMargin
510510
)
511+
512+
@Test def `apply-rename` =
513+
check(
514+
"""|object B {
515+
| def locally = {
516+
| object A{ def app@@ly(a: Int) = ??? }
517+
| A(123)
518+
| A.apply(123)
519+
| }
520+
|}
521+
|""".stripMargin,
522+
wrap = false
523+
)
524+
525+
@Test def `constructor-rename` =
526+
check(
527+
"""|object B {
528+
| def locally = {
529+
| class A(a : String){ def th@@is(a: Int) = this(a.toString) }
530+
| A(123)
531+
| A.apply(123)
532+
| }
533+
|}
534+
|""".stripMargin,
535+
wrap = false
536+
)

0 commit comments

Comments
 (0)