Skip to content

Commit

Permalink
improvement: Add fallback for inlay hints
Browse files Browse the repository at this point in the history
For Scala 3.nightly, it maps old synthetic decorations to inlay hints.
Its needed because inlay hints are not available for every Scala version
  • Loading branch information
jkciesluk committed Feb 28, 2024
1 parent 4ecdb89 commit 6496ae8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
34 changes: 31 additions & 3 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import scala.meta.pc.HoverSignature
import scala.meta.pc.OffsetParams
import scala.meta.pc.PresentationCompiler
import scala.meta.pc.SymbolSearch
import scala.meta.pc.SyntheticDecorationsParams

import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import ch.epfl.scala.bsp4j.CompileReport
Expand All @@ -46,6 +47,7 @@ import org.eclipse.lsp4j.Diagnostic
import org.eclipse.lsp4j.DocumentHighlight
import org.eclipse.lsp4j.InitializeParams
import org.eclipse.lsp4j.InlayHint
import org.eclipse.lsp4j.InlayHintKind
import org.eclipse.lsp4j.InlayHintParams
import org.eclipse.lsp4j.RenameParams
import org.eclipse.lsp4j.SelectionRange
Expand Down Expand Up @@ -569,8 +571,24 @@ class Compilers(
token: CancelToken,
): Future[ju.List[InlayHint]] = {
withPCAndAdjustLsp(params) { (pc, pos, adjust) =>
val rangeParams =
CompilerRangeParamsUtils.fromPos(pos, token)
def inlayHintsFallback(
params: SyntheticDecorationsParams
): Future[ju.List[InlayHint]] = {
pc.syntheticDecorations(params)
.asScala
.map(
_.map { d =>
val hint = new InlayHint()
hint.setPosition(d.range().getStart())
hint.setLabel(d.label())
val kind =
if (d.kind() <= 2) InlayHintKind.Type
else InlayHintKind.Parameter
hint.setKind(kind)
hint
}
)
}

def adjustInlayHints(
inlayHints: ju.List[InlayHint]
Expand All @@ -588,6 +606,8 @@ class Compilers(
.asJava
}

val rangeParams =
CompilerRangeParamsUtils.fromPos(pos, token)
val pcParams = CompilerInlayHintsParams(
rangeParams,
typeParameters = userConfig().showInferredType.contains("true"),
Expand All @@ -596,10 +616,18 @@ class Compilers(
implicitParameters = userConfig().showImplicitArguments,
implicitConversions = userConfig().showImplicitConversionsAndClasses,
)

pc
.inlayHints(pcParams)
.asScala
.map(adjustInlayHints)
.flatMap { hints =>
if (hints.isEmpty) {
inlayHintsFallback(pcParams.toSyntheticDecorationsParams)
.map(adjustInlayHints)
} else {
Future.successful(adjustInlayHints(hints))
}
}
}
.getOrElse(Future.successful(Nil.asJava))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ case class CompilerInlayHintsParams(
override def token(): CancelToken = rangeParams.token
override def offset(): Int = rangeParams.offset
override def endOffset(): Int = rangeParams.endOffset

def toSyntheticDecorationsParams: CompilerSyntheticDecorationsParams = {
CompilerSyntheticDecorationsParams(
rangeParams,
inferredTypes = inferredTypes,
typeParameters = typeParameters,
implicitConversions = implicitConversions,
implicitParameters = implicitParameters
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package scala.meta.internal.metals

import java.net.URI

import scala.meta.pc.CancelToken
import scala.meta.pc.SyntheticDecorationsParams
import scala.meta.pc.VirtualFileParams

case class CompilerSyntheticDecorationsParams(
virtualFileParams: VirtualFileParams,
inferredTypes: Boolean,
typeParameters: Boolean,
implicitParameters: Boolean,
implicitConversions: Boolean
) extends SyntheticDecorationsParams {
override def uri(): URI = virtualFileParams.uri
override def text(): String = virtualFileParams.text
override def token(): CancelToken = virtualFileParams.token
}

0 comments on commit 6496ae8

Please sign in to comment.