Skip to content

Commit

Permalink
Sort the quick fix menu for imports in a order that makes sense
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
  • Loading branch information
snjeza committed Feb 14, 2018
1 parent 75db668 commit 4ac48f2
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 588 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -68,6 +69,7 @@ public List<Command> getCodeActionCommands(CodeActionParams params, IProgressMon
List<Command> $ = new ArrayList<>();
try {
CUCorrectionProposal[] corrections = this.quickFixProcessor.getCorrections(context, locations);
Arrays.sort(corrections, new CUCorrectionProposalComparator());
for (CUCorrectionProposal proposal : corrections) {
Command command = this.getCommandFromProposal(proposal);
$.add(command);
Expand All @@ -78,6 +80,7 @@ public List<Command> getCodeActionCommands(CodeActionParams params, IProgressMon

try {
CUCorrectionProposal[] corrections = this.quickAssistProcessor.getAssists(context, locations);
Arrays.sort(corrections, new CUCorrectionProposalComparator());
for (CUCorrectionProposal proposal : corrections) {
Command command = this.getCommandFromProposal(proposal);
$.add(command);
Expand Down Expand Up @@ -133,4 +136,19 @@ private static CompilationUnit getASTRoot(ICompilationUnit unit) {
return SharedASTProvider.getInstance().getAST(unit, new NullProgressMonitor());
}

private static class CUCorrectionProposalComparator implements Comparator<CUCorrectionProposal> {

@Override
public int compare(CUCorrectionProposal p1, CUCorrectionProposal p2) {
int r1 = p1.getRelevance();
int r2 = p2.getRelevance();
int relevanceDif = r2 - r1;
if (relevanceDif != 0) {
return relevanceDif;
}
return p1.getName().compareToIgnoreCase(p2.getName());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void testMissingParam1() throws Exception {
buf.append(" public void foo(int a, int b, int c) {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@param' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@param' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand Down Expand Up @@ -114,8 +114,8 @@ public void testMissingParam2() throws Exception {
buf.append(" public void foo(int a, int b, int c) {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@param' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@param' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand Down Expand Up @@ -151,8 +151,8 @@ public void testMissingParam3() throws Exception {
buf.append(" public void foo(int a, int b, int c) {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@param' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@param' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand Down Expand Up @@ -188,8 +188,8 @@ public void testMissingParam4() throws Exception {
buf.append(" public <A, B> void foo(int a) {\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@param' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@param' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand All @@ -213,8 +213,8 @@ public void testMissingParam5() throws Exception {
buf.append(" */\n");
buf.append("public class E<A, B> {\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@param' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@param' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand All @@ -238,8 +238,8 @@ public void testMissingParam6() throws Exception {
buf.append(" */\n");
buf.append("public class E<A> {\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@param' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@param' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand Down Expand Up @@ -277,8 +277,8 @@ public void testMissingReturn1() throws Exception {
buf.append(" return 1;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@return' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@return' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand Down Expand Up @@ -310,8 +310,8 @@ public void testMissingReturn2() throws Exception {
buf.append(" return 1;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@return' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@return' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand Down Expand Up @@ -379,8 +379,8 @@ public void testMissingThrows() throws Exception {
buf.append(" return 1;\n");
buf.append(" }\n");
buf.append("}\n");
Expected e1 = new Expected("Add '@throws' tag", buf.toString());
Expected e2 = new Expected("Add all missing tags", buf.toString());
Expected e1 = new Expected("Add all missing tags", buf.toString());
Expected e2 = new Expected("Add '@throws' tag", buf.toString());
assertCodeActions(cu, e1, e2);
}

Expand Down
Loading

0 comments on commit 4ac48f2

Please sign in to comment.