Search in sources :

Example 26 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class JavaSourceUtil method scanChildren.

private static void scanChildren(CompositeElement element, ASTNode node, TokenSet listTypes, boolean forward) {
    ASTNode child = node;
    while (true) {
        child = (forward ? child.getTreeNext() : child.getTreePrev());
        if (child == null || child.getElementType() == JavaTokenType.COMMA)
            break;
        if (listTypes.contains(child.getElementType())) {
            CharTable charTable = SharedImplUtil.findCharTableByTree(element);
            PsiManager manager = element.getPsi().getManager();
            TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, charTable, manager);
            element.addInternal(comma, comma, (forward ? node : child), Boolean.FALSE);
            break;
        }
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) LighterASTNode(com.intellij.lang.LighterASTNode) CharTable(com.intellij.util.CharTable)

Example 27 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class JavaSourceUtil method addParenthToReplacedChild.

public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType, @NotNull TreeElement newChild, @NotNull PsiManager manager) {
    CompositeElement parenthExpr = ASTFactory.composite(parenthType);
    TreeElement dummyExpr = (TreeElement) newChild.clone();
    final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild);
    new DummyHolder(manager, parenthExpr, null, charTableByTree);
    parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree);
    parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "("));
    parenthExpr.rawAddChildren(dummyExpr);
    parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")"));
    try {
        CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
        PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr));
        parenthExpr = (CompositeElement) SourceTreeToPsiMap.psiToTreeNotNull(formatted);
    } catch (IncorrectOperationException e) {
        // should not happen
        LOG.error(e);
    }
    newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild));
    dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild);
    // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification
    TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr));
    return parenthExpr;
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) DummyHolder(com.intellij.psi.impl.source.DummyHolder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CharTable(com.intellij.util.CharTable)

Example 28 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class AnnotationParamListElement method addInternal.

@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
    if (first.getElementType() == JavaElementType.NAME_VALUE_PAIR && last.getElementType() == JavaElementType.NAME_VALUE_PAIR) {
        ASTNode lparenth = findChildByType(JavaTokenType.LPARENTH);
        if (lparenth == null) {
            CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
            LeafElement created = Factory.createSingleLeafElement(JavaTokenType.LPARENTH, "(", 0, 1, treeCharTab, getManager());
            super.addInternal(created, created, getFirstChildNode(), true);
        }
        ASTNode rparenth = findChildByType(JavaTokenType.RPARENTH);
        if (rparenth == null) {
            CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
            LeafElement created = Factory.createSingleLeafElement(JavaTokenType.RPARENTH, ")", 0, 1, treeCharTab, getManager());
            super.addInternal(created, created, getLastChildNode(), false);
        }
        ASTNode[] nodes = getChildren(NAME_VALUE_PAIR_BIT_SET);
        if (nodes.length == 1) {
            ASTNode node = nodes[0];
            if (node instanceof PsiNameValuePair) {
                PsiNameValuePair pair = (PsiNameValuePair) node;
                if (pair.getName() == null) {
                    PsiAnnotationMemberValue value = pair.getValue();
                    if (value != null) {
                        try {
                            PsiElementFactory factory = JavaPsiFacade.getInstance(getPsi().getProject()).getElementFactory();
                            PsiAnnotation annotation = factory.createAnnotationFromText("@AAA(value = " + value.getText() + ")", null);
                            replaceChild(node, annotation.getParameterList().getAttributes()[0].getNode());
                        } catch (IncorrectOperationException e) {
                            LOG.error(e);
                        }
                    }
                }
            }
        }
        if (anchor == null && before != null) {
            anchor = findChildByType(before ? JavaTokenType.RPARENTH : JavaTokenType.LPARENTH);
        }
        TreeElement firstAdded = super.addInternal(first, last, anchor, before);
        JavaSourceUtil.addSeparatingComma(this, first, NAME_VALUE_PAIR_BIT_SET);
        return firstAdded;
    }
    return super.addInternal(first, last, anchor, before);
}
Also used : ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CharTable(com.intellij.util.CharTable)

Example 29 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class ClassElement method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull ASTNode child) {
    if (isEnum() && child.getElementType() == ENUM_CONSTANT) {
        JavaSourceUtil.deleteSeparatingComma(this, child);
    }
    if (child.getElementType() == FIELD) {
        final ASTNode nextField = TreeUtil.findSibling(child.getTreeNext(), FIELD);
        if (nextField != null && ((PsiField) nextField.getPsi()).getTypeElement().equals(((PsiField) child.getPsi()).getTypeElement())) {
            final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
            final ASTNode modifierList = child.findChildByType(MODIFIER_LIST);
            if (modifierList != null) {
                LeafElement whitespace = Factory.createSingleLeafElement(WHITE_SPACE, " ", 0, 1, treeCharTab, getManager());
                final ASTNode first = nextField.getFirstChildNode();
                nextField.addChild(whitespace, first);
                final ASTNode typeElement = child.findChildByType(TYPE);
                if (typeElement == null) {
                    final TreeElement modifierListCopy = ChangeUtil.copyElement((TreeElement) modifierList, treeCharTab);
                    nextField.addChild(modifierListCopy, whitespace);
                } else {
                    ASTNode run = modifierList;
                    do {
                        final TreeElement copy = ChangeUtil.copyElement((TreeElement) run, treeCharTab);
                        nextField.addChild(copy, whitespace);
                        if (run == typeElement)
                            break;
                        else
                            run = run.getTreeNext();
                    } while (true);
                }
            }
        }
    }
    super.deleteChildInternal(child);
}
Also used : ASTNode(com.intellij.lang.ASTNode) CharTable(com.intellij.util.CharTable)

Example 30 with CharTable

use of com.intellij.util.CharTable in project intellij-community by JetBrains.

the class ClassElement method addInternal.

@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
    PsiClass psiClass = (PsiClass) SourceTreeToPsiMap.treeElementToPsi(this);
    if (anchor == null) {
        if (first.getElementType() != JavaDocElementType.DOC_COMMENT) {
            if (before == null) {
                if (first == last) {
                    PsiElement firstPsi = SourceTreeToPsiMap.treeElementToPsi(first);
                    if (firstPsi instanceof PsiEnumConstant) {
                        anchor = findEnumConstantListDelimiterPlace();
                        before = anchor != findChildByRole(ChildRole.LBRACE);
                    } else {
                        PsiElement psiElement = firstPsi instanceof PsiMember ? JavaPsiImplementationHelper.getInstance(psiClass.getProject()).getDefaultMemberAnchor(psiClass, (PsiMember) firstPsi) : null;
                        anchor = psiElement != null ? SourceTreeToPsiMap.psiElementToTree(psiElement) : null;
                        before = Boolean.TRUE;
                    }
                } else {
                    anchor = findChildByRole(ChildRole.RBRACE);
                    before = Boolean.TRUE;
                }
            } else if (!before.booleanValue()) {
                anchor = findChildByRole(ChildRole.LBRACE);
            } else {
                anchor = findChildByRole(ChildRole.RBRACE);
            }
        }
    }
    if (isEnum()) {
        if (!ENUM_CONSTANT_LIST_ELEMENTS_BIT_SET.contains(first.getElementType())) {
            ASTNode semicolonPlace = findEnumConstantListDelimiterPlace();
            boolean commentsOrWhiteSpaces = true;
            for (ASTNode child = first; child != null; child = child.getTreeNext()) {
                if (!PsiImplUtil.isWhitespaceOrComment(child)) {
                    commentsOrWhiteSpaces = false;
                    break;
                }
            }
            if (!commentsOrWhiteSpaces && (semicolonPlace == null || semicolonPlace.getElementType() != SEMICOLON)) {
                final LeafElement semicolon = Factory.createSingleLeafElement(SEMICOLON, ";", 0, 1, SharedImplUtil.findCharTableByTree(this), getManager());
                addInternal(semicolon, semicolon, semicolonPlace, Boolean.FALSE);
                semicolonPlace = semicolon;
            }
            for (ASTNode run = anchor; run != null; run = run.getTreeNext()) {
                if (run == semicolonPlace) {
                    anchor = before.booleanValue() ? semicolonPlace.getTreeNext() : semicolonPlace;
                    if (anchor != null && PsiImplUtil.isWhitespaceOrComment(anchor)) {
                        anchor = PsiTreeUtil.skipSiblingsForward(anchor.getPsi(), PsiWhiteSpace.class, PsiComment.class).getNode();
                    }
                    break;
                }
            }
        }
    }
    ASTNode afterLast = last.getTreeNext();
    ASTNode next;
    for (ASTNode child = first; child != afterLast; child = next) {
        next = child.getTreeNext();
        if (child.getElementType() == JavaElementType.METHOD && ((PsiMethod) SourceTreeToPsiMap.treeElementToPsi(child)).isConstructor()) {
            ASTNode oldIdentifier = ((CompositeElement) child).findChildByRole(ChildRole.NAME);
            ASTNode newIdentifier = findChildByRole(ChildRole.NAME).copyElement();
            newIdentifier.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(this));
            child.replaceChild(oldIdentifier, newIdentifier);
        }
    }
    if (psiClass.isEnum()) {
        for (ASTNode child = first; child != afterLast; child = next) {
            next = child.getTreeNext();
            if ((child.getElementType() == JavaElementType.METHOD && ((PsiMethod) SourceTreeToPsiMap.treeElementToPsi(child)).isConstructor()) || child.getElementType() == JavaElementType.ENUM_CONSTANT) {
                CompositeElement modifierList = (CompositeElement) ((CompositeElement) child).findChildByRole(ChildRole.MODIFIER_LIST);
                while (true) {
                    ASTNode modifier = modifierList.findChildByType(MODIFIERS_TO_REMOVE_IN_ENUM_BIT_SET);
                    if (modifier == null)
                        break;
                    modifierList.deleteChildInternal(modifier);
                }
            }
        }
    } else if (psiClass.isInterface()) {
        final boolean level8OrHigher = PsiUtil.isLanguageLevel8OrHigher(psiClass);
        for (ASTNode child = first; child != afterLast; child = next) {
            next = child.getTreeNext();
            final IElementType childElementType = child.getElementType();
            if (childElementType == JavaElementType.METHOD || childElementType == JavaElementType.FIELD) {
                CompositeElement modifierList = (CompositeElement) ((CompositeElement) child).findChildByRole(ChildRole.MODIFIER_LIST);
                final TokenSet removeModifiersBitSet = level8OrHigher && childElementType == JavaElementType.METHOD ? MODIFIERS_TO_REMOVE_IN_INTERFACE_BIT_SET_18_METHOD : MODIFIERS_TO_REMOVE_IN_INTERFACE_BIT_SET;
                while (true) {
                    ASTNode modifier = modifierList.findChildByType(removeModifiersBitSet);
                    if (modifier == null)
                        break;
                    modifierList.deleteChildInternal(modifier);
                }
            }
        }
    }
    final TreeElement firstAdded = super.addInternal(first, last, anchor, before);
    if (firstAdded.getElementType() == ENUM_CONSTANT) {
        final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
        for (ASTNode child = ((ASTNode) first).getTreeNext(); child != null; child = child.getTreeNext()) {
            final IElementType elementType = child.getElementType();
            if (elementType == COMMA || elementType == SEMICOLON)
                break;
            if (elementType == ENUM_CONSTANT) {
                TreeElement comma = Factory.createSingleLeafElement(COMMA, ",", 0, 1, treeCharTab, getManager());
                super.addInternal(comma, comma, first, Boolean.FALSE);
                break;
            }
        }
        for (ASTNode child = ((ASTNode) first).getTreePrev(); child != null; child = child.getTreePrev()) {
            final IElementType elementType = child.getElementType();
            if (elementType == COMMA || elementType == SEMICOLON)
                break;
            if (elementType == ENUM_CONSTANT) {
                TreeElement comma = Factory.createSingleLeafElement(COMMA, ",", 0, 1, treeCharTab, getManager());
                super.addInternal(comma, comma, child, Boolean.FALSE);
                break;
            }
        }
    }
    return firstAdded;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TokenSet(com.intellij.psi.tree.TokenSet) ASTNode(com.intellij.lang.ASTNode) CharTable(com.intellij.util.CharTable)

Aggregations

CharTable (com.intellij.util.CharTable)35 ASTNode (com.intellij.lang.ASTNode)22 IElementType (com.intellij.psi.tree.IElementType)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 PsiElement (com.intellij.psi.PsiElement)3 PsiFile (com.intellij.psi.PsiFile)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 FileASTNode (com.intellij.lang.FileASTNode)2 Language (com.intellij.lang.Language)2 LighterASTNode (com.intellij.lang.LighterASTNode)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiClass (com.intellij.psi.PsiClass)2 DummyHolder (com.intellij.psi.impl.source.DummyHolder)2 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)2 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)2 NotNull (org.jetbrains.annotations.NotNull)2 PlainTextLanguage (com.intellij.openapi.fileTypes.PlainTextLanguage)1 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 PomModel (com.intellij.pom.PomModel)1