Skip to content

Backport from Metals #22665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Loading