Search in sources :

Example 16 with CompositeElement

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

the class GroovyChangeUtilSupport method decodeInformation.

@Override
public TreeElement decodeInformation(TreeElement element, final Map<Object, Object> decodingState) {
    if (element instanceof CompositeElement) {
        if (element.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || element.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
            GrReferenceElement ref = (GrReferenceElement) SourceTreeToPsiMap.treeElementToPsi(element);
            final PsiMember refMember = element.getCopyableUserData(REFERENCED_MEMBER_KEY);
            if (refMember != null) {
                element.putCopyableUserData(REFERENCED_MEMBER_KEY, null);
                PsiElement refElement1 = ref.resolve();
                if (!refMember.getManager().areElementsEquivalent(refMember, refElement1)) {
                    try {
                        if (!(refMember instanceof PsiClass) || ref.getQualifier() == null) {
                            // can restore only if short (otherwise qualifier should be already restored)
                            ref = (GrReferenceElement) ref.bindToElement(refMember);
                        }
                    } catch (IncorrectOperationException ignored) {
                    }
                    return (TreeElement) SourceTreeToPsiMap.psiElementToTree(ref);
                }
            }
            return element;
        }
    }
    return null;
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 17 with CompositeElement

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

the class GroovyChangeUtilSupport method encodeInformation.

@Override
public void encodeInformation(final TreeElement element, final ASTNode original, final Map<Object, Object> encodingState) {
    if (original instanceof CompositeElement) {
        if (original.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || original.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
            PsiElement psi = original.getPsi();
            Project project = psi.getProject();
            if (!PsiUtil.isThisOrSuperRef(psi) && project.isInitialized() && !DumbService.isDumb(project)) {
                final GroovyResolveResult result = ((GrReferenceElement) psi).advancedResolve();
                if (result != null) {
                    final PsiElement target = result.getElement();
                    if (target instanceof PsiClass || (target instanceof PsiMethod || target instanceof PsiField) && ((PsiMember) target).hasModifierProperty(PsiModifier.STATIC) && result.getCurrentFileResolveContext() instanceof GrImportStatement) {
                        element.putCopyableUserData(REFERENCED_MEMBER_KEY, (PsiMember) target);
                    }
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 18 with CompositeElement

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

the class PsiModifierListImpl method hasExplicitModifier.

@Override
public boolean hasExplicitModifier(@NotNull String name) {
    PsiModifierListStub stub = getGreenStub();
    if (stub != null) {
        return BitUtil.isSet(stub.getModifiersMask(), ModifierFlags.NAME_TO_MODIFIER_FLAG_MAP.get(name));
    }
    final CompositeElement tree = (CompositeElement) getNode();
    final IElementType type = NAME_TO_KEYWORD_TYPE_MAP.get(name);
    return tree.findChildByType(type) != null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiModifierListStub(com.intellij.psi.impl.java.stubs.PsiModifierListStub) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement)

Example 19 with CompositeElement

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

the class JavaChildWrapArranger method arrange.

/**
   * Provides implementation of {@link JavaWrapManager#arrangeChildWrap} method.
   *
   * @param child                   child node which {@link Wrap wrap} is to be defined
   * @param parent                  direct or indirect parent of the given {@code 'child'} node. Defines usage context
   *                                of {@code 'child'} node processing
   * @param settings                code style settings to use during wrap definition
   * @param suggestedWrap           wrap suggested to use by clients of current class. I.e. those clients offer wrap to
   *                                use based on their information about current processing state. However, it's possible
   *                                that they don't know details of fine-grained wrap definition algorithm encapsulated
   *                                at the current class. Hence, this method takes suggested wrap into consideration but
   *                                is not required to use it all the time node based on the given parameters
   * @param reservedWrapsProvider   reserved {@code 'element type -> wrap instance'} mappings provider. <b>Note:</b> this
   *                                argument is considered to be a part of legacy heritage and is intended to be removed as
   *                                soon as formatting code refactoring is done
   * @return                        wrap to use for the given {@code 'child'} node if it's possible to define the one;
   *                                {@code null} otherwise
   */
@SuppressWarnings({ "MethodMayBeStatic" })
@Nullable
public Wrap arrange(ASTNode child, ASTNode parent, CommonCodeStyleSettings settings, JavaCodeStyleSettings javaSettings, Wrap suggestedWrap, AbstractJavaBlock reservedWrapsProvider) {
    ASTNode directParent = child.getTreeParent();
    int role = ((CompositeElement) directParent).getChildRole(child);
    if (parent instanceof PsiPolyadicExpression) {
        if (role == ChildRole.OPERATION_SIGN && !settings.BINARY_OPERATION_SIGN_ON_NEXT_LINE)
            return null;
        boolean rOperand = ArrayUtil.indexOf(((PsiPolyadicExpression) parent).getOperands(), child.getPsi()) > 0;
        if (settings.BINARY_OPERATION_SIGN_ON_NEXT_LINE && rOperand)
            return null;
        return suggestedWrap;
    }
    IElementType nodeType = parent.getElementType();
    IElementType childType = child.getElementType();
    if (childType == JavaElementType.EXTENDS_LIST || childType == JavaElementType.IMPLEMENTS_LIST) {
        return Wrap.createWrap(settings.EXTENDS_KEYWORD_WRAP, true);
    } else if (childType == JavaElementType.THROWS_LIST) {
        return Wrap.createWrap(settings.THROWS_KEYWORD_WRAP, true);
    } else if (nodeType == JavaElementType.EXTENDS_LIST || nodeType == JavaElementType.IMPLEMENTS_LIST) {
        if (role == ChildRole.REFERENCE_IN_LIST) {
            return suggestedWrap;
        } else {
            return null;
        }
    } else if (nodeType == JavaElementType.THROWS_LIST) {
        if (role == ChildRole.REFERENCE_IN_LIST) {
            return suggestedWrap;
        } else {
            return null;
        }
    } else if (nodeType == JavaElementType.CONDITIONAL_EXPRESSION) {
        if (role == ChildRole.COLON && !settings.TERNARY_OPERATION_SIGNS_ON_NEXT_LINE)
            return null;
        if (role == ChildRole.QUEST && !settings.TERNARY_OPERATION_SIGNS_ON_NEXT_LINE)
            return null;
        if (role == ChildRole.THEN_EXPRESSION && settings.TERNARY_OPERATION_SIGNS_ON_NEXT_LINE)
            return null;
        if (role == ChildRole.ELSE_EXPRESSION && settings.TERNARY_OPERATION_SIGNS_ON_NEXT_LINE)
            return null;
        return suggestedWrap;
    } else if (JavaFormatterUtil.isAssignment(parent) && role != ChildRole.TYPE) {
        if (role == ChildRole.INITIALIZER_EQ)
            return settings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE ? suggestedWrap : null;
        if (role == ChildRole.OPERATION_SIGN)
            return settings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE ? suggestedWrap : null;
        if (role == ChildRole.INITIALIZER)
            return settings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE ? null : suggestedWrap;
        if (role == ChildRole.ROPERAND)
            return settings.PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE ? null : suggestedWrap;
        if (role == ChildRole.CLOSING_SEMICOLON)
            return null;
        return suggestedWrap;
    } else if (nodeType == JavaElementType.REFERENCE_EXPRESSION) {
        if (role == ChildRole.DOT) {
            return reservedWrapsProvider.getReservedWrap(JavaElementType.REFERENCE_EXPRESSION);
        } else {
            return suggestedWrap;
        }
    } else if (nodeType == JavaElementType.FOR_STATEMENT) {
        if (role == ChildRole.FOR_INITIALIZATION || role == ChildRole.CONDITION || role == ChildRole.FOR_UPDATE) {
            return suggestedWrap;
        }
        if (role == ChildRole.LOOP_BODY) {
            final boolean dontWrap = (childType == JavaElementType.CODE_BLOCK || childType == JavaElementType.BLOCK_STATEMENT) && settings.BRACE_STYLE == CommonCodeStyleSettings.END_OF_LINE;
            return Wrap.createWrap(dontWrap ? WrapType.NONE : WrapType.NORMAL, true);
        } else {
            return null;
        }
    } else if (parent.getPsi() instanceof PsiModifierListOwner) {
        ASTNode prev = FormatterUtil.getPreviousNonWhitespaceSibling(child);
        if (prev != null && prev.getElementType() == JavaElementType.MODIFIER_LIST) {
            ASTNode last = prev.getLastChildNode();
            if (last != null && last.getElementType() == JavaElementType.ANNOTATION) {
                if (isTypeAnnotationOrFalseIfDumb(last) || javaSettings.DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION && isFieldModifierListWithSingleAnnotation(prev)) {
                    return Wrap.createWrap(WrapType.NONE, false);
                } else {
                    return Wrap.createWrap(getWrapType(getAnnotationWrapType(parent, child, settings)), true);
                }
            }
        }
        return null;
    } else if (nodeType == JavaElementType.MODIFIER_LIST) {
        if (childType == JavaElementType.ANNOTATION) {
            ASTNode prev = FormatterUtil.getPreviousNonWhitespaceSibling(child);
            if (prev instanceof PsiKeyword) {
                return null;
            }
            if (isTypeAnnotationOrFalseIfDumb(child)) {
                if (prev == null || prev.getElementType() != JavaElementType.ANNOTATION || isTypeAnnotationOrFalseIfDumb(prev)) {
                    return Wrap.createWrap(WrapType.NONE, false);
                }
            }
            return Wrap.createWrap(getWrapType(getAnnotationWrapType(parent.getTreeParent(), child, settings)), true);
        } else if (childType == JavaTokenType.END_OF_LINE_COMMENT) {
            return Wrap.createWrap(WrapType.NORMAL, true);
        }
        ASTNode prev = FormatterUtil.getPreviousNonWhitespaceSibling(child);
        if (prev != null && prev.getElementType() == JavaElementType.ANNOTATION) {
            if (javaSettings.DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION && isFieldModifierListWithSingleAnnotation(parent)) {
                return Wrap.createWrap(WrapType.NONE, false);
            }
            Wrap wrap = Wrap.createWrap(getWrapType(getAnnotationWrapType(parent.getTreeParent(), child, settings)), true);
            putPreferredWrapInParentBlock(reservedWrapsProvider, wrap);
            return wrap;
        }
        return null;
    } else if (nodeType == JavaElementType.ASSERT_STATEMENT) {
        if (role == ChildRole.CONDITION) {
            return suggestedWrap;
        }
        if (role == ChildRole.ASSERT_DESCRIPTION && !settings.ASSERT_STATEMENT_COLON_ON_NEXT_LINE) {
            return suggestedWrap;
        }
        if (role == ChildRole.COLON && settings.ASSERT_STATEMENT_COLON_ON_NEXT_LINE) {
            return suggestedWrap;
        }
        return null;
    } else if (nodeType == JavaElementType.CODE_BLOCK) {
        if (child.getPsi() instanceof PsiStatement) {
            return suggestedWrap;
        } else {
            return null;
        }
    } else if (nodeType == JavaElementType.IF_STATEMENT) {
        if (childType == JavaElementType.IF_STATEMENT && role == ChildRole.ELSE_BRANCH && settings.SPECIAL_ELSE_IF_TREATMENT) {
            return Wrap.createWrap(WrapType.NONE, false);
        }
        if (role == ChildRole.THEN_BRANCH || role == ChildRole.ELSE_BRANCH) {
            if (childType == JavaElementType.BLOCK_STATEMENT) {
                return null;
            } else {
                return Wrap.createWrap(WrapType.NORMAL, true);
            }
        }
    } else if (nodeType == JavaElementType.FOREACH_STATEMENT || nodeType == JavaElementType.WHILE_STATEMENT) {
        if (role == ChildRole.LOOP_BODY) {
            if (childType == JavaElementType.BLOCK_STATEMENT) {
                return null;
            } else {
                return Wrap.createWrap(WrapType.NORMAL, true);
            }
        }
    } else if (nodeType == JavaElementType.DO_WHILE_STATEMENT) {
        if (role == ChildRole.LOOP_BODY) {
            return Wrap.createWrap(WrapType.NORMAL, true);
        } else if (role == ChildRole.WHILE_KEYWORD) {
            return Wrap.createWrap(WrapType.NORMAL, true);
        }
    } else if (nodeType == JavaElementType.ANNOTATION_ARRAY_INITIALIZER) {
        if (suggestedWrap != null) {
            return suggestedWrap;
        }
        if (role == ChildRole.ANNOTATION_VALUE) {
            return Wrap.createWrap(WrapType.NORMAL, true);
        }
    }
    return suggestedWrap;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Wrap(com.intellij.formatting.Wrap) ASTNode(com.intellij.lang.ASTNode) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with CompositeElement

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

the class JavaStubPsiElement method delete.

@Override
public void delete() throws IncorrectOperationException {
    ASTNode treeElement = calcTreeElement();
    LOG.assertTrue(treeElement.getTreeParent() != null);
    CheckUtil.checkWritable(this);
    ((CompositeElement) treeElement.getTreeParent()).deleteChildInternal(treeElement);
}
Also used : ASTNode(com.intellij.lang.ASTNode) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement)

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