Search in sources :

Example 1 with ResolveProcessor

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

Aggregations

JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 ResolveProcessor (com.intellij.lang.javascript.psi.resolve.ResolveProcessor)1 BaseCreateMethodsFix (com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix)1 JSAttributeListWrapper (com.intellij.lang.javascript.validation.fixes.JSAttributeListWrapper)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 ResolveState (com.intellij.psi.ResolveState)1 MultiMap (com.intellij.util.containers.MultiMap)1 NotNull (org.jetbrains.annotations.NotNull)1