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 variable commands when no reference found #1573

Merged
merged 1 commit 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 @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since refactoring.checkInitialialConditions() will also invoke refactoring.getReferences() and cache the result to a field. The second call refactoring.getReferences() will just return the previous result, and no extra cost.

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);
}
}