Invoking CreateClassAction manually

I'm trying to add a quick fix with creates a class from user input. I found that CreateClassAction does the thing I need. But I'm not sure how to invoke it manually. I saw that it was being used as an extension. Can it be used only as a extension?

10 comments

You may find QuickFixFactory to be useful

0

But using QuickFixFactory.createCreateClassOrInterfaceFix(PsiElement context, String qualifiedName, final boolean createClass, final String superClass);  I have to pass a PsiElement as context and a class name which I don't know yet. I have to get it from the user and create. I have to get a pop up of create a class window like the one I have attached.



Attachment(s):
Screen Shot 2014-06-06 at 11.43.42 am.png
0

I'm still not sure what values to pass while creating AnActionEvent and how to acutally invoke it. And how can I hook up the class creation window pop up to it? A small code example could help.

0

The linked thread points to com.intellij.openapi.actionSystem.ex.CheckboxAction#createCustomComponent which has a full code sample which you can use as template for your plugin. If you invoke the action, popup will be shown and CreateClassAction logic will be executed.

0

I have created the create class pop up using

    ActionManager instance = ActionManager.getInstance();
    AnActionEvent anActionEvent = new AnActionEvent(null, DataManager.getInstance().getDataContext(), ActionPlaces.UNKNOWN, new Presentation("Create Class"), instance, 0);
    CreateClassAction createClassAction = new CreateClassAction();
    createClassAction.actionPerformed(anActionEvent);

But when I enter the class name and press ok, there is a null pointer exception at  com.intellij.ide.actions.CreateClassAction.getActionName(CreateClassAction.java:94)
because of JavaDirectoryService.getInstance().getPackage(directory)
the directory that is passed is got from the context of the file from which I invoked(which is not a java class, but a custom language file) the action. So is there a way to ask to choose the directory in which the java class has to go into?

0

You'll have to provide custom "fake" implementation via DataContext passed into ActionEvent (key LangDataKeys.IDE_VIEW) to return proper PsiDirectory via com.intellij.ide.IdeView#getOrChooseDirectory

see com.intellij.ide.actions.CreateFromTemplateAction#actionPerformed

0

Ah, Seems to be working. Isn't there a way to return the PsiJavaFile that was created as a process of that action? So that I can manipulate the file just after creation, like inserting some code into it.

0

You could override com.intellij.ide.actions.CreateClassAction#postProcess and store created PsiClass for later manipulation


0

Please sign in to leave a comment.