|
22 | 22 | import java.util.Set; |
23 | 23 | import java.util.stream.Stream; |
24 | 24 |
|
| 25 | +import com.google.common.collect.Sets; |
| 26 | + |
25 | 27 | import org.eclipse.core.runtime.CoreException; |
26 | 28 | import org.eclipse.core.runtime.IProgressMonitor; |
27 | 29 | import org.eclipse.core.runtime.NullProgressMonitor; |
|
34 | 36 | import org.eclipse.jdt.core.dom.ASTNode; |
35 | 37 | import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; |
36 | 38 | import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; |
| 39 | +import org.eclipse.jdt.core.dom.BodyDeclaration; |
37 | 40 | import org.eclipse.jdt.core.dom.CompilationUnit; |
38 | 41 | import org.eclipse.jdt.core.dom.ITypeBinding; |
39 | | -import org.eclipse.jdt.core.dom.SimpleName; |
| 42 | +import org.eclipse.jdt.core.dom.Statement; |
| 43 | +import org.eclipse.jdt.core.dom.Type; |
40 | 44 | import org.eclipse.jdt.core.dom.TypeDeclaration; |
41 | 45 | import org.eclipse.jdt.core.manipulation.CoreASTProvider; |
42 | 46 | import org.eclipse.jdt.core.manipulation.OrganizeImportsOperation; |
|
77 | 81 | import org.eclipse.lsp4j.jsonrpc.messages.Either; |
78 | 82 | import org.eclipse.text.edits.TextEdit; |
79 | 83 |
|
80 | | -import com.google.common.collect.Sets; |
81 | | - |
82 | 84 | public class SourceAssistProcessor { |
83 | 85 |
|
84 | 86 | private static final Set<String> UNSUPPORTED_RESOURCES = Sets.newHashSet("module-info.java", "package-info.java"); |
@@ -146,9 +148,18 @@ public List<Either<Command, CodeAction>> getSourceActionCommands(CodeActionParam |
146 | 148 | if (node == null) { |
147 | 149 | node = context.getCoveringNode(); |
148 | 150 | } |
149 | | - if (node instanceof SimpleName) { |
150 | | - ASTNode parent = node.getParent(); |
151 | | - if (parent instanceof TypeDeclaration) { |
| 151 | + ASTNode declarationNode = getDeclarationNode(node); |
| 152 | + if (declarationNode instanceof TypeDeclaration) { |
| 153 | + TypeDeclaration typeDeclaration = (TypeDeclaration)declarationNode; |
| 154 | + List<ASTNode> candidates = new ArrayList<>(); |
| 155 | + Type superType = typeDeclaration.getSuperclassType(); |
| 156 | + if (superType != null) { |
| 157 | + candidates.add(superType); |
| 158 | + } |
| 159 | + candidates.add(typeDeclaration.getName()); |
| 160 | + candidates.addAll(typeDeclaration.modifiers()); |
| 161 | + candidates.addAll(typeDeclaration.superInterfaceTypes()); |
| 162 | + if (isCovered(node, candidates)) { |
152 | 163 | quickAssistHashCodeEquals = getHashCodeEqualsAction(params, JavaCodeActionKind.QUICK_ASSIST); |
153 | 164 | addSourceActionCommand($, params.getContext(), quickAssistHashCodeEquals); |
154 | 165 | } |
@@ -574,4 +585,27 @@ public static ICompilationUnit getCompilationUnit(CodeActionParams params) { |
574 | 585 | return JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri()); |
575 | 586 | } |
576 | 587 |
|
| 588 | + private static ASTNode getDeclarationNode(ASTNode node) { |
| 589 | + if (node == null) { |
| 590 | + return null; |
| 591 | + } |
| 592 | + if (node instanceof BodyDeclaration) { |
| 593 | + return null; |
| 594 | + } |
| 595 | + while (node != null && !(node instanceof BodyDeclaration) && !(node instanceof Statement)) { |
| 596 | + node = node.getParent(); |
| 597 | + } |
| 598 | + return node; |
| 599 | + } |
| 600 | + |
| 601 | + private static boolean isCovered(ASTNode node, List<ASTNode> candidates) { |
| 602 | + int startPosition = node.getStartPosition(); |
| 603 | + int endPosition = startPosition + node.getLength(); |
| 604 | + for (ASTNode candidate : candidates) { |
| 605 | + if (startPosition >= candidate.getStartPosition() && endPosition <= candidate.getStartPosition() + candidate.getLength()) { |
| 606 | + return true; |
| 607 | + } |
| 608 | + } |
| 609 | + return false; |
| 610 | + } |
577 | 611 | } |
0 commit comments