Skip to content

Commit

Permalink
Avoid duplicate quick fixes when shown at line level (#2023)
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored Mar 16, 2022
1 parent 6f080e8 commit 8650004
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -159,8 +161,9 @@ public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams
try {
codeActions.addAll(nonProjectFixProcessor.getCorrections(params, context, locations));
List<ChangeCorrectionProposal> quickfixProposals = this.quickFixProcessor.getCorrections(context, locations);
quickfixProposals.sort(comparator);
proposals.addAll(quickfixProposals);
Set<ChangeCorrectionProposal> quickSet = new TreeSet<>(comparator);
quickSet.addAll(quickfixProposals);
proposals.addAll(quickSet);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem resolving quick fix code actions", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,48 @@ public void testDuplicateQuickFix() throws Exception {
}
}

// https:/redhat-developer/vscode-java/issues/2339
@Test
public void testDuplicateQuickFix2() throws Exception {
String showQuickFixes = preferenceManager.getPreferences().getJavaQuickFixShowAt();
try {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.util.List;\n");
buf.append("public class Foo {\n");
buf.append(" public void foo(List<Integer> list) {\n");
buf.append(" for (element: list) {");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Foo.java", buf.toString(), true, null);
cu.becomeWorkingCopy(null);
preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
CodeActionParams codeActionParams = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
codeActionParams.setTextDocument(textDocument);
codeActionParams.setRange(new Range(new Position(4, 13), new Position(4, 20)));
CodeActionContext context = new CodeActionContext();
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 5);
context.setDiagnostics(diagnostics);
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
codeActionParams.setContext(context);
List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(10, codeActions.size());
CodeAction action = codeActions.get(0).getRight();
assertNotNull(action);
assertEquals("Create loop variable 'element'", action.getTitle());
action = codeActions.get(1).getRight();
assertNotNull(action);
assertNotEquals("Create loop variable 'element'", action.getTitle());
} finally {
preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
}
}

private List<Diagnostic> getDiagnostics(ICompilationUnit cu, CompilationUnit astRoot, int line) {
List<IProblem> problems = new ArrayList<>();
for (IProblem problem : astRoot.getProblems()) {
Expand Down

0 comments on commit 8650004

Please sign in to comment.