Search in sources :

Example 21 with JSAttributeList

use of com.intellij.lang.javascript.psi.ecmal4.JSAttributeList 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 22 with JSAttributeList

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

Aggregations

JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)22 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)13 PsiElement (com.intellij.psi.PsiElement)12 JSFunction (com.intellij.lang.javascript.psi.JSFunction)5 JSAttributeListOwner (com.intellij.lang.javascript.psi.ecmal4.JSAttributeListOwner)5 NotNull (org.jetbrains.annotations.NotNull)5 JSAttribute (com.intellij.lang.javascript.psi.ecmal4.JSAttribute)4 JSAttributeNameValuePair (com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair)4 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)3 JSFile (com.intellij.lang.javascript.psi.JSFile)3 XmlFile (com.intellij.psi.xml.XmlFile)3 XmlTag (com.intellij.psi.xml.XmlTag)3 THashMap (gnu.trove.THashMap)3 ASTNode (com.intellij.lang.ASTNode)2 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)2 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)2 JSPackageStatement (com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement)2 Module (com.intellij.openapi.module.Module)2 TextRange (com.intellij.openapi.util.TextRange)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2