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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations