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

Add 'hashCode()' and 'equals()' to Show Fixes for type declaration #1842

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Set;
import java.util.stream.Stream;

import com.google.common.collect.Sets;
testforstephen marked this conversation as resolved.
Show resolved Hide resolved

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
Expand All @@ -34,8 +36,12 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.Type;
testforstephen marked this conversation as resolved.
Show resolved Hide resolved
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.core.manipulation.OrganizeImportsOperation;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
Expand Down Expand Up @@ -75,8 +81,6 @@
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.text.edits.TextEdit;

import com.google.common.collect.Sets;

public class SourceAssistProcessor {

private static final Set<String> UNSUPPORTED_RESOURCES = Sets.newHashSet("module-info.java", "package-info.java");
Expand Down Expand Up @@ -138,8 +142,22 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam

// Generate hashCode() and equals()
if (supportsHashCodeEquals(context, type, monitor)) {
Optional<Either<Command, CodeAction>> hashCodeEquals = getHashCodeEqualsAction(params);
addSourceActionCommand($, params.getContext(), hashCodeEquals);
// Generate QuickAssist
Optional<Either<Command, CodeAction>> quickAssistHashCodeEquals = Optional.empty();
ASTNode node = context.getCoveredNode();
if (node == null) {
node = context.getCoveringNode();
}
ASTNode declarationNode = getDeclarationNode(node);
if (declarationNode instanceof TypeDeclaration) {
quickAssistHashCodeEquals = getHashCodeEqualsAction(params, JavaCodeActionKind.QUICK_ASSIST);
addSourceActionCommand($, params.getContext(), quickAssistHashCodeEquals);
}

// Generate Source Action
Optional<Either<Command, CodeAction>> sourceActionHashCodeEquals = getHashCodeEqualsAction(params, JavaCodeActionKind.SOURCE_GENERATE_HASHCODE_EQUALS);
addSourceActionCommand($, params.getContext(), sourceActionHashCodeEquals);

}

// Generate toString()
Expand Down Expand Up @@ -304,16 +322,16 @@ private boolean supportsHashCodeEquals(IInvocationContext context, IType type, I
}
}

private Optional<Either<Command, CodeAction>> getHashCodeEqualsAction(CodeActionParams params) {
private Optional<Either<Command, CodeAction>> getHashCodeEqualsAction(CodeActionParams params, String kind) {
if (!preferenceManager.getClientPreferences().isHashCodeEqualsPromptSupported()) {
return Optional.empty();
}
Command command = new Command(ActionMessages.GenerateHashCodeEqualsAction_label, COMMAND_ID_ACTION_HASHCODEEQUALSPROMPT, Collections.singletonList(params));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_HASHCODE_EQUALS)) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateHashCodeEqualsAction_label);
codeAction.setKind(JavaCodeActionKind.SOURCE_GENERATE_HASHCODE_EQUALS);
codeAction.setKind(kind);
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.EMPTY_LIST);
codeAction.setDiagnostics(Collections.emptyList());
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
Expand Down Expand Up @@ -428,7 +446,7 @@ private Optional<Either<Command, CodeAction>> addFinalModifierWherePossibleActio
JavaLanguageServerPlugin.logException("Problem converting proposal to code actions", e);
return Optional.empty();
}

if (!ChangeUtil.hasChanges(edit)) {
return Optional.empty();
}
Expand Down Expand Up @@ -459,7 +477,7 @@ private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(CodeActi
if (!ChangeUtil.hasChanges(edit)) {
return Optional.empty();
}

Command command = new Command(name, CodeActionHandler.COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) {
CodeAction codeAction = new CodeAction(name);
Expand Down Expand Up @@ -555,4 +573,17 @@ public static InnovationContext getInnovationContext(CodeActionParams params, IP
public static ICompilationUnit getCompilationUnit(CodeActionParams params) {
return JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
}

private static ASTNode getDeclarationNode(ASTNode node) {
if (node == null) {
return null;
}
if (node instanceof BodyDeclaration) {
return null;
}
while (node != null && !(node instanceof BodyDeclaration) && !(node instanceof Statement)) {
node = node.getParent();
}
return node;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,21 @@ public static Either<Command, CodeAction> findAction(List<Either<Command, CodeAc
Optional<Either<Command, CodeAction>> any = codeActions.stream().filter((action) -> Objects.equals(kind, action.getLeft() == null ? action.getRight().getKind() : action.getLeft().getCommand())).findFirst();
return any.isPresent() ? any.get() : null;
}

public static List<Either<Command, CodeAction>> findActions(List<Either<Command, CodeAction>> codeActions, String kind) {
return codeActions.stream().filter((action) -> Objects.equals(kind, action.getLeft() == null ? action.getRight().getKind() : action.getLeft().getCommand())).collect(Collectors.toList());
}

public static boolean commandExists(List<Either<Command, CodeAction>> codeActions, String command) {
if (codeActions.isEmpty()) {
return false;
}
for (Either<Command, CodeAction> codeAction : codeActions) {
Command actionCommand = getCommand(codeAction);
if (actionCommand != null && actionCommand.getCommand().equals(command)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.jdt.ls.core.internal.JavaClientConnection;
import org.eclipse.jdt.ls.core.internal.JavaCodeActionKind;
import org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner;
import org.eclipse.jdt.ls.core.internal.text.correction.SourceAssistProcessor;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.Command;
Expand Down Expand Up @@ -120,4 +121,29 @@ public void testHashCodeEqualsDisabled_enum() throws JavaModelException {
Assert.assertNotNull(codeActions);
Assert.assertFalse("The operation is not applicable to enums", CodeActionHandlerTest.containsKind(codeActions, JavaCodeActionKind.SOURCE_GENERATE_HASHCODE_EQUALS));
}

@Test
public void testHashCodeEqualsQuickAssist() throws JavaModelException {
//@formatter:off
ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" +
"\r\n" +
"public class A {\r\n" +
" public final String name = \"test\";\r\n" +
"}"
, true, null);
//@formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "A");
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
List<Either<Command, CodeAction>> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST);
Assert.assertFalse(quickAssistActions.isEmpty());
Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, SourceAssistProcessor.COMMAND_ID_ACTION_HASHCODEEQUALSPROMPT));
// Test if the quick assist exists only for type declaration
params = CodeActionUtil.constructCodeActionParams(unit, "String name");
codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST);
Assert.assertFalse(quickAssistActions.isEmpty());
Assert.assertFalse(CodeActionHandlerTest.commandExists(quickAssistActions, SourceAssistProcessor.COMMAND_ID_ACTION_HASHCODEEQUALSPROMPT));
}
}