diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala index a4e33785e04..f3ea1f21dab 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala @@ -70,7 +70,7 @@ abstract class PcCollector[T]( case _ => rawPath def collect( parent: Option[Tree] - )(tree: Tree, pos: SourcePosition, symbol: Option[Symbol]): T + )(tree: Tree | EndMarker, pos: SourcePosition, symbol: Option[Symbol]): T /** * @return (adjusted position, should strip backticks) @@ -435,7 +435,7 @@ abstract class PcCollector[T]( parent: Option[Tree], ): Set[T] = def collect( - tree: Tree, + tree: Tree | EndMarker, pos: SourcePosition, symbol: Option[Symbol] = None, ) = @@ -493,6 +493,25 @@ abstract class PcCollector[T]( case df: NamedDefTree if df.span.isCorrect && df.nameSpan.isCorrect && filter(df) && !isGeneratedGiven(df) => + def collectEndMarker = + val name = df.name.toString() + val endMarker = s"end $name" + val endMarkerEnd = df.span.end + val endMarkerStart = endMarkerEnd - endMarker.length() + def potentialEndMarker = + sourceText.slice(endMarkerStart, endMarkerEnd) + Option.when( + endMarkerStart >= 0 && potentialEndMarker == endMarker + ) { + collect( + EndMarker(name), + pos + .withStart(endMarkerEnd - name.length()) + .withEnd(endMarkerEnd), + ) + } + end collectEndMarker + val annots = collectTrees(df.mods.annotations) val traverser = new PcCollector.DeepFolderWithParent[Set[T]]( @@ -502,7 +521,7 @@ abstract class PcCollector[T]( occurences + collect( df, pos.withSpan(df.nameSpan), - ) + ) ++ collectEndMarker ) { case (set, tree) => traverser(set, tree) } @@ -667,3 +686,5 @@ case class ExtensionParamOccurence( sym: Symbol, methods: List[untpd.Tree], ) + +case class EndMarker(name: String) diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/PcDocumentHighlightProvider.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/PcDocumentHighlightProvider.scala index e36fa9c152b..de9561feebb 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/PcDocumentHighlightProvider.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/PcDocumentHighlightProvider.scala @@ -18,7 +18,7 @@ final class PcDocumentHighlightProvider( def collect( parent: Option[Tree] )( - tree: Tree, + tree: Tree | EndMarker, toAdjust: SourcePosition, sym: Option[Symbol], ): DocumentHighlight = diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/PcInlineValueProviderImpl.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/PcInlineValueProviderImpl.scala index a249125ed0c..4942fa17fb8 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/PcInlineValueProviderImpl.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/PcInlineValueProviderImpl.scala @@ -19,7 +19,7 @@ import org.eclipse.{lsp4j as l} final class PcInlineValueProviderImpl( val driver: InteractiveDriver, val params: OffsetParams, -) extends PcCollector[Occurence](driver, params) +) extends PcCollector[Option[Occurence]](driver, params) with InlineValueProvider: val text = params.text.toCharArray() @@ -27,16 +27,18 @@ final class PcInlineValueProviderImpl( val position: l.Position = pos.toLsp.getStart() override def collect(parent: Option[Tree])( - tree: Tree, + tree: Tree | EndMarker, pos: SourcePosition, sym: Option[Symbol], - ): Occurence = + ): Option[Occurence] = val (adjustedPos, _) = adjust(pos) - Occurence(tree, parent, adjustedPos) + tree match + case tree: Tree => Some(Occurence(tree, parent, adjustedPos)) + case _ => None override def defAndRefs(): Either[String, (Definition, List[Reference])] = val newctx = driver.currentCtx.fresh.setCompilationUnit(unit) - val allOccurences = result() + val allOccurences = result().flatten for definition <- allOccurences .collectFirst { case Occurence(defn: ValDef, _, pos) => diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/PcRenameProvider.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/PcRenameProvider.scala index 1837d3aa420..d415ac4e20c 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/PcRenameProvider.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/PcRenameProvider.scala @@ -33,7 +33,11 @@ final class PcRenameProvider( def collect( parent: Option[Tree] - )(tree: Tree, toAdjust: SourcePosition, sym: Option[Symbol]): l.TextEdit = + )( + tree: Tree | EndMarker, + toAdjust: SourcePosition, + sym: Option[Symbol], + ): l.TextEdit = val (pos, stripBackticks) = adjust(toAdjust, forRename = true) l.TextEdit( pos.toLsp, diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala index 3f6654e8ff0..1bf18e23e6e 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/PcSemanticTokensProvider.scala @@ -60,18 +60,25 @@ final class PcSemanticTokensProvider( object Collector extends PcCollector[Option[Node]](driver, params): override def collect( parent: Option[Tree] - )(tree: Tree, pos: SourcePosition, symbol: Option[Symbol]): Option[Node] = - val sym = symbol.fold(tree.symbol)(identity) - if !pos.exists || sym == null || sym == NoSymbol then None - else - Some( - makeNode( - sym = sym, - pos = adjust(pos)._1, - isDefinition = isDefinition(tree), - isDeclaration = isDeclaration(tree), - ) - ) + )( + tree: Tree | EndMarker, + pos: SourcePosition, + symbol: Option[Symbol], + ): Option[Node] = + tree match + case tree: Tree => + val sym = symbol.fold(tree.symbol)(identity) + if !pos.exists || sym == null || sym == NoSymbol then None + else + Some( + makeNode( + sym = sym, + pos = adjust(pos)._1, + isDefinition = isDefinition(tree), + isDeclaration = isDeclaration(tree), + ) + ) + case _ => None end collect end Collector diff --git a/tests/cross/src/test/scala/tests/pc/PcRenameSuite.scala b/tests/cross/src/test/scala/tests/pc/PcRenameSuite.scala index d454728eb71..bacd3ecfcc0 100644 --- a/tests/cross/src/test/scala/tests/pc/PcRenameSuite.scala +++ b/tests/cross/src/test/scala/tests/pc/PcRenameSuite.scala @@ -488,4 +488,12 @@ class PcRenameSuite extends BasePcRenameSuite { | } yield b |""".stripMargin, ) + + check( + "end-marker".tag(IgnoreScala2), + """|def <>(a: Int) = + | ??? + |end <> + |""".stripMargin, + ) }