Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve signature help by more stable position calculation + better named arg support #19214

Merged
merged 14 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
if (t.symbol.exists && t.hasType) {
if (!t.symbol.isCompleted) t.symbol.info = UnspecifiedErrorType
t.symbol.annotations.foreach { annot =>
/* In some cases annotations are are used on themself (possibly larger cycles).
/* In some cases annotations are used on themself (possibly larger cycles).
* This is the case with the java.lang.annotation.Target annotation, would end
* in an infinite loop while cleaning. The `seen` is added to ensure that those
* trees are not cleand twice.
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ extends SyntaxMsg(UnboundPlaceholderParameterID) {
|"""
}

class IllegalStartSimpleExpr(illegalToken: String)(using Context)
class IllegalStartSimpleExpr(val illegalToken: String)(using Context)
rochala marked this conversation as resolved.
Show resolved Hide resolved
extends SyntaxMsg(IllegalStartSimpleExprID) {
def msg(using Context) = i"expression expected but ${Red(illegalToken)} found"
def explain(using Context) = {
Expand Down Expand Up @@ -1171,7 +1171,7 @@ extends ReferenceMsg(ForwardReferenceExtendsOverDefinitionID) {
|"""
}

class ExpectedTokenButFound(expected: Token, found: Token, prefix: String = "")(using Context)
class ExpectedTokenButFound(val expected: Token, found: Token, prefix: String = "")(using Context)
extends SyntaxMsg(ExpectedTokenButFoundID) {

private def foundText = Tokens.showToken(found)
Expand Down
403 changes: 253 additions & 150 deletions compiler/src/dotty/tools/dotc/util/Signatures.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,18 @@ class DottyLanguageServer extends LanguageServer
override def signatureHelp(params: TextDocumentPositionParams) = computeAsync { canceltoken =>
val uri = new URI(params.getTextDocument.getUri)
val driver = driverFor(uri)
implicit def ctx: Context = driver.currentCtx

val pos = sourcePosition(driver, uri, params.getPosition)
val trees = driver.openedTrees(uri)
val path = Interactive.pathTo(trees, pos)
val (paramN, callableN, signatures) = Signatures.signatureHelp(path, pos.span)
new SignatureHelp(signatures.map(signatureToSignatureInformation).asJava, callableN, paramN)
driver.compilationUnits.get(uri) match
case Some(unit) =>
given newCtx: Context = driver.currentCtx.fresh.setCompilationUnit(unit)
val pos = sourcePosition(driver, uri, params.getPosition)
val adjustedSpan = pos.span.withEnd(pos.span.end + 1)
val path = Interactive.pathTo(ctx.compilationUnit.tpdTree, adjustedSpan)
val (paramN, callableN, signatures) = Signatures.signatureHelp(path, adjustedSpan)

new SignatureHelp(signatures.map(signatureToSignatureInformation).asJava, callableN, paramN)

case _ => new SignatureHelp()

}

Expand Down Expand Up @@ -930,23 +935,25 @@ object DottyLanguageServer {

/** Convert `signature` to a `SignatureInformation` */
def signatureToSignatureInformation(signature: Signatures.Signature): lsp4j.SignatureInformation = {
val tparams = signature.tparams.map(Signatures.Param("", _))
val paramInfoss =
(tparams ::: signature.paramss.flatten).map(paramToParameterInformation)
(signature.paramss.flatten).map(paramToParameterInformation)
val paramLists =
if signature.paramss.forall(_.isEmpty) && tparams.nonEmpty then
""
else
signature.paramss
.map { paramList =>
val labels = paramList.map(_.show)
val prefix = if paramList.exists(_.isImplicit) then "using " else ""
labels.mkString(prefix, ", ", "")
}
.mkString("(", ")(", ")")
val tparamsLabel = if (signature.tparams.isEmpty) "" else signature.tparams.mkString("[", ", ", "]")
signature.paramss
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this whole duplication is because there is no good place for it, such that it could be used by both modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dotty language server should actually be removed from the repository as it is no longer necessary. It is not so easy tho as the test coverage is different. This change was only made to make the tests pass

.map { paramList =>
val labels = paramList.map(_.show)
val isImplicit = paramList.exists:
case p: Signatures.MethodParam => p.isImplicit
case _ => false
val prefix = if isImplicit then "using " else ""
val isTypeParams = paramList.forall(_.isInstanceOf[Signatures.TypeParam]) && paramList.nonEmpty
val wrap: String => String = label => if isTypeParams then
s"[$label]"
else
s"($label)"
wrap(labels.mkString(prefix, ", ", ""))
}.mkString
val returnTypeLabel = signature.returnType.map(t => s": $t").getOrElse("")
val label = s"${signature.name}$tparamsLabel$paramLists$returnTypeLabel"
val label = s"${signature.name}$paramLists$returnTypeLabel"
val documentation = signature.doc.map(DottyLanguageServer.markupContent)
val sig = new lsp4j.SignatureInformation(label)
sig.setParameters(paramInfoss.asJava)
Expand Down
Loading
Loading