Search in sources :

Example 1 with CreateClassParameters

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

the class CreateJSSubclassIntention method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final JSClass jsClass = PsiTreeUtil.getParentOfType(element, JSClass.class);
    if (jsClass == null)
        return;
    final PsiElement parent = jsClass.getParent();
    if (!(parent instanceof JSPackageStatement))
        return;
    final JSPackageStatement jsPackageStatement = (JSPackageStatement) parent;
    final String defaultTemplateName = ActionScriptCreateClassOrInterfaceFix.ACTION_SCRIPT_CLASS_WITH_SUPERS_TEMPLATE_NAME;
    final String className;
    final String packageName;
    final String templateName;
    final PsiDirectory targetDirectory;
    final Collection<String> interfaces;
    final Map<String, Object> templateAttributes;
    final JSClass superClass;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        className = suggestSubclassName(jsClass.getName());
        packageName = "foo";
        templateName = defaultTemplateName;
        targetDirectory = WriteAction.compute(() -> ActionScriptCreateClassOrInterfaceFix.findOrCreateDirectory(packageName, jsPackageStatement));
        interfaces = jsClass.isInterface() ? Collections.singletonList(jsClass.getQualifiedName()) : Collections.emptyList();
        templateAttributes = Collections.emptyMap();
        superClass = jsClass.isInterface() ? null : jsClass;
    } else {
        CreateClassParameters p = ActionScriptCreateClassOrInterfaceFix.createAndShow(defaultTemplateName, jsClass, suggestSubclassName(jsClass.getName()), true, jsPackageStatement.getQualifiedName(), jsClass, JSBundle.message("new.actionscript.class.dialog.title"), () -> ActionScriptCreateClassOrInterfaceFix.getApplicableTemplates(ActionScriptCreateClassOrInterfaceFix.ACTIONSCRIPT_TEMPLATES_EXTENSIONS, project));
        if (p == null)
            return;
        className = p.getClassName();
        packageName = p.getPackageName();
        templateName = p.getTemplateName();
        targetDirectory = p.getTargetDirectory();
        superClass = ActionScriptCreateClassOrInterfaceFix.calcClass(p.getSuperclassFqn(), element);
        interfaces = p.getInterfacesFqns();
        templateAttributes = new HashMap<>(p.getTemplateAttributes());
    }
    JSClass createdClass = ActionScriptCreateClassOrInterfaceFix.createClass(templateName, className, packageName, superClass, interfaces, targetDirectory, getTitle(jsClass), true, templateAttributes, aClass -> {
        if (aClass != null && !aClass.isInterface() && (jsClass.isInterface() || !interfaces.isEmpty())) {
            new MyImplementMethodsHandler(aClass).execute();
        }
    });
    if (createdClass != null) {
        createdClass.navigate(true);
    }
}
Also used : CreateClassParameters(com.intellij.lang.javascript.validation.fixes.CreateClassParameters) JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) PsiDirectory(com.intellij.psi.PsiDirectory) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 JSPackageStatement (com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement)1 CreateClassParameters (com.intellij.lang.javascript.validation.fixes.CreateClassParameters)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1