Search in sources :

Example 1 with RenameFileFix

use of com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix 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)

Example 2 with RenameFileFix

use of com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix in project intellij-plugins by JetBrains.

the class ActionScriptAnnotatingVisitor method checkNamedObjectIsInCorrespondingFile.

private void checkNamedObjectIsInCorrespondingFile(final JSNamedElement aClass) {
    final PsiFile containingFile = aClass.getContainingFile();
    if (containingFile.getContext() != null)
        return;
    final VirtualFile file = containingFile.getVirtualFile();
    if (file != null && !file.getNameWithoutExtension().equals(aClass.getName()) && ProjectRootManager.getInstance(containingFile.getProject()).getFileIndex().getSourceRootForFile(file) != null) {
        final ASTNode node = aClass.findNameIdentifier();
        if (node != null) {
            final String name = aClass.getName();
            String nameWithExtension = name + "." + file.getExtension();
            final String message = JSBundle.message(aClass instanceof JSClass ? "javascript.validation.message.class.should.be.in.file" : aClass instanceof JSNamespaceDeclaration ? "javascript.validation.message.namespace.should.be.in.file" : aClass instanceof JSVariable ? "javascript.validation.message.variable.should.be.in.file" : "javascript.validation.message.function.should.be.in.file", name, nameWithExtension);
            final Annotation annotation = myHolder.createErrorAnnotation(node, message);
            annotation.registerFix(new RenameFileFix(nameWithExtension));
            annotation.registerFix(new RenameElementFix(aClass) {

                final String text;

                final String familyName;

                {
                    String term = message.substring(0, message.indexOf(' '));
                    text = super.getText().replace("class", StringUtil.decapitalize(term));
                    familyName = super.getFamilyName().replace("Class", term);
                }

                @NotNull
                @Override
                public String getText() {
                    return text;
                }

                @NotNull
                @Override
                public String getFamilyName() {
                    return familyName;
                }
            });
        }
    }
    checkFileUnderSourceRoot(aClass, new SimpleErrorReportingClient());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RenameElementFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) ASTNode(com.intellij.lang.ASTNode) RenameFileFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix)

Aggregations

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