Search in sources :

Example 1 with CodeGenerationSettings

use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.

the class ConstructorFromSuperclassProposal method getRewrite.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
	 */
@Override
protected ASTRewrite getRewrite() throws CoreException {
    AST ast = fTypeNode.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    createImportRewrite((CompilationUnit) fTypeNode.getRoot());
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
    if (!settings.createComments) {
        settings = null;
    }
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(fTypeNode, getImportRewrite());
    MethodDeclaration newMethodDecl = createNewMethodDeclaration(ast, fSuperConstructor, rewrite, importRewriteContext, settings);
    rewrite.getListRewrite(fTypeNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY).insertFirst(newMethodDecl, null);
    addLinkedRanges(rewrite, newMethodDecl);
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 2 with CodeGenerationSettings

use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.

the class CodeAssist method createOrganizeImportOperation.

private OrganizeImportResult createOrganizeImportOperation(ICompilationUnit compilationUnit, List<String> chosen) throws CoreException {
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(compilationUnit.getJavaProject());
    OrganizeImportsOperation operation = new OrganizeImportsOperation(compilationUnit, null, settings.importIgnoreLowercase, !compilationUnit.isWorkingCopy(), true, chosen, null);
    NullProgressMonitor monitor = new NullProgressMonitor();
    TextEdit edit = operation.createTextEdit(monitor);
    OrganizeImportResult result = DtoFactory.newDto(OrganizeImportResult.class);
    TypeNameMatch[][] choices = operation.getChoices();
    //or all conflicts were resolved (!chosen.isEmpty())
    if ((chosen != null && !chosen.isEmpty()) || choices == null || choices.length == 0) {
        IBuffer buffer = compilationUnit.getBuffer();
        IDocument document = new Document(buffer.getContents());
        DocumentChangeListener documentChangeListener = new DocumentChangeListener(document);
        try {
            edit.apply(document);
        } catch (BadLocationException e) {
            LOG.debug("Applying Organize import text edits goes wrong:", e);
        }
        result.setChanges(documentChangeListener.getChanges());
        return result;
    }
    result.setConflicts(createListOfDTOMatches(choices));
    return result;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) OrganizeImportResult(org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult) TextEdit(org.eclipse.text.edits.TextEdit) DocumentChangeListener(org.eclipse.jdt.internal.corext.format.DocumentChangeListener) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IBuffer(org.eclipse.jdt.core.IBuffer) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with CodeGenerationSettings

use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.

the class AddUnimplementedMethodsOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
    IMethodBinding[] unimplementedMethods = getUnimplementedMethods(fTypeNode);
    if (unimplementedMethods.length == 0)
        return;
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite());
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ICompilationUnit unit = cuRewrite.getCu();
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(unit.getJavaProject());
    ListRewrite listRewrite;
    if (fTypeNode instanceof AnonymousClassDeclaration) {
        AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fTypeNode;
        listRewrite = rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
        settings.createComments = false;
    } else if (fTypeNode instanceof AbstractTypeDeclaration) {
        AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fTypeNode;
        listRewrite = rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
    } else if (fTypeNode instanceof EnumConstantDeclaration) {
        EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) fTypeNode;
        AnonymousClassDeclaration anonymousClassDeclaration = enumConstantDeclaration.getAnonymousClassDeclaration();
        if (anonymousClassDeclaration == null) {
            anonymousClassDeclaration = rewrite.getAST().newAnonymousClassDeclaration();
            rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
        }
        listRewrite = rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
        settings.createComments = false;
    } else {
        //$NON-NLS-1$
        Assert.isTrue(false, "Unknown type node");
        return;
    }
    ImportRewrite imports = cuRewrite.getImportRewrite();
    for (int i = 0; i < unimplementedMethods.length; i++) {
        IMethodBinding curr = unimplementedMethods[i];
        MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass().getName(), settings, false);
        listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 4 with CodeGenerationSettings

use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.

the class MethodDeclarationCompletionProposal method updateReplacementString.

/* (non-Javadoc)
     * @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
     */
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
    boolean addComments = settings.createComments;
    String[] empty = new String[0];
    String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
    String declTypeName = fType.getTypeQualifiedName('.');
    boolean isInterface = fType.isInterface();
    StringBuffer buf = new StringBuffer();
    if (addComments) {
        String comment = CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim);
        if (comment != null) {
            buf.append(comment);
            buf.append(lineDelim);
        }
    }
    if (fReturnTypeSig != null) {
        if (!isInterface) {
            //$NON-NLS-1$
            buf.append("private ");
        }
    } else {
        if (fType.isEnum())
            //$NON-NLS-1$
            buf.append("private ");
        else
            //$NON-NLS-1$
            buf.append("public ");
    }
    if (fReturnTypeSig != null) {
        buf.append(Signature.toString(fReturnTypeSig));
    }
    buf.append(' ');
    buf.append(fMethodName);
    if (isInterface) {
        //$NON-NLS-1$
        buf.append("();");
        buf.append(lineDelim);
    } else {
        //$NON-NLS-1$
        buf.append("() {");
        buf.append(lineDelim);
        String body = CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", //$NON-NLS-1$
        lineDelim);
        if (body != null) {
            buf.append(body);
            buf.append(lineDelim);
        }
        //$NON-NLS-1$
        buf.append("}");
        buf.append(lineDelim);
    }
    String stub = buf.toString();
    // use the code formatter
    IRegion region = document.getLineInformationOfOffset(getReplacementOffset());
    int lineStart = region.getOffset();
    int indent = Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);
    String replacement = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, fType.getJavaProject());
    if (replacement.endsWith(lineDelim)) {
        replacement = replacement.substring(0, replacement.length() - lineDelim.length());
    }
    setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
    return true;
}
Also used : CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) StyledString(org.eclipse.jface.viewers.StyledString) IRegion(org.eclipse.jface.text.IRegion)

Example 5 with CodeGenerationSettings

use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.

the class JavaPreferencesSettings method getCodeGenerationSettings.

public static CodeGenerationSettings getCodeGenerationSettings(IJavaProject project) {
    CodeGenerationSettings res = new CodeGenerationSettings();
    //Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, project)).booleanValue();
    res.createComments = false;
    //Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_KEYWORD_THIS, project)).booleanValue();
    res.useKeywordThis = false;
    //Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, project)).booleanValue();
    res.overrideAnnotation = true;
    //Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_IGNORELOWERCASE, project)).booleanValue();
    res.importIgnoreLowercase = true;
    res.tabWidth = CodeFormatterUtil.getTabWidth(project);
    res.indentWidth = CodeFormatterUtil.getIndentWidth(project);
    return res;
}
Also used : CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)

Aggregations

CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)13 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)5 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 IDocument (org.eclipse.jface.text.IDocument)4 StyledString (org.eclipse.jface.viewers.StyledString)4 ArrayList (java.util.ArrayList)3 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)3 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Document (org.eclipse.jface.text.Document)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IType (org.eclipse.jdt.core.IType)2 AST (org.eclipse.jdt.core.dom.AST)2