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 10, 2023
1 parent 26e9af1 commit aeb168a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/util-interface/src/main/java/xsbti/Action.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;

/**
* An Action 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 Action {

/** 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();
}
21 changes: 21 additions & 0 deletions internal/util-interface/src/main/java/xsbti/FileChanges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.List;

/** A collection of TextEdits that belong to a given URI. */
public interface FileChanges {

/** The URI that the edits belong to. */
URI uri();

/** The edits belonging to the URI. */
List<TextEdit> edits();
}
25 changes: 25 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,29 @@ 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:
*
* <ul>
* <li>An option to implement a stub method for `foo()` and `bar()`
* <li>An option to make `MyExample` abstract
* </ul>
*/
default List<Action> actions() {
return Collections.emptyList();
}
}
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.List;

/**
* 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 {

/** List of [[xsbti.FileChanges]] that belong to this WorkspaceEdit. */
List<FileChanges> changes();
}

0 comments on commit aeb168a

Please sign in to comment.