-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for actionable diagnotics from scala-cli
- Loading branch information
Showing
8 changed files
with
157 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
metals/src/main/scala/scala/meta/internal/metals/ScalacDiagnostic.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
metals/src/main/scala/scala/meta/internal/metals/codeactions/ActionableDiagnostic.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package scala.meta.internal.metals.codeactions | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
|
||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
import scala.meta.internal.metals._ | ||
import scala.meta.pc.CancelToken | ||
|
||
import org.eclipse.{lsp4j => l} | ||
|
||
class ActionableDiagnostic() extends CodeAction { | ||
override def kind: String = l.CodeActionKind.QuickFix | ||
|
||
override def contribute( | ||
params: l.CodeActionParams, | ||
token: CancelToken, | ||
)(implicit ec: ExecutionContext): Future[Seq[l.CodeAction]] = { | ||
|
||
def createActionableDiagnostic( | ||
diagnostic: l.Diagnostic, | ||
textEdit: l.TextEdit, | ||
): l.CodeAction = { | ||
val diagMessage = diagnostic.getMessage | ||
val uri = params.getTextDocument().getUri() | ||
|
||
CodeActionBuilder.build( | ||
title = | ||
s"Apply suggestion: ${diagMessage.linesIterator.headOption.getOrElse(diagMessage)}", | ||
kind = l.CodeActionKind.QuickFix, | ||
changes = List(uri.toAbsolutePath -> Seq(textEdit)), | ||
diagnostics = List(diagnostic), | ||
) | ||
} | ||
|
||
val codeActions = params | ||
.getContext() | ||
.getDiagnostics() | ||
.asScala | ||
.groupBy { | ||
case ScalacDiagnostic.ScalaAction(textEdit) => | ||
Some(textEdit) | ||
case _ => None | ||
} | ||
.collect { | ||
case (Some(textEdit), diags) | ||
if params.getRange().overlapsWith(diags.head.getRange()) => | ||
createActionableDiagnostic(diags.head, textEdit) | ||
} | ||
.toList | ||
.sorted | ||
|
||
Future.successful(codeActions) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/unit/src/test/scala/tests/codeactions/ActionableDiagnosticsSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tests.codeactions | ||
|
||
import scala.meta.internal.mtags.CoursierComplete | ||
|
||
import coursier.version.Version | ||
|
||
class ActionableDiagnosticsSuite | ||
extends BaseCodeActionLspSuite("actionableDiagnostic") { | ||
|
||
val oldOsLibVersion: Version = Version("0.7.8") | ||
val newestOsLib: String = CoursierComplete | ||
.complete("com.lihaoyi::os-lib:") | ||
.headOption | ||
.getOrElse("0.8.1") | ||
|
||
check( | ||
"actionable-diagnostic-update", | ||
s"""|//> <<>>using lib "com.lihaoyi::os-lib:${oldOsLibVersion.repr}" | ||
| | ||
|object Hello extends App { | ||
| println("Hello") | ||
|} | ||
|""".stripMargin, | ||
s"Apply suggestion: com.lihaoyi::os-lib:0.7.8 is outdated, update to $newestOsLib", | ||
s"""|//> using lib "com.lihaoyi::os-lib:$newestOsLib" | ||
| | ||
|object Hello extends App { | ||
| println("Hello") | ||
|} | ||
|""".stripMargin, | ||
scalaCliOptions = List("--actions", "-S", scalaVersion), | ||
expectNoDiagnostics = false, | ||
scalaCliLayout = true, | ||
) | ||
|
||
} |