From ec610bd4771533bac44fd65301d984d15eb50d2e Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Tue, 25 Feb 2025 12:40:07 +0100 Subject: [PATCH 1/2] chore: check name when renaming --- .../dotty/tools/pc/PcRenameProvider.scala | 16 +++++++++--- .../tools/pc/tests/edit/PcRenameSuite.scala | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcRenameProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/PcRenameProvider.scala index 666ccf9c614f..467f331ea7dc 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcRenameProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcRenameProvider.scala @@ -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", "", "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) => diff --git a/presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala index 23c81fcf515a..3226018f88d4 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala @@ -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 + ) From 666f393f9fe1e36e35774210b77cb270e4bc1831 Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Tue, 25 Feb 2025 12:49:20 +0100 Subject: [PATCH 2/2] test: add type params selection range test --- .../tools/pc/tests/SelectionRangeSuite.scala | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/SelectionRangeSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/SelectionRangeSuite.scala index e277a67c466b..143d998a0ec1 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/SelectionRangeSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/SelectionRangeSuite.scala @@ -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< - | a + b - |}""".stripMargin, - """|object Main extends App { - | val func = (>>region>>a: Int, b: Int< - | a + b - |}""".stripMargin, - """|object Main extends App { - | val func = >>region>>(a: Int, b: Int) => - | a + b<>region>>val func = (a: Int, b: Int) => - | a + b<>region>>Type <: T1<>region>>Type <: T1, B<>region>>def foo[Type <: T1, B](hi: Int, b: Int, c:Int) = ???<