use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.
the class ShowAllQuickFixTest method testShowAt.
@Test
public void testShowAt() 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.math.BigDecimal;\n");
buf.append("public class F {\n");
buf.append(" private int i; private BigDecimal b;private void test() {}\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" System.out.println(greeting());\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("F.java", buf.toString(), true, null);
cu.becomeWorkingCopy(null);
CodeActionParams codeActionParams = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
codeActionParams.setTextDocument(textDocument);
codeActionParams.setRange(new Range(new Position(3, 9), new Position(3, 9)));
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Collections.emptyList());
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
codeActionParams.setContext(context);
List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(0, codeActions.size());
codeActionParams.setRange(new Range(new Position(3, 4), new Position(3, 40)));
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(0, codeActions.size());
codeActionParams.setRange(new Range(new Position(5, 1), new Position(5, 1)));
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(0, codeActions.size());
preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
codeActionParams.setRange(new Range(new Position(3, 9), new Position(3, 9)));
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
cu.makeConsistent(null);
List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 4);
context.setDiagnostics(diagnostics);
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(3, codeActions.size());
codeActionParams.setRange(new Range(new Position(3, 4), new Position(3, 40)));
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(3, codeActions.size());
codeActionParams.setRange(new Range(new Position(5, 1), new Position(5, 1)));
diagnostics = getDiagnostics(cu, astRoot, 6);
context.setDiagnostics(diagnostics);
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(1, codeActions.size());
CodeAction greeting = codeActions.get(0).getRight();
assertNotNull(greeting);
assertEquals("Create method 'greeting()'", greeting.getTitle());
} finally {
preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
}
}
use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.
the class ShowAllQuickFixTest method testDuplicateQuickFix.
// https://github.com/redhat-developer/vscode-java/issues/2236
@Test
public void testDuplicateQuickFix() 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("public class F {\n");
buf.append(" List list = List.of();\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("F.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(2, 6), new Position(2, 6)));
CodeActionContext context = new CodeActionContext();
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 3);
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(5, codeActions.size());
CodeAction action = codeActions.get(0).getRight();
assertNotNull(action);
assertEquals("Import 'List' (java.util)", action.getTitle());
action = codeActions.get(1).getRight();
assertNotNull(action);
assertNotEquals("Import 'List' (java.util)", action.getTitle());
} finally {
preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
}
}
use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.
the class AbstractQuickFixTest method evaluateCodeActions.
protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit cu, Range range) throws JavaModelException {
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
CodeActionParams parms = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
parms.setTextDocument(textDocument);
parms.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true));
context.setOnly(onlyKinds);
parms.setContext(context);
List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
if (onlyKinds != null && !onlyKinds.isEmpty()) {
for (Either<Command, CodeAction> codeAction : codeActions) {
Stream<String> acceptedActionKinds = onlyKinds.stream();
String kind = codeAction.getRight().getKind();
assertTrue(codeAction.getRight().getTitle() + " has kind " + kind + " but only " + onlyKinds + " are accepted", acceptedActionKinds.filter(k -> kind != null && kind.startsWith(k)).findFirst().isPresent());
}
}
if (this.ignoredKinds != null) {
List<Either<Command, CodeAction>> filteredList = codeActions.stream().filter(Either::isRight).filter(codeAction -> {
for (String str : this.ignoredKinds) {
if (codeAction.getRight().getKind().matches(str)) {
return true;
}
}
return false;
}).collect(Collectors.toList());
codeActions.removeAll(filteredList);
}
if (this.ignoredCommands != null) {
List<Either<Command, CodeAction>> filteredList = new ArrayList<>();
for (Either<Command, CodeAction> codeAction : codeActions) {
for (String str : this.ignoredCommands) {
if (getTitle(codeAction).matches(str)) {
filteredList.add(codeAction);
break;
}
}
}
codeActions.removeAll(filteredList);
}
return codeActions;
}
use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.
the class NonProjectFixTest method getCodeActions.
private List<Either<Command, CodeAction>> getCodeActions(ICompilationUnit cu, IProblem problem) throws JavaModelException {
CodeActionParams parms = new CodeActionParams();
Range range = JDTUtils.toRange(cu, problem.getSourceStart(), 0);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
parms.setTextDocument(textDocument);
parms.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problem), true));
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
parms.setContext(context);
return new CodeActionHandler(this.preferenceManager).getCodeActionCommands(parms, new NullProgressMonitor());
}
use of org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler in project eclipse.jdt.ls by eclipse.
the class ShowAllQuickFixTest method testHandledProblems.
// preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
@Test
public void testHandledProblems() throws Exception {
String showQuickFixes = preferenceManager.getPreferences().getJavaQuickFixShowAt();
try {
preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
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 F implements List {\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("F.java", buf.toString(), true, null);
CodeActionParams codeActionParams = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
codeActionParams.setTextDocument(textDocument);
codeActionParams.setRange(new Range(new Position(2, 13), new Position(2, 16)));
CodeActionContext context = new CodeActionContext();
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 3);
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(1, codeActions.size());
} finally {
preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
}
}
Aggregations