use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-plugins by JetBrains.
the class Java8StepDefinitionCreator method wrapStepDefWithLineBreakAndSemicolon.
protected void wrapStepDefWithLineBreakAndSemicolon(PsiElement addedStepDef) {
LeafElement linebreak = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, "\n", 0, 1, null, addedStepDef.getManager());
addedStepDef.getParent().addBefore(linebreak.getPsi(), addedStepDef);
LeafElement semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, null, addedStepDef.getManager());
addedStepDef.getParent().addAfter(semicolon.getPsi(), addedStepDef);
}
use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.
the class ExternalSystemProjectsWatcher method calculateCrc.
@NotNull
private Long calculateCrc(VirtualFile file) {
Long newCrc;
PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
if (psiFile != null) {
final CRC32 crc32 = new CRC32();
ApplicationManager.getApplication().runReadAction(() -> {
psiFile.acceptChildren(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LeafElement && !(element instanceof PsiWhiteSpace) && !(element instanceof PsiComment)) {
String text = element.getText();
if (!text.trim().isEmpty()) {
for (int i = 0, end = text.length(); i < end; i++) {
crc32.update(text.charAt(i));
}
}
}
super.visitElement(element);
}
});
});
newCrc = crc32.getValue();
} else {
newCrc = file.getModificationStamp();
}
return newCrc;
}
use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.
the class PsiLiteralExpressionImpl method updateText.
@Override
public PsiLanguageInjectionHost updateText(@NotNull final String text) {
ASTNode valueNode = getNode().getFirstChildNode();
assert valueNode instanceof LeafElement;
((LeafElement) valueNode).replaceWithText(text);
return this;
}
use of com.intellij.psi.impl.source.tree.LeafElement in project intellij-community by JetBrains.
the class AbstractTreeHasher method computeElementHash.
/**
* Computes element hash using children hashes.
* Creates only single PsiFragment.
*/
protected TreeHashResult computeElementHash(@NotNull final PsiElement root, final PsiFragment upper, final NodeSpecificHasher hasher) {
if (myForIndexing) {
return TreeHashingUtils.computeElementHashForIndexing(this, myCallBack, root, upper, hasher);
}
ProgressManager.checkCanceled();
final List<PsiElement> children = hasher.getNodeChildren(root);
final int size = children.size();
final int[] childHashes = new int[size];
final int[] childCosts = new int[size];
final PsiFragment fragment = buildFragment(hasher, root, getCost(root));
if (upper != null) {
fragment.setParent(upper);
}
if (size == 0 && !(root instanceof LeafElement)) {
return new TreeHashResult(hasher.getNodeHash(root), hasher.getNodeCost(root), fragment);
}
for (int i = 0; i < size; i++) {
final TreeHashResult res = hash(children.get(i), fragment, hasher);
childHashes[i] = res.getHash();
childCosts[i] = res.getCost();
}
final int c = hasher.getNodeCost(root) + vector(childCosts);
final int h1 = hasher.getNodeHash(root);
final int discardCost = getDiscardCost(root);
for (int i = 0; i < size; i++) {
if (childCosts[i] <= discardCost && ignoreChildHash(children.get(i))) {
childHashes[i] = 0;
}
}
final int h = h1 + vector(childHashes);
if (myCallBack != null) {
myCallBack.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 DuplicatesMatchingVisitor method match.
@Override
public boolean match(PsiElement element1, PsiElement element2) {
if (element1 == null || element2 == null) {
return element1 == element2;
}
if (myDiscardCost > 0) {
final int cost1 = myTreeHasher.hash(element1, null, myNodeSpecificHasher).getCost();
final int cost2 = myTreeHasher.hash(element2, null, myNodeSpecificHasher).getCost();
if (cost1 < myDiscardCost || cost2 < myDiscardCost) {
return true;
}
}
final DuplicatesProfileBase duplicatesProfile = myNodeSpecificHasher.getDuplicatesProfile();
final PsiElementRole role1 = duplicatesProfile.getRole(element1);
final PsiElementRole role2 = duplicatesProfile.getRole(element2);
final Set<PsiElementRole> skippedRoles = EnumSet.noneOf(PsiElementRole.class);
final ExternalizableDuplocatorState duplocatorState = duplicatesProfile.getDuplocatorState(duplicatesProfile.getLanguage(element1));
for (PsiElementRole role : PsiElementRole.values()) {
if (!duplocatorState.distinguishRole(role)) {
skippedRoles.add(role);
}
}
if (role1 == role2 && skippedRoles.contains(role1)) {
return true;
}
final EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(element1);
EquivalenceDescriptor descriptor1 = descriptorProvider != null ? descriptorProvider.buildDescriptor(element1) : null;
EquivalenceDescriptor descriptor2 = descriptorProvider != null ? descriptorProvider.buildDescriptor(element2) : null;
PsiElement newElement1 = DuplocatorUtil.skipNodeIfNeccessary(element1, descriptor1, myNodeFilter);
PsiElement newElement2 = DuplocatorUtil.skipNodeIfNeccessary(element2, descriptor2, myNodeFilter);
if (newElement1 != element1 || newElement2 != element2) {
return match(newElement1, newElement2);
}
if (!element1.getClass().equals(element2.getClass())) {
return false;
}
if (descriptor1 != null && descriptor2 != null) {
return DuplocatorUtil.match(descriptor1, descriptor2, this, skippedRoles, duplicatesProfile);
}
if (element1 instanceof LeafElement) {
IElementType elementType1 = ((LeafElement) element1).getElementType();
IElementType elementType2 = ((LeafElement) element2).getElementType();
if (!duplocatorState.distinguishLiterals() && duplicatesProfile.getLiterals().contains(elementType1) && duplicatesProfile.getLiterals().contains(elementType2)) {
return true;
}
return element1.getText().equals(element2.getText());
}
if (element1.getFirstChild() == null && element1.getTextLength() == 0) {
return element2.getFirstChild() == null && element2.getTextLength() == 0;
}
return matchSequentially(new FilteringNodeIterator(new SiblingNodeIterator(element1.getFirstChild()), getNodeFilter()), new FilteringNodeIterator(new SiblingNodeIterator(element2.getFirstChild()), getNodeFilter()));
}
Aggregations