Search in sources :

Example 1 with GenerateGetterSetterOperation

use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation in project eclipse.jdt.ls by eclipse.

the class GenerateAccessorsHandler method generateAccessors.

public static TextEdit generateAccessors(IType type, AccessorField[] accessors, boolean generateComments, Range cursor, IProgressMonitor monitor) {
    if (type == null || type.getCompilationUnit() == null) {
        return null;
    }
    try {
        ASTNode declarationNode = null;
        CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
        if (astRoot != null && cursor != null) {
            ASTNode node = NodeFinder.perform(astRoot, DiagnosticsHelper.getStartOffset(type.getCompilationUnit(), cursor), DiagnosticsHelper.getLength(type.getCompilationUnit(), cursor));
            declarationNode = SourceAssistProcessor.getTypeDeclarationNode(node);
        }
        // If cursor position is not specified, then insert to the last by default.
        IJavaElement insertPosition = (declarationNode != null) ? CodeGenerationUtils.findInsertElement(type, null) : CodeGenerationUtils.findInsertElement(type, cursor);
        GenerateGetterSetterOperation operation = new GenerateGetterSetterOperation(type, null, generateComments, insertPosition);
        return operation.createTextEdit(null, accessors);
    } catch (OperationCanceledException | CoreException e) {
        JavaLanguageServerPlugin.logException("Failed to generate the accessors.", e);
        return null;
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) GenerateGetterSetterOperation(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation)

Example 2 with GenerateGetterSetterOperation

use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation 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)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 GenerateGetterSetterOperation (org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 AccessorField (org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField)1 CodeActionProposal (org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal)1 CodeAction (org.eclipse.lsp4j.CodeAction)1 Command (org.eclipse.lsp4j.Command)1 TextEdit (org.eclipse.text.edits.TextEdit)1