Search in sources :

Example 61 with Position

use of org.eclipse.lsp4j.Position 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"));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IProject(org.eclipse.core.resources.IProject) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) LanguageServerWorkingCopyOwner(org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IType(org.eclipse.jdt.core.IType) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Optional(java.util.Optional) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) IProject(org.eclipse.core.resources.IProject) IType(org.eclipse.jdt.core.IType) IJavaProject(org.eclipse.jdt.core.IJavaProject) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 62 with Position

use of org.eclipse.lsp4j.Position 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);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Example 63 with Position

use of org.eclipse.lsp4j.Position 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);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) CodeAction(org.eclipse.lsp4j.CodeAction) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Example 64 with Position

use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.

the class RenameHandlerTest method testRenameField.

@Test
public void testRenameField() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "public class E {\n", "	private int myValue = 2;\n", "   public void bar() {\n", "		myValue|* = 3;\n", "   }\n", "}\n" };
    StringBuilder builder = new StringBuilder();
    Position pos = mergeCode(builder, codes);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
    WorkspaceEdit edit = getRenameEdit(cu, pos, "newname");
    assertNotNull(edit);
    assertEquals(edit.getChanges().size(), 1);
    assertEquals(TextEditUtil.apply(builder.toString(), edit.getChanges().get(JDTUtils.toURI(cu))), "package test1;\n" + "public class E {\n" + "	private int newname = 2;\n" + "   public void bar() {\n" + "		newname = 3;\n" + "   }\n" + "}\n");
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 65 with Position

use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.

the class RenameHandlerTest method testRenameSystemLibrary.

@Test(expected = ResponseErrorException.class)
public void testRenameSystemLibrary() throws JavaModelException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    String[] codes = { "package test1;\n", "public class E {\n", "   public int bar() {\n", "		String str = new String();\n", "   	str.len|*gth();\n", "   }\n", "}\n" };
    StringBuilder builder = new StringBuilder();
    Position pos = mergeCode(builder, codes);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
    getRenameEdit(cu, pos, "newname");
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

Position (org.eclipse.lsp4j.Position)240 Test (org.junit.Test)149 Range (org.eclipse.lsp4j.Range)103 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)95 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)73 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)70 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)50 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)36 Location (org.eclipse.lsp4j.Location)34 List (java.util.List)29 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)29 URI (java.net.URI)27 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)23 ArrayList (java.util.ArrayList)21 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)18 TextEdit (org.eclipse.lsp4j.TextEdit)15 CompletionList (org.eclipse.lsp4j.CompletionList)14 Document (org.eclipse.xtext.ide.server.Document)14 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)13 Command (org.eclipse.lsp4j.Command)12