Skip to content

Commit

Permalink
Somewhat working version of range hover action
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Aug 21, 2021
1 parent 9f8ccde commit dff09c0
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import scala.meta.internal.metals.ClientConfiguration
import scala.meta.internal.metals.Diagnostics
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.UserConfiguration
import scala.meta.internal.metals.HoverExtParams
import scala.meta.internal.metap.PrinterSymtab
import scala.meta.internal.mtags.Md5Fingerprints
import scala.meta.internal.mtags.Semanticdbs
Expand All @@ -31,7 +32,6 @@ import scala.meta.io.AbsolutePath
import scala.meta.tokens.Token.RightParen
import scala.meta.tokens.{Token => T}

import org.eclipse.lsp4j.TextDocumentPositionParams
import org.eclipse.{lsp4j => l}

final class SyntheticsDecorationProvider(
Expand Down Expand Up @@ -98,12 +98,12 @@ final class SyntheticsDecorationProvider(
}

def addSyntheticsHover(
params: TextDocumentPositionParams,
params: HoverExtParams,
pcHover: Option[l.Hover]
): Option[l.Hover] =
if (isSyntheticsEnabled) {
val path = params.getTextDocument().getUri().toAbsolutePath
val line = params.getPosition().getLine()
val path = params.textDocument.getUri().toAbsolutePath
val line = params.getPosition.getLine()
val newHover = currentDocument(path) match {
case Some(textDocument) =>
val edit = buffer.tokenEditDistance(path, textDocument.text, trees)
Expand Down Expand Up @@ -140,7 +140,7 @@ final class SyntheticsDecorationProvider(
path,
syntheticsAtLine,
pcHover,
params.getPosition()
params.getPosition
)
} else {
createHoverAtLine(path, syntheticsAtLine, pcHover).orElse(
Expand Down
36 changes: 30 additions & 6 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,24 @@ class Compilers(
}

def hover(
params: TextDocumentPositionParams,
params: HoverExtParams,
token: CancelToken
): Future[Option[Hover]] =
withPCAndAdjustLsp(params) { (pc, pos, adjust) =>
pc.hover(CompilerOffsetParams.fromPos(pos, token))
.asScala
.map(_.asScala.map { hover => adjust.adjustHoverResp(hover) })
): Future[Option[Hover]] = {
if (params.range != null) {
withPCAndAdjustLsp(params) { (pc, pos, adjust) =>
pc.hover(CompilerRangeParams.fromPos(pos, token))
.asScala
.map(_.asScala.map { hover => adjust.adjustHoverResp(hover) })
}
} else {
val param: TextDocumentPositionParams = new TextDocumentPositionParams(params.textDocument, params.position)
withPCAndAdjustLsp(param) { (pc, pos, adjust) =>
pc.hover(CompilerOffsetParams.fromPos(pos, token))
.asScala
.map(_.asScala.map { hover => adjust.adjustHoverResp(hover) })
}
}
}

def definition(
params: TextDocumentPositionParams,
Expand Down Expand Up @@ -536,7 +546,21 @@ class Compilers(
)
val metaPos = pos.toMeta(input)
fn(compiler, metaPos, adjust)
}

private def withPCAndAdjustLsp[T](
params: HoverExtParams
)(fn: (PresentationCompiler, Position, AdjustLspData) => T): T = {
val path = params.textDocument.getUri.toAbsolutePath
val compiler = loadCompiler(path).getOrElse(fallbackCompiler)

val (input, _, adjust) =
sourceAdjustments(
new TextDocumentPositionParams(params.textDocument, params.position),
compiler.scalaVersion()
)
val metaPos = params.range.toMeta(input)
fn(compiler, metaPos, adjust)
}

private def sourceAdjustments(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package scala.meta.internal.metals

import javax.annotation.Nullable

import org.eclipse.lsp4j.Position
import org.eclipse.lsp4j.Range
import org.eclipse.lsp4j.TextDocumentIdentifier

case class HoverExtParams(
textDocument: TextDocumentIdentifier,
@Nullable position: Position = null,
@Nullable range: Range = null
) {
def getPosition: Position =
if (position != null) position
else range.getStart()
}
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ class MetalsLanguageServer(
}

@JsonRequest("textDocument/hover")
def hover(params: TextDocumentPositionParams): CompletableFuture[Hover] =
def hover(params: HoverExtParams): CompletableFuture[Hover] = {
CancelTokens.future { token =>
compilers
.hover(params, token)
Expand All @@ -1328,14 +1328,15 @@ class MetalsLanguageServer(
}
.map(
_.orElse {
val path = params.getTextDocument.getUri.toAbsolutePath
val path = params.textDocument.getUri.toAbsolutePath
if (path.isWorksheet)
worksheetProvider.hover(path, params.getPosition())
worksheetProvider.hover(path, params.getPosition)
else
None
}.orNull
)
}
}

@JsonRequest("textDocument/documentHighlight")
def documentHighlights(
Expand Down
13 changes: 13 additions & 0 deletions mtags-interfaces/src/main/java/scala/meta/pc/RangeParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package scala.meta.pc;

/**
* Parameters for a presentation compiler request at a given offset in a single source file.
*/
public interface RangeParams extends OffsetParams {

/**
* The character offset of the request.
*/
int endOffset();

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ trait MtagsEnrichments extends CommonMtagsEnrichments {
}
}

implicit class XtensionRangeLspInverse(range: l.Range) {
def toMeta(input: m.Input): m.Position = {
m.Position.Range(
input,
range.getStart.getLine,
range.getStart.getCharacter,
range.getEnd.getLine,
range.getEnd.getCharacter
)
}
}

implicit class XtensionPositionLspInverse(pos: l.Position) {
def toMeta(input: m.Input): m.Position = {
m.Position.Range(
Expand Down
Loading

0 comments on commit dff09c0

Please sign in to comment.