Search in sources :

Example 16 with CharTable

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

the class TypeParameterExtendsBoundsListElement method addInternal.

@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
    if (first == last && first.getElementType() == JAVA_CODE_REFERENCE) {
        if (getLastChildNode() != null && getLastChildNode().getElementType() == ERROR_ELEMENT) {
            super.deleteChildInternal(getLastChildNode());
        }
    }
    final TreeElement firstAdded = super.addInternal(first, last, anchor, before);
    final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
    if (first == last && first.getElementType() == JAVA_CODE_REFERENCE) {
        ASTNode element = first;
        for (ASTNode child = element.getTreeNext(); child != null; child = child.getTreeNext()) {
            if (child.getElementType() == AND)
                break;
            if (child.getElementType() == JAVA_CODE_REFERENCE) {
                TreeElement comma = Factory.createSingleLeafElement(AND, "&", 0, 1, treeCharTab, getManager());
                super.addInternal(comma, comma, element, Boolean.FALSE);
                break;
            }
        }
        for (ASTNode child = element.getTreePrev(); child != null; child = child.getTreePrev()) {
            if (child.getElementType() == AND)
                break;
            if (child.getElementType() == JAVA_CODE_REFERENCE) {
                TreeElement comma = Factory.createSingleLeafElement(AND, "&", 0, 1, treeCharTab, getManager());
                super.addInternal(comma, comma, child, Boolean.FALSE);
                break;
            }
        }
    }
    final IElementType keywordType = JavaTokenType.EXTENDS_KEYWORD;
    final String keywordText = PsiKeyword.EXTENDS;
    if (findChildByType(keywordType) == null && findChildByType(JAVA_CODE_REFERENCE) != null) {
        LeafElement keyword = Factory.createSingleLeafElement(keywordType, keywordText, treeCharTab, getManager());
        super.addInternal(keyword, keyword, getFirstChildNode(), Boolean.TRUE);
    }
    return firstAdded;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) CharTable(com.intellij.util.CharTable)

Example 17 with CharTable

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

the class ReferenceListElement method addInternal.

@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
    if (first == last && first.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
        if (getLastChildNode() != null && getLastChildNode().getElementType() == TokenType.ERROR_ELEMENT) {
            super.deleteChildInternal(getLastChildNode());
        }
    }
    final TreeElement firstAdded = super.addInternal(first, last, anchor, before);
    final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
    if (first == last && first.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
        ASTNode element = first;
        for (ASTNode child = element.getTreeNext(); child != null; child = child.getTreeNext()) {
            if (child.getElementType() == JavaTokenType.COMMA)
                break;
            if (child.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
                TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, treeCharTab, getManager());
                super.addInternal(comma, comma, element, Boolean.FALSE);
                break;
            }
        }
        for (ASTNode child = element.getTreePrev(); child != null; child = child.getTreePrev()) {
            if (child.getElementType() == JavaTokenType.COMMA)
                break;
            if (child.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
                TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, treeCharTab, getManager());
                super.addInternal(comma, comma, child, Boolean.FALSE);
                break;
            }
        }
    }
    IElementType keywordType = getKeywordType();
    String keywordText = getKeywordText();
    if (findChildByType(keywordType) == null && findChildByType(JavaElementType.JAVA_CODE_REFERENCE) != null) {
        LeafElement keyword = Factory.createSingleLeafElement(keywordType, keywordText, SharedImplUtil.findCharTableByTree(this), getManager());
        super.addInternal(keyword, keyword, getFirstChildNode(), Boolean.TRUE);
    }
    return firstAdded;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) CharTable(com.intellij.util.CharTable)

Example 18 with CharTable

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

the class PsiExpressionListImpl method addInternal.

@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
    final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
    if (anchor == null) {
        if (before == null || before.booleanValue()) {
            anchor = findChildByRole(ChildRole.RPARENTH);
            if (anchor == null) {
                LeafElement lparenth = Factory.createSingleLeafElement(JavaTokenType.LPARENTH, "(", 0, 1, treeCharTab, getManager());
                super.addInternal(lparenth, lparenth, null, Boolean.FALSE);
                LeafElement rparenth = Factory.createSingleLeafElement(JavaTokenType.RPARENTH, ")", 0, 1, treeCharTab, getManager());
                super.addInternal(rparenth, rparenth, null, Boolean.TRUE);
                anchor = findChildByRole(ChildRole.RPARENTH);
                LOG.assertTrue(anchor != null);
            }
            before = Boolean.TRUE;
        } else {
            anchor = findChildByRole(ChildRole.LPARENTH);
            if (anchor == null) {
                LeafElement lparenth = Factory.createSingleLeafElement(JavaTokenType.LPARENTH, "(", 0, 1, treeCharTab, getManager());
                super.addInternal(lparenth, lparenth, null, Boolean.FALSE);
                LeafElement rparenth = Factory.createSingleLeafElement(JavaTokenType.RPARENTH, ")", 0, 1, treeCharTab, getManager());
                super.addInternal(rparenth, rparenth, null, Boolean.TRUE);
                anchor = findChildByRole(ChildRole.LPARENTH);
                LOG.assertTrue(anchor != null);
            }
            before = Boolean.FALSE;
        }
    }
    TreeElement firstAdded = super.addInternal(first, last, anchor, before);
    if (ElementType.EXPRESSION_BIT_SET.contains(first.getElementType())) {
        JavaSourceUtil.addSeparatingComma(this, first, ElementType.EXPRESSION_BIT_SET);
    }
    return firstAdded;
}
Also used : CharTable(com.intellij.util.CharTable)

Example 19 with CharTable

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

the class CodeStyleManagerImpl method doFindWhiteSpaceNode.

@NotNull
private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@NotNull PsiFile file, int offset) {
    ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file);
    if (!(astNode instanceof FileElement)) {
        return new Pair<>(null, null);
    }
    PsiElement elementAt = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset);
    final CharTable charTable = ((FileElement) astNode).getCharTable();
    if (elementAt == null) {
        elementAt = findElementInTreeWithFormatterEnabled(file, offset);
    }
    if (elementAt == null) {
        return new Pair<>(null, charTable);
    }
    ASTNode node = elementAt.getNode();
    if (node == null || node.getElementType() != TokenType.WHITE_SPACE) {
        return new Pair<>(null, charTable);
    }
    return Pair.create(elementAt, charTable);
}
Also used : FileElement(com.intellij.psi.impl.source.tree.FileElement) CharTable(com.intellij.util.CharTable) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with CharTable

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

the class FormatterUtil method replaceWhiteSpace.

public static void replaceWhiteSpace(final String whiteSpace, final ASTNode leafElement, final IElementType whiteSpaceToken, @Nullable final TextRange textRange) {
    final CharTable charTable = SharedImplUtil.findCharTableByTree(leafElement);
    ASTNode treePrev = findPreviousWhiteSpace(leafElement, whiteSpaceToken);
    if (treePrev == null) {
        treePrev = getWsCandidate(leafElement);
    }
    if (treePrev != null && treePrev.getText().trim().isEmpty() && treePrev.getElementType() != whiteSpaceToken && treePrev.getTextLength() > 0 && !whiteSpace.isEmpty()) {
        LeafElement whiteSpaceElement = Factory.createSingleLeafElement(treePrev.getElementType(), whiteSpace, charTable, SharedImplUtil.getManagerByTree(leafElement));
        ASTNode treeParent = treePrev.getTreeParent();
        treeParent.replaceChild(treePrev, whiteSpaceElement);
    } else {
        LeafElement whiteSpaceElement = Factory.createSingleLeafElement(whiteSpaceToken, whiteSpace, charTable, SharedImplUtil.getManagerByTree(leafElement));
        if (treePrev == null) {
            if (!whiteSpace.isEmpty()) {
                addWhiteSpace(leafElement, whiteSpaceElement);
            }
        } else {
            if (!(treePrev.getElementType() == whiteSpaceToken)) {
                if (!whiteSpace.isEmpty()) {
                    addWhiteSpace(treePrev, whiteSpaceElement);
                }
            } else {
                if (treePrev.getElementType() == whiteSpaceToken) {
                    final CompositeElement treeParent = (CompositeElement) treePrev.getTreeParent();
                    if (!whiteSpace.isEmpty()) {
                        //          LOG.assertTrue(textRange == null || treeParent.getTextRange().equals(textRange));
                        treeParent.replaceChild(treePrev, whiteSpaceElement);
                    } else {
                        treeParent.removeChild(treePrev);
                    }
                    // There is a possible case that more than one PSI element is matched by the target text range.
                    // That is the case, for example, for Python's multi-line expression. It may looks like below:
                    //     import contextlib,\
                    //       math, decimal
                    // Here single range contains two blocks: '\' & '\n  '. So, we may want to replace that range to another text, hence,
                    // we replace last element located there with it ('\n  ') and want to remove any remaining elements ('\').
                    ASTNode removeCandidate = findPreviousWhiteSpace(whiteSpaceElement, whiteSpaceToken);
                    while (textRange != null && removeCandidate != null && removeCandidate.getStartOffset() >= textRange.getStartOffset()) {
                        treePrev = findPreviousWhiteSpace(removeCandidate, whiteSpaceToken);
                        removeCandidate.getTreeParent().removeChild(removeCandidate);
                        removeCandidate = treePrev;
                    }
                //treeParent.subtreeChanged();
                }
            }
        }
    }
}
Also used : 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