Example usage for org.eclipse.jdt.core IBuffer getLength

List of usage examples for org.eclipse.jdt.core IBuffer getLength

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IBuffer getLength.

Prototype

public int getLength();

Source Link

Document

Returns number of characters stored in this buffer.

Usage

From source file:org.jboss.tools.common.java.generation.JavaPropertyGenerator.java

License:Open Source License

public static String getLineDelimiterUsed(ICompilationUnit cu) {
    if (cu == null || !cu.exists()) {
        return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
    }//from   w  w  w. j  a  v  a  2  s  .  com
    IBuffer buf = null;
    try {
        buf = cu.getBuffer();
    } catch (JavaModelException e) {
        ModelPlugin.getPluginLog().logError(e);
    }
    if (buf == null) {
        return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    int length = buf.getLength();
    for (int i = 0; i < length; i++) {
        char ch = buf.getChar(i);
        if (ch == SWT.CR) {
            if (i + 1 < length) {
                if (buf.getChar(i + 1) == SWT.LF) {
                    return "\r\n"; //$NON-NLS-1$
                }
            }
            return "\r"; //$NON-NLS-1$
        } else if (ch == SWT.LF) {
            return "\n"; //$NON-NLS-1$
        }
    }
    return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
}

From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java

License:Open Source License

public static void addAnnotation(String qualifiedName, ICompilationUnit compilationUnit, IJavaElement element,
        String params, MultiTextEdit rootEdit) throws JavaModelException {
    IJavaElement workingCopyElement = findWorkingCopy(compilationUnit, element);
    if (workingCopyElement == null) {
        return;/*from   ww  w . j  av  a 2  s.com*/
    }

    if (!(workingCopyElement instanceof IMember))
        return;

    IMember workingCopyMember = (IMember) workingCopyElement;

    IAnnotation annotation = findAnnotation(workingCopyMember, qualifiedName);
    if (annotation != null && annotation.exists())
        return;

    CompilationUnit cuNode = ASTTools.buildASTRoot(compilationUnit);

    ASTNode elementNode = null;
    if (workingCopyElement instanceof JavaElement) {
        elementNode = ((JavaElement) workingCopyElement).findNode(cuNode);
    }

    boolean duplicateShortName = addImport(qualifiedName, compilationUnit, rootEdit);

    IBuffer buffer = compilationUnit.getBuffer();
    String shortName = getShortName(qualifiedName);

    if (duplicateShortName)
        shortName = qualifiedName;

    String str = AT + shortName + params;

    int position = workingCopyMember.getSourceRange().getOffset();

    if (elementNode != null) {
        position = elementNode.getStartPosition();
        if (elementNode instanceof BodyDeclaration && ((BodyDeclaration) elementNode).getJavadoc() != null) {
            position += ((BodyDeclaration) elementNode).getJavadoc().getLength();
            char c = buffer.getChar(position);
            while ((c == C_CARRIAGE_RETURN || c == C_NEW_LINE) && position < buffer.getLength() - 2) {
                position++;
                c = buffer.getChar(position);
            }
        }
        while (position < buffer.getLength() - 1) {
            char c = buffer.getChar(position);
            if (c != C_CARRIAGE_RETURN && c != C_NEW_LINE && c != C_SPACE && c != C_TAB) {
                break;
            }
            position++;
        }
    }

    if (!(workingCopyMember instanceof ILocalVariable)) {

        str += compilationUnit.findRecommendedLineSeparator();

        str += getLeadingSpacesToInsert(position, buffer);

    } else {
        str += SPACE;
    }

    if (rootEdit != null) {
        TextEdit edit = new InsertEdit(position, str);
        rootEdit.addChild(edit);
    } else {
        buffer.replace(position, 0, str);

        synchronized (compilationUnit) {
            compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
        }
    }
}

From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java

License:Open Source License

private static int getNumberOfSpacesToDelete(int startPosition, IBuffer buffer) {
    int position = startPosition;
    int numberOfSpaces = 0;
    if (position < buffer.getLength() - 1) {
        char c = buffer.getChar(position);
        while ((c == C_SPACE || c == C_TAB || c == C_NEW_LINE || c == C_CARRIAGE_RETURN)
                && position < buffer.getLength() - 1) {
            numberOfSpaces++;// www.j a  va  2s  . c o m
            position++;
            c = buffer.getChar(position);
        }
    }
    return numberOfSpaces;
}

From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java

License:Open Source License

public static void deleteImportForAnnotation(String qualifiedName, IAnnotation annotation,
        ICompilationUnit compilationUnit, IBuffer buffer, MultiTextEdit rootEdit) throws JavaModelException {
    IImportDeclaration importDeclaration = compilationUnit.getImport(qualifiedName);
    IImportContainer importContainer = compilationUnit.getImportContainer();
    if (importDeclaration.exists() && importContainer.exists()) {
        int importSize = importContainer.getSourceRange().getOffset()
                + importContainer.getSourceRange().getLength();

        if (rootEdit != null) {
            int annotationStart = annotation.getSourceRange().getOffset();
            int annotationEnd = annotationStart + annotation.getSourceRange().getLength();
            String textBefore = buffer.getText(importSize, annotationStart - importSize);
            String textAfter = buffer.getText(annotationEnd, buffer.getLength() - annotationEnd);
            if (checkImport(textBefore, qualifiedName) && checkImport(textAfter, qualifiedName)) {
                int numberOfSpaces = 0;
                if (!isLastImport(importContainer, importDeclaration)) {
                    numberOfSpaces = getNumberOfSpacesToDelete(importDeclaration.getSourceRange().getOffset()
                            + importDeclaration.getSourceRange().getLength(), buffer);
                }/*from   w ww  .j  a  va  2s  . co  m*/

                TextEdit edit = new DeleteEdit(importDeclaration.getSourceRange().getOffset(),
                        importDeclaration.getSourceRange().getLength() + numberOfSpaces);
                rootEdit.addChild(edit);
            }
        } else {
            String text = buffer.getText(importSize, buffer.getLength() - importSize);
            if (checkImport(text, qualifiedName)) {
                importDeclaration.delete(false, new NullProgressMonitor());
            }
        }
    }

    if (rootEdit == null) {
        synchronized (compilationUnit) {
            compilationUnit.reconcile(ICompilationUnit.NO_AST, true, null, null);
        }
    }
}

From source file:org.jboss.tools.common.ui.marker.AddSuppressWarningsMarkerResolution.java

License:Open Source License

private CompilationUnitChange addAnnotation(String name, ICompilationUnit compilationUnit, IJavaElement element,
        ASTNode node) throws JavaModelException {
    if (!(element instanceof ISourceReference))
        return null;

    ISourceReference workingCopySourceReference = (ISourceReference) element;

    IBuffer buffer = compilationUnit.getBuffer();

    int position = workingCopySourceReference.getSourceRange().getOffset();

    if (node != null) {
        position = node.getStartPosition();
        if (node instanceof BodyDeclaration && ((BodyDeclaration) node).getJavadoc() != null) {
            position += ((BodyDeclaration) node).getJavadoc().getLength();
            char c = buffer.getChar(position);
            while ((c == '\r' || c == '\n') && position < buffer.getLength() - 2) {
                position++;//from www.  j a v  a2  s.co  m
                c = buffer.getChar(position);
            }
        }
        while (position < buffer.getLength() - 1) {
            char c = buffer.getChar(position);
            if (c != '\r' && c != '\n' && c != ' ' && c != '\t') {
                break;
            }
            position++;
        }
    }

    String str = AT + name;

    if (!(workingCopySourceReference instanceof ILocalVariable)) {

        str += compilationUnit.findRecommendedLineSeparator();

        int index = position;
        while (index >= 0) {
            char c = buffer.getChar(index);
            if (c == '\r' || c == '\n')
                break;
            index--;
        }
        index++;
        if (index < position) {
            String spaces = buffer.getText(index, position - index);
            str += spaces;
        }

    } else {
        str += SPACE;
    }

    CompilationUnitChange change = new CompilationUnitChange("", compilationUnit);

    InsertEdit edit = new InsertEdit(position, str);

    change.setEdit(edit);

    return change;
}