Search in sources :

Example 1 with JSLiteralImplicitElementCustomProvider

use of com.intellij.lang.javascript.psi.literal.JSLiteralImplicitElementCustomProvider 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)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 JSLiteralImplicitElementCustomProvider (com.intellij.lang.javascript.psi.literal.JSLiteralImplicitElementCustomProvider)1 JSElementIndexingData (com.intellij.lang.javascript.psi.stubs.JSElementIndexingData)1 JSImplicitElementImpl (com.intellij.lang.javascript.psi.stubs.impl.JSImplicitElementImpl)1 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1