Skip to content

Commit

Permalink
feat: add in actions() to Problem
Browse files Browse the repository at this point in the history
This adds a new field into `xsbti.Problem` allowing for the compiler to
forward "actions" that can address diagnostics. The idea largely mimics
a very minimal `CodeAction` that can be found in the [LSP
Spec](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeAction)
in order to ensure it will work with a variety of difference clients
that use LSP, and those that don't. In the future the `WorkspaceEdit`
that was created here could also be expanded to handle more advanced
changes, aka resource operations, like creating/moving/deleting files.
For now we only focus on a small subset of these features.
  • Loading branch information
ckipp01 committed May 8, 2023
1 parent 26e9af1 commit 2caf931
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/util-interface/src/main/java/xsbti/Problem.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,27 @@ default Optional<DiagnosticCode> diagnosticCode() {
default List<DiagnosticRelatedInformation> diagnosticRelatedInforamation() {
return Collections.emptyList();
}

/**
* Actions (aka quick fixes) that are able to either fix or address the issue that is causing this
* Problem.
*
* <p>For example given the following code:
*
* <pre>
* trait Example:
* def foo(): Unit
* def bar(): Unit
*
* class MyExample extends Example
* </pre>
*
* You could expect this to have multiple actions attatched:
*
* <p>- An option to implement a stub method for `foo()` and `bar()` - An option to make
* `MyExample` abstract
*/
default List<ScalaAction> actions() {
return Collections.emptyList();
}
}
32 changes: 32 additions & 0 deletions internal/util-interface/src/main/java/xsbti/ScalaAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/

package xsbti;

import java.util.Optional;

/**
* A ScalaAction is very miminal representation of a `CodeAction` in the LSP protocol.
*
* <p>However it only focuses on the actual title, description, and edit, leaving it up to the
* language server to communicate with the client and put together a proper codeAction in accordance
* to client capabilities.
*
* @see <a href=
* "https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeAction">`CodeAction`</a>
*/
public interface ScalaAction {

/** Title of the action that will be shown to the user client side. */
String title();

/** Optional description that may be shown to the user client side to explain the action. */
Optional<String> description();

/** The actual edit contained in the action. */
WorkspaceEdit edit();
}
25 changes: 25 additions & 0 deletions internal/util-interface/src/main/java/xsbti/TextEdit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/

package xsbti;

/**
* A representation of the `TextEdit` found in the LSP protocol.
*
* <p>NOTE: That instead of a `Range` we use the internal [[xsbti.Position]].
*
* @see <a
* href="https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit">`TextEdit`</a>
*/
public interface TextEdit {

/** The position this edit will be applied to. */
Position position();

/** The next text that will be inserted into the given [[TextEdit.position]]. */
String newText();
}
27 changes: 27 additions & 0 deletions internal/util-interface/src/main/java/xsbti/WorkspaceEdit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/

package xsbti;

import java.net.URI;
import java.util.Map;

/**
* A minimal representatin of the `WorkspaceEdit` found in the LSP protocol.
*
* <p>However it only supports the minimal `changes` to ensure the fixes will work with all clients.
*
* <p>NOTE: In the future this may be expanded to handle resource operations via `documentChanges`.
*
* @see <a href=
* "https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspaceEdit">`WorkspaceEdit`</a>
*/
public interface WorkspaceEdit {

/** Map of the `TextEdit`s that belong to a specific URI. */
Map<URI, TextEdit> changes();
}

0 comments on commit 2caf931

Please sign in to comment.