Skip to content

Commit

Permalink
add compat
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek authored and tgodzik committed Dec 14, 2024
1 parent cea0466 commit 0adf847
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,8 @@ class Compilers(
else InlayHintKind.Parameter
hint.setKind(kind)
hint.setData(
internal.pc.InlayHints.toData(params.uri(), List(Left("")))
internal.pc.InlayHints
.toData(params.uri().toString(), List(Left("")))
)
hint
}
Expand All @@ -624,7 +625,10 @@ class Compilers(
}
.map { hint =>
hint.setPosition(adjust.adjustPos(hint.getPosition()))
hint
InlayHintCompat.maybeFixInlayHintData(
hint,
params.getTextDocument().getUri(),
)
}
.asJava
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,27 @@ final class InlayHintResolveProvider(
}

}

object InlayHintCompat {
private def parseData(
data: Array[Any]
): List[Either[String, l.Position]] =
data.map {
case data: l.Position => Right(data)
case data: String => Left(data)
}.toList

// for compatibility with old inlay hint data
def maybeFixInlayHintData(hint: InlayHint, uri: String): InlayHint = {
if (hint.getData.isInstanceOf[Array[_]]) {
try {
val labelParts = parseData(hint.getData.asInstanceOf[Array[Any]])
hint.setData(InlayHints.toData(uri, labelParts))
} catch {
case e: Throwable =>
scribe.warn(s"Failed to fix inlay hint data: $e")
}
}
hint
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ case class InlayHints(
hint.setPosition(pos)
val (label, dataInfo) = labelParts.map(lp => (lp.label, lp.data)).unzip
hint.setLabel(label.asJava)
hint.setData(InlayHints.toData(uri, dataInfo))
hint.setData(InlayHints.toData(uri.toString(), dataInfo))
hint.setKind(kind)
hint
}
Expand Down Expand Up @@ -96,22 +96,24 @@ object InlayHints {
buffer.toList.filter(_.name.nonEmpty)
}

def toData(uri: URI, data: List[Either[String, l.Position]]): JsonElement =
def toData(uri: String, data: List[Either[String, l.Position]]): JsonElement =
gson.toJsonTree(
InlineHintData(
uri,
data.map {
case Left(str) => LabelPartData("string", str, null)
case Right(pos) => LabelPartData("position", null, pos)
}.asJava
}.toArray
)
)

def fromData(json: JsonElement): (URI, List[Either[String, l.Position]]) = {
def fromData(
json: JsonElement
): (String, List[Either[String, l.Position]]) = {
val data = gson.fromJson(json, classOf[InlineHintData])
(
data.uri,
data.labelParts.asScala.toList.map { part =>
data.labelParts.toList.map { part =>
part.dataType match {
case "position" => Right(part.position)
case "string" => Left(part.string)
Expand All @@ -122,8 +124,8 @@ object InlayHints {
}

final case class InlineHintData(
uri: URI,
labelParts: java.util.List[LabelPartData]
uri: String,
labelParts: Array[LabelPartData]
)

// "string" or "position"
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/src/main/scala/tests/BaseInlayHintsExpectSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import scala.meta.internal.metals.CompilerInlayHintsParams
import scala.meta.internal.metals.CompilerRangeParams
import scala.meta.internal.metals.EmptyCancelToken
import scala.meta.internal.metals.InlayHintCompat
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.pc.PresentationCompiler

Expand Down Expand Up @@ -35,7 +36,15 @@ abstract class BaseInlayHintsExpectSuite(
true,
)
val inlayHints =
compiler.inlayHints(pcParams).get().asScala.toList
compiler
.inlayHints(pcParams)
.get()
.asScala
.toList
.map(
InlayHintCompat
.maybeFixInlayHintData(_, file.file.toURI.toString())
)
TestInlayHints.applyInlayHints(file.code, inlayHints)
},
)
Expand Down

0 comments on commit 0adf847

Please sign in to comment.