Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: complete port of 'Add an id to track the type of each CodeAction' #995

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.codeaction.CodeActionFactory;
import org.eclipse.lsp4mp.ls.commons.CodeActionFactory;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.codeaction.IJavaCodeActionParticipant;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionContext;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.codeaction.JavaCodeActionResolveContext;
Expand All @@ -52,6 +52,7 @@
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData;
import org.eclipse.lsp4mp.commons.codeaction.MicroProfileCodeActionId;

/**
* QuickFix for fixing
Expand Down Expand Up @@ -99,7 +100,7 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,
// the properties file exists
TextDocumentItem document = new TextDocumentItem(uri, "properties", 0, insertText);
CodeAction codeAction = CodeActionFactory.insert(
getTitle(propertyName, configSource.getConfigFileName()), new Position(0, 0), insertText,
getTitle(propertyName, configSource.getConfigFileName()), MicroProfileCodeActionId.AssignValueToProperty, new Position(0, 0), insertText,
document, diagnostic);
codeActions.add(codeAction);
}
Expand Down Expand Up @@ -127,8 +128,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
String insertText = propertyName + "=" + lineSeparator;
TextDocumentEdit tde = insertTextEdit(new TextDocumentItem(uri, "properties", 0, insertText), insertText,
new Position(0, 0));
WorkspaceEdit workspaceEdit = new WorkspaceEdit(Collections.singletonList(Either.forLeft(tde)));
unresolved.setEdit(workspaceEdit);
unresolved.setEdit(new WorkspaceEdit(Collections.singletonList(Either.forLeft(tde))));
return unresolved;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData;
import org.eclipse.lsp4mp.commons.codeaction.MicroProfileCodeActionId;

Expand Down Expand Up @@ -82,10 +81,8 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
ChangeCorrectionProposal proposal = new ImplementInterfaceProposal(context.getCompilationUnit(), parentType,
context.getASTRoot(), MicroProfileHealthConstants.HEALTH_CHECK_INTERFACE, 0,
context.getSource().getCompilationUnit());
WorkspaceEdit workspaceEdit;
try {
workspaceEdit = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(workspaceEdit);
toResolve.setEdit(context.convertToWorkspaceEdit(proposal));
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unable to create workspace edit to make the class implement @HealthCheck", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData;
import org.eclipse.lsp4mp.commons.codeaction.MicroProfileCodeActionId;

Expand Down Expand Up @@ -100,8 +99,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
context.getASTRoot(), parentType, 0, addAnnotation, context.getSource().getCompilationUnit(),
REMOVE_ANNOTATION_NAMES);
try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
toResolve.setEdit(context.convertToWorkspaceEdit(proposal));
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to create workspace edit to replace bean scope annotation", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData;
import org.eclipse.lsp4mp.commons.codeaction.MicroProfileCodeActionId;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;

/**
* Generate OpenAPI annotations by the "Source" kind code action.
Expand Down Expand Up @@ -124,8 +118,7 @@ public CodeAction resolveCodeAction(JavaCodeActionResolveContext context) {
context.getSource().getCompilationUnit());

try {
WorkspaceEdit we = context.convertToWorkspaceEdit(proposal);
toResolve.setEdit(we);
toResolve.setEdit(context.convertToWorkspaceEdit(proposal));
} catch (Exception e) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.PropertiesManagerForJava;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.diagnostics.IJavaErrorCode;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4mp.commons.*;
import org.eclipse.lsp4mp.commons.codeaction.CodeActionData;
import org.eclipse.lsp4mp.commons.codeaction.MicroProfileCodeActionId;
import org.junit.Assert;

import java.io.File;
Expand All @@ -30,7 +31,7 @@
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

import static org.junit.Assert.assertTrue;
/**
* MicroProfile assert for java files for JUnit tests.
*
Expand Down Expand Up @@ -70,6 +71,11 @@ public static void assertCodeActions(List<? extends CodeAction> actual, CodeActi
// we don't want to compare title, etc
ca.setCommand(null);
ca.setKind(null);

if (ca.getEdit() != null && ca.getEdit().getChanges() != null) {
assertTrue(ca.getEdit().getChanges().isEmpty());
ca.getEdit().setChanges(null);
}
if (ca.getDiagnostics() != null) {
ca.getDiagnostics().forEach(d -> {
d.setSeverity(null);
Expand All @@ -83,10 +89,11 @@ public static void assertCodeActions(List<? extends CodeAction> actual, CodeActi
for (int i = 0; i < expected.length; i++) {
assertEquals("Assert title [" + i + "]", expected[i].getTitle(), actual.get(i).getTitle());
assertEquals("Assert edit [" + i + "]", expected[i].getEdit(), actual.get(i).getEdit());
assertEquals("Assert id [" + i + "]", ((CodeActionData)(expected[i].getData())).getCodeActionId(), ((CodeActionData)(actual.get(i).getData())).getCodeActionId());
}
}

public static CodeAction ca(String uri, String title, Diagnostic d, TextEdit... te) {
public static CodeAction ca(String uri, String title, MicroProfileCodeActionId id, Diagnostic d, TextEdit... te) {
CodeAction codeAction = new CodeAction();
codeAction.setTitle(title);
codeAction.setDiagnostics(Arrays.asList(d));
Expand All @@ -95,8 +102,8 @@ public static CodeAction ca(String uri, String title, Diagnostic d, TextEdit...

TextDocumentEdit textDocumentEdit = new TextDocumentEdit(versionedTextDocumentIdentifier, Arrays.asList(te));
WorkspaceEdit workspaceEdit = new WorkspaceEdit(Arrays.asList(Either.forLeft(textDocumentEdit)));
workspaceEdit.setChanges(Collections.emptyMap());
codeAction.setEdit(workspaceEdit);
codeAction.setData(new CodeActionData(id));
return codeAction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeActionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsSettings;
import org.eclipse.lsp4mp.commons.codeaction.MicroProfileCodeActionId;
import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -165,16 +166,16 @@ public void testCodeActionForUnassigned() throws Exception {

MicroProfileJavaCodeActionParams codeActionParams1 = createCodeActionParams(javaFileUri, d1, false);
assertJavaCodeAction(codeActionParams1, utils, //
ca(javaFileUri, "Insert 'defaultValue' attribute", d1, //
ca(javaFileUri, "Insert 'defaultValue' attribute", MicroProfileCodeActionId.ConfigPropertyInsertDefaultValue, d1, //
te(0, 0, 18, 0, "package org.acme.config;\n\nimport org.eclipse.microprofile.config.inject.ConfigProperty;\n\nimport io.quarkus.arc.config.ConfigProperties;\n\npublic class UnassignedValue {\n\n @ConfigProperty(name = \"foo\", defaultValue = \"\")\n private String foo;\n\n @ConfigProperties(prefix = \"server\")\n private class Server {\n\n @ConfigProperty(name = \"url\")\n private String url;\n }\n}\n")),
ca(propertiesFileUri, "Insert 'foo' property in 'META-INF/microprofile-config.properties'", d1, //
ca(propertiesFileUri, "Insert 'foo' property in 'META-INF/microprofile-config.properties'", MicroProfileCodeActionId.AssignValueToProperty, d1, //
te(0, 0, 0, 0, "foo=" + lineSeparator)));

MicroProfileJavaCodeActionParams codeActionParams2 = createCodeActionParams(javaFileUri, d2, false);
assertJavaCodeAction(codeActionParams2, utils, //
ca(javaFileUri, "Insert 'defaultValue' attribute", d2, //
ca(javaFileUri, "Insert 'defaultValue' attribute", MicroProfileCodeActionId.ConfigPropertyInsertDefaultValue, d2, //
te(0, 0, 18, 0, "package org.acme.config;\n\nimport org.eclipse.microprofile.config.inject.ConfigProperty;\n\nimport io.quarkus.arc.config.ConfigProperties;\n\npublic class UnassignedValue {\n\n @ConfigProperty(name = \"foo\")\n private String foo;\n\n @ConfigProperties(prefix = \"server\")\n private class Server {\n\n @ConfigProperty(name = \"url\", defaultValue = \"\")\n private String url;\n }\n}\n")),
ca(propertiesFileUri, "Insert 'server.url' property in 'META-INF/microprofile-config.properties'", d2, //
ca(propertiesFileUri, "Insert 'server.url' property in 'META-INF/microprofile-config.properties'", MicroProfileCodeActionId.AssignValueToProperty, d2, //
te(0, 0, 0, 0, "server.url=" + lineSeparator)));

// Same code actions but with exclude
Expand All @@ -193,18 +194,18 @@ public void testCodeActionForUnassigned() throws Exception {
codeActionParams1_1.setCommandConfigurationUpdateSupported(true);
assertJavaCodeAction(codeActionParams1_1, utils, //
MicroProfileCodeActionFactory.createAddToUnassignedExcludedCodeAction("foo", d1_1),
ca(javaFileUri, "Insert 'defaultValue' attribute", d1_1, //
ca(javaFileUri, "Insert 'defaultValue' attribute", MicroProfileCodeActionId.ConfigPropertyInsertDefaultValue, d1_1, //
te(0, 0, 18, 0, "package org.acme.config;\n\nimport org.eclipse.microprofile.config.inject.ConfigProperty;\n\nimport io.quarkus.arc.config.ConfigProperties;\n\npublic class UnassignedValue {\n\n @ConfigProperty(name = \"foo\", defaultValue = \"\")\n private String foo;\n\n @ConfigProperties(prefix = \"server\")\n private class Server {\n\n @ConfigProperty(name = \"url\")\n private String url;\n }\n}\n")),
ca(propertiesFileUri, "Insert 'foo' property in 'META-INF/microprofile-config.properties'", d1_1, //
ca(propertiesFileUri, "Insert 'foo' property in 'META-INF/microprofile-config.properties'", MicroProfileCodeActionId.AssignValueToProperty, d1_1, //
te(0, 0, 0, 0, "foo=" + lineSeparator)));

MicroProfileJavaCodeActionParams codeActionParams2_1 = createCodeActionParams(javaFileUri, d2_1);
codeActionParams2_1.setCommandConfigurationUpdateSupported(true);
assertJavaCodeAction(codeActionParams2_1, utils, //
MicroProfileCodeActionFactory.createAddToUnassignedExcludedCodeAction("server.url", d2_1),
ca(javaFileUri, "Insert 'defaultValue' attribute", d2_1, //
ca(javaFileUri, "Insert 'defaultValue' attribute", MicroProfileCodeActionId.ConfigPropertyInsertDefaultValue, d2_1, //
te(0, 0, 18, 0, "package org.acme.config;\n\nimport org.eclipse.microprofile.config.inject.ConfigProperty;\n\nimport io.quarkus.arc.config.ConfigProperties;\n\npublic class UnassignedValue {\n\n @ConfigProperty(name = \"foo\")\n private String foo;\n\n @ConfigProperties(prefix = \"server\")\n private class Server {\n\n @ConfigProperty(name = \"url\", defaultValue = \"\")\n private String url;\n }\n}\n")),
ca(propertiesFileUri, "Insert 'server.url' property in 'META-INF/microprofile-config.properties'", d2_1, //
ca(propertiesFileUri, "Insert 'server.url' property in 'META-INF/microprofile-config.properties'", MicroProfileCodeActionId.AssignValueToProperty, d2_1, //
te(0, 0, 0, 0, "server.url=" + lineSeparator)));

}
Expand Down
Loading