Skip to content

Commit

Permalink
Hide inline variable commands when no reference
Browse files Browse the repository at this point in the history
  • Loading branch information
CsCherrYY committed Oct 16, 2020
1 parent e84cba2 commit 8cbd159
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private boolean getInlineProposal(IInvocationContext context, ASTNode node, Coll
// Inline Local Variable
if (binding.getJavaElement() instanceof ILocalVariable && RefactoringAvailabilityTesterCore.isInlineTempAvailable((ILocalVariable) binding.getJavaElement())) {
InlineTempRefactoring refactoring= new InlineTempRefactoring((VariableDeclaration) decl);
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK() && refactoring.getReferences().length > 0) {
String label = CorrectionMessages.QuickAssistProcessor_inline_local_description;
int relevance = IProposalRelevance.INLINE_LOCAL;
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.RefactorInline, context.getCompilationUnit(), refactoring, relevance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,22 @@ public void testInlineLocalVariable() throws Exception {
Expected expected = new Expected(INLINE_LOCAL_VARIABLE, buf.toString(), CodeActionKind.RefactorInline);
assertCodeActions(cu, expected);
}

@Test
public void testInlineLocalVariableWithNoReferences() 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(" public void foo(String[] parameters, int j) {\n");
buf.append(" int temp = parameters.length + j;\n");
buf.append(" int /*]*/temp1/*[*/ = temp;\n");
buf.append(" System.out.println(temp);\n");
buf.append(" }\n");
buf.append("}\n");

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

0 comments on commit 8cbd159

Please sign in to comment.