Search in sources :

Example 21 with CompositeElement

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

the class ASTDelegatePsiElement method delete.

@Override
public void delete() throws IncorrectOperationException {
    PsiElement parent = getParent();
    if (parent instanceof ASTDelegatePsiElement) {
        CheckUtil.checkWritable(this);
        ((ASTDelegatePsiElement) parent).deleteChildInternal(getNode());
    } else if (parent instanceof CompositeElement) {
        CheckUtil.checkWritable(this);
        ((CompositeElement) parent).deleteChildInternal(getNode());
    } else if (parent instanceof PsiFile) {
        CheckUtil.checkWritable(this);
        parent.deleteChildRange(this, this);
    } else {
        throw new UnsupportedOperationException(getClass().getName() + " under " + (parent == null ? "null" : parent.getClass().getName()));
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) PsiElement(com.intellij.psi.PsiElement)

Example 22 with CompositeElement

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

the class IndentHelperImpl method getIndentInner.

protected int getIndentInner(Project project, FileType fileType, final ASTNode element, boolean includeNonSpace, int recursionLevel) {
    if (recursionLevel > TOO_BIG_WALK_THRESHOLD)
        return 0;
    if (element.getTreePrev() != null) {
        ASTNode prev = element.getTreePrev();
        ASTNode lastCompositePrev;
        while (prev instanceof CompositeElement && !TreeUtil.isStrongWhitespaceHolder(prev.getElementType())) {
            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);
        }
        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 {
            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 23 with CompositeElement

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

the class IElementTypeTest method testManipulatorRegistered.

public void testManipulatorRegistered() {
    LanguageExtensionPoint[] extensions = Extensions.getExtensions(new ExtensionPointName<LanguageExtensionPoint>("com.intellij.lang.parserDefinition"));
    Set<String> classes = new HashSet<>();
    List<String> failures = new ArrayList<>();
    int total = 0;
    for (LanguageExtensionPoint e : extensions) {
        ParserDefinition definition = (ParserDefinition) e.getInstance();
        for (IElementType type : IElementType.enumerate(IElementType.TRUE)) {
            if (type instanceof ILeafElementType)
                continue;
            try {
                CompositeElement treeElement = ASTFactory.composite(type);
                total++;
                PsiElement element = treeElement instanceof PsiElement ? (PsiElement) treeElement : definition.createElement(treeElement);
                if (element instanceof PsiLanguageInjectionHost && classes.add(element.getClass().getName())) {
                    boolean ok = ElementManipulators.getManipulator(element) != null;
                    System.out.println((ok ? "OK  " : "FAIL") + " " + element.getClass().getSimpleName() + " [" + definition.getClass().getSimpleName() + "]");
                    if (!ok)
                        failures.add(element.getClass().getName());
                }
            } catch (Throwable ignored) {
            }
        }
    }
    System.out.println("count: " + classes.size() + ", total: " + total);
    assertEmpty("PsiLanguageInjectionHost requires " + ElementManipulators.EP_NAME, failures);
}
Also used : ArrayList(java.util.ArrayList) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 24 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 25 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)

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