Search in sources :

Example 16 with Command

use of org.eclipse.lsp4j.Command 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 17 with Command

use of org.eclipse.lsp4j.Command 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);
}
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) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 18 with Command

use of org.eclipse.lsp4j.Command 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);
}
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) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 19 with Command

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

the class StandardProjectsManager method fileChanged.

@Override
public void fileChanged(String uriString, CHANGE_TYPE changeType) {
    if (uriString == null) {
        return;
    }
    boolean configureNeeded = false;
    String formatterUrl = preferenceManager.getPreferences().getFormatterUrl();
    if (formatterUrl != null && JavaLanguageServerPlugin.getInstance().getProtocol() != null) {
        URI uri = JDTUtils.toURI(uriString);
        List<URI> uris = getURIs(formatterUrl);
        boolean changed = false;
        for (URI formatterUri : uris) {
            if (URIUtil.sameURI(formatterUri, uri)) {
                changed = true;
                break;
            }
        }
        if (changed) {
            if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CREATED) {
                registerWatchers();
            }
            configureNeeded = true;
        }
    }
    String settingsUrl = preferenceManager.getPreferences().getSettingsUrl();
    if (settingsUrl != null && JavaLanguageServerPlugin.getInstance().getProtocol() != null) {
        URI uri = JDTUtils.toURI(uriString);
        List<URI> uris = getURIs(settingsUrl);
        boolean changed = false;
        for (URI settingsURI : uris) {
            if (URIUtil.sameURI(settingsURI, uri)) {
                changed = true;
                break;
            }
        }
        if (changed) {
            if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CREATED) {
                registerWatchers();
            }
            configureNeeded = true;
        }
    }
    if (configureNeeded) {
        configureSettings(preferenceManager.getPreferences());
    }
    IResource resource = JDTUtils.getFileOrFolder(uriString);
    if (resource == null) {
        return;
    }
    try {
        Optional<IBuildSupport> bs = getBuildSupport(resource.getProject());
        if (bs.isPresent()) {
            IBuildSupport buildSupport = bs.get();
            if (JDTUtils.isExcludedFile(buildSupport.getExcludedFilePatterns(), uriString)) {
                return;
            }
            boolean requireConfigurationUpdate = buildSupport.fileChanged(resource, changeType, new NullProgressMonitor());
            if (requireConfigurationUpdate) {
                FeatureStatus status = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus();
                switch(status) {
                    case automatic:
                        // do not force the build, because it's not started by user and should be done only if build file has changed
                        updateProject(resource.getProject(), false);
                        break;
                    case disabled:
                        break;
                    default:
                        if (client != null) {
                            String cmd = "java.projectConfiguration.status";
                            TextDocumentIdentifier uri = new TextDocumentIdentifier(uriString);
                            ActionableNotification updateProjectConfigurationNotification = new ActionableNotification().withSeverity(MessageType.Info).withMessage("A build file was modified. Do you want to synchronize the Java classpath/configuration?").withCommands(asList(new Command("Never", cmd, asList(uri, FeatureStatus.disabled)), new Command("Now", cmd, asList(uri, FeatureStatus.interactive)), new Command("Always", cmd, asList(uri, FeatureStatus.automatic))));
                            client.sendActionableNotification(updateProjectConfigurationNotification);
                        }
                }
            }
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem refreshing workspace", e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FeatureStatus(org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus) ActionableNotification(org.eclipse.jdt.ls.core.internal.ActionableNotification) URI(java.net.URI) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) IResource(org.eclipse.core.resources.IResource)

Example 20 with Command

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

the class AbstractQuickFixTest method evaluateCodeActionCommand.

protected String evaluateCodeActionCommand(Either<Command, CodeAction> codeAction) throws BadLocationException, JavaModelException {
    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 we = (WorkspaceEdit) c.getArguments().get(0);
    return evaluateWorkspaceEdit(we);
}
Also used : Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit)

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