Search in sources :

Example 6 with CompositeElement

use of com.intellij.psi.impl.source.tree.CompositeElement in project intellij-community by JetBrains.

the class StubBasedPsiElementBase method dumpCreationTraces.

@NotNull
private String dumpCreationTraces(@NotNull FileElement fileElement) {
    final StringBuilder traces = new StringBuilder("\nNow " + Thread.currentThread() + "\n");
    traces.append("My creation trace:\n").append(getUserData(CREATION_TRACE));
    traces.append("AST creation traces:\n");
    fileElement.acceptTree(new RecursiveTreeElementWalkingVisitor(false) {

        @Override
        public void visitComposite(CompositeElement composite) {
            PsiElement psi = composite.getPsi();
            if (psi != null) {
                traces.append(psi).append("@").append(System.identityHashCode(psi)).append("\n");
                String trace = psi.getUserData(CREATION_TRACE);
                if (trace != null) {
                    traces.append(trace).append("\n");
                }
            }
            super.visitComposite(composite);
        }
    });
    return traces.toString();
}
Also used : RecursiveTreeElementWalkingVisitor(com.intellij.psi.impl.source.tree.RecursiveTreeElementWalkingVisitor) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with CompositeElement

use of com.intellij.psi.impl.source.tree.CompositeElement in project intellij-community by JetBrains.

the class PsiJavaCodeReferenceCodeFragmentImpl method getReferenceElement.

@Override
public PsiJavaCodeReferenceElement getReferenceElement() {
    final CompositeElement treeElement = calcTreeElement();
    LOG.assertTrue(treeElement.getFirstChildNode().getElementType() == JavaElementType.JAVA_CODE_REFERENCE);
    return (PsiJavaCodeReferenceElement) SourceTreeToPsiMap.treeElementToPsi(treeElement.getFirstChildNode());
}
Also used : PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement)

Example 8 with CompositeElement

use of com.intellij.psi.impl.source.tree.CompositeElement in project intellij-community by JetBrains.

the class JavaIndentHelper method getIndentInner.

@Override
protected int getIndentInner(Project project, FileType fileType, final ASTNode element, final boolean includeNonSpace, final int recursionLevel) {
    if (recursionLevel > TOO_BIG_WALK_THRESHOLD)
        return 0;
    if (element.getTreePrev() != null) {
        ASTNode prev = element.getTreePrev();
        while (prev instanceof CompositeElement && !TreeUtil.isStrongWhitespaceHolder(prev.getElementType())) {
            ASTNode lastCompositePrev = prev;
            prev = prev.getLastChildNode();
            if (prev == null) {
                // element.prev is "empty composite"
                return getIndentInner(project, fileType, lastCompositePrev, includeNonSpace, recursionLevel + 1);
            }
        }
        String text = prev.getText();
        int index = Math.max(text.lastIndexOf('\n'), text.lastIndexOf('\r'));
        if (index >= 0) {
            return getIndent(project, fileType, text.substring(index + 1), includeNonSpace);
        }
        if (includeNonSpace) {
            return getIndentInner(project, fileType, prev, includeNonSpace, recursionLevel + 1) + getIndent(project, fileType, text, includeNonSpace);
        }
        if (element.getElementType() == JavaElementType.CODE_BLOCK) {
            ASTNode parent = element.getTreeParent();
            if (parent.getElementType() == JavaElementType.BLOCK_STATEMENT) {
                parent = parent.getTreeParent();
            }
            if (parent.getElementType() != JavaElementType.CODE_BLOCK) {
                // e.g. for method it could be declaration start, not doc-comment
                return getIndentInner(project, fileType, parent, includeNonSpace, recursionLevel + 1);
            }
        } else {
            if (element.getElementType() == JavaTokenType.LBRACE) {
                return getIndentInner(project, fileType, element.getTreeParent(), includeNonSpace, recursionLevel + 1);
            }
        }
        //Q: any other cases?
        ASTNode parent = prev.getTreeParent();
        ASTNode child = prev;
        while (parent != null) {
            if (child.getTreePrev() != null)
                break;
            child = parent;
            parent = parent.getTreeParent();
        }
        if (parent == null) {
            return getIndent(project, fileType, text, includeNonSpace);
        } else {
            if (prev.getTreeParent().getElementType() == JavaElementType.LABELED_STATEMENT) {
                return getIndentInner(project, fileType, prev, true, recursionLevel + 1) + getIndent(project, fileType, text, true);
            } else
                return getIndentInner(project, fileType, prev, includeNonSpace, recursionLevel + 1);
        }
    } else {
        if (element.getTreeParent() == null) {
            return 0;
        }
        return getIndentInner(project, fileType, element.getTreeParent(), includeNonSpace, recursionLevel + 1);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement)

Example 9 with CompositeElement

use of com.intellij.psi.impl.source.tree.CompositeElement in project intellij-community by JetBrains.

the class ReplaceExpressionUtil method isNeedParenthesis.

public static boolean isNeedParenthesis(ASTNode oldExpr, ASTNode newExpr) {
    final ASTNode oldParent = oldExpr.getTreeParent();
    if (!ElementType.EXPRESSION_BIT_SET.contains(oldParent.getElementType()))
        return false;
    int priority = getExpressionPriority(newExpr);
    int parentPriority = getExpressionPriority(oldParent);
    if (priority > parentPriority)
        return false;
    IElementType i = oldParent.getElementType();
    if (i == JavaElementType.ASSIGNMENT_EXPRESSION) {
        return priority < parentPriority || ((CompositeElement) oldParent).getChildRole(oldExpr) == ChildRole.LOPERAND;
    } else if (i == JavaElementType.CONDITIONAL_EXPRESSION) {
        int role = ((CompositeElement) oldParent).getChildRole(oldExpr);
        if (role == ChildRole.THEN_EXPRESSION)
            return false;
        return priority < parentPriority || role != ChildRole.ELSE_EXPRESSION;
    } else if (i == JavaElementType.BINARY_EXPRESSION || i == JavaElementType.POLYADIC_EXPRESSION) {
        if (priority < parentPriority)
            return true;
        PsiElement element = SourceTreeToPsiMap.treeElementToPsi(oldParent);
        IElementType opType = ((PsiPolyadicExpression) element).getOperationTokenType();
        return ((CompositeElement) oldParent).getChildRole(oldExpr) != ChildRole.LOPERAND && opType != JavaTokenType.PLUS && opType != JavaTokenType.ASTERISK && opType != JavaTokenType.ANDAND && opType != JavaTokenType.OROR;
    } else if (i == JavaElementType.INSTANCE_OF_EXPRESSION) {
        return priority < parentPriority;
    } else if (i == JavaElementType.PREFIX_EXPRESSION || i == JavaElementType.TYPE_CAST_EXPRESSION) {
        return priority < parentPriority;
    } else if (i == JavaElementType.POSTFIX_EXPRESSION) {
        return priority <= parentPriority;
    } else if (i == JavaElementType.REFERENCE_EXPRESSION || i == JavaElementType.METHOD_REF_EXPRESSION) {
        return priority < parentPriority;
    } else if (i == JavaElementType.METHOD_CALL_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.NEW_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.ARRAY_ACCESS_EXPRESSION) {
        int role = ((CompositeElement) oldParent).getChildRole(oldExpr);
        return role != ChildRole.ARRAY_DIMENSION && role != ChildRole.INDEX && priority < parentPriority;
    } else if (i == JavaElementType.ARRAY_INITIALIZER_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.PARENTH_EXPRESSION) {
        return false;
    } else if (i == JavaElementType.LITERAL_EXPRESSION || i == JavaElementType.THIS_EXPRESSION || i == JavaElementType.SUPER_EXPRESSION || i == JavaElementType.CLASS_OBJECT_ACCESS_EXPRESSION || i == JavaElementType.LAMBDA_EXPRESSION) {
        return false;
    }
    LOG.assertTrue(false);
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) ASTNode(com.intellij.lang.ASTNode) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) PsiElement(com.intellij.psi.PsiElement)

Example 10 with CompositeElement

use of com.intellij.psi.impl.source.tree.CompositeElement in project intellij-community by JetBrains.

the class PsiModifierListImpl method setModifierProperty.

@Override
public void setModifierProperty(@NotNull String name, boolean value) throws IncorrectOperationException {
    checkSetModifierProperty(name, value);
    PsiElement parent = getParent();
    PsiElement grandParent = parent != null ? parent.getParent() : null;
    IElementType type = NAME_TO_KEYWORD_TYPE_MAP.get(name);
    CompositeElement treeElement = (CompositeElement) getNode();
    // changes horizontal position of parameters list start, hence, we need to reformat them in order to preserve alignment.
    if (parent instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) parent;
        CodeEditUtil.markToReformat(method.getParameterList().getNode(), true);
    }
    if (value) {
        if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.PRIVATE_KEYWORD || type == JavaTokenType.PROTECTED_KEYWORD || type == null) /* package-private */
        {
            if (type != JavaTokenType.PUBLIC_KEYWORD) {
                setModifierProperty(PUBLIC, false);
            }
            if (type != JavaTokenType.PRIVATE_KEYWORD) {
                setModifierProperty(PRIVATE, false);
            }
            if (type != JavaTokenType.PROTECTED_KEYWORD) {
                setModifierProperty(PROTECTED, false);
            }
            if (type == null)
                return;
        }
        if (parent instanceof PsiField && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.STATIC_KEYWORD || type == JavaTokenType.FINAL_KEYWORD)
                return;
        } else if (parent instanceof PsiMethod && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD)
                return;
        } else if (parent instanceof PsiClass && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD)
                return;
        } else if (parent instanceof PsiAnnotationMethod && grandParent instanceof PsiClass && ((PsiClass) grandParent).isAnnotationType()) {
            if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD)
                return;
        }
        if (treeElement.findChildByType(type) == null) {
            TreeElement keyword = Factory.createSingleLeafElement(type, name, null, getManager());
            treeElement.addInternal(keyword, keyword, null, null);
        }
    } else {
        if (type == null) /* package-private */
        {
            //?
            throw new IncorrectOperationException("Cannot reset package-private modifier.");
        }
        ASTNode child = treeElement.findChildByType(type);
        if (child != null) {
            SourceTreeToPsiMap.treeToPsiNotNull(child).delete();
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Aggregations

CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)30 ASTNode (com.intellij.lang.ASTNode)15 PsiElement (com.intellij.psi.PsiElement)6 IElementType (com.intellij.psi.tree.IElementType)5 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)3 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)2 Wrap (com.intellij.formatting.Wrap)1 LighterASTNode (com.intellij.lang.LighterASTNode)1 JSTreeUtil (com.intellij.lang.javascript.psi.util.JSTreeUtil)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 TreeChange (com.intellij.pom.tree.events.TreeChange)1 PsiFile (com.intellij.psi.PsiFile)1 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)1