use of org.eclipse.lsp4j.TextDocumentIdentifier in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_AnnotationQuickFixes.
// See https://github.com/redhat-developer/vscode-java/issues/1992
@Test
public void testResolveCodeAction_AnnotationQuickFixes() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
importProjects("maven/salut4");
IProject proj = WorkspaceHelper.getProject("salut4");
IJavaProject javaProject = JavaCore.create(proj);
assertTrue(javaProject.exists());
IType type = javaProject.findType("org.sample.MyTest");
ICompilationUnit unit = type.getCompilationUnit();
assertFalse(unit.getSource().contains("import org.junit.jupiter.api.Test"));
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
Position position = new Position(3, 4);
final Range range = new Range(position, position);
params.setRange(range);
CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UndefinedType), range)), Collections.singletonList(CodeActionKind.QuickFix));
params.setContext(context);
List<Either<Command, CodeAction>> quickfixActions = server.codeAction(params).join();
assertNotNull(quickfixActions);
assertFalse("No quickfix actions were found", quickfixActions.isEmpty());
Optional<Either<Command, CodeAction>> importTest = quickfixActions.stream().filter(codeAction -> {
return "Import 'Test' (org.junit.jupiter.api)".equals(codeAction.getRight().getTitle());
}).findFirst();
CodeAction codeAction = importTest.get().getRight();
assertEquals(1, codeAction.getDiagnostics().size());
CodeAction resolvedCodeAction = server.resolveCodeAction(codeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
assertTrue(actual.contains("import org.junit.jupiter.api.Test"));
}
use of org.eclipse.lsp4j.TextDocumentIdentifier in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testAssignAllParamsToFields.
@Test
public void testAssignAllParamsToFields() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class App {\n");
buf.append(" private String s;\n");
buf.append("\n");
buf.append(" public App(String s, String s3, String s2) {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("App.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
Range range = CodeActionUtil.getRange(unit, "s3");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(JavaCodeActionKind.QUICK_ASSIST));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No quickassist actions were found", codeActions.isEmpty());
Optional<Either<Command, CodeAction>> assignAllToNewFieldsResponse = codeActions.stream().filter(codeAction -> {
return "Assign all parameters to new fields".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quick assist 'Assign all parameters to new fields'", assignAllToNewFieldsResponse.isPresent());
CodeAction unresolvedCodeAction = assignAllToNewFieldsResponse.get().getRight();
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class App {\n");
buf.append(" private String s;\n");
buf.append(" private String s4;\n");
buf.append(" private String s3;\n");
buf.append(" private String s2;\n");
buf.append("\n");
buf.append(" public App(String s, String s3, String s2) {\n");
buf.append(" s4 = s;\n");
buf.append(" this.s3 = s3;\n");
buf.append(" this.s2 = s2;\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.eclipse.lsp4j.TextDocumentIdentifier in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_SourceActions.
@Test
public void testResolveCodeAction_SourceActions() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
StringBuilder buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" private void hello() {\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit unit = defaultPackage.createCompilationUnit("E.java", buf.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "hello");
params.setRange(range);
CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Source));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
Assert.assertNotNull(codeActions);
Assert.assertFalse("No source actions were found", codeActions.isEmpty());
for (Either<Command, CodeAction> codeAction : codeActions) {
Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
}
Optional<Either<Command, CodeAction>> generateConstructorResponse = codeActions.stream().filter(codeAction -> {
return "Generate Constructors".equals(codeAction.getRight().getTitle());
}).findFirst();
Assert.assertTrue("Should return the quick assist 'Convert to lambda expression'", generateConstructorResponse.isPresent());
CodeAction unresolvedCodeAction = generateConstructorResponse.get().getRight();
Assert.assertNotNull("Should preserve the data property for the unresolved code action", unresolvedCodeAction.getData());
CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
buf = new StringBuilder();
buf.append("public class E {\n");
buf.append(" private void hello() {\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" /**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append(" public E() {\n");
buf.append(" }\n");
buf.append("}\n");
Assert.assertEquals(buf.toString(), actual);
}
use of org.eclipse.lsp4j.TextDocumentIdentifier 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.lsp4j.TextDocumentIdentifier 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);
}
}
Aggregations