public void animate() { switch (shieldType) { case REDBALL: float beta = 0.0f; float coeff = 1.0f; for (CompositeElement composite : composites) { composite.animate(); Element elem = composite.getRefElement(); elem.x = (int) (affected.x + 5 * Math.sin((alpha * alpha + beta) * coeff)); elem.y = (int) (affected.y + 10 + 2 * Math.cos((alpha + beta) * coeff)); elem.z = 20; // - beta * 10; elem.y -= elem.getSprModel().getTaille_y() / 2; elem.setAjustedX((int) elem.x); elem.setAjustedY((int) elem.y); beta += 0.8f; // gamma; //0.9;//Math.PI / 4; coeff *= -1f; } alpha += 0.1; gamma += 0.2; break; } }
protected final void rawInsertAfterMeWithoutNotifications(TreeElement firstNew) { firstNew.rawRemoveUpToWithoutNotifications(null, false); final CompositeElement p = getTreeParent(); final TreeElement treeNext = getTreeNext(); firstNew.setTreePrev(this); setTreeNext(firstNew); while (true) { final TreeElement n = firstNew.getTreeNext(); assert n != this : "Attempt to create cycle"; firstNew.setTreeParent(p); if (n == null) break; firstNew = n; } if (treeNext == null) { if (p != null) { firstNew.setTreeParent(p); p.setLastChildNode(firstNew); } } else { firstNew.setTreeNext(treeNext); treeNext.setTreePrev(firstNew); } DebugUtil.checkTreeStructure(this); }
public static PsiStatement[] getChildStatements(CompositeElement psiCodeBlock) { ApplicationManager.getApplication().assertReadAccessAllowed(); // no lock is needed because all chameleons are expanded already int count = 0; for (ASTNode child1 = psiCodeBlock.getFirstChildNode(); child1 != null; child1 = child1.getTreeNext()) { if (child1.getPsi() instanceof PsiStatement) { count++; } } PsiStatement[] result = PsiStatement.ARRAY_FACTORY.create(count); if (count == 0) return result; int idx = 0; for (ASTNode child = psiCodeBlock.getFirstChildNode(); child != null && idx < count; child = child.getTreeNext()) { PsiElement element = child.getPsi(); if (element instanceof PsiStatement) { result[idx++] = (PsiStatement) element; } } return result; }
private static void addNewLineToTag(CompositeElement tag, Project project) { LOG.assertTrue(tag != null && tag.getElementType() == DOC_TAG); ASTNode current = tag.getLastChildNode(); while (current != null && current.getElementType() == DOC_COMMENT_DATA && isWhitespaceCommentData(current)) { current = current.getTreePrev(); } if (current != null && current.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) return; final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(tag); final ASTNode newLine = Factory.createSingleLeafElement( DOC_COMMENT_DATA, "\n", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addChild(newLine, null); ASTNode leadingWhitespaceAnchor = null; if (JavaCodeStyleSettingsFacade.getInstance(project).isJavaDocLeadingAsterisksEnabled()) { final TreeElement leadingAsterisk = Factory.createSingleLeafElement( DOC_COMMENT_LEADING_ASTERISKS, "*", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); leadingWhitespaceAnchor = tag.addInternal(leadingAsterisk, leadingAsterisk, null, Boolean.TRUE); } final TreeElement commentData = Factory.createSingleLeafElement( DOC_COMMENT_DATA, " ", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addInternal(commentData, commentData, leadingWhitespaceAnchor, Boolean.TRUE); }
public void rawInsertAfterMe(@NotNull TreeElement firstNew) { rawInsertAfterMeWithoutNotifications(firstNew); final CompositeElement parent = getTreeParent(); if (parent != null) { parent.subtreeChanged(); } }
// remove nodes from this[including] to end[excluding] from the parent public void rawRemoveUpTo(@Nullable TreeElement end) { CompositeElement parent = getTreeParent(); rawRemoveUpToWithoutNotifications(end, true); if (parent != null) { parent.subtreeChanged(); } }
protected void invalidate() { CompositeElement parent = getTreeParent(); if (parent != null) { parent.subtreeChanged(); } // invalidate replaced element setTreeNext(null); setTreePrev(null); setTreeParent(null); onInvalidated(); }
@Override public void deleteChildInternal(@NotNull ASTNode child) { if (child.getElementType() == DOC_TAG) { if (child.getTreeNext() == null || child.getTreeNext().getElementType() != DOC_TAG) { ASTNode prev = child.getTreePrev(); while (prev != null && prev.getElementType() == DOC_COMMENT_DATA) { prev = prev.getTreePrev(); } ASTNode next = child.getTreeNext(); while (next != null && (next.getElementType() == DOC_COMMENT_DATA || next.getElementType() == WHITE_SPACE)) { next = next.getTreeNext(); } if (prev != null && prev.getElementType() == DOC_COMMENT_LEADING_ASTERISKS && !(next instanceof PsiDocTag)) { ASTNode leadingAsterisk = prev; if (leadingAsterisk.getTreePrev() != null) { super.deleteChildInternal(leadingAsterisk.getTreePrev()); super.deleteChildInternal(leadingAsterisk); } } else if (prev != null && prev.getElementType() == DOC_TAG) { final CompositeElement compositePrev = (CompositeElement) prev; final ASTNode lastPrevChild = compositePrev.getLastChildNode(); ASTNode prevChild = lastPrevChild; while (prevChild != null && prevChild.getElementType() == DOC_COMMENT_DATA) { prevChild = prevChild.getTreePrev(); } if (prevChild != null && prevChild.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) { ASTNode current = prevChild; while (current != null) { final ASTNode nextChild = current.getTreeNext(); compositePrev.deleteChildInternal(current); current = nextChild; } } } else { next = child.getTreeNext(); if (next != null && next.getElementType() == WHITE_SPACE) { next.getTreeParent().removeChild(next); } } } } super.deleteChildInternal(child); }
private static void removeEndingAsterisksFromTag(CompositeElement tag) { ASTNode current = tag.getLastChildNode(); while (current != null && current.getElementType() == DOC_COMMENT_DATA) { current = current.getTreePrev(); } if (current != null && current.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) { final ASTNode prevWhiteSpace = TreeUtil.skipElementsBack(current.getTreePrev(), ElementType.JAVA_WHITESPACE_BIT_SET); ASTNode toBeDeleted = prevWhiteSpace.getTreeNext(); while (toBeDeleted != null) { ASTNode next = toBeDeleted.getTreeNext(); tag.deleteChildInternal(toBeDeleted); toBeDeleted = next; } } }
private static void deQualifyImpl(@NotNull CompositeElement reference) { ASTNode qualifier = reference.findChildByRole(ChildRole.QUALIFIER); if (qualifier != null) { ASTNode firstChildNode = qualifier.getFirstChildNode(); boolean markToReformatBefore = firstChildNode instanceof TreeElement && CodeEditUtil.isMarkedToReformatBefore((TreeElement) firstChildNode); reference.deleteChildInternal(qualifier); if (markToReformatBefore) { firstChildNode = reference.getFirstChildNode(); if (firstChildNode != null) { CodeEditUtil.markToReformatBefore(firstChildNode, true); } } } }
@Override public void rawAddChildrenWithoutNotifications(@NotNull TreeElement first) { if (myText() != null) { LOG.error("Mutating collapsed chameleon"); } super.rawAddChildrenWithoutNotifications(first); }
@Override final void setLastChildNode(TreeElement child) { if (myText() != null) { LOG.error("Mutating collapsed chameleon"); } super.setLastChildNode(child); }
// remove nodes from this[including] to end[excluding] from the parent protected final void rawRemoveUpToWithoutNotifications(TreeElement end, boolean invalidate) { if (this == end) return; final CompositeElement parent = getTreeParent(); final TreeElement startPrev = getTreePrev(); final TreeElement endPrev = end != null ? end.getTreePrev() : null; assert end == null || end.getTreeParent() == parent : "Trying to remove non-child"; if (end != null) { TreeElement element; for (element = this; element != end && element != null; element = element.getTreeNext()) ; assert element == end : end + " is not successor of " + this + " in the .getTreeNext() chain"; } if (parent != null) { if (this == parent.getFirstChildNode()) { parent.setFirstChildNode(end); } if (end == null) { parent.setLastChildNode(startPrev); } } if (startPrev != null) { startPrev.setTreeNext(end); } if (end != null) { end.setTreePrev(startPrev); } setTreePrev(null); if (endPrev != null) { endPrev.setTreeNext(null); } if (parent != null) { for (TreeElement element = this; element != null; element = element.getTreeNext()) { element.setTreeParent(null); if (invalidate) { element.onInvalidated(); } } } DebugUtil.checkTreeStructure(parent); DebugUtil.checkTreeStructure(this); }
@Override public void clearCaches() { super.clearCaches(); synchronized (lock) { if (myText != null) { setCachedLength(myText.length()); } } }
private static ASTNode makeShortReference( @NotNull CompositeElement reference, @NotNull PsiClass refClass, boolean addImports) { @NotNull final PsiJavaCodeReferenceElement psiReference = (PsiJavaCodeReferenceElement) reference.getPsi(); final PsiQualifiedReference reference1 = getClassReferenceToShorten(refClass, addImports, psiReference); if (reference1 != null) replaceReferenceWithShort(reference1); return reference; }
public ShieldEffect(Element p_linked, ShieldType p_shieldType) { affected = p_linked; shieldType = p_shieldType; switch (shieldType) { case REDBALL: for (int i = 0; i < 4; i++) { Element ball = new Element(); ball.setNSpr(ElementDescription.REDBALL3.ordinal()); ball.setNBank(SpriteBank.BANK_ELEMENTS); ball.setSpecialEffect(EngineFX.SHINY); EngineZildo.spriteManagement.spawnSprite(ball); CompositeElement composite = new CompositeElement(ball); composite.followShape(); composites.add(composite); ball.x = affected.x; ball.y = affected.y; ball.z = affected.z; } break; } }
public void rawInsertBeforeMe(@NotNull TreeElement firstNew) { final TreeElement anchorPrev = getTreePrev(); if (anchorPrev == null) { firstNew.rawRemoveUpToLast(); final CompositeElement p = getTreeParent(); if (p != null) p.setFirstChildNode(firstNew); while (true) { final TreeElement treeNext = firstNew.getTreeNext(); assert treeNext != this : "Attempt to create cycle"; firstNew.setTreeParent(p); if (treeNext == null) break; firstNew = treeNext; } setTreePrev(firstNew); firstNew.setTreeNext(this); if (p != null) { p.subtreeChanged(); } } else anchorPrev.rawInsertAfterMe(firstNew); DebugUtil.checkTreeStructure(this); }
private void insertOuters(TreeElement root, Lexer lexer, final CharTable table) { TreePatcher patcher = TREE_PATCHER.forLanguage(root.getPsi().getLanguage()); int treeOffset = 0; LeafElement leaf = TreeUtil.findFirstLeaf(root); while (lexer.getTokenType() != null) { IElementType tt = lexer.getTokenType(); if (tt != myTemplateElementType) { while (leaf != null && treeOffset < lexer.getTokenStart()) { treeOffset += leaf.getTextLength(); if (treeOffset > lexer.getTokenStart()) { leaf = patcher.split( leaf, leaf.getTextLength() - (treeOffset - lexer.getTokenStart()), table); treeOffset = lexer.getTokenStart(); } leaf = (LeafElement) TreeUtil.nextLeaf(leaf); } if (leaf == null) break; final OuterLanguageElementImpl newLeaf = createOuterLanguageElement(lexer, table, myOuterElementType); patcher.insert(leaf.getTreeParent(), leaf, newLeaf); leaf.getTreeParent().subtreeChanged(); leaf = newLeaf; } lexer.advance(); } if (lexer.getTokenType() != null) { assert lexer.getTokenType() != myTemplateElementType; final OuterLanguageElementImpl newLeaf = createOuterLanguageElement(lexer, table, myOuterElementType); ((CompositeElement) root).rawAddChildren(newLeaf); ((CompositeElement) root).subtreeChanged(); } }
@Nullable public static ASTNode findDocComment(@NotNull CompositeElement element) { TreeElement node = element.getFirstChildNode(); while (node != null && (isWhitespaceOrComment(node) && !(node.getPsi() instanceof PsiDocComment))) { node = node.getTreeNext(); } if (node != null && node.getElementType() == JavaDocElementType.DOC_COMMENT) { return node; } else { return null; } }
public void rawRemove() { final TreeElement next = getTreeNext(); final CompositeElement parent = getTreeParent(); final TreeElement prev = getTreePrev(); if (prev != null) { prev.setTreeNext(next); } else if (parent != null) { parent.setFirstChildNode(next); } if (next != null) { next.setTreePrev(prev); } else if (parent != null) { parent.setLastChildNode(prev); } DebugUtil.checkTreeStructure(parent); DebugUtil.checkTreeStructure(prev); DebugUtil.checkTreeStructure(next); invalidate(); }
private void ensureParsed() { if (!ourParsingAllowed) { LOG.error("Parsing not allowed!!!"); } CharSequence text = myText(); if (text == null) return; if (TreeUtil.getFileElement(this) == null) { LOG.error("Chameleons must not be parsed till they're in file tree: " + this); } ApplicationManager.getApplication().assertReadAccessAllowed(); DebugUtil.startPsiModification("lazy-parsing"); try { ILazyParseableElementType type = (ILazyParseableElementType) getElementType(); ASTNode parsedNode = type.parseContents(this); if (parsedNode == null && text.length() > 0) { CharSequence diagText = ApplicationManager.getApplication().isInternal() ? text : ""; LOG.error( "No parse for a non-empty string: " + diagText + "; type=" + LogUtil.objectAndClass(type)); } synchronized (lock) { if (myText == null) return; if (rawFirstChild() != null) { LOG.error("Reentrant parsing?"); } myText = null; if (parsedNode == null) return; super.rawAddChildrenWithoutNotifications((TreeElement) parsedNode); } } finally { DebugUtil.finishPsiModification(); } if (!Boolean.TRUE.equals(ourSuppressEagerPsiCreation.get())) { // create PSI all at once, to reduce contention of PsiLock in CompositeElement.getPsi() // create PSI outside the 'lock' since this method grabs PSI_LOCK and deadlock is possible // when someone else locks in the other order. createAllChildrenPsiIfNecessary(); } }
@Override public ASTNode parseContents(ASTNode chameleon) { final CharTable table = SharedImplUtil.findCharTableByTree(chameleon); final FileElement treeElement = new DummyHolder(((TreeElement) chameleon).getManager(), null, table).getTreeElement(); final PsiFile file = (PsiFile) TreeUtil.getFileElement((TreeElement) chameleon).getPsi(); PsiFile originalFile = file.getOriginalFile(); final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider) originalFile.getViewProvider(); final Language language = getTemplateFileLanguage(viewProvider); final CharSequence chars = chameleon.getChars(); final PsiFile templateFile = createTemplateFile(file, language, chars, viewProvider); final TreeElement parsed = ((PsiFileImpl) templateFile).calcTreeElement(); Lexer langLexer = LanguageParserDefinitions.INSTANCE.forLanguage(language).createLexer(file.getProject()); final Lexer lexer = new MergingLexerAdapter( new TemplateBlackAndWhiteLexer( createBaseLexer(viewProvider), langLexer, myTemplateElementType, myOuterElementType), TokenSet.create(myTemplateElementType, myOuterElementType)); lexer.start(chars); insertOuters(parsed, lexer, table); if (parsed != null) { final TreeElement element = parsed.getFirstChildNode(); if (element != null) { ((CompositeElement) parsed).rawRemoveAllChildren(); treeElement.rawAddChildren(element); } } treeElement.subtreeChanged(); TreeElement childNode = treeElement.getFirstChildNode(); DebugUtil.checkTreeStructure(parsed); DebugUtil.checkTreeStructure(treeElement); DebugUtil.checkTreeStructure(chameleon); DebugUtil.checkTreeStructure(file.getNode()); DebugUtil.checkTreeStructure(originalFile.getNode()); return childNode; }
private static void dequalifyImpl(@NotNull CompositeElement reference) { final ASTNode qualifier = reference.findChildByRole(ChildRole.QUALIFIER); if (qualifier != null) { reference.deleteChildInternal(qualifier); } }
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(); } } } } }
public void kill() { for (CompositeElement composite : composites) { composite.die(false); } }