Search in sources :

Example 11 with LeafElement

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

the class TreeHashingUtils method computeElementHashForIndexing.

static TreeHashResult computeElementHashForIndexing(AbstractTreeHasher base, FragmentsCollector callBack, PsiElement root, PsiFragment upper, NodeSpecificHasher hasher) {
    final List<PsiElement> children = hasher.getNodeChildren(root);
    final PsiFragment fragment = base.buildFragment(hasher, root, base.getCost(root));
    if (upper != null) {
        fragment.setParent(upper);
    }
    final int size = children.size();
    if (size == 0 && !(root instanceof LeafElement)) {
        // contains only whitespaces and other unmeaning children
        return new TreeHashResult(0, hasher.getNodeCost(root), fragment);
    }
    final int discardCost = base.getDiscardCost(root);
    int c = hasher.getNodeCost(root);
    int h = hasher.getNodeHash(root);
    for (int i = 0; i < size; i++) {
        PsiElement child = children.get(i);
        final TreeHashResult res = base.hash(child, fragment, hasher);
        int childCost = res.getCost();
        c += childCost;
        if (childCost > discardCost || !base.ignoreChildHash(child)) {
            h += res.getHash();
        }
    }
    if (base.shouldAnonymize(root, hasher)) {
        h = 0;
    }
    if (callBack != null) {
        callBack.add(h, c, fragment);
    }
    return new TreeHashResult(h, c, fragment);
}
Also used : PsiFragment(com.intellij.dupLocator.util.PsiFragment) PsiElement(com.intellij.psi.PsiElement) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 12 with LeafElement

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

the class DuplocatorUtil method isIgnoredNode.

public static boolean isIgnoredNode(PsiElement element) {
    if (element instanceof PsiWhiteSpace || element instanceof PsiErrorElement || element instanceof PsiComment) {
        return true;
    }
    if (!(element instanceof LeafElement)) {
        return false;
    }
    if (CharArrayUtil.containsOnlyWhiteSpaces(element.getText())) {
        return true;
    }
    EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(element);
    if (descriptorProvider == null) {
        return false;
    }
    final IElementType elementType = ((LeafElement) element).getElementType();
    return descriptorProvider.getIgnoredTokens().contains(elementType);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiErrorElement(com.intellij.psi.PsiErrorElement) PsiComment(com.intellij.psi.PsiComment) EquivalenceDescriptorProvider(com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 13 with LeafElement

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

the class MultiHostRegistrarImpl method patchLeaves.

private static void patchLeaves(@NotNull ASTNode parsedNode, @NotNull List<LiteralTextEscaper<? extends PsiLanguageInjectionHost>> escapers, @NotNull Place shreds) {
    LeafPatcher patcher = new LeafPatcher(shreds, escapers);
    ((TreeElement) parsedNode).acceptTree(patcher);
    assert ((TreeElement) parsedNode).textMatches(patcher.catLeafs) : "Malformed PSI structure: leaf texts do not add up to the whole file text." + "\nFile text (from tree)  :'" + parsedNode.getText() + "'" + "\nFile text (from PSI)   :'" + parsedNode.getPsi().getText() + "'" + "\nLeaf texts concatenated:'" + patcher.catLeafs + "';" + "\nFile root: " + parsedNode + "\nLanguage: " + parsedNode.getPsi().getLanguage() + "\nHost file: " + shreds.getHostPointer().getVirtualFile();
    DebugUtil.startPsiModification("injection leaf patching");
    try {
        for (Map.Entry<LeafElement, String> entry : patcher.newTexts.entrySet()) {
            LeafElement leaf = entry.getKey();
            String newText = entry.getValue();
            leaf.rawReplaceWithText(newText);
        }
    } finally {
        DebugUtil.finishPsiModification();
    }
    TreeUtil.clearCaches((TreeElement) parsedNode);
}
Also used : Map(java.util.Map) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 14 with LeafElement

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

the class PomModelImpl method tryReparseOneLeaf.

@Nullable
private static Runnable tryReparseOneLeaf(@NotNull FileElement treeElement, @NotNull CharSequence newText, @NotNull TextRange changedPsiRange) {
    final LeafElement leaf = treeElement.findLeafElementAt(changedPsiRange.getStartOffset());
    IElementType leafType = leaf == null ? null : leaf.getElementType();
    if (!(leafType instanceof IReparseableLeafElementType))
        return null;
    CharSequence newLeafText = getLeafChangedText(leaf, treeElement, newText, changedPsiRange);
    //noinspection unchecked
    final ASTNode copy = newLeafText == null ? null : ((IReparseableLeafElementType) leafType).reparseLeaf(leaf, newLeafText);
    return copy == null ? null : () -> leaf.getTreeParent().replaceChild(leaf, copy);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IReparseableLeafElementType(com.intellij.psi.tree.IReparseableLeafElementType) ASTNode(com.intellij.lang.ASTNode) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method updateText.

@NotNull
public static QuotedLiteralString updateText(@NotNull QuotedLiteralString quotedLiteralString, @NotNull String text) {
    if (text.length() > 2) {
        if (quotedLiteralString.getText() != null) {
            StringBuilder outChars = new StringBuilder();
            BallerinaStringLiteralEscaper.escapeString(text.substring(1, text.length() - 1), outChars);
            outChars.insert(0, '"');
            outChars.append('"');
            text = outChars.toString();
        }
    }
    ASTNode valueNode = quotedLiteralString.getNode();
    assert valueNode instanceof LeafElement;
    ((LeafElement) valueNode).replaceWithText(text);
    return quotedLiteralString;
}
Also used : ASTNode(com.intellij.lang.ASTNode) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) NotNull(org.jetbrains.annotations.NotNull)

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