Search in sources :

Example 1 with UnknownFileType

use of com.intellij.openapi.fileTypes.UnknownFileType in project intellij-community by JetBrains.

the class FileReferenceQuickFixProvider method registerQuickFix.

@NotNull
public static List<? extends LocalQuickFix> registerQuickFix(@NotNull FileReference reference) {
    final FileReferenceSet fileReferenceSet = reference.getFileReferenceSet();
    int index = reference.getIndex();
    if (index < 0)
        return Collections.emptyList();
    final String newFileName = reference.getFileNameToCreate();
    // check if we could create file
    if (newFileName.isEmpty() || newFileName.indexOf('\\') != -1 || newFileName.indexOf('*') != -1 || newFileName.indexOf('?') != -1 || SystemInfo.isWindows && newFileName.indexOf(':') != -1) {
        return Collections.emptyList();
    }
    PsiFileSystemItem context = null;
    PsiElement element = reference.getElement();
    PsiFile containingFile = element == null ? null : element.getContainingFile();
    if (index > 0) {
        context = fileReferenceSet.getReference(index - 1).resolve();
    } else {
        // index == 0
        final Collection<PsiFileSystemItem> defaultContexts = fileReferenceSet.getDefaultContexts();
        if (defaultContexts.isEmpty()) {
            return Collections.emptyList();
        }
        Module module = containingFile == null ? null : ModuleUtilCore.findModuleForPsiElement(containingFile);
        for (PsiFileSystemItem defaultContext : defaultContexts) {
            if (defaultContext != null) {
                final VirtualFile virtualFile = defaultContext.getVirtualFile();
                if (virtualFile != null && defaultContext.isDirectory() && virtualFile.isInLocalFileSystem()) {
                    if (context == null) {
                        context = defaultContext;
                    }
                    if (module != null && module == getModuleForContext(defaultContext)) {
                        // fixes IDEA-64156
                        // todo: fix it on PsiFileReferenceHelper level in 10.X
                        context = defaultContext;
                        break;
                    }
                }
            }
        }
        if (context == null && ApplicationManager.getApplication().isUnitTestMode()) {
            context = defaultContexts.iterator().next();
        }
    }
    if (context == null)
        return Collections.emptyList();
    final VirtualFile virtualFile = context.getVirtualFile();
    if (virtualFile == null || !virtualFile.isValid())
        return Collections.emptyList();
    final PsiDirectory directory = context.getManager().findDirectory(virtualFile);
    if (directory == null)
        return Collections.emptyList();
    if (fileReferenceSet.isCaseSensitive()) {
        final PsiElement psiElement = containingFile == null ? null : reference.innerSingleResolve(false, containingFile);
        if (psiElement != null) {
            final String existingElementName = ((PsiNamedElement) psiElement).getName();
            final RenameFileReferenceIntentionAction renameRefAction = new RenameFileReferenceIntentionAction(existingElementName, reference);
            final RenameFileFix renameFileFix = new RenameFileFix(newFileName);
            return Arrays.asList(renameRefAction, renameFileFix);
        }
    }
    final boolean isdirectory;
    if (!reference.isLast()) {
        // directory
        try {
            directory.checkCreateSubdirectory(newFileName);
        } catch (IncorrectOperationException ex) {
            return Collections.emptyList();
        }
        isdirectory = true;
    } else {
        FileType ft = FileTypeManager.getInstance().getFileTypeByFileName(newFileName);
        if (ft instanceof UnknownFileType)
            return Collections.emptyList();
        try {
            directory.checkCreateFile(newFileName);
        } catch (IncorrectOperationException ex) {
            return Collections.emptyList();
        }
        isdirectory = false;
    }
    final CreateFileFix action = new MyCreateFileFix(isdirectory, newFileName, directory, reference);
    return Collections.singletonList(action);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) FileType(com.intellij.openapi.fileTypes.FileType) UnknownFileType(com.intellij.openapi.fileTypes.UnknownFileType) UnknownFileType(com.intellij.openapi.fileTypes.UnknownFileType) RenameFileFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RenameFileFix (com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix)1 FileType (com.intellij.openapi.fileTypes.FileType)1 UnknownFileType (com.intellij.openapi.fileTypes.UnknownFileType)1 Module (com.intellij.openapi.module.Module)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 FileReferenceSet (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1