From 6496ae809d9d83a74171f389ee136ce7ed98bc82 Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Tue, 20 Feb 2024 14:40:11 +0100 Subject: [PATCH] improvement: Add fallback for inlay hints For Scala 3.nightly, it maps old synthetic decorations to inlay hints. Its needed because inlay hints are not available for every Scala version --- .../meta/internal/metals/Compilers.scala | 34 +++++++++++++++++-- .../metals/CompilerInlayHintsParams.scala | 10 ++++++ .../CompilerSyntheticDecorationsParams.scala | 19 +++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerSyntheticDecorationsParams.scala diff --git a/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala b/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala index 127cff2df38..8da3a94e58e 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/Compilers.scala @@ -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 @@ -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 @@ -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] @@ -588,6 +606,8 @@ class Compilers( .asJava } + val rangeParams = + CompilerRangeParamsUtils.fromPos(pos, token) val pcParams = CompilerInlayHintsParams( rangeParams, typeParameters = userConfig().showInferredType.contains("true"), @@ -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)) } diff --git a/mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerInlayHintsParams.scala b/mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerInlayHintsParams.scala index 3f145850455..aba471e5cf4 100644 --- a/mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerInlayHintsParams.scala +++ b/mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerInlayHintsParams.scala @@ -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 + ) + } } diff --git a/mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerSyntheticDecorationsParams.scala b/mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerSyntheticDecorationsParams.scala new file mode 100644 index 00000000000..3366595aa43 --- /dev/null +++ b/mtags-shared/src/main/scala/scala/meta/internal/metals/CompilerSyntheticDecorationsParams.scala @@ -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 +}