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

Hide inline constant commands when no reference found #1575

Merged
merged 2 commits into from
Oct 19, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.eclipse.jdt.core.manipulation.CleanUpOptionsCore;
import org.eclipse.jdt.core.manipulation.CleanUpRequirementsCore;
import org.eclipse.jdt.core.manipulation.ICleanUpFixCore;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
Expand All @@ -83,6 +84,7 @@
import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore;
import org.eclipse.jdt.internal.corext.fix.VariableDeclarationFixCore;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore;
import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange;
import org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring;
import org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring;
import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
Expand All @@ -107,6 +109,7 @@
import org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CheckConditionsOperation;
import org.eclipse.ltk.core.refactoring.CreateChangeOperation;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
Expand Down Expand Up @@ -327,6 +330,15 @@ private boolean getInlineProposal(IInvocationContext context, ASTNode node, Coll
CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
final CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
create.run(new NullProgressMonitor());
int referenceCount = 0;
Change change = create.getChange();
Change[] refactoringChanges = ((DynamicValidationRefactoringChange)change).getChildren();
for (Change refactoringChange : refactoringChanges) {
referenceCount += ((CompilationUnitChange)refactoringChange).getChangeGroups().length;
}
if (referenceCount <= 1 && refactoring.isDeclarationSelected()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CsCherrYY Below is a corner case. The change group will be 2 (1 for remove imports, 1 for remove the declaration) even though no references are using the constant field. The "Inline" menu won't be hidden.

import java.util.HashMap;
import java.util.Map;

public class Hello {
    public static final Map map = new HashMap<>();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@testforstephen Thanks a lot for kind reminding, I would like to implement this clearer and avoid corner case like this by changing some APIs in jdt later.

return true;
}
String label = ActionMessages.InlineConstantRefactoringAction_label;
int relevance = IProposalRelevance.INLINE_LOCAL;
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(label, CodeActionKind.RefactorInline, create.getChange(), relevance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,18 @@ public void testInlineConstant_InvocationSelected() throws Exception {
Expected expected = new Expected(INLINE_CONSTANT, buf.toString(), CodeActionKind.RefactorInline);
assertCodeActions(cu, expected);
}

@Test
public void testInlineConstant_NoReference() throws Exception {
IPackageFragment pack1 = testSourceFolder.createPackageFragment("test", false, null);

StringBuilder buf = new StringBuilder();
buf.append("package test;\n");
buf.append("public class E {\n");
buf.append(" private static final String /*]*/LOGGER_NAME/*[*/ = \"TEST.E\";\n");
buf.append("}\n");

ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
assertCodeActionNotExists(cu, INLINE_CONSTANT);
}
}