Search in sources :

Example 6 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class ExtractTempRefactoring method getSelectedExpression.

private IExpressionFragment getSelectedExpression() throws JavaModelException {
    if (fSelectedExpression != null)
        return fSelectedExpression;
    IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
    if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
        fSelectedExpression = (IExpressionFragment) selectedFragment;
    } else if (selectedFragment != null) {
        if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
            ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
            Expression expression = exprStatement.getExpression();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
        } else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
            Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
        }
    }
    if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
        fSelectedExpression = null;
    }
    return fSelectedExpression;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SourceRange(org.eclipse.jdt.core.SourceRange)

Example 7 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class AssociativeInfixExpressionFragment method getRangeOfOperands.

private static ISourceRange getRangeOfOperands(List<Expression> operands) {
    Expression first = operands.get(0);
    Expression last = operands.get(operands.size() - 1);
    return new SourceRange(first.getStartPosition(), last.getStartPosition() + last.getLength() - first.getStartPosition());
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange)

Example 8 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class StatementAnalyzer method checkSelectedNodes.

protected void checkSelectedNodes() {
    ASTNode[] nodes = getSelectedNodes();
    if (nodes.length == 0)
        return;
    ASTNode node = nodes[0];
    int selectionOffset = getSelection().getOffset();
    try {
        int start = fScanner.getNextStartOffset(selectionOffset, true);
        if (start == node.getStartPosition()) {
            int lastNodeEnd = ASTNodes.getExclusiveEnd(nodes[nodes.length - 1]);
            int pos = fScanner.getNextStartOffset(lastNodeEnd, true);
            int selectionEnd = getSelection().getInclusiveEnd();
            if (pos <= selectionEnd) {
                IScanner scanner = fScanner.getScanner();
                //see https://bugs.eclipse.org/324237
                char[] token = scanner.getCurrentTokenSource();
                if (start < lastNodeEnd && token.length == 1 && (token[0] == ';' || token[0] == ',')) {
                    setSelection(Selection.createFromStartEnd(start, lastNodeEnd - 1));
                } else {
                    ISourceRange range = new SourceRange(lastNodeEnd, pos - lastNodeEnd);
                    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_end_of_selection, JavaStatusContext.create(fCUnit, range));
                }
            }
            // success
            return;
        }
    } catch (CoreException e) {
    // fall through
    }
    ISourceRange range = new SourceRange(selectionOffset, node.getStartPosition() - selectionOffset + 1);
    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_beginning_of_selection, JavaStatusContext.create(fCUnit, range));
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) CoreException(org.eclipse.core.runtime.CoreException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SourceRange(org.eclipse.jdt.core.SourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 9 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class SourceMapper method enterField.

/**
     * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor
     */
public void enterField(FieldInfo fieldInfo) {
    if (this.typeDepth >= 0) {
        this.memberDeclarationStart[this.typeDepth] = fieldInfo.declarationStart;
        this.memberNameRange[this.typeDepth] = new SourceRange(fieldInfo.nameSourceStart, fieldInfo.nameSourceEnd - fieldInfo.nameSourceStart + 1);
        String fieldName = new String(fieldInfo.name);
        this.memberName[this.typeDepth] = fieldName;
        // categories
        IType currentType = this.types[this.typeDepth];
        IField field = currentType.getField(fieldName);
        addCategories(field, fieldInfo.categories);
    }
}
Also used : ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 10 with SourceRange

use of org.eclipse.jdt.core.SourceRange in project che by eclipse.

the class SourceMapper method enterType.

/**
     * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor
     */
public void enterType(TypeInfo typeInfo) {
    this.typeDepth++;
    if (this.typeDepth == this.types.length) {
        // need to grow
        System.arraycopy(this.types, 0, this.types = new IType[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.typeNameRanges, 0, this.typeNameRanges = new SourceRange[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.typeDeclarationStarts, 0, this.typeDeclarationStarts = new int[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberName, 0, this.memberName = new String[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberDeclarationStart, 0, this.memberDeclarationStart = new int[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.memberNameRange, 0, this.memberNameRange = new SourceRange[this.typeDepth * 2], 0, this.typeDepth);
        System.arraycopy(this.methodParameterTypes, 0, this.methodParameterTypes = new char[this.typeDepth * 2][][], 0, this.typeDepth);
        System.arraycopy(this.methodParameterNames, 0, this.methodParameterNames = new char[this.typeDepth * 2][][], 0, this.typeDepth);
        System.arraycopy(this.typeModifiers, 0, this.typeModifiers = new int[this.typeDepth * 2], 0, this.typeDepth);
    }
    if (typeInfo.name.length == 0) {
        this.anonymousCounter++;
        if (this.anonymousCounter == this.anonymousClassName) {
            this.types[this.typeDepth] = getType(this.binaryType.getElementName());
        } else {
            this.types[this.typeDepth] = getType(new String(typeInfo.name));
        }
    } else {
        this.types[this.typeDepth] = getType(new String(typeInfo.name));
    }
    this.typeNameRanges[this.typeDepth] = new SourceRange(typeInfo.nameSourceStart, typeInfo.nameSourceEnd - typeInfo.nameSourceStart + 1);
    this.typeDeclarationStarts[this.typeDepth] = typeInfo.declarationStart;
    IType currentType = this.types[this.typeDepth];
    // type parameters
    if (typeInfo.typeParameters != null) {
        for (int i = 0, length = typeInfo.typeParameters.length; i < length; i++) {
            TypeParameterInfo typeParameterInfo = typeInfo.typeParameters[i];
            ITypeParameter typeParameter = currentType.getTypeParameter(new String(typeParameterInfo.name));
            setSourceRange(typeParameter, new SourceRange(typeParameterInfo.declarationStart, typeParameterInfo.declarationEnd - typeParameterInfo.declarationStart + 1), new SourceRange(typeParameterInfo.nameSourceStart, typeParameterInfo.nameSourceEnd - typeParameterInfo.nameSourceStart + 1));
        }
    }
    // type modifiers
    this.typeModifiers[this.typeDepth] = typeInfo.modifiers;
    // categories
    addCategories(currentType, typeInfo.categories);
}
Also used : ITypeParameter(org.eclipse.jdt.core.ITypeParameter) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) IType(org.eclipse.jdt.core.IType)

Aggregations

SourceRange (org.eclipse.jdt.core.SourceRange)14 ISourceRange (org.eclipse.jdt.core.ISourceRange)13 IType (org.eclipse.jdt.core.IType)6 IMethod (org.eclipse.jdt.core.IMethod)4 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IMember (org.eclipse.jdt.core.IMember)2 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 Expression (org.eclipse.jdt.core.dom.Expression)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 RefactoringStatusContext (org.eclipse.ltk.core.refactoring.RefactoringStatusContext)2 HashSet (java.util.HashSet)1 CoreException (org.eclipse.core.runtime.CoreException)1 IClassFile (org.eclipse.jdt.core.IClassFile)1 IField (org.eclipse.jdt.core.IField)1 IScanner (org.eclipse.jdt.core.compiler.IScanner)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)1