-
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
174 additions
and
35 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
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
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 path = | ||
params.getTextDocument.getUri.toAbsolutePath.toURI.toString | ||
|
||
def createDiagnosticAction( | ||
diagnostic: l.Diagnostic, | ||
textEdit: l.TextEdit, | ||
): l.CodeAction = { | ||
val codeAction = new l.CodeAction() | ||
val diagMessage = diagnostic.getMessage | ||
codeAction.setKind(l.CodeActionKind.QuickFix) | ||
codeAction.setTitle( | ||
s"Apply suggestion: ${diagMessage.split(System.lineSeparator()).headOption.getOrElse(diagMessage)}" | ||
) | ||
codeAction.setDiagnostics(List(diagnostic).asJava) | ||
codeAction.setEdit( | ||
new l.WorkspaceEdit( | ||
Map(path -> List(textEdit).asJava).asJava | ||
) | ||
) | ||
codeAction | ||
} | ||
|
||
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()) => | ||
createDiagnosticAction(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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
package tests.codeactions | ||
|
||
import coursier.version.Version | ||
|
||
class ActionableDiagnosticsSuite | ||
extends BaseCodeActionLspSuite("actionableDiagnostic") { | ||
|
||
val oldOsLibVersion: Version = Version("0.7.8") | ||
|
||
check( | ||
"actionable-diagnostic-update", | ||
s"""|//> <<>>using lib "com.lihaoyi::os-lib:${oldOsLibVersion.repr}" | ||
| | ||
|object Hello extends App { | ||
| println("Hello") | ||
|} | ||
|""".stripMargin, | ||
"Apply suggestion: com.lihaoyi::os-lib:0.7.8 is outdated", | ||
expectedCode = "", | ||
scalaCliOptions = List("--actions", "-S", scalaVersion), | ||
expectNoDiagnostics = false, | ||
scalaCliLayout = true, | ||
assertion = Some { _ => | ||
val sourceContent = server.bufferContents("a/src/main/scala/a/A.scala") | ||
val newOsLibVersion = raw"\d+\.\d+\.\d+".r | ||
.findFirstIn(sourceContent) | ||
.map(Version(_)) | ||
assert( | ||
newOsLibVersion.nonEmpty, | ||
"os-lib dependency should have been updated", | ||
) | ||
assert( | ||
oldOsLibVersion < newOsLibVersion.get, | ||
s"os-lib dependency should have been updated to newer version than ${oldOsLibVersion.repr}", | ||
) | ||
}, | ||
) | ||
|
||
} |