Search in sources :

Example 1 with GoDeleteImportQuickFix

use of com.goide.quickfix.GoDeleteImportQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoInvalidPackageImportInspection method checkFile.

@Override
protected void checkFile(@NotNull GoFile file, @NotNull ProblemsHolder problemsHolder) {
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    VirtualFile sdkHome = GoSdkUtil.getSdkSrcDir(file.getProject(), module);
    boolean supportsVendoring = GoVendoringUtil.isVendoringEnabled(module);
    String sdkVersion = GoSdkService.getInstance(file.getProject()).getSdkVersion(module);
    boolean supportsInternalPackages = GoVendoringUtil.supportsInternalPackages(sdkVersion);
    boolean supportsInternalPackagesInSdk = sdkHome != null && GoVendoringUtil.supportsSdkInternalPackages(sdkVersion);
    for (GoImportSpec importSpec : file.getImports()) {
        if (importSpec.isCImport()) {
            PsiElement dot = importSpec.getDot();
            if (dot != null) {
                problemsHolder.registerProblem(importSpec, "Cannot rename import `C`", new GoDeleteImportSpecAlias(), new GoDeleteImportQuickFix());
            }
            PsiElement identifier = importSpec.getIdentifier();
            if (identifier != null) {
                problemsHolder.registerProblem(importSpec, "Cannot import 'builtin' package", new GoDeleteImportSpecAlias(), new GoDeleteImportQuickFix());
            }
            continue;
        }
        PsiDirectory resolve = importSpec.getImportString().resolve();
        if (resolve != null) {
            if (GoPackageUtil.isBuiltinPackage(resolve)) {
                problemsHolder.registerProblem(importSpec, "Cannot import 'builtin' package", new GoDeleteImportQuickFix());
            }
            Collection<String> packagesInDirectory = GoPackageUtil.getAllPackagesInDirectory(resolve, module, true);
            if (packagesInDirectory.isEmpty()) {
                problemsHolder.registerProblem(importSpec, "'" + resolve.getVirtualFile().getPath() + "' has no buildable Go source files", new GoDeleteImportQuickFix());
                continue;
            }
            if (!GoTestFinder.isTestFile(file) && packagesInDirectory.size() == 1 && packagesInDirectory.contains(GoConstants.MAIN)) {
                problemsHolder.registerProblem(importSpec, "'" + importSpec.getPath() + "' is a program, not an importable package", new GoDeleteImportQuickFix());
                continue;
            }
            if (packagesInDirectory.size() > 1) {
                problemsHolder.registerProblem(importSpec, "Found several packages [" + StringUtil.join(packagesInDirectory, ", ") + "] in '" + resolve.getVirtualFile().getPath() + "'", new GoDeleteImportQuickFix());
                continue;
            }
            VirtualFile contextFile = file.getVirtualFile();
            VirtualFile resolvedFile = resolve.getVirtualFile();
            boolean resolvedToSdk = sdkHome != null && VfsUtilCore.isAncestor(sdkHome, resolvedFile, false);
            boolean validateInternal = supportsInternalPackages || supportsInternalPackagesInSdk && resolvedToSdk;
            if (supportsVendoring || validateInternal || resolvedToSdk) {
                Set<VirtualFile> sourceRoots = GoSdkUtil.getSourcesPathsToLookup(file.getProject(), module);
                for (PsiReference reference : importSpec.getImportString().getReferences()) {
                    if (reference instanceof GoImportReference) {
                        String canonicalText = reference.getCanonicalText();
                        if (resolvedToSdk && GoConstants.TESTDATA_NAME.equals(canonicalText)) {
                            problemsHolder.registerProblem(importSpec, "Use of testdata package from SDK is not allowed", new GoDeleteImportQuickFix());
                            break;
                        } else if (validateInternal && GoConstants.INTERNAL.equals(canonicalText)) {
                            if (GoSdkUtil.isUnreachableInternalPackage(resolvedFile, contextFile, sourceRoots)) {
                                problemsHolder.registerProblem(importSpec, "Use of internal package is not allowed", new GoDeleteImportQuickFix());
                                break;
                            }
                        } else if (supportsVendoring && GoConstants.VENDOR.equals(canonicalText)) {
                            if (GoSdkUtil.isUnreachableVendoredPackage(resolvedFile, contextFile, sourceRoots)) {
                                problemsHolder.registerProblem(importSpec, "Use of vendored package is not allowed", new GoDeleteImportQuickFix(), GoDisableVendoringInModuleQuickFix.create(module));
                                break;
                            } else {
                                String vendoredImportPath = GoSdkUtil.getImportPath(resolve, true);
                                if (vendoredImportPath != null) {
                                    problemsHolder.registerProblem(importSpec, "Must be imported as '" + vendoredImportPath + "'", new GoReplaceImportPath(vendoredImportPath), new GoDeleteImportQuickFix(), GoDisableVendoringInModuleQuickFix.create(module));
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } else {
            for (PsiReference reference : importSpec.getImportString().getReferences()) {
                if (reference instanceof GoImportReference) {
                    if (((GoImportReference) reference).getFileReferenceSet().isAbsolutePathReference()) {
                        problemsHolder.registerProblem(importSpec, "Cannot import absolute path", new GoDeleteImportQuickFix());
                        break;
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GoDeleteImportQuickFix(com.goide.quickfix.GoDeleteImportQuickFix) GoImportSpec(com.goide.psi.GoImportSpec) PsiReference(com.intellij.psi.PsiReference) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) GoImportReference(com.goide.psi.impl.imports.GoImportReference)

Example 2 with GoDeleteImportQuickFix

use of com.goide.quickfix.GoDeleteImportQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoImportReference method getQuickFixes.

@Override
public LocalQuickFix[] getQuickFixes() {
    if (GoPackageUtil.isBuiltinPackage(resolve())) {
        return new LocalQuickFix[] { new GoDeleteImportQuickFix() };
    }
    List<LocalQuickFix> result = ContainerUtil.newArrayList();
    FileReferenceSet fileReferenceSet = getFileReferenceSet();
    if (fileReferenceSet instanceof GoImportReferenceSet && !((GoImportReferenceSet) fileReferenceSet).isRelativeImport() && !fileReferenceSet.isAbsolutePathReference()) {
        result.add(new GoGetPackageFix(fileReferenceSet.getPathString()));
    }
    String fileNameToCreate = getFileNameToCreate();
    for (PsiFileSystemItem context : getContexts()) {
        if (context instanceof PsiDirectory) {
            try {
                ((PsiDirectory) context).checkCreateSubdirectory(fileNameToCreate);
                String targetPath = context.getVirtualFile().getPath();
                result.add(new CreateFileFix(true, fileNameToCreate, (PsiDirectory) context) {

                    @NotNull
                    @Override
                    public String getText() {
                        return "Create Directory " + fileNameToCreate + " at " + targetPath;
                    }
                });
            } catch (IncorrectOperationException ignore) {
            }
        }
    }
    return result.toArray(new LocalQuickFix[result.size()]);
}
Also used : GoDeleteImportQuickFix(com.goide.quickfix.GoDeleteImportQuickFix) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) NotNull(org.jetbrains.annotations.NotNull) CreateFileFix(com.intellij.codeInsight.daemon.quickFix.CreateFileFix) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GoGetPackageFix(com.goide.codeInsight.imports.GoGetPackageFix)

Aggregations

GoDeleteImportQuickFix (com.goide.quickfix.GoDeleteImportQuickFix)2 GoGetPackageFix (com.goide.codeInsight.imports.GoGetPackageFix)1 GoImportSpec (com.goide.psi.GoImportSpec)1 GoImportReference (com.goide.psi.impl.imports.GoImportReference)1 CreateFileFix (com.intellij.codeInsight.daemon.quickFix.CreateFileFix)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 Module (com.intellij.openapi.module.Module)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)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