Skip to content

Commit

Permalink
bugfix: rename end marker for lolac definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 31, 2023
1 parent 684e67f commit a09c27a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
27 changes: 24 additions & 3 deletions mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
) =
Expand Down Expand Up @@ -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]](
Expand All @@ -502,7 +521,7 @@ abstract class PcCollector[T](
occurences + collect(
df,
pos.withSpan(df.nameSpan),
)
) ++ collectEndMarker
) { case (set, tree) =>
traverser(set, tree)
}
Expand Down Expand Up @@ -667,3 +686,5 @@ case class ExtensionParamOccurence(
sym: Symbol,
methods: List[untpd.Tree],
)

case class EndMarker(name: String)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class PcDocumentHighlightProvider(
def collect(
parent: Option[Tree]
)(
tree: Tree,
tree: Tree | EndMarker,
toAdjust: SourcePosition,
sym: Option[Symbol],
): DocumentHighlight =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ 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()

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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions tests/cross/src/test/scala/tests/pc/PcRenameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,12 @@ class PcRenameSuite extends BasePcRenameSuite {
| } yield b
|""".stripMargin,
)

check(
"end-marker".tag(IgnoreScala2),
"""|def <<he@@llo>>(a: Int) =
| ???
|end <<hello>>
|""".stripMargin,
)
}

0 comments on commit a09c27a

Please sign in to comment.