From aeb168aa9387f6a6b634520682653a29ddc09db0 Mon Sep 17 00:00:00 2001 From: Chris Kipp Date: Mon, 8 May 2023 15:07:15 +0200 Subject: [PATCH] feat: add in `actions()` to `Problem` 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. --- .../src/main/java/xsbti/Action.java | 32 +++++++++++++++++++ .../src/main/java/xsbti/FileChanges.java | 21 ++++++++++++ .../src/main/java/xsbti/Problem.java | 25 +++++++++++++++ .../src/main/java/xsbti/TextEdit.java | 25 +++++++++++++++ .../src/main/java/xsbti/WorkspaceEdit.java | 27 ++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 internal/util-interface/src/main/java/xsbti/Action.java create mode 100644 internal/util-interface/src/main/java/xsbti/FileChanges.java create mode 100644 internal/util-interface/src/main/java/xsbti/TextEdit.java create mode 100644 internal/util-interface/src/main/java/xsbti/WorkspaceEdit.java diff --git a/internal/util-interface/src/main/java/xsbti/Action.java b/internal/util-interface/src/main/java/xsbti/Action.java new file mode 100644 index 0000000000..6bfc128266 --- /dev/null +++ b/internal/util-interface/src/main/java/xsbti/Action.java @@ -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. + * + *

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 `CodeAction` + */ +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 description(); + + /** The actual edit contained in the action. */ + WorkspaceEdit edit(); +} diff --git a/internal/util-interface/src/main/java/xsbti/FileChanges.java b/internal/util-interface/src/main/java/xsbti/FileChanges.java new file mode 100644 index 0000000000..52ea6e5731 --- /dev/null +++ b/internal/util-interface/src/main/java/xsbti/FileChanges.java @@ -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 edits(); +} diff --git a/internal/util-interface/src/main/java/xsbti/Problem.java b/internal/util-interface/src/main/java/xsbti/Problem.java index e63a95b64b..ffe1292cde 100644 --- a/internal/util-interface/src/main/java/xsbti/Problem.java +++ b/internal/util-interface/src/main/java/xsbti/Problem.java @@ -48,4 +48,29 @@ default Optional diagnosticCode() { default List diagnosticRelatedInforamation() { return Collections.emptyList(); } + + /** + * Actions (aka quick fixes) that are able to either fix or address the issue that is causing this + * Problem. + * + *

For example given the following code: + * + *

+   *  trait Example:
+   *    def foo(): Unit
+   *    def bar(): Unit
+   *
+   *  class MyExample extends Example
+   * 
+ * + * You could expect this to have multiple actions attatched: + * + * + */ + default List actions() { + return Collections.emptyList(); + } } diff --git a/internal/util-interface/src/main/java/xsbti/TextEdit.java b/internal/util-interface/src/main/java/xsbti/TextEdit.java new file mode 100644 index 0000000000..caffb642d2 --- /dev/null +++ b/internal/util-interface/src/main/java/xsbti/TextEdit.java @@ -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. + * + *

NOTE: That instead of a `Range` we use the internal [[xsbti.Position]]. + * + * @see `TextEdit` + */ +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(); +} diff --git a/internal/util-interface/src/main/java/xsbti/WorkspaceEdit.java b/internal/util-interface/src/main/java/xsbti/WorkspaceEdit.java new file mode 100644 index 0000000000..438e38aad2 --- /dev/null +++ b/internal/util-interface/src/main/java/xsbti/WorkspaceEdit.java @@ -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. + * + *

However it only supports the minimal `changes` to ensure the fixes will work with all clients. + * + *

NOTE: In the future this may be expanded to handle resource operations via `documentChanges`. + * + * @see `WorkspaceEdit` + */ +public interface WorkspaceEdit { + + /** List of [[xsbti.FileChanges]] that belong to this WorkspaceEdit. */ + List changes(); +}