From e68cc699387ca74166b3a7f044bca562c3343e26 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 19 Mar 2025 09:46:10 +0100 Subject: [PATCH] Ignore ending `$` when looking at end marker names (#22798) ## Fix scalameta/metals#7246 - Add a test for renaming local objects - Drop ending `$` marker when looking for end marker names [Cherry-picked 8027996fa50b0861983c466082df0eed6efba687] --- .../src/main/dotty/tools/pc/PcCollector.scala | 2 +- .../dotty/tools/pc/tests/edit/PcRenameSuite.scala | 11 +++++++++++ .../tools/pc/tests/tokens/SemanticTokensSuite.scala | 13 +++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala b/presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala index 1ebfd405768e..f3fd7c8738d5 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala @@ -315,7 +315,7 @@ object EndMarker: def getPosition(df: NamedDefTree, pos: SourcePosition, sourceText: String)( implicit ct: Context ): Option[SourcePosition] = - val name = df.name.toString() + val name = df.name.toString().stripSuffix("$") val endMarkerLine = sourceText.slice(df.span.start, df.span.end).split('\n').last val index = endMarkerLine.length() - name.length() 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 3226018f88d4..50806322b09e 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala @@ -534,3 +534,14 @@ class PcRenameSuite extends BasePcRenameSuite: |""".stripMargin, wrap = false ) + + @Test def `local-object-with-end-rename` = + check( + """|def bar = + | object <>: + | def aaa = ??? + | end <> + | 1 + |""".stripMargin, + wrap = false + ) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala index fd90a8dfaca0..9f8d82020806 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala @@ -258,7 +258,7 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite: | } = new: | def <>/*method,definition*/ = "4.0" | <>/*variable,readonly*/.<>/*method*/ - |end StructuralTypes + |end <>/*class,definition*/ |""".stripMargin ) @@ -431,4 +431,13 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite: | <>/*method*/[<