Search in sources :

Example 1 with CreateClassOrPackageFix

use of com.intellij.codeInsight.daemon.quickFix.CreateClassOrPackageFix in project intellij-community by JetBrains.

the class JavaClassReference method registerFixes.

@Nullable
private List<? extends LocalQuickFix> registerFixes() {
    final List<LocalQuickFix> list = QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarImpl(null), this);
    final String[] extendClasses = getExtendClassNames();
    final String extendClass = extendClasses != null && extendClasses.length > 0 ? extendClasses[0] : null;
    final JavaClassReference[] references = getJavaClassReferenceSet().getAllReferences();
    PsiPackage contextPackage = null;
    for (int i = myIndex; i >= 0; i--) {
        final PsiElement context = references[i].getContext();
        if (context != null) {
            if (context instanceof PsiPackage) {
                contextPackage = (PsiPackage) context;
            }
            break;
        }
    }
    boolean createJavaClass = !canReferencePackage();
    ClassKind kind = createJavaClass ? getClassKind() : null;
    if (createJavaClass && kind == null)
        kind = ClassKind.CLASS;
    final String templateName = JavaClassReferenceProvider.CLASS_TEMPLATE.getValue(getOptions());
    final TextRange range = new TextRange(references[0].getRangeInElement().getStartOffset(), getRangeInElement().getEndOffset());
    final String qualifiedName = range.substring(getElement().getText());
    final CreateClassOrPackageFix action = CreateClassOrPackageFix.createFix(qualifiedName, getScope(getJavaContextFile()), getElement(), contextPackage, kind, extendClass, templateName);
    if (action != null) {
        if (list == null) {
            return Collections.singletonList(action);
        } else {
            final ArrayList<LocalQuickFix> fixes = new ArrayList<>(list.size() + 1);
            fixes.addAll(list);
            fixes.add(action);
            return fixes;
        }
    }
    return list;
}
Also used : CreateClassOrPackageFix(com.intellij.codeInsight.daemon.quickFix.CreateClassOrPackageFix) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) TextRange(com.intellij.openapi.util.TextRange) QuickFixActionRegistrarImpl(com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl) ClassKind(com.intellij.psi.util.ClassKind) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

QuickFixActionRegistrarImpl (com.intellij.codeInsight.daemon.impl.quickfix.QuickFixActionRegistrarImpl)1 CreateClassOrPackageFix (com.intellij.codeInsight.daemon.quickFix.CreateClassOrPackageFix)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 TextRange (com.intellij.openapi.util.TextRange)1 ClassKind (com.intellij.psi.util.ClassKind)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1