Search in sources :

Example 81 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class FlexIconProvider method getIcon.

@Override
public Icon getIcon(@NotNull PsiElement element, int flags) {
    int transformedFlags = ElementBase.transformFlags(element, flags);
    Icon icon = null;
    if (element instanceof XmlFile) {
        if (JavaScriptSupportLoader.isFlexMxmFile((PsiFile) element)) {
            final JSClass jsClass = XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) element);
            if (jsClass != null) {
                icon = jsClass.getIcon(flags);
            }
        }
    } else if (element instanceof JSFileImpl) {
        final JSNamedElement mainDeclaredElement = ActionScriptResolveUtil.findMainDeclaredElement((JSFileImpl) element);
        if (mainDeclaredElement != null) {
            icon = mainDeclaredElement.getIcon(transformedFlags);
        }
    }
    if (icon != null) {
        final PsiFile psiFile = element.getContainingFile();
        final VirtualFile vFile = psiFile == null ? null : psiFile.getVirtualFile();
        CompilerManager compilerManager = CompilerManager.getInstance(element.getProject());
        if (vFile != null && compilerManager != null && compilerManager.isExcludedFromCompilation(vFile)) {
            icon = new LayeredIcon(icon, PlatformIcons.EXCLUDED_FROM_COMPILE_ICON);
        }
    }
    return icon;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LayeredIcon(com.intellij.ui.LayeredIcon) XmlFile(com.intellij.psi.xml.XmlFile) CompilerManager(com.intellij.openapi.compiler.CompilerManager) JSNamedElement(com.intellij.lang.javascript.psi.JSNamedElement) PsiFile(com.intellij.psi.PsiFile) LayeredIcon(com.intellij.ui.LayeredIcon) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) JSFileImpl(com.intellij.lang.javascript.psi.impl.JSFileImpl)

Example 82 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class ActionScriptGenerateDelegatesHandler method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    final JSClass jsClass = findClass(file, editor);
    if (jsClass == null)
        return;
    Collection<JSField> fields = findCandidateFields(jsClass);
    final JSField field;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        LOG.assertTrue(fields.size() == 1);
        field = fields.iterator().next();
    } else {
        final MemberChooser<JSNamedElementNode> targetChooser = createMemberChooserDialog(project, jsClass, wrap(fields), false, false, CodeInsightBundle.message("generate.delegate.target.chooser.title"));
        targetChooser.show();
        if (targetChooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
        field = (JSField) targetChooser.getSelectedElements().get(0).getPsiElement();
    }
    JSType fieldType = field.getType();
    if (fieldType == null)
        return;
    JSClass fieldClass = fieldType.resolveClass();
    if (fieldClass == null)
        return;
    final boolean allowPackageLocal = !JSPsiImplUtils.differentPackageName(StringUtil.getPackageName(fieldClass.getQualifiedName()), StringUtil.getPackageName(jsClass.getQualifiedName()));
    // don't add members along with their supers
    class MemberDescriptor {

        private final String name;

        @Nullable
        private final JSFunction.FunctionKind kind;

        public MemberDescriptor(JSFunction method) {
            name = method.getName();
            kind = method.getKind();
        }

        public MemberDescriptor(JSVariable field) {
            name = field.getName();
            kind = null;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            MemberDescriptor that = (MemberDescriptor) o;
            if (kind != that.kind)
                return false;
            if (!name.equals(that.name))
                return false;
            return true;
        }

        @Override
        public int hashCode() {
            int result = name.hashCode();
            result = 31 * result + (kind != null ? kind.hashCode() : 0);
            return result;
        }
    }
    final Map<MemberDescriptor, JSNamedElement> memberCandidates = new HashMap<>();
    ResolveProcessor p = new ResolveProcessor(null) {

        {
            setToProcessHierarchy(true);
        }

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            JSClass clazz = JSUtils.getMemberContainingClass(element);
            if (clazz == null || JSResolveUtil.isObjectClass(clazz) || clazz == jsClass) {
                return true;
            }
            if (element instanceof JSFunction) {
                JSFunction method = (JSFunction) element;
                if (memberCandidates.containsKey(method.getName())) {
                    return true;
                }
                JSAttributeList attributeList = method.getAttributeList();
                if (attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE || attributeList.getAccessType() == JSAttributeList.AccessType.PROTECTED) {
                    return true;
                }
                if (!allowPackageLocal && attributeList.getNamespace() == null && attributeList.getAccessType() == JSAttributeList.AccessType.PACKAGE_LOCAL) {
                    return true;
                }
                if (method.getKind() == JSFunction.FunctionKind.CONSTRUCTOR) {
                    return true;
                }
                if (attributeList.hasModifier(JSAttributeList.ModifierType.STATIC)) {
                    return true;
                }
                if (JSInheritanceUtil.findMethodInClass(method, jsClass, true) != null) {
                    return true;
                }
                memberCandidates.put(new MemberDescriptor(method), method);
            } else if (element instanceof JSVariable) {
                JSVariable f = (JSVariable) element;
                if (memberCandidates.containsKey(f.getName())) {
                    return true;
                }
                JSAttributeList attributeList = f.getAttributeList();
                if (attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE || attributeList.getAccessType() == JSAttributeList.AccessType.PROTECTED) {
                    return true;
                }
                if (!allowPackageLocal && attributeList.getAccessType() == JSAttributeList.AccessType.PACKAGE_LOCAL) {
                    return true;
                }
                if (jsClass.findFunctionByName(f.getName()) != null) {
                    return true;
                }
                memberCandidates.put(new MemberDescriptor(f), f);
            }
            return true;
        }
    };
    fieldClass.processDeclarations(p, ResolveState.initial(), fieldClass, fieldClass);
    Collection<JSNamedElementNode> selected;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        LOG.assertTrue(!memberCandidates.isEmpty());
        selected = wrap(memberCandidates.values());
    } else {
        final MemberChooser<JSNamedElementNode> methodsChooser = createMemberChooserDialog(project, jsClass, wrap(memberCandidates.values()), false, true, CodeInsightBundle.message("generate.delegate.method.chooser.title"));
        methodsChooser.show();
        if (methodsChooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
        selected = methodsChooser.getSelectedElements();
    }
    BaseCreateMethodsFix fix = new BaseCreateMethodsFix<JSNamedElement>(jsClass) {

        final JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix generateGetterFix = new JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix(JSGetterSetterGenerationMode.Getter, jsClass, null, false, field.getName());

        final JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix generateSetterFix = new JavaScriptGenerateAccessorHandler.MyBaseCreateMethodsFix(JSGetterSetterGenerationMode.Setter, jsClass, null, false, field.getName());

        @Override
        protected void adjustAttributeList(JSAttributeListWrapper attributeListWrapper, JSNamedElement function) {
            attributeListWrapper.overrideAccessType(JSAttributeList.AccessType.PUBLIC);
            attributeListWrapper.overrideModifier(JSAttributeList.ModifierType.STATIC, field.getAttributeList().hasModifier(JSAttributeList.ModifierType.STATIC));
            for (JSAttributeList.ModifierType modifierType : new JSAttributeList.ModifierType[] { JSAttributeList.ModifierType.NATIVE, JSAttributeList.ModifierType.DYNAMIC, JSAttributeList.ModifierType.FINAL, JSAttributeList.ModifierType.OVERRIDE, JSAttributeList.ModifierType.VIRTUAL }) {
                attributeListWrapper.overrideModifier(modifierType, false);
            }
        }

        @Override
        protected void processElements(Project project, MultiMap<String, String> types, Set<JSNamedElement> elementsToProcess) {
            for (JSNamedElement e : elementsToProcess) {
                if (e instanceof JSFunction) {
                    anchor = doAddOneMethod(project, buildFunctionText(e, types), anchor);
                } else {
                    anchor = doAddOneMethod(project, generateGetterFix.buildFunctionText(e, types), anchor);
                    anchor = doAddOneMethod(project, generateSetterFix.buildFunctionText(e, types), anchor);
                }
            }
        }

        @Override
        protected String buildFunctionBodyText(final String retType, final JSParameterList parameterList, final JSNamedElement element) {
            return OverrideMethodsFix.buildDelegatingText(retType, parameterList, ((JSFunction) element), field.getName(), anchor != null ? anchor : myJsClass);
        }
    };
    doInvoke(project, editor, file, selected, fix);
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) NotNull(org.jetbrains.annotations.NotNull) JSAttributeListWrapper(com.intellij.lang.javascript.validation.fixes.JSAttributeListWrapper) MultiMap(com.intellij.util.containers.MultiMap) PsiElement(com.intellij.psi.PsiElement) ResolveProcessor(com.intellij.lang.javascript.psi.resolve.ResolveProcessor) ResolveState(com.intellij.psi.ResolveState) BaseCreateMethodsFix(com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix) Project(com.intellij.openapi.project.Project) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

Example 83 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class ActionScriptGenerateEventHandler method getEventConstantInfo.

/**
   * Trinity.first is JSExpressionStatement (if it looks like ButtonEvent.CLICK),
   * Trinity.second is event class FQN (like "flash.events.MouseEvent"),
   * Trinity.third is event name (like "click")
   */
@Nullable
public static Trinity<JSExpressionStatement, String, String> getEventConstantInfo(final PsiFile psiFile, final Editor editor) {
    if (!(psiFile instanceof JSFile)) {
        return null;
    }
    final JSClass jsClass = BaseJSGenerateHandler.findClass(psiFile, editor);
    if (jsClass == null || !ActionScriptEventDispatchUtils.isEventDispatcher(jsClass)) {
        return null;
    }
    final PsiElement elementAtCursor = psiFile.findElementAt(editor.getCaretModel().getOffset());
    final JSExpressionStatement expressionStatement = PsiTreeUtil.getParentOfType(elementAtCursor, JSExpressionStatement.class);
    final PsiElement expressionStatementParent = expressionStatement == null ? null : expressionStatement.getParent();
    final JSFunction jsFunction = PsiTreeUtil.getParentOfType(expressionStatement, JSFunction.class);
    final JSExpression expression = expressionStatement == null ? null : expressionStatement.getExpression();
    final JSReferenceExpression refExpression = expression instanceof JSReferenceExpression ? (JSReferenceExpression) expression : null;
    final JSExpression qualifier = refExpression == null ? null : refExpression.getQualifier();
    final PsiReference qualifierReference = qualifier == null ? null : qualifier.getReference();
    final PsiElement referenceNameElement = refExpression == null ? null : refExpression.getReferenceNameElement();
    JSAttributeList functionAttributes;
    if (jsFunction == null || ((functionAttributes = jsFunction.getAttributeList()) != null && functionAttributes.hasModifier(JSAttributeList.ModifierType.STATIC)) || qualifierReference == null || !(referenceNameElement instanceof LeafPsiElement) || (!(expressionStatementParent instanceof JSFunction) && !(expressionStatementParent instanceof JSBlockStatement))) {
        return null;
    }
    final PsiElement qualifierResolve = qualifierReference.resolve();
    if (!(qualifierResolve instanceof JSClass) || !isEventClass((JSClass) qualifierResolve)) {
        return null;
    }
    final PsiElement expressionResolve = refExpression.resolve();
    if (expressionResolve instanceof JSVariable) {
        final JSAttributeList varAttributes = ((JSVariable) expressionResolve).getAttributeList();
        final String text = ((JSVariable) expressionResolve).getLiteralOrReferenceInitializerText();
        if (varAttributes != null && varAttributes.hasModifier(JSAttributeList.ModifierType.STATIC) && varAttributes.getAccessType() == JSAttributeList.AccessType.PUBLIC && text != null && StringUtil.isQuotedString(text)) {
            return Trinity.create(expressionStatement, ((JSClass) qualifierResolve).getQualifiedName(), initializerToPartialMethodName(text));
        }
    }
    return null;
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 84 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class GenerateFlexUnitMethodActionBase method getGenerateHandler.

protected BaseJSGenerateHandler getGenerateHandler() {
    return new BaseJSGenerateHandler() {

        protected String getTitleKey() {
            return null;
        }

        protected BaseCreateMethodsFix createFix(JSClass clazz) {
            return new BaseCreateMethodsFix(clazz) {

                public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
                    evalAnchor(editor, file);
                    final PsiElement addedElement = doAddOneMethod(project, "public function fake():void{}", anchor);
                    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
                    final TemplateManager manager = TemplateManager.getInstance(project);
                    final Template template = manager.createTemplate("", "");
                    template.setToReformat(true);
                    buildTemplate(template, myJsClass);
                    final TextRange range = addedElement.getTextRange();
                    editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), "");
                    editor.getCaretModel().moveToOffset(range.getStartOffset());
                    manager.startTemplate(editor, template);
                }
            };
        }

        protected boolean canHaveEmptySelectedElements() {
            return true;
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) BaseCreateMethodsFix(com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix) Template(com.intellij.codeInsight.template.Template)

Example 85 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class ActionScriptAnnotatingVisitor method visitJSAttributeNameValuePair.

@Override
public void visitJSAttributeNameValuePair(@NotNull final JSAttributeNameValuePair attributeNameValuePair) {
    final boolean ok = checkReferences(attributeNameValuePair);
    if (!ok)
        return;
    // check if attribute value must be FQN of a class class inherited from some other class
    if (attributeNameValuePair.getValueNode() == null)
        return;
    final PsiElement parent = attributeNameValuePair.getParent();
    final XmlElementDescriptor descriptor = parent instanceof JSAttributeImpl ? ((JSAttributeImpl) parent).getBackedDescriptor() : null;
    final String attributeName = StringUtil.notNullize(attributeNameValuePair.getName(), JSAttributeNameValuePair.DEFAULT);
    final XmlAttributeDescriptor attributeDescriptor = descriptor == null ? null : descriptor.getAttributeDescriptor(attributeName, null);
    final String baseClassFqns = attributeDescriptor == null ? null : attributeDescriptor.getDefaultValue();
    if (baseClassFqns != null && !StringUtil.isEmptyOrSpaces(baseClassFqns)) {
        final PsiReference[] references = attributeNameValuePair.getReferences();
        PsiReference lastReference = references.length > 0 ? references[0] : null;
        for (final PsiReference reference : references) {
            if (reference.getRangeInElement().getEndOffset() > lastReference.getRangeInElement().getEndOffset()) {
                lastReference = reference;
            }
        }
        final PsiElement resolved = lastReference != null ? lastReference.resolve() : null;
        if (resolved instanceof JSClass) {
            boolean correctClass = false;
            final Collection<String> resolvedBaseClasses = new ArrayList<>();
            final GlobalSearchScope scope = JSResolveUtil.getResolveScope(attributeNameValuePair);
            for (String baseClassFqn : StringUtil.split(baseClassFqns, ",")) {
                if ("Object".equals(baseClassFqn)) {
                    correctClass = true;
                    break;
                }
                final PsiElement baseClass = ActionScriptClassResolver.findClassByQNameStatic(baseClassFqn, attributeNameValuePair);
                if (baseClass instanceof JSClass) {
                    resolvedBaseClasses.add(baseClassFqn);
                    if (JSInheritanceUtil.isParentClass((JSClass) resolved, (JSClass) baseClass, false, scope)) {
                        correctClass = true;
                        break;
                    }
                }
            }
            if (!correctClass) {
                final String classesForMessage = resolvedBaseClasses.isEmpty() ? StringUtil.replace(baseClassFqns, ",", ", ") : StringUtil.join(resolvedBaseClasses, ", ");
                myHolder.createErrorAnnotation(calcRangeForReferences(lastReference), JSBundle.message("javascript.expected.class.or.descendant", classesForMessage));
            }
        } else if (resolved != attributeNameValuePair) {
            // for some reason int and uint are resolved to self-reference JSResolveUtil.MyResolveResult() instead of usual JSClass
            myHolder.createErrorAnnotation(attributeNameValuePair.getValueNode(), JSBundle.message("javascript.qualified.class.name.expected"));
        }
    }
}
Also used : JSAttributeImpl(com.intellij.lang.javascript.psi.ecmal4.impl.JSAttributeImpl) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Aggregations

JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)141 PsiElement (com.intellij.psi.PsiElement)65 Nullable (org.jetbrains.annotations.Nullable)27 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)23 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)22 JSFunction (com.intellij.lang.javascript.psi.JSFunction)18 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)18 Module (com.intellij.openapi.module.Module)17 XmlFile (com.intellij.psi.xml.XmlFile)17 Project (com.intellij.openapi.project.Project)16 PsiFile (com.intellij.psi.PsiFile)16 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)15 ArrayList (java.util.ArrayList)14 PsiDirectory (com.intellij.psi.PsiDirectory)11 XmlTag (com.intellij.psi.xml.XmlTag)11 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)8 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)8 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)7