Search in sources :

Example 1 with BaseCreateMethodsFix

use of com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix 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 2 with BaseCreateMethodsFix

use of com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix in project intellij-plugins by JetBrains.

the class ActionScriptGenerateToStringHandler method createFix.

protected BaseCreateMethodsFix createFix(final JSClass jsClass) {
    return new BaseCreateMethodsFix<JSVariable>(jsClass) {

        @Override
        public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
            evalAnchor(editor, file);
            final boolean[] needOverride = new boolean[1];
            JSInheritanceUtil.processOverrides(jsClass, new JSOverrideHandler() {

                public boolean process(@NotNull List<JSPsiElementBase> elements, final PsiElement scope, final String className) {
                    needOverride[0] = !"Object".equals(className);
                    return false;
                }
            }, "toString", null, myJsClass, true);
            @NonNls String functionText = "public " + (needOverride[0] ? "override " : "") + "function toString():String {\nreturn " + (needOverride[0] ? "super.toString()" : "\"" + jsClass.getName());
            final String semicolon = JSCodeStyleSettings.getSemicolon(file);
            boolean first = true;
            final Set<JSVariable> jsVariables = getElementsToProcess();
            if (!jsVariables.isEmpty()) {
                functionText += needOverride[0] ? " + \"{" : "{";
                for (JSVariable var : jsVariables) {
                    if (!first) {
                        functionText += " + \",";
                    }
                    first = false;
                    functionText += var.getName() + "=\" + String(" + var.getName() + ")";
                }
                functionText += "+\"}\"";
            } else {
                if (!needOverride[0])
                    functionText += "\"";
            }
            functionText += semicolon + "\n}";
            doAddOneMethod(project, functionText, anchor);
        }
    };
}
Also used : NonNls(org.jetbrains.annotations.NonNls) JSOverrideHandler(com.intellij.lang.javascript.psi.resolve.JSOverrideHandler) JSVariable(com.intellij.lang.javascript.psi.JSVariable) NotNull(org.jetbrains.annotations.NotNull) BaseCreateMethodsFix(com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix) Project(com.intellij.openapi.project.Project) JSPsiElementBase(com.intellij.lang.javascript.psi.JSPsiElementBase) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 3 with BaseCreateMethodsFix

use of com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix 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)

Aggregations

BaseCreateMethodsFix (com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix)3 Project (com.intellij.openapi.project.Project)3 PsiElement (com.intellij.psi.PsiElement)3 NotNull (org.jetbrains.annotations.NotNull)3 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 PsiFile (com.intellij.psi.PsiFile)2 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 JSPsiElementBase (com.intellij.lang.javascript.psi.JSPsiElementBase)1 JSVariable (com.intellij.lang.javascript.psi.JSVariable)1 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)1 JSOverrideHandler (com.intellij.lang.javascript.psi.resolve.JSOverrideHandler)1 ResolveProcessor (com.intellij.lang.javascript.psi.resolve.ResolveProcessor)1 JSAttributeListWrapper (com.intellij.lang.javascript.validation.fixes.JSAttributeListWrapper)1 TextRange (com.intellij.openapi.util.TextRange)1 ResolveState (com.intellij.psi.ResolveState)1 MultiMap (com.intellij.util.containers.MultiMap)1 NonNls (org.jetbrains.annotations.NonNls)1