Example usage for org.eclipse.jdt.core.formatter IndentManipulation measureIndentUnits

List of usage examples for org.eclipse.jdt.core.formatter IndentManipulation measureIndentUnits

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.formatter IndentManipulation measureIndentUnits.

Prototype

public static int measureIndentUnits(CharSequence line, int tabWidth, int indentWidth) 

Source Link

Document

Returns the indentation of the given line in indentation units.

Usage

From source file:com.google.gwt.eclipse.core.editors.java.JsniFormattingUtil.java

License:Open Source License

private static int getLineIndentLevel(String line, int tabWidth, int indentWidth) {
    return IndentManipulation.measureIndentUnits(line, tabWidth, indentWidth);
}

From source file:com.google.gwt.eclipse.core.editors.java.JsniMethodBodyCompletionProposalComputer.java

License:Open Source License

/**
 * Returns the indentation units for a given project, document, line and line
 * offset./*from www.j a v  a 2  s.  c  o  m*/
 */
static int measureIndentationUnits(IDocument document, int lineOfInvocationOffset, int lineOffset,
        IJavaProject project) throws BadLocationException {
    Map<?, ?> options = project.getOptions(true);
    String lineText = document.get(lineOffset, document.getLineLength(lineOfInvocationOffset));
    int indentationUnits = IndentManipulation.measureIndentUnits(lineText,
            IndentManipulation.getTabWidth(options), IndentManipulation.getIndentWidth(options));
    return indentationUnits;
}

From source file:jmockit.assist.MockMethodCompletionProposal.java

License:Open Source License

private String generateMethodDeclaration(final IDocument document, final Document recoveredDocument,
        final ASTNode node, final ASTRewrite rewrite, final CodeGenerationSettings settings,
        final MethodDeclaration stub) throws BadLocationException {
    ChildListPropertyDescriptor descriptor = getPropDescriptor(node);
    ListRewrite rewriter = rewrite.getListRewrite(node, descriptor);
    rewriter.insertFirst(stub, null);//from w  ww  .j av a2s . com

    ITrackedNodePosition position = rewrite.track(stub);

    rewrite.rewriteAST(recoveredDocument, fJavaProject.getOptions(true)).apply(recoveredDocument);

    String generatedCode = recoveredDocument.get(position.getStartPosition(), position.getLength());

    int generatedIndent = IndentManipulation.measureIndentUnits(
            getIndentAt(recoveredDocument, position.getStartPosition(), settings), settings.tabWidth,
            settings.indentWidth);

    String indent = getIndentAt(document, getReplacementOffset(), settings);
    String methodDeclarationText = IndentManipulation.changeIndent(generatedCode, generatedIndent,
            settings.tabWidth, settings.indentWidth, indent, TextUtilities.getDefaultLineDelimiter(document));

    return methodDeclarationText;
}

From source file:org.eclipse.objectteams.otdt.internal.ui.assist.CompletionAdaptor.OverrideRoleCompletionProposal.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
    @Override//from  w w w  . j a  va2s. c  om
    protected boolean updateReplacementString(IDocument document, char trigger, int offset,
            ImportRewrite importRewrite) throws CoreException, BadLocationException {
        Document recoveredDocument = new Document();
        CompilationUnit unit = getRecoveredAST(document, offset, recoveredDocument);

        // find enclosing team type:
        ASTNode node = NodeFinder.perform(unit, fReplaceStart, 0);
        while (node != null && !(node instanceof AbstractTypeDeclaration)) {
            node = node.getParent();
        }

        if (node != null) {
            AbstractTypeDeclaration teamDecl = ((AbstractTypeDeclaration) node);

            // create and setup the rewrite:
            AST ast = unit.getAST();
            ASTRewrite rewrite = ASTRewrite.create(ast);
            rewrite.setToOTJ();

            // create type
            TypeDeclaration newType = ast.newTypeDeclaration();
            newType.setName(ast.newSimpleName(this.fRoleName));
            newType.setInterface(Flags.isInterface(this.fModifiers));
            newType.setTeam(Flags.isTeam(this.fModifiers));
            // add @Override:
            Annotation overrideAnnotation = ast.newMarkerAnnotation();
            overrideAnnotation.setTypeName(ast.newSimpleName("Override")); //$NON-NLS-1$
            List modifiers = newType.modifiers();
            modifiers.add(overrideAnnotation);
            // add protected or public
            modifiers.add(ast.newModifier(Flags.isPublic(this.fModifiers) ? ModifierKeyword.PUBLIC_KEYWORD
                    : ModifierKeyword.PROTECTED_KEYWORD));
            // add team keyword?
            if (Flags.isTeam(this.fModifiers))
                modifiers.add(ast.newModifier(ModifierKeyword.TEAM_KEYWORD));

            insertStub(rewrite, teamDecl, teamDecl.getBodyDeclarationsProperty(), this.fReplaceStart, newType);

            // create the replacementString from the rewrite:
            ITrackedNodePosition position = rewrite.track(newType);
            try {
                rewrite.rewriteAST(recoveredDocument, fJavaProject.getOptions(true)).apply(recoveredDocument);

                String generatedCode = recoveredDocument.get(position.getStartPosition(), position.getLength());
                CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fJavaProject);
                int generatedIndent = IndentManipulation.measureIndentUnits(
                        getIndentAt(recoveredDocument, position.getStartPosition(), settings), settings.tabWidth,
                        settings.indentWidth);

                String indent = getIndentAt(document, getReplacementOffset(), settings);
                setReplacementString(
                        IndentManipulation.changeIndent(generatedCode, generatedIndent, settings.tabWidth,
                                settings.indentWidth, indent, TextUtilities.getDefaultLineDelimiter(document)));

            } catch (MalformedTreeException exception) {
                JavaPlugin.log(exception);
            } catch (BadLocationException exception) {
                JavaPlugin.log(exception);
            }
        }
        return true;
    }

From source file:org.jboss.tools.websockets.ui.internal.ca.MethodCompletionProposal.java

License:Open Source License

@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset,
        ImportRewrite importRewrite) throws CoreException, BadLocationException {
    Document recoveredDocument = new Document();
    CompilationUnit unit = getRecoveredAST(document, offset, recoveredDocument);
    if (importRewrite == null) {
        importRewrite = StubUtility.createImportRewrite(unit, true); // create a dummy import rewriter to have one
    }/*from  www.ja  v a2s. co  m*/

    ITypeBinding declaringType = null;
    ChildListPropertyDescriptor descriptor = null;
    ASTNode node = NodeFinder.perform(unit, position, 1);
    if (node instanceof SimpleName || node instanceof Modifier) {
        node = node.getParent();
        if (node instanceof MarkerAnnotation) {
            node = node.getParent();
        }
    }
    if (node instanceof MethodDeclaration || node instanceof FieldDeclaration) {
        node = node.getParent();
    }
    if (node instanceof AnonymousClassDeclaration) {
        declaringType = ((AnonymousClassDeclaration) node).resolveBinding();
        descriptor = AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY;
    } else if (node instanceof AbstractTypeDeclaration) {
        AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) node;
        descriptor = declaration.getBodyDeclarationsProperty();
        declaringType = declaration.resolveBinding();
    }
    if (declaringType != null) {
        ASTRewrite rewrite = ASTRewrite.create(unit.getAST());
        CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fJavaProject);

        String delimiter = StubUtility.getLineDelimiterUsed(unit.getTypeRoot());

        StringBuffer content = new StringBuffer();
        if (methodInfo.annotation != null) {
            importRewrite.addImport(methodInfo.annotation);
            int dot = methodInfo.annotation.lastIndexOf('.');
            String simpleName = methodInfo.annotation.substring(dot + 1);
            content.append("@").append(simpleName).append(delimiter); //$NON-NLS-1$
        }
        content.append("public void " + methodInfo.methodName + "("); //$NON-NLS-1$ //$NON-NLS-2$
        boolean first = true;
        for (int i = 0; i < methodInfo.paramTypes.length; i++) {
            String paramType = methodInfo.paramTypes[i];
            if (!first) {
                content.append(", "); //$NON-NLS-1$
            } else {
                first = false;
            }
            importRewrite.addImport(paramType);
            String simpleName = paramType.substring(paramType.lastIndexOf('.') + 1);
            String name = methodInfo.paramNames != null ? methodInfo.paramNames[i]
                    : simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
            content.append(simpleName).append(" ").append(name); //$NON-NLS-1$
        }
        content.append(") {").append(delimiter).append("\t\t"); //$NON-NLS-1$ //$NON-NLS-2$
        String body = StubUtility.getMethodBodyContent(false, fJavaProject, declaringType.getName(),
                declaringType.getName(), "", delimiter); //$NON-NLS-1$
        if (body == null || body.length() == 0) {
            body = delimiter;
        }
        content.append(body);
        content.append("}").append(delimiter); //$NON-NLS-1$

        MethodDeclaration stub = (MethodDeclaration) rewrite.createStringPlaceholder(
                CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, content.toString(), 0,
                        delimiter, unit.getTypeRoot().getJavaProject()),
                ASTNode.METHOD_DECLARATION);

        ListRewrite rewriter = rewrite.getListRewrite(node, descriptor);
        rewriter.insertFirst(stub, null);

        ITrackedNodePosition position = rewrite.track(stub);
        try {
            rewrite.rewriteAST(recoveredDocument, fJavaProject.getOptions(true)).apply(recoveredDocument);

            String generatedCode = recoveredDocument.get(position.getStartPosition(), position.getLength());
            int generatedIndent = IndentManipulation.measureIndentUnits(
                    getIndentAt(recoveredDocument, position.getStartPosition(), settings), settings.tabWidth,
                    settings.indentWidth);

            String indent = getIndentAt(document, getReplacementOffset(), settings);
            if (this.position > offset && indent.length() == 0) {
                indent = "\t"; //$NON-NLS-1$
            }
            String replacementString = IndentManipulation.changeIndent(generatedCode, generatedIndent,
                    settings.tabWidth, settings.indentWidth, indent,
                    TextUtilities.getDefaultLineDelimiter(document));
            if (this.position > offset) {
                replacementString = "\t" + replacementString + delimiter; //$NON-NLS-1$
                setReplacementLength(getReplacementLength());
                setReplacementOffset(getReplacementOffset() + this.position - offset);
            } else if (prefixLength > 0) {
                setReplacementLength(getReplacementLength() + prefixLength);
                setReplacementOffset(getReplacementOffset() - prefixLength);
            }
            setReplacementString(replacementString);
        } catch (MalformedTreeException exception) {
            JavaPlugin.log(exception);
        } catch (BadLocationException exception) {
            JavaPlugin.log(exception);
        }
    }
    return true;
}