Search in sources :

Example 26 with LeafElement

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

the class PatternCompiler method findAllTypedVarOffsets.

@NotNull
private static int[] findAllTypedVarOffsets(final PsiFile file, final Pattern[] substitutionPatterns) {
    final TIntHashSet result = new TIntHashSet();
    file.accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            super.visitElement(element);
            if (element instanceof LeafElement) {
                final String text = element.getText();
                for (Pattern pattern : substitutionPatterns) {
                    final Matcher matcher = pattern.matcher(text);
                    while (matcher.find()) {
                        result.add(element.getTextRange().getStartOffset() + matcher.end());
                    }
                }
            }
        }
    });
    final int[] resultArray = result.toArray();
    Arrays.sort(resultArray);
    return resultArray;
}
Also used : CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) TIntHashSet(gnu.trove.TIntHashSet) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with LeafElement

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

the class JsonStringLiteralMixin method updateText.

@Override
public PsiLanguageInjectionHost updateText(@NotNull String text) {
    ASTNode valueNode = getNode().getFirstChildNode();
    assert valueNode instanceof LeafElement;
    ((LeafElement) valueNode).replaceWithText(text);
    return this;
}
Also used : ASTNode(com.intellij.lang.ASTNode) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 28 with LeafElement

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

the class ShiftIndentInsideHelper method shiftIndentInside.

public ASTNode shiftIndentInside(ASTNode element, int indentShift) {
    if (indentShift == 0)
        return element;
    final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(element);
    String text = element.getText();
    for (int offset = 0; offset < text.length(); offset++) {
        char c = text.charAt(offset);
        if (c == '\n' || c == '\r') {
            int offset1;
            for (offset1 = offset + 1; offset1 < text.length(); offset1++) {
                c = text.charAt(offset1);
                if (c != ' ' && c != '\t')
                    break;
            }
            if (c == '\n' || c == '\r')
                continue;
            String space = text.substring(offset + 1, offset1);
            int indent = IndentHelperImpl.getIndent(myProject, myFileType, space, true);
            int newIndent = indent + indentShift;
            newIndent = Math.max(newIndent, 0);
            String newSpace = IndentHelperImpl.fillIndent(myProject, myFileType, newIndent);
            ASTNode leaf = element.findLeafElementAt(offset);
            if (!mayShiftIndentInside(leaf)) {
                LOG.error("Error", leaf.getElementType().toString(), "Type: " + leaf.getElementType() + " text: " + leaf.getText());
            }
            if (offset1 < text.length()) {
                ASTNode next = element.findLeafElementAt(offset1);
                if ((next.getElementType() == JavaTokenType.END_OF_LINE_COMMENT || next.getElementType() == JavaTokenType.C_STYLE_COMMENT || next.getElementType() == JspTokenType.JSP_COMMENT) && next != element) {
                    if (mySettings.KEEP_FIRST_COLUMN_COMMENT) {
                        int commentIndent = myIndentIndentHelper.getIndent(myProject, myFileType, next, true);
                        if (commentIndent == 0)
                            continue;
                    }
                } else if (next.getElementType() == XmlTokenType.XML_DATA_CHARACTERS) {
                    continue;
                }
            }
            int leafOffset = getStartOffset(element, leaf);
            if (leaf.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA && leafOffset + leaf.getTextLength() == offset + 1) {
                ASTNode next = element.findLeafElementAt(offset + 1);
                if (next.getElementType() == TokenType.WHITE_SPACE) {
                    leaf = next;
                    leafOffset = getStartOffset(element, leaf);
                } else {
                    if (!newSpace.isEmpty()) {
                        LeafElement newLeaf = ASTFactory.whitespace(newSpace);
                        next.getTreeParent().addChild(newLeaf, next);
                    }
                    text = text.substring(0, offset + 1) + newSpace + text.substring(offset1);
                    continue;
                }
            }
            int startOffset = offset + 1 - leafOffset;
            int endOffset = offset1 - leafOffset;
            if (!LOG.assertTrue(0 <= startOffset && startOffset <= endOffset && endOffset <= leaf.getTextLength())) {
                continue;
            }
            String leafText = leaf.getText();
            String newLeafText = leafText.substring(0, startOffset) + newSpace + leafText.substring(endOffset);
            if (!newLeafText.isEmpty()) {
                LeafElement newLeaf = Factory.createSingleLeafElement(leaf.getElementType(), newLeafText, charTableByTree, SharedImplUtil.getManagerByTree(leaf));
                if (leaf.getTreeParent() != null) {
                    leaf.getTreeParent().replaceChild(leaf, newLeaf);
                }
                if (leaf == element) {
                    element = newLeaf;
                }
            } else {
                ASTNode parent = leaf.getTreeParent();
                if (parent != null) {
                    parent.removeChild(leaf);
                }
            }
            text = text.substring(0, offset + 1) + newSpace + text.substring(offset1);
        }
    }
    return element;
}
Also used : ASTNode(com.intellij.lang.ASTNode) CharTable(com.intellij.util.CharTable) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 29 with LeafElement

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

the class TreeChangeImpl method addChange.

@Override
public void addChange(ASTNode child, @NotNull ChangeInfo changeInfo) {
    LOG.assertTrue(child.getTreeParent() == myParent);
    final ChangeInfo current = myChanges.get(child);
    if (current != null && changeInfo.getChangeType() == ChangeInfo.CONTENTS_CHANGED) {
        return;
    }
    if (changeInfo.getChangeType() == ChangeInfo.REPLACE) {
        final ReplaceChangeInfoImpl replaceChangeInfo = (ReplaceChangeInfoImpl) changeInfo;
        final ASTNode replaced = replaceChangeInfo.getReplaced();
        final ChangeInfo replacedInfo = myChanges.get(replaced);
        if (replacedInfo == null) {
            addChangeInternal(child, changeInfo);
        } else {
            switch(replacedInfo.getChangeType()) {
                case ChangeInfo.REPLACE:
                    replaceChangeInfo.setOldLength(replacedInfo.getOldLength());
                    replaceChangeInfo.setReplaced(((ReplaceChangeInfo) replacedInfo).getReplaced());
                    break;
                case ChangeInfo.ADD:
                    changeInfo = ChangeInfoImpl.create(ChangeInfo.ADD, replaced);
                    removeChangeInternal(replaced);
                    break;
            }
            addChangeInternal(child, changeInfo);
        }
        return;
    }
    if (current != null && current.getChangeType() == ChangeInfo.REMOVED) {
        if (changeInfo.getChangeType() == ChangeInfo.ADD) {
            if (!(child instanceof LeafElement)) {
                // remove/add -> changed
                changeInfo = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, child);
                ((ChangeInfoImpl) changeInfo).setOldLength(current.getOldLength());
                myChanges.put(child, changeInfo);
            } else {
                removeChangeInternal(child);
            }
        }
        return;
    }
    // add + remove == no op
    if (current != null && current.getChangeType() == ChangeInfo.ADD) {
        if (changeInfo.getChangeType() == ChangeInfo.REMOVED) {
            removeChangeInternal(child);
        }
        return;
    }
    if (changeInfo.getChangeType() == ChangeInfo.REMOVED) {
        if (child instanceof LeafElement) {
            final CharSequence charTabIndex = child.getChars();
            if (checkLeaf(child.getTreeNext(), charTabIndex) || checkLeaf(child.getTreePrev(), charTabIndex))
                return;
        }
        addChangeInternal(child, changeInfo);
        if (current != null) {
            ((ChangeInfoImpl) changeInfo).setOldLength(current.getOldLength());
        }
        return;
    }
    if (current == null) {
        addChangeInternal(child, changeInfo);
    }
}
Also used : ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ASTNode(com.intellij.lang.ASTNode) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 30 with LeafElement

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

the class XmlParsingTest method _testPerformance1.

public void _testPerformance1() throws Exception {
    final String text = loadFile("pallada.xml");
    long time = System.currentTimeMillis();
    final PsiFile file = createFile("test.xml", text);
    transformAllChildren(file.getNode());
    System.out.println("Old parsing took " + (System.currentTimeMillis() - time) + "ms");
    int index = 0;
    while (index++ < 10) {
        newParsing(text);
    }
    LeafElement firstLeaf = TreeUtil.findFirstLeaf(file.getNode());
    index = 0;
    do {
        index++;
    } while ((firstLeaf = TreeUtil.nextLeaf(firstLeaf, null)) != null);
    System.out.println("For " + index + " lexems");
}
Also used : PsiFile(com.intellij.psi.PsiFile) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Aggregations

LeafElement (com.intellij.psi.impl.source.tree.LeafElement)41 ASTNode (com.intellij.lang.ASTNode)15 PsiElement (com.intellij.psi.PsiElement)9 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)6 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)4 IElementType (com.intellij.psi.tree.IElementType)4 PsiFragment (com.intellij.dupLocator.util.PsiFragment)3 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 EquivalenceDescriptorProvider (com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 CharTable (com.intellij.util.CharTable)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1