Search in sources :

Example 16 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project Perl5-IDEA by Camelcade.

the class PerlLeafOrLeafOwnerManipulator method handleContentChange.

@Override
public PsiElement handleContentChange(@NotNull PsiElement element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    PsiElement modifyableLeaf;
    if (element instanceof LeafPsiElement) {
        modifyableLeaf = element;
    } else {
        modifyableLeaf = element.getFirstChild();
        assert modifyableLeaf instanceof LeafPsiElement;
        assert element.getTextRange().equals(modifyableLeaf.getTextRange());
    }
    LeafElement newElement = ((LeafPsiElement) modifyableLeaf).replaceWithText(range.replace(modifyableLeaf.getText(), newContent));
    return element == modifyableLeaf ? newElement.getPsi() : newElement.getTreeParent().getPsi();
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 17 with LeafElement

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

the class CssWriter method writeEmbed.

private void writeEmbed(CssFunction cssFunction, CssTermList termList) throws InvalidPropertyException {
    VirtualFile source = null;
    String symbol = null;
    String mimeType = null;
    for (PsiElement child = termList.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof CssTerm) {
            PsiElement firstChild = child.getFirstChild();
            if (firstChild instanceof LeafElement && ((LeafElement) firstChild).getElementType() == CssElementTypes.CSS_IDENT) {
                CharSequence name = firstChild.getNode().getChars();
                @SuppressWarnings("ConstantConditions") PsiElement valueElement = child.getLastChild().getFirstChild();
                if (StringUtil.equals(name, "source")) {
                    source = InjectionUtil.getReferencedFile(valueElement);
                } else {
                    String value = ((CssString) valueElement).getValue();
                    if (StringUtil.equals(name, "symbol")) {
                        symbol = value;
                    } else if (StringUtil.equals(name, "mimeType")) {
                        mimeType = value;
                    } else {
                        LOG.warn("unsupported embed param: " + name + "=" + value);
                    }
                }
            } else if (firstChild instanceof CssTermList) {
                CssTerm[] terms = ((CssTermList) firstChild).getTerms();
                if (terms.length == 2) {
                    CharSequence name = terms[0].getNode().getChars();
                    if (StringUtil.equals(name, "source")) {
                        source = InjectionUtil.getReferencedFile(terms[1].getFirstChild());
                    } else {
                        String value = ((CssString) terms[1].getFirstChild()).getValue();
                        if (StringUtil.equals(name, "symbol")) {
                            symbol = value;
                        } else if (StringUtil.equals(name, "mimeType")) {
                            mimeType = value;
                        } else {
                            LOG.warn("unsupported embed param: " + name + "=" + value);
                        }
                    }
                } else {
                    LOG.warn("unsupported embed: " + firstChild.getText());
                }
            } else if (firstChild instanceof CssString) {
                source = InjectionUtil.getReferencedFile(firstChild);
            } else {
                LOG.warn("unsupported embed statement: " + cssFunction.getNode().getChars());
            }
        }
    }
    if (source == null) {
        throw new InvalidPropertyException(cssFunction, FlashUIDesignerBundle.message("embed.source.not.specified", cssFunction.getText()));
    }
    final int fileId;
    final boolean isSwf = InjectionUtil.isSwf(source, mimeType);
    if (isSwf) {
        fileId = EmbedSwfManager.getInstance().add(source, symbol, assetCounter);
    } else if (InjectionUtil.isImage(source, mimeType)) {
        fileId = EmbedImageManager.getInstance().add(source, mimeType, assetCounter);
    } else {
        throw new InvalidPropertyException(cssFunction, FlashUIDesignerBundle.message("unsupported.embed.asset.type", cssFunction.getText()));
    }
    propertyOut.write(isSwf ? AmfExtendedTypes.SWF : AmfExtendedTypes.IMAGE);
    propertyOut.writeUInt29(fileId);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiElement(com.intellij.psi.PsiElement) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 18 with LeafElement

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

the class AngularJS2IndexingHandler method createLiteralImplicitElementCustomProvider.

@Nullable
@Override
public JSLiteralImplicitElementCustomProvider createLiteralImplicitElementCustomProvider() {
    return new JSLiteralImplicitElementCustomProvider() {

        @Override
        public boolean checkIfCandidate(@NotNull ASTNode literalExpression) {
            ASTNode parent = TreeUtil.findParent(literalExpression, JSStubElementTypes.CALL_EXPRESSION);
            LeafElement leaf = parent != null ? TreeUtil.findFirstLeaf(parent) : null;
            return leaf != null && leaf.getText().startsWith(DECORATE);
        }

        @Override
        public void fillIndexingDataForCandidate(@NotNull JSLiteralExpression argument, @NotNull JSElementIndexingData outIndexingData) {
            String name = argument.isQuotedLiteral() ? AngularJSIndexingHandler.unquote(argument) : null;
            if (name == null)
                return;
            JSCallExpression callExpression = PsiTreeUtil.getParentOfType(argument, JSCallExpression.class);
            if (callExpression == null)
                return;
            JSExpression first = callExpression.getArguments()[0];
            if (!(first instanceof JSArrayLiteralExpression))
                return;
            JSExpression[] expressions = ((JSArrayLiteralExpression) first).getExpressions();
            if (expressions.length != 2)
                return;
            JSExpression decorator = expressions[0];
            String decoratorName = getCallName(decorator);
            if (!isInterestingDecorator(decoratorName))
                return;
            JSExpression metadata = expressions[1];
            String metadataName = getCallName(metadata);
            if (metadataName == null || !metadataName.startsWith("__metadata"))
                return;
            JSExpression[] meta = ((JSCallExpression) metadata).getArguments();
            if (meta.length != 2)
                return;
            if (!(meta[0] instanceof JSLiteralExpression))
                return;
            String type = AngularJSIndexingHandler.unquote(meta[0]);
            if (!"design:type".equals(type))
                return;
            JSImplicitElementImpl.Builder builder = new JSImplicitElementImpl.Builder(getDecoratedName(name, decorator), argument).setUserString(DECORATORS).setTypeString(decoratorName + ";" + meta[1].getText());
            outIndexingData.addImplicitElement(builder.toImplicitElement());
        }

        private String getDecoratedName(String name, JSExpression decorator) {
            if (decorator instanceof JSCallExpression) {
                final JSExpression expression = ((JSCallExpression) decorator).getMethodExpression();
                if (expression instanceof JSReferenceExpression) {
                    JSExpression[] arguments = ((JSCallExpression) decorator).getArguments();
                    if (arguments.length > 0 && arguments[0] instanceof JSLiteralExpression) {
                        Object value = ((JSLiteralExpression) arguments[0]).getValue();
                        if (value instanceof String)
                            return (String) value;
                    }
                }
            }
            return name;
        }

        private String getCallName(JSExpression call) {
            if (call instanceof JSCallExpression) {
                JSExpression expression = ((JSCallExpression) call).getMethodExpression();
                if (expression instanceof JSReferenceExpression) {
                    return ((JSReferenceExpression) expression).getReferenceName();
                }
            }
            return null;
        }
    };
}
Also used : JSImplicitElementImpl(com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl) JSLiteralImplicitElementCustomProvider(com.intellij.lang.javascript.psi.literal.JSLiteralImplicitElementCustomProvider) NotNull(org.jetbrains.annotations.NotNull) JSElementIndexingData(com.intellij.lang.javascript.psi.stubs.JSElementIndexingData) ASTNode(com.intellij.lang.ASTNode) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with LeafElement

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

the class SimpleTreePatcher method split.

@Override
public LeafElement split(LeafElement leaf, int offset, final CharTable table) {
    final CharSequence chars = leaf.getChars();
    final LeafElement leftPart = ASTFactory.leaf(leaf.getElementType(), table.intern(chars, 0, offset));
    final LeafElement rightPart = ASTFactory.leaf(leaf.getElementType(), table.intern(chars, offset, chars.length()));
    leaf.rawInsertAfterMe(leftPart);
    leftPart.rawInsertAfterMe(rightPart);
    leaf.rawRemove();
    return leftPart;
}
Also used : LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 20 with LeafElement

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

the class TreeHasherBase method computeElementHash.

@Override
protected TreeHashResult computeElementHash(@NotNull PsiElement root, PsiFragment upper, NodeSpecificHasher hasher) {
    if (myForIndexing) {
        return TreeHashingUtils.computeElementHashForIndexing(this, myCallBack, root, upper, hasher);
    }
    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)) {
        // contains only whitespaces and other unmeaning children
        return new TreeHashResult(0, hasher.getNodeCost(root), fragment);
    }
    for (int i = 0; i < size; i++) {
        final TreeHashResult res = this.hash(children.get(i), fragment, hasher);
        childHashes[i] = res.getHash();
        childCosts[i] = res.getCost();
    }
    final int c = hasher.getNodeCost(root) + AbstractTreeHasher.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;
        }
    }
    int h = h1 + AbstractTreeHasher.vector(childHashes);
    if (shouldBeAnonymized(root, (NodeSpecificHasherBase) hasher)) {
        h = 0;
    }
    if (myCallBack != null) {
        myCallBack.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)

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