Search in sources :

Example 46 with Command

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

the class CodeActionHandlerTest method testCodeAction_allKindsOfActions.

@Test
public void testCodeAction_allKindsOfActions() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "	void foo() {\n" + "		String bar = \"astring\";" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit, "bar");
    params.setRange(range);
    CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)));
    params.setContext(context);
    List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
    Assert.assertNotNull(codeActions);
    Assert.assertFalse("No code actions were found", codeActions.isEmpty());
    boolean hasQuickFix = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.QuickFix));
    assertTrue("No quickfix actions were found", hasQuickFix);
    boolean hasRefactor = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Refactor));
    assertTrue("No refactor actions were found", hasRefactor);
    boolean hasSource = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Source));
    assertTrue("No source actions were found", hasSource);
    List<String> baseKinds = codeActions.stream().map(codeAction -> getBaseKind(codeAction.getRight().getKind())).collect(Collectors.toList());
    assertTrue("quickfix actions should be ahead of refactor actions", baseKinds.lastIndexOf(CodeActionKind.QuickFix) < baseKinds.indexOf(CodeActionKind.Refactor));
    assertTrue("refactor actions should be ahead of source actions", baseKinds.lastIndexOf(CodeActionKind.Refactor) < baseKinds.indexOf(CodeActionKind.Source));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AbstractSourceTestCase(org.eclipse.jdt.ls.core.internal.codemanipulation.AbstractSourceTestCase) Arrays(java.util.Arrays) ProjectUtils(org.eclipse.jdt.ls.core.internal.ProjectUtils) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) DefaultCodeFormatterConstants(org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) URI(java.net.URI) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Optional(java.util.Optional) IProblem(org.eclipse.jdt.core.compiler.IProblem) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) JavaModelException(org.eclipse.jdt.core.JavaModelException) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) CorrectionMessages(org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) 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) 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) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) Mockito(org.mockito.Mockito) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Ignore(org.junit.Ignore) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Assert(org.junit.Assert) Collections(java.util.Collections) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 47 with Command

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

the class CodeActionHandlerTest method testCodeAction_sourceActionsOnly.

@Test
public void testCodeAction_sourceActionsOnly() throws Exception {
    // @formatter:off
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n" + "	void foo() {\n" + "	}\n" + "}\n");
    // @formatter:on
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit, "foo()");
    params.setRange(range);
    params.setContext(new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Source)));
    List<Either<Command, CodeAction>> sourceActions = getCodeActions(params);
    Assert.assertNotNull(sourceActions);
    Assert.assertFalse("No source actions were found", sourceActions.isEmpty());
    for (Either<Command, CodeAction> codeAction : sourceActions) {
        Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.Source));
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 48 with Command

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

the class CodeActionHandlerTest method testCodeAction_customFileFormattingOptions.

@Test
public void testCodeAction_customFileFormattingOptions() throws Exception {
    when(clientPreferences.isWorkspaceConfigurationSupported()).thenReturn(true);
    when(connection.configuration(Mockito.any())).thenReturn(Arrays.asList(4, true));
    server.setClientConnection(connection);
    JavaLanguageServerPlugin.getInstance().setProtocol(server);
    IJavaProject javaProject = ProjectUtils.getJavaProject(project);
    Map<String, String> projectOptions = javaProject.getOptions(false);
    // Indent using Tabs
    projectOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
    projectOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
    javaProject.setOptions(projectOptions);
    IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(project.getFolder("src"));
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    StringBuilder builder = new StringBuilder();
    builder.append("package test1;\n");
    builder.append("interface I {\n");
    builder.append("	void method();\n");
    builder.append("}\n");
    builder.append("public class E {\n");
    builder.append("	void bar(I i) {\n");
    builder.append("	}\n");
    builder.append("	void foo() {\n");
    builder.append("		bar(() /*[*//*]*/-> {\n");
    builder.append("		});\n");
    builder.append("	}\n");
    builder.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(cu)));
    final Range range = CodeActionUtil.getRange(cu, "/*[*//*]*/");
    params.setRange(range);
    params.setContext(new CodeActionContext(Collections.emptyList(), Arrays.asList(CodeActionKind.Refactor)));
    List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
    Assert.assertNotNull(codeActions);
    Optional<Either<Command, CodeAction>> found = codeActions.stream().filter((codeAction) -> {
        return codeAction.isRight() && Objects.equals("Convert to anonymous class creation", codeAction.getRight().getTitle());
    }).findAny();
    Assert.assertTrue(found.isPresent());
    Either<Command, CodeAction> codeAction = found.get();
    Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
    Assert.assertNotNull(c.getArguments());
    Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
    WorkspaceEdit edit = (WorkspaceEdit) c.getArguments().get(0);
    String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(edit);
    builder = new StringBuilder();
    builder.append("package test1;\n");
    builder.append("interface I {\n");
    builder.append("	void method();\n");
    builder.append("}\n");
    builder.append("public class E {\n");
    builder.append("	void bar(I i) {\n");
    builder.append("	}\n");
    builder.append("	void foo() {\n");
    builder.append("		bar(new I() {\n");
    builder.append("            @Override\n");
    builder.append("            public void method() {\n");
    builder.append("            }\n");
    builder.append("        });\n");
    builder.append("	}\n");
    builder.append("}\n");
    AbstractSourceTestCase.compareSource(builder.toString(), actual);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AbstractSourceTestCase(org.eclipse.jdt.ls.core.internal.codemanipulation.AbstractSourceTestCase) Arrays(java.util.Arrays) ProjectUtils(org.eclipse.jdt.ls.core.internal.ProjectUtils) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) DefaultCodeFormatterConstants(org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) URI(java.net.URI) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Optional(java.util.Optional) IProblem(org.eclipse.jdt.core.compiler.IProblem) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) JavaModelException(org.eclipse.jdt.core.JavaModelException) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) CorrectionMessages(org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) 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) 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) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) Mockito(org.mockito.Mockito) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Ignore(org.junit.Ignore) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Assert(org.junit.Assert) Collections(java.util.Collections) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Range(org.eclipse.lsp4j.Range) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 49 with Command

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

the class CodeActionHandlerTest method testCodeAction_ignoringOtherDiagnosticWithoutCode.

@Test
public void testCodeAction_ignoringOtherDiagnosticWithoutCode() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n" + "	void foo() {\n" + "	}\n" + "}\n");
    // unit.save(new NullProgressMonitor(), true);
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit, "java.sql");
    params.setRange(range);
    Diagnostic diagnosticWithoutCode = new Diagnostic(new Range(new Position(0, 0), new Position(0, 1)), "fake dignostic without code");
    params.setContext(new CodeActionContext(Arrays.asList(diagnosticWithoutCode, getDiagnostic(Integer.toString(IProblem.UnusedImport), range))));
    List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
    Assert.assertNotNull(codeActions);
    Assert.assertTrue(codeActions.size() >= 3);
    List<Either<Command, CodeAction>> quickAssistActions = findActions(codeActions, CodeActionKind.QuickFix);
    Assert.assertNotNull(quickAssistActions);
    Assert.assertTrue(quickAssistActions.size() >= 1);
    List<Either<Command, CodeAction>> organizeImportActions = findActions(codeActions, CodeActionKind.SourceOrganizeImports);
    Assert.assertNotNull(organizeImportActions);
    Assert.assertEquals(1, organizeImportActions.size());
    Command c = codeActions.get(0).getRight().getCommand();
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Position(org.eclipse.lsp4j.Position) Command(org.eclipse.lsp4j.Command) Diagnostic(org.eclipse.lsp4j.Diagnostic) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 50 with Command

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

the class CodeActionHandlerTest method testCodeAction_removeUnterminatedString.

@Test
public void testCodeAction_removeUnterminatedString() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "	void foo() {\n" + "String s = \"some str\n" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit, "some str");
    params.setRange(range);
    params.setContext(new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UnterminatedString), range))));
    List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
    Assert.assertNotNull(codeActions);
    Assert.assertFalse(codeActions.isEmpty());
    Assert.assertEquals(codeActions.get(0).getRight().getKind(), CodeActionKind.QuickFix);
    Command c = codeActions.get(0).getRight().getCommand();
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Aggregations

Command (org.eclipse.lsp4j.Command)95 CodeAction (org.eclipse.lsp4j.CodeAction)58 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 Test (org.junit.Test)46 Range (org.eclipse.lsp4j.Range)36 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)35 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)26 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)23 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)22 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)21 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)17 Diagnostic (org.eclipse.lsp4j.Diagnostic)17 Arrays (java.util.Arrays)14 Collections (java.util.Collections)14 JDTUtils (org.eclipse.jdt.ls.core.internal.JDTUtils)13 CodeActionKind (org.eclipse.lsp4j.CodeActionKind)13 Optional (java.util.Optional)12