Skip to content

Commit

Permalink
Fix Bug 148012 - add quick fix to assign statement to local variable (#…
Browse files Browse the repository at this point in the history
…1004)

* Fix Bug 148012 - add quick fix to assign statement to local variable

- see https://bugs.eclipse.org/bugs/show_bug.cgi?id=148012
- use similar logic from QuickAssistProcessor in QuickFixProcessor
  when problem is IProblem.ParsingErrorInsertToComplete
- add new test to QuickFixTest1d8
- don't add light-bulb for ParsingErrorInsertToComplete
- fix QuickFixTest to have a version of collect completions that
  doesn't verify the light bulb marker
  • Loading branch information
jjohnstn committed Jan 16, 2024
1 parent 417ba9a commit 24d4eae
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -70,12 +70,12 @@

import org.eclipse.jdt.ui.text.java.IInvocationContext;
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore;
import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal;
import org.eclipse.jdt.ui.text.java.correction.ICommandAccess;

import org.eclipse.jdt.internal.ui.text.correction.AssistContext;
import org.eclipse.jdt.internal.ui.text.correction.GetterSetterCorrectionSubProcessor.SelfEncapsulateFieldProposal;
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore;
import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation;
import org.eclipse.jdt.internal.ui.text.correction.ReorgCorrectionsSubProcessor;
Expand Down Expand Up @@ -298,6 +298,19 @@ protected static final ArrayList<IJavaCompletionProposal> collectCorrections(ICo
return proposals;
}

protected static final ArrayList<IJavaCompletionProposal> collectCorrectionsNoCheck(ICompilationUnit cu, IProblem curr, IInvocationContext context) throws CoreException {
int offset= curr.getSourceStart();
int length= curr.getSourceEnd() + 1 - offset;
if (context == null) {
context= new AssistContext(cu, offset, length);
}

ProblemLocation problem= new ProblemLocation(curr);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(context, problem);

return proposals;
}

protected static ArrayList<IJavaCompletionProposal> collectCorrections(IInvocationContext context, IProblemLocationCore problem) throws CoreException {
ArrayList<IJavaCompletionProposal> proposals= new ArrayList<>();
IStatus status= JavaCorrectionProcessor.collectCorrections(context, new IProblemLocationCore[] { problem }, proposals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2871,4 +2871,65 @@ public void testIssue717_2() throws Exception {
assertExpectedExistInProposals(proposals, new String[] {expected1});
}

// Bug 148012 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=148012
@Test
public void testBug148012() throws Exception {
Hashtable<String, String> options = JavaCore.getOptions();
JavaCore.setOptions(options);
JavaProjectHelper.addLibrary(fJProject1, new Path(Java1d8ProjectTestSetup.getJdtAnnotations20Path()));
IPackageFragment pack2= fSourceFolder.createPackageFragment("test1", false, null);


StringBuilder buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" foo()[0];\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" static Object[] foo() {\n");
buf.append(" return null;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack2.createCompilationUnit("E.java", buf.toString(), false, null);

CompilationUnit astRoot= getASTRoot(cu);
IProblem[] problems= astRoot.getProblems();
assertNumberOfProblems(1, problems);
List<IJavaCompletionProposal> proposals= collectCorrectionsNoCheck(cu, problems[0], null);
assertCorrectLabels(proposals);

buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" Object object = foo()[0];\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" static Object[] foo() {\n");
buf.append(" return null;\n");
buf.append(" }\n");
buf.append("}\n");

String expected1 = buf.toString();

buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" private static Object object;\n");
buf.append("\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" object = foo()[0];\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" static Object[] foo() {\n");
buf.append(" return null;\n");
buf.append(" }\n");
buf.append("}\n");

String expected2 = buf.toString();

assertExpectedExistInProposals(proposals, new String[] {expected1, expected2});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,9 @@ private static boolean getSplitVariableProposals(IInvocationContext context, AST
}

public static boolean getAssignToVariableProposals(IInvocationContext context, ASTNode node, IProblemLocationCore[] locations, Collection<ICommandAccess> resultingCollections) {
// don't add if already added as quick fix
if (containsMatchingProblem(locations, IProblem.ParsingErrorInsertToComplete))
return false;
Statement statement= ASTResolving.findParentStatement(node);
if (!(statement instanceof ExpressionStatement)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public boolean hasCorrections(ICompilationUnit cu, int problemId) {
case IProblem.IllegalTotalPatternWithDefault:
case IProblem.IllegalFallthroughToPattern:
return true;

default:
return SuppressWarningsSubProcessorCore.hasSuppressWarningsProposal(cu.getJavaProject(), problemId)
|| ConfigureProblemSeveritySubProcessor.hasConfigureProblemSeverityProposal(problemId);
Expand Down Expand Up @@ -968,6 +969,11 @@ private void process(IInvocationContext context, IProblemLocationCore problem, C
case IProblem.PotentiallyUnclosedCloseable:
LocalCorrectionsSubProcessor.getTryWithResourceProposals(context, problem, proposals);
break;
case IProblem.ParsingErrorInsertToComplete:
CompilationUnit astRoot1= context.getASTRoot();
ASTNode selectedNode1= problem.getCoveringNode(astRoot1);
QuickAssistProcessor.getAssignToVariableProposals(context, selectedNode1, new IProblemLocationCore[] {}, proposals);
break;
default:
}
if (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
Expand Down

0 comments on commit 24d4eae

Please sign in to comment.