Search in sources :

Example 71 with Command

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

the class SourceAssistProcessor method getHashCodeEqualsAction.

private Optional<Either<Command, CodeAction>> getHashCodeEqualsAction(CodeActionParams params, String kind) {
    if (!preferenceManager.getClientPreferences().isHashCodeEqualsPromptSupported()) {
        return Optional.empty();
    }
    Command command = new Command(ActionMessages.GenerateHashCodeEqualsAction_label, COMMAND_ID_ACTION_HASHCODEEQUALSPROMPT, Collections.singletonList(params));
    if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_HASHCODE_EQUALS)) {
        CodeAction codeAction = new CodeAction(ActionMessages.GenerateHashCodeEqualsAction_label);
        codeAction.setKind(kind);
        codeAction.setCommand(command);
        codeAction.setDiagnostics(Collections.emptyList());
        return Optional.of(Either.forRight(codeAction));
    } else {
        return Optional.of(Either.forLeft(command));
    }
}
Also used : Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction)

Example 72 with Command

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

the class SourceAssistProcessor method getGenerateDelegateMethodsAction.

private Optional<Either<Command, CodeAction>> getGenerateDelegateMethodsAction(CodeActionParams params, IInvocationContext context, IType type) {
    try {
        if (!preferenceManager.getClientPreferences().isGenerateDelegateMethodsPromptSupported() || !GenerateDelegateMethodsHandler.supportsGenerateDelegateMethods(type)) {
            return Optional.empty();
        }
    } catch (JavaModelException e) {
        return Optional.empty();
    }
    Command command = new Command(ActionMessages.GenerateDelegateMethodsAction_label, COMMAND_ID_ACTION_GENERATEDELEGATEMETHODSPROMPT, Collections.singletonList(params));
    if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_DELEGATE_METHODS)) {
        CodeAction codeAction = new CodeAction(ActionMessages.GenerateDelegateMethodsAction_label);
        codeAction.setKind(JavaCodeActionKind.SOURCE_GENERATE_DELEGATE_METHODS);
        codeAction.setCommand(command);
        codeAction.setDiagnostics(Collections.EMPTY_LIST);
        return Optional.of(Either.forRight(codeAction));
    } else {
        return Optional.of(Either.forLeft(command));
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction)

Example 73 with Command

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

the class SourceAssistProcessor method getGenerateConstructorsAction.

private Optional<Either<Command, CodeAction>> getGenerateConstructorsAction(CodeActionParams params, IInvocationContext context, IType type, String kind, IProgressMonitor monitor) {
    try {
        if (type == null || type.isAnnotation() || type.isInterface() || type.isAnonymous() || type.getCompilationUnit() == null) {
            return Optional.empty();
        }
    } catch (JavaModelException e) {
        return Optional.empty();
    }
    if (preferenceManager.getClientPreferences().isGenerateConstructorsPromptSupported()) {
        CheckConstructorsResponse status = GenerateConstructorsHandler.checkConstructorStatus(type, monitor);
        if (status.constructors.length == 0) {
            return Optional.empty();
        }
        if (status.constructors.length == 1 && status.fields.length == 0) {
            CodeActionProposal generateConstructorsProposal = (pm) -> {
                TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields, params.getRange(), pm);
                return convertToWorkspaceEdit(type.getCompilationUnit(), edit);
            };
            return getCodeActionFromProposal(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, generateConstructorsProposal);
        }
        Command command = new Command(ActionMessages.GenerateConstructorsAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, Collections.singletonList(params));
        if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS)) {
            CodeAction codeAction = new CodeAction(ActionMessages.GenerateConstructorsAction_ellipsisLabel);
            codeAction.setKind(kind);
            codeAction.setCommand(command);
            codeAction.setDiagnostics(Collections.emptyList());
            return Optional.of(Either.forRight(codeAction));
        } else {
            return Optional.of(Either.forLeft(command));
        }
    }
    return Optional.empty();
}
Also used : CodeActionProposal(org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) CoreException(org.eclipse.core.runtime.CoreException) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) GenerateToStringHandler(org.eclipse.jdt.ls.core.internal.handlers.GenerateToStringHandler) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) Statement(org.eclipse.jdt.core.dom.Statement) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) DiagnosticsHelper(org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) VariableDeclarationFixCore(org.eclipse.jdt.internal.corext.fix.VariableDeclarationFixCore) CodeActionProposal(org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal) OrganizeImportsHandler(org.eclipse.jdt.ls.core.internal.handlers.OrganizeImportsHandler) ASTNodes(org.eclipse.jdt.internal.corext.dom.ASTNodes) CodeAction(org.eclipse.lsp4j.CodeAction) ASTNode(org.eclipse.jdt.core.dom.ASTNode) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) IProposalRelevance(org.eclipse.jdt.ls.core.internal.corrections.proposals.IProposalRelevance) FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) Set(java.util.Set) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TextEditConverter(org.eclipse.jdt.ls.core.internal.TextEditConverter) Sets(com.google.common.collect.Sets) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GenerateDelegateMethodsHandler(org.eclipse.jdt.ls.core.internal.handlers.GenerateDelegateMethodsHandler) IType(org.eclipse.jdt.core.IType) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) IInvocationContext(org.eclipse.jdt.ls.core.internal.corrections.IInvocationContext) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) List(java.util.List) Stream(java.util.stream.Stream) Command(org.eclipse.lsp4j.Command) Modifier(java.lang.reflect.Modifier) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) Optional(java.util.Optional) ChangeUtil(org.eclipse.jdt.ls.core.internal.ChangeUtil) GenerateGetterSetterOperation(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation) IField(org.eclipse.jdt.core.IField) JavaModelException(org.eclipse.jdt.core.JavaModelException) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) CodeGenerationUtils(org.eclipse.jdt.ls.core.internal.handlers.CodeGenerationUtils) CorrectionMessages(org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) GenerateConstructorsHandler(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse) AccessorField(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField) JdtFlags(org.eclipse.jdt.internal.corext.util.JdtFlags) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) TextEdit(org.eclipse.text.edits.TextEdit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) IJavaElement(org.eclipse.jdt.core.IJavaElement) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Collections(java.util.Collections) JavaModelException(org.eclipse.jdt.core.JavaModelException) Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.text.edits.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse)

Example 74 with Command

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

the class SourceAssistProcessor method getGetterSetterAction.

private Optional<Either<Command, CodeAction>> getGetterSetterAction(CodeActionParams params, IInvocationContext context, IType type, String kind, boolean isInTypeDeclaration) {
    try {
        AccessorField[] accessors = GenerateGetterSetterOperation.getUnimplementedAccessors(type);
        if (accessors == null || accessors.length == 0) {
            return Optional.empty();
        } else if (accessors.length == 1 || !preferenceManager.getClientPreferences().isAdvancedGenerateAccessorsSupported()) {
            CodeActionProposal getAccessorsProposal = (pm) -> {
                // If cursor position is not specified, then insert to the last by default.
                IJavaElement insertBefore = isInTypeDeclaration ? CodeGenerationUtils.findInsertElement(type, null) : CodeGenerationUtils.findInsertElement(type, params.getRange());
                GenerateGetterSetterOperation operation = new GenerateGetterSetterOperation(type, context.getASTRoot(), preferenceManager.getPreferences().isCodeGenerationTemplateGenerateComments(), insertBefore);
                TextEdit edit = operation.createTextEdit(pm, accessors);
                return convertToWorkspaceEdit(context.getCompilationUnit(), edit);
            };
            return getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateGetterSetterAction_label, kind, getAccessorsProposal);
        } else {
            Command command = new Command(ActionMessages.GenerateGetterSetterAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATEACCESSORSPROMPT, Collections.singletonList(params));
            if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS)) {
                CodeAction codeAction = new CodeAction(ActionMessages.GenerateGetterSetterAction_ellipsisLabel);
                codeAction.setKind(kind);
                codeAction.setCommand(command);
                codeAction.setDiagnostics(Collections.emptyList());
                return Optional.of(Either.forRight(codeAction));
            } else {
                return Optional.of(Either.forLeft(command));
            }
        }
    } catch (OperationCanceledException | CoreException e) {
        JavaLanguageServerPlugin.logException("Failed to generate Getter and Setter source action", e);
        return Optional.empty();
    }
}
Also used : CodeActionProposal(org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal) IJavaElement(org.eclipse.jdt.core.IJavaElement) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.text.edits.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) AccessorField(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField) GenerateGetterSetterOperation(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation)

Example 75 with Command

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

the class CodeActionHandlerTest method testCodeAction_removeUnusedImport.

@Test
public void testCodeAction_removeUnusedImport() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n" + "	void foo() {\n" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit, "java.sql");
    params.setRange(range);
    params.setContext(new CodeActionContext(Arrays.asList(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) 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